Files
mixtape/zero.Backoffice.UI/app/core/zeroRuntime.ts
T

166 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-12-07 01:11:18 +01:00
2021-12-20 16:13:09 +01:00
import { App, Component } from 'vue';
2021-12-07 01:11:18 +01:00
import { ZeroInstallOptions } from './types/zeroInstallOptions';
2021-12-07 15:59:29 +01:00
import { ZeroPluginOptions } from './types/zeroPluginOptions';
2021-12-14 13:33:23 +01:00
import { Zero, ZeroOptions } from './types/zero';
2021-12-07 15:59:29 +01:00
import { createRouter, RouteRecordRaw, RouterOptions } from 'vue-router';
2021-12-07 01:11:18 +01:00
import registerDirectives from '../directives/register';
import registerComponents from '../components/register';
2021-12-22 15:41:11 +01:00
import registerEditorComponents from '../editor/register';
2021-12-07 15:59:29 +01:00
import { getRouterConfig, appendRouterGuards } from './router/routerConfig';
2021-12-21 14:41:50 +01:00
import
{
countryPlugin,
applicationPlugin,
settingsPlugin,
languagePlugin,
mediaPlugin,
spacePlugin,
pagePlugin,
mailTemplatePlugin,
2021-12-22 00:14:48 +01:00
translationPlugin,
2021-12-26 01:42:21 +01:00
integrationPlugin,
userPlugin
2021-12-21 14:41:50 +01:00
} from '../modules';
2021-12-21 13:58:48 +01:00
import editorPlugin from '../editor/plugin';
2021-12-10 14:59:35 +01:00
import { ZeroSchema } from 'zero/schemas';
import { ZeroSchemaProp } from './zero';
2021-12-14 13:33:23 +01:00
import * as zeroOptions from '../options';
2021-12-29 01:25:35 +01:00
import plugins from '../plugins.generated';
2021-12-07 01:11:18 +01:00
export class ZeroRuntime implements Zero
{
_app: App;
2021-12-14 13:33:23 +01:00
_installOptions: ZeroInstallOptions;
2021-12-07 15:59:29 +01:00
_routerConfig: RouterOptions;
2021-12-10 14:23:14 +01:00
_schemas: Record<string, ZeroSchemaProp> = {};
2021-12-20 16:13:09 +01:00
_fieldTypes: Record<string, Component> = {};
2021-12-07 01:11:18 +01:00
2021-12-07 15:59:29 +01:00
/**
* version of zero backoffice
**/
2021-12-07 01:11:18 +01:00
get version(): string
{
return "0.0.1";
}
2021-12-14 13:33:23 +01:00
/**
* options
**/
options: ZeroOptions;
2021-12-14 16:06:54 +01:00
/**
* runtime variables
**/
runtimeVariables: Record<string, any> = {};
2021-12-07 01:11:18 +01:00
constructor(app: App, options?: ZeroInstallOptions)
{
this._app = app;
2021-12-14 13:33:23 +01:00
this._installOptions = options || {};
2021-12-07 15:59:29 +01:00
this._routerConfig = getRouterConfig('/zero', this);
2021-12-14 13:33:23 +01:00
this.options = zeroOptions;
2021-12-07 01:11:18 +01:00
}
2021-12-07 15:59:29 +01:00
/**
* register core components
**/
useZero()
2021-12-07 01:11:18 +01:00
{
2021-12-07 15:59:29 +01:00
let app = this._app;
registerDirectives(app);
registerComponents(app);
2021-12-22 15:41:11 +01:00
registerEditorComponents(app);
2021-12-07 15:59:29 +01:00
}
/**
* create plugin options which are passed to install() for all plugins
**/
usePlugins()
{
const pluginOptions = {
vue: this._app,
routes: this._routerConfig.routes,
2021-12-10 14:23:14 +01:00
schemas: this._schemas,
2021-12-20 16:13:09 +01:00
fieldTypes: this._fieldTypes,
2021-12-07 15:59:29 +01:00
route(route: RouteRecordRaw)
{
this.routes.push(route);
2021-12-10 14:23:14 +01:00
},
2021-12-20 16:13:09 +01:00
schema(alias: string, schema: ZeroSchemaProp)
2021-12-10 14:23:14 +01:00
{
this.schemas[alias] = schema;
2021-12-20 16:13:09 +01:00
},
fieldType(alias: string, component: Component)
{
this.fieldTypes[alias] = component;
2021-12-07 15:59:29 +01:00
}
} as ZeroPluginOptions;
2021-12-20 16:13:09 +01:00
2021-12-07 15:59:29 +01:00
// install all plugins
2021-12-20 16:13:09 +01:00
editorPlugin.install(pluginOptions);
2021-12-07 15:59:29 +01:00
countryPlugin.install(pluginOptions);
2021-12-09 14:18:38 +01:00
applicationPlugin.install(pluginOptions);
2021-12-09 16:02:39 +01:00
settingsPlugin.install(pluginOptions);
2021-12-15 21:52:00 +01:00
languagePlugin.install(pluginOptions);
2021-12-16 00:19:51 +01:00
mediaPlugin.install(pluginOptions);
2021-12-19 14:18:16 +01:00
spacePlugin.install(pluginOptions);
2021-12-20 00:39:39 +01:00
pagePlugin.install(pluginOptions);
2021-12-21 13:58:48 +01:00
mailTemplatePlugin.install(pluginOptions);
2021-12-21 14:41:50 +01:00
translationPlugin.install(pluginOptions);
2021-12-22 00:14:48 +01:00
integrationPlugin.install(pluginOptions);
2021-12-26 01:42:21 +01:00
userPlugin.install(pluginOptions);
2021-12-29 01:25:35 +01:00
plugins.forEach(plugin =>
{
plugin.install(pluginOptions);
});
2021-12-07 15:59:29 +01:00
}
/**
* after all plugins were installed we can create the router
* with the designated routes
**/
useRouter()
{
const router = createRouter(this._routerConfig);
appendRouterGuards(router);
this._app.use(router);
2021-12-07 01:11:18 +01:00
}
2021-12-10 14:23:14 +01:00
2021-12-21 13:58:48 +01:00
/**
* get a defined field type component
**/
getFieldTypeComponent(alias: string): Component | undefined
{
return this._fieldTypes[alias];
}
2021-12-10 14:23:14 +01:00
/**
* get a defined list or editor schema
**/
async getSchema(alias: string): Promise<ZeroSchema | null>
{
2021-12-10 14:59:35 +01:00
const schema: ZeroSchemaProp = this._schemas[alias];
2021-12-10 14:23:14 +01:00
if (!schema)
{
return Promise.resolve(null);
}
if (typeof schema === 'function')
{
const res = await schema();
return res.default;
}
return Promise.resolve(schema);
}
2021-12-07 01:11:18 +01:00
}