Files
mixtape/zero.Backoffice.UI/old_app/core/zero.ts
T

235 lines
4.2 KiB
TypeScript
Raw Normal View History

2020-10-16 11:23:54 +02:00
// ref:
// https://github.com/vuejs/vue-router/blob/dev/src/index.js
import ZeroPlugin from './plugin.zero.js';
2020-10-16 14:02:10 +02:00
import VueRouter from 'vue-router';
2020-11-01 23:56:22 +01:00
import plugins from './plugins.js';
2021-03-05 15:35:55 +01:00
import mediaConfig from '../config/media.config.js';
import linksConfig from '../config/links.config.js';
2020-10-16 14:02:10 +02:00
import routerConfig from '../config/router.config.js'
2021-03-05 15:35:55 +01:00
import Axios from 'axios';
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
2021-03-05 15:35:55 +01:00
config = { };
2020-10-16 11:23:54 +02:00
2021-05-08 15:20:23 +02:00
_vue = null;
_plugins = [];
_editors = [];
_lists = [];
_routes = [];
_router = null;
_setupDone = false;
2020-10-16 11:23:54 +02:00
constructor(vue, opts)
{
2021-03-05 15:35:55 +01:00
let initialConfig = JSON.parse(document.getElementById('zeroconfig').innerHTML);
this.config = {
...initialConfig,
media: mediaConfig,
linkPicker: linksConfig,
...(opts || {})
};
console.info(this.config);
2021-05-08 15:20:23 +02:00
this._vue = vue;
2020-10-16 11:23:54 +02:00
this.use(ZeroPlugin);
2020-11-01 22:20:29 +01:00
//this.use(CommercePlugin);
//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()
{
2021-05-08 15:20:23 +02:00
return this._plugins;
2020-10-16 11:23:54 +02:00
}
2020-10-16 14:02:10 +02:00
/*
* Get all installed zero plugins
*/
get router()
{
2021-05-08 15:20:23 +02:00
return this._router;
2020-10-16 14:02:10 +02:00
}
2021-03-05 15:35:55 +01:00
/*
* Reload config
*/
reloadConfig(config)
{
return Axios.get('zerovue/config').then(res =>
{
this.config = {
...res.data,
media: mediaConfig,
linkPicker: linksConfig,
...(config || {})
};
});
}
2020-10-16 11:23:54 +02:00
/*
* Locates zero plugins and install them
*/
setup()
{
2021-05-08 15:20:23 +02:00
this._router = new VueRouter({
2020-10-16 14:02:10 +02:00
...routerConfig,
2021-05-08 15:20:23 +02:00
routes: this._routes
2020-10-16 14:02:10 +02:00
});
2021-05-08 15:20:23 +02:00
this._router.beforeEach(routerConfig.beforeEach);
2020-10-16 14:02:10 +02:00
//const result = await Axios.get('zerovue/config');
//this.config = { ...this.config, ...result.data };
2021-05-08 15:20:23 +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')
{
2021-05-08 15:20:23 +02:00
plugin.install(this._vue, this);
2020-10-16 11:23:54 +02:00
}
2021-05-08 15:20:23 +02:00
this._plugins.push(plugin);
2020-10-16 11:23:54 +02:00
2020-10-16 14:02:10 +02:00
// append routes
2021-05-08 15:20:23 +02:00
if (this._setupDone)
2020-10-19 15:58:32 +02:00
{
2021-05-08 15:20:23 +02:00
this._router.addRoutes(plugin.routes);
2020-10-19 15:58:32 +02:00
}
else
{
2021-05-08 15:20:23 +02:00
plugin.routes.forEach(x => this._routes.push(x));
2020-10-19 15:58:32 +02:00
}
2020-10-16 14:15:45 +02:00
2020-10-18 23:47:59 +02:00
// append editors
2021-05-08 15:20:23 +02:00
plugin.editors.forEach(x => this.addOrReplace(this._editors, x, 'alias'));
2020-10-16 14:02:10 +02:00
2020-10-19 14:48:21 +02:00
// append lists
2021-05-08 15:20:23 +02:00
plugin.lists.forEach(x => this.addOrReplace(this._lists, x, 'alias'));
2020-10-19 14:48:21 +02:00
2021-01-30 16:52:36 +01:00
// append
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
{
2021-05-08 15:20:23 +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)
{
2021-05-08 15:20:23 +02:00
const renderer = this._lists.find(x => x.alias === alias);
2020-10-19 14:48:21 +02:00
if (!renderer)
{
console.warn(`[zero] Could not find list renderer ${alias}`);
2020-10-16 14:15:45 +02:00
}
return renderer;
}
2020-11-17 15:44:04 +01:00
/*
* Adds or replaces an item in an array by key
*/
private addOrReplace(array, item, byKey)
{
const existingRenderer = array.find(x => x[byKey] === item[byKey]);
if (existingRenderer)
{
const index = array.indexOf(existingRenderer);
array.splice(index, 1, item);
}
else
{
array.push(item);
}
}
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
2020-11-01 23:56:22 +01:00
plugins.forEach(plugin =>
{
zero.use(plugin);
});
2020-10-16 11:23:54 +02:00
};
export default Zero;