Files
mixtape/zero.Backoffice.UI/app/core/zeroRuntime.ts
T
2021-12-14 14:21:48 +01:00

118 lines
2.7 KiB
TypeScript

import { App } from 'vue';
import { ZeroInstallOptions } from './types/zeroInstallOptions';
import { ZeroPluginOptions } from './types/zeroPluginOptions';
import { Zero, ZeroOptions } from './types/zero';
import { createRouter, RouteRecordRaw, RouterOptions } from 'vue-router';
import registerDirectives from '../directives/register';
import registerComponents from '../components/register';
import registerFormComponents from '../forms/register';
import registerEditorComponents from '../editor/register';
import { getRouterConfig, appendRouterGuards } from './router/routerConfig';
import { countryPlugin, applicationPlugin, settingsPlugin } from '../modules';
import { ZeroSchema } from 'zero/schemas';
import { ZeroSchemaProp } from './zero';
import * as zeroOptions from '../options';
export class ZeroRuntime implements Zero
{
_app: App;
_installOptions: ZeroInstallOptions;
_routerConfig: RouterOptions;
_schemas: Record<string, ZeroSchemaProp> = {};
/**
* version of zero backoffice
**/
get version(): string
{
return "0.0.1";
}
/**
* options
**/
options: ZeroOptions;
constructor(app: App, options?: ZeroInstallOptions)
{
this._app = app;
this._installOptions = options || {};
this._routerConfig = getRouterConfig('/zero', this);
this.options = zeroOptions;
}
/**
* register core components
**/
useZero()
{
let app = this._app;
registerDirectives(app);
registerComponents(app);
registerFormComponents(app);
registerEditorComponents(app);
}
/**
* create plugin options which are passed to install() for all plugins
**/
usePlugins()
{
const pluginOptions = {
vue: this._app,
routes: this._routerConfig.routes,
schemas: this._schemas,
route(route: RouteRecordRaw)
{
this.routes.push(route);
},
schema(alias: 'string', schema: ZeroSchemaProp)
{
this.schemas[alias] = schema;
}
} as ZeroPluginOptions;
// install all plugins
countryPlugin.install(pluginOptions);
applicationPlugin.install(pluginOptions);
settingsPlugin.install(pluginOptions);
}
/**
* 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);
}
/**
* get a defined list or editor schema
**/
async getSchema(alias: string): Promise<ZeroSchema | null>
{
const schema: ZeroSchemaProp = this._schemas[alias];
if (!schema)
{
return Promise.resolve(null);
}
if (typeof schema === 'function')
{
const res = await schema();
return res.default;
}
return Promise.resolve(schema);
}
}