Files
mixtape/zero.Web.UI/App/core/zero.js
T

196 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-10-16 11:23:54 +02:00
// ref:
// https://github.com/vuejs/vue-router/blob/dev/src/index.js
2020-10-16 14:02:10 +02:00
import Axios from 'axios';
2020-10-16 11:23:54 +02:00
import ZeroPlugin from './plugin.zero.js';
2020-10-16 14:02:10 +02:00
import EventHub from '../services/eventhub.js';
import VueRouter from 'vue-router';
import Vue from 'vue';
2020-10-19 22:47:10 +02:00
import CommercePlugin from '../../../zero.Commerce/Plugins/zero.Commerce/plugin.js'; // TODO dynPath
2020-10-20 15:01:34 +02:00
import TestPlugin from '../../../../Laola/Laola.Backoffice/Plugin/plugin.js'; // TODO dynPath
2020-10-16 11:23:54 +02:00
import options from './options.js';
2020-10-16 14:02:10 +02:00
import routerConfig from '../config/router.config.js'
2020-10-16 11:23:54 +02:00
class Zero
{
static install;
2020-10-19 15:58:32 +02:00
static instance;
2020-10-16 11:23:54 +02:00
config = { ...options };
#vue = null;
#plugins = [];
2020-10-18 23:47:59 +02:00
#editors = [];
2020-10-19 14:48:21 +02:00
#lists = [];
2020-10-16 14:02:10 +02:00
#routes = [];
#router = null;
2020-10-19 15:58:32 +02:00
#setupDone = false;
2020-10-16 11:23:54 +02:00
constructor(vue, opts)
{
this.config = { ...options, ...opts };
this.#vue = vue;
this.use(ZeroPlugin);
2020-10-19 22:47:10 +02:00
this.use(CommercePlugin);
2020-10-20 15:01:34 +02:00
this.use(TestPlugin);
2020-10-16 11:23:54 +02:00
}
/*
* Get the currently installed version of the zero frontend
*/
get version()
{
return this.config.version;
}
/*
* Get all installed zero plugins
*/
get plugins()
{
return this.#plugins;
}
2020-10-16 14:02:10 +02:00
/*
* Get all installed zero plugins
*/
get router()
{
return this.#router;
}
2020-10-16 11:23:54 +02:00
/*
* Locates zero plugins and install them
*/
setup()
{
2020-10-16 14:02:10 +02:00
this.#router = new VueRouter({
...routerConfig,
routes: this.#routes
});
//const result = await Axios.get('zerovue/config');
//this.config = { ...this.config, ...result.data };
//console.info(this.#vue.router);
2020-10-19 15:58:32 +02:00
this.#setupDone = true;
2020-10-16 14:02:10 +02:00
//EventHub.$emit('zero.setup');
2020-10-16 11:23:54 +02:00
}
2020-10-19 15:58:32 +02:00
/*
* Installs a zero plugin by a given path
* Each plugin is a superset of a vue (client) plugin and can therefore hook into the vue system
*/
useByPath(pluginPath)
{
//const plugin = import('../../../' + pluginPath + '/plugin.js').then(res =>
//{
// this.use(res.default);
//});
}
2020-10-16 11:23:54 +02:00
/*
* Installs a zero plugin
* Each plugin is a superset of a vue (client) plugin and can therefore hook into the vue system
*/
use(plugin)
{
if (typeof plugin.install === 'function')
{
plugin.install(this.#vue, this);
}
this.#plugins.push(plugin);
2020-10-16 14:02:10 +02:00
// append routes
2020-10-19 15:58:32 +02:00
if (this.#setupDone)
{
this.#router.addRoutes(plugin.routes);
}
else
{
plugin.routes.forEach(x => this.#routes.push(x));
}
2020-10-16 14:15:45 +02:00
2020-10-18 23:47:59 +02:00
// append editors
plugin.editors.forEach(x => this.#editors.push(x));
2020-10-16 14:02:10 +02:00
2020-10-19 14:48:21 +02:00
// append lists
plugin.lists.forEach(x => this.#lists.push(x));
2020-10-16 11:23:54 +02:00
console.log(`[zero] Installed %c${plugin.name}%cplugin`, 'font-style:italic;');
}
2020-10-16 14:15:45 +02:00
/*
2020-10-19 14:48:21 +02:00
* Returns an editor renderer
2020-10-16 14:15:45 +02:00
*/
2020-10-18 23:47:59 +02:00
getEditor(alias)
2020-10-16 14:15:45 +02:00
{
2020-10-18 23:47:59 +02:00
const renderer = this.#editors.find(x => x.alias === alias);
2020-10-16 14:15:45 +02:00
if (!renderer)
{
2020-10-19 14:48:21 +02:00
console.warn(`[zero] Could not find editor renderer ${alias}`);
}
return renderer;
}
/*
* Returns a list renderer
*/
getList(alias)
{
const renderer = this.#lists.find(x => x.alias === alias);
if (!renderer)
{
console.warn(`[zero] Could not find list renderer ${alias}`);
2020-10-16 14:15:45 +02:00
}
return renderer;
}
2020-10-16 11:23:54 +02:00
};
/*
* Registers zero within the vue system
*/
Zero.install = (vue, opts) =>
{
const zero = new Zero(vue, opts);
2020-10-19 15:58:32 +02:00
Zero.instance = zero;
2020-10-16 11:23:54 +02:00
Object.defineProperty(vue.prototype, 'zero', {
get: () => zero
});
2020-10-16 14:02:10 +02:00
zero.setup();
2020-10-22 11:11:45 +02:00
console.log('[zero] Setup completed');
2020-10-19 15:58:32 +02:00
// add plugins
//__zero.plugins.filter(x => !!x.pluginPath).forEach(x =>
//{
// zero.useByPath(x.pluginPath);
//});
//import('@/../zero.Commerce/Plugins/zero.Commerce/plugin.js').then(res =>
//{
// zero.use(res.default);
//});
2020-10-16 11:23:54 +02:00
};
export default Zero;