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

224 lines
4.9 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';
2022-01-12 17:15:05 +01:00
import { Zero, ZeroLinkArea, 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,
2022-01-12 17:15:05 +01:00
userPlugin,
2022-01-12 18:49:36 +01:00
linksPlugin,
pageModulePlugin
2021-12-21 14:41:50 +01:00
} from '../modules';
2021-12-21 13:58:48 +01:00
import editorPlugin from '../editor/plugin';
2022-01-14 11:51:38 +01:00
import { ZeroSchema, ZeroSchemaExtension } from 'zero/schemas';
2021-12-10 14:59:35 +01:00
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';
2022-01-12 14:11:41 +01:00
import eventHub from '../services/eventhub';
import { Emitter, EventType } from 'mitt';
2021-12-07 01:11:18 +01:00
2022-01-07 11:11:06 +01:00
plugins.push(
2022-01-10 16:13:59 +01:00
editorPlugin, countryPlugin, applicationPlugin, settingsPlugin, languagePlugin,
2022-01-07 11:11:06 +01:00
mediaPlugin, spacePlugin, pagePlugin, mailTemplatePlugin,
2022-01-12 18:49:36 +01:00
translationPlugin, integrationPlugin, userPlugin, linksPlugin, pageModulePlugin
2022-01-07 11:11:06 +01:00
);
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> = {};
2022-01-14 11:51:38 +01:00
_schemaExtensions: ZeroSchemaExtension[] = [];
2021-12-20 16:13:09 +01:00
_fieldTypes: Record<string, Component> = {};
2022-01-12 17:15:05 +01:00
_linkAreas: Record<string, ZeroLinkArea> = {};
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";
}
2022-01-12 14:11:41 +01:00
/**
* access the event hub
**/
get events(): Emitter<Record<EventType, unknown>>
{
return eventHub;
}
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,
2022-01-14 11:51:38 +01:00
schemaExtensions: this._schemaExtensions,
2021-12-20 16:13:09 +01:00
fieldTypes: this._fieldTypes,
2022-01-12 17:15:05 +01:00
linkAreas: this._linkAreas,
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
},
2022-01-14 11:51:38 +01:00
extendSchema(alias: string, extension: ((schema: ZeroSchema) => void))
{
this.schemaExtensions.push({ alias, extension });
},
2022-01-12 17:15:05 +01:00
fieldType(alias: string, component: Component) // TODO v3 allow default field options
2021-12-20 16:13:09 +01:00
{
this.fieldTypes[alias] = component;
2022-01-12 17:15:05 +01:00
},
linkArea(alias: string, name: string, component: Component)
{
this.linkAreas[alias] = { alias, name, component };
2021-12-07 15:59:29 +01:00
}
} as ZeroPluginOptions;
// install all plugins
2021-12-29 01:25:35 +01:00
plugins.forEach(plugin =>
{
plugin.install(pluginOptions);
});
2021-12-07 15:59:29 +01:00
}
2022-01-07 11:11:06 +01:00
runPlugins()
{
plugins.forEach(plugin =>
{
if (typeof plugin.run === 'function')
{
plugin.run(this);
}
});
}
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>
{
2022-01-14 11:51:38 +01:00
let 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();
2022-01-14 11:51:38 +01:00
schema = res.default as ZeroSchema;
2021-12-10 14:23:14 +01:00
}
2022-01-14 11:51:38 +01:00
schema.alias = alias;
2021-12-10 14:23:14 +01:00
return Promise.resolve(schema);
}
2022-01-12 17:15:05 +01:00
2022-01-14 11:51:38 +01:00
/**
* get all defined extensions for a schema
**/
getSchemaExtensions(alias: string): ZeroSchemaExtension[]
{
return this._schemaExtensions.filter(x => x.alias == alias);
}
2022-01-12 17:15:05 +01:00
/**
* get all defined link areas
**/
get linkAreas(): ZeroLinkArea[]
{
return Object.values(this._linkAreas);
}
/**
* get a defined link area by alias
**/
getLinkArea(alias: string): ZeroLinkArea | undefined
{
return this._linkAreas[alias];
}
2021-12-07 01:11:18 +01:00
}