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

109 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-12-07 01:11:18 +01:00
import { App } from 'vue';
import { ZeroInstallOptions } from './types/zeroInstallOptions';
2021-12-07 15:59:29 +01:00
import { ZeroPluginOptions } from './types/zeroPluginOptions';
2021-12-07 01:11:18 +01:00
import { Zero } 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-09 14:18:38 +01:00
import registerFormComponents from '../forms/register';
2021-12-07 15:59:29 +01:00
import { getRouterConfig, appendRouterGuards } from './router/routerConfig';
2021-12-09 16:02:39 +01:00
import { countryPlugin, applicationPlugin, settingsPlugin } from '../modules';
2021-12-10 14:59:35 +01:00
import { ZeroSchema } from 'zero/schemas';
import { ZeroSchemaProp } from './zero';
2021-12-07 01:11:18 +01:00
export class ZeroRuntime implements Zero
{
_app: App;
_options: 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-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";
}
constructor(app: App, options?: ZeroInstallOptions)
{
this._app = app;
this._options = options || {};
2021-12-07 15:59:29 +01:00
this._routerConfig = getRouterConfig('/zero', this);
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);
registerFormComponents(app);
}
/**
* 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-07 15:59:29 +01:00
route(route: RouteRecordRaw)
{
this.routes.push(route);
2021-12-10 14:23:14 +01:00
},
schema(alias: 'string', schema: ZeroSchemaProp)
{
this.schemas[alias] = schema;
2021-12-07 15:59:29 +01:00
}
} as ZeroPluginOptions;
// install all plugins
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-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
/**
* 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
}