2020-11-24 16:35:11 +00:00
|
|
|
import { boot } from 'quasar/wrappers';
|
|
|
|
import { Store } from 'vuex';
|
|
|
|
import { FG_Plugin } from 'src/plugins';
|
2020-10-31 14:09:02 +00:00
|
|
|
import routes from 'src/router/routes';
|
2021-01-30 07:38:44 +00:00
|
|
|
import { api } from 'boot/axios';
|
2020-11-24 16:35:11 +00:00
|
|
|
import { AxiosResponse } from 'axios';
|
2021-01-30 03:19:30 +00:00
|
|
|
import { Router, RouteRecordRaw } from 'vue-router';
|
2021-01-30 04:45:24 +00:00
|
|
|
import { UserSessionState } from 'src/plugins/user/store';
|
2020-10-12 21:49:05 +00:00
|
|
|
|
2021-01-30 07:38:44 +00:00
|
|
|
const config: { [key: string]: Array<string> } = {
|
2020-10-14 20:27:20 +00:00
|
|
|
// Do not change required Modules !!
|
2020-10-31 14:09:02 +00:00
|
|
|
requiredModules: ['User'],
|
2020-10-14 20:27:20 +00:00
|
|
|
// here you can import plugins.
|
2021-03-18 16:23:57 +00:00
|
|
|
loadModules: ['Balance', 'Schedule', 'Pricelist'],
|
2020-10-14 20:27:20 +00:00
|
|
|
};
|
2020-10-12 21:49:05 +00:00
|
|
|
|
2020-10-14 20:27:20 +00:00
|
|
|
// do not change anything here !!
|
|
|
|
|
|
|
|
// combine routes from source to target
|
2020-10-31 14:09:02 +00:00
|
|
|
|
2020-11-13 12:42:15 +00:00
|
|
|
interface BackendPlugin {
|
|
|
|
permissions: string[];
|
|
|
|
version: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface BackendPlugins {
|
2020-11-24 16:35:11 +00:00
|
|
|
[key: string]: BackendPlugin | null;
|
2020-11-13 12:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Backend {
|
2020-11-24 16:35:11 +00:00
|
|
|
plugins: BackendPlugins[];
|
2020-11-13 12:42:15 +00:00
|
|
|
version: string;
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:35:11 +00:00
|
|
|
export { Backend };
|
2020-11-13 12:42:15 +00:00
|
|
|
|
2021-01-30 03:19:30 +00:00
|
|
|
function setPermissions(object: FG_Plugin.PluginRouteConfig) {
|
|
|
|
if (object.route.meta === undefined) object.route.meta = {};
|
|
|
|
object.route.meta['permissions'] = object.permissions;
|
|
|
|
}
|
|
|
|
|
2021-01-30 07:38:44 +00:00
|
|
|
function convertRoutes(parent: RouteRecordRaw, children?: FG_Plugin.PluginRouteConfig[]) {
|
|
|
|
if (children === undefined) return;
|
|
|
|
|
|
|
|
children.forEach((child) => {
|
|
|
|
setPermissions(child);
|
|
|
|
convertRoutes(child.route, child.children);
|
|
|
|
if (parent.children === undefined) parent.children = [];
|
|
|
|
parent.children.push(child.route);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:09:02 +00:00
|
|
|
function combineRoutes(
|
2021-01-30 03:19:30 +00:00
|
|
|
target: RouteRecordRaw[],
|
2020-10-31 14:09:02 +00:00
|
|
|
source: FG_Plugin.PluginRouteConfig[],
|
|
|
|
mainPath: '/' | '/main' = '/'
|
2021-01-30 03:19:30 +00:00
|
|
|
): RouteRecordRaw[] {
|
2021-01-30 07:38:44 +00:00
|
|
|
// Search parent
|
2021-03-18 16:23:57 +00:00
|
|
|
target.forEach((target) => {
|
2020-10-31 14:09:02 +00:00
|
|
|
if (target.path === mainPath) {
|
2021-01-30 07:38:44 +00:00
|
|
|
// Parent found = target
|
2020-10-31 14:09:02 +00:00
|
|
|
source.forEach((sourceMainConfig: FG_Plugin.PluginRouteConfig) => {
|
2021-01-30 07:38:44 +00:00
|
|
|
// Check if source is already in target
|
2021-01-30 03:19:30 +00:00
|
|
|
const targetMainConfig = target.children?.find((targetMainConfig: RouteRecordRaw) => {
|
|
|
|
return sourceMainConfig.route.path === targetMainConfig.path;
|
2020-11-24 17:35:37 +00:00
|
|
|
});
|
2021-01-30 07:38:44 +00:00
|
|
|
// Already in target routes, add only children
|
2020-10-31 14:09:02 +00:00
|
|
|
if (targetMainConfig) {
|
2021-01-30 07:38:44 +00:00
|
|
|
convertRoutes(targetMainConfig, sourceMainConfig.children);
|
2020-10-31 14:09:02 +00:00
|
|
|
} else {
|
2021-01-30 07:38:44 +00:00
|
|
|
// Append to target
|
2020-10-31 14:09:02 +00:00
|
|
|
if (target.children === undefined) {
|
|
|
|
target.children = [];
|
2020-10-13 18:17:00 +00:00
|
|
|
}
|
2021-01-30 07:38:44 +00:00
|
|
|
convertRoutes(sourceMainConfig.route, sourceMainConfig.children);
|
2020-11-24 17:35:37 +00:00
|
|
|
if (
|
|
|
|
sourceMainConfig.children &&
|
|
|
|
sourceMainConfig.children.length > 0 &&
|
2021-01-30 03:19:30 +00:00
|
|
|
!sourceMainConfig.route.component
|
2020-11-24 17:35:37 +00:00
|
|
|
)
|
2021-01-30 07:38:44 +00:00
|
|
|
Object.assign(sourceMainConfig.route, {
|
2021-01-30 03:19:30 +00:00
|
|
|
component: () => import('src/components/navigation/EmptyParent.vue'),
|
|
|
|
});
|
2021-01-30 07:38:44 +00:00
|
|
|
target.children.push(sourceMainConfig.route);
|
2020-10-13 18:17:00 +00:00
|
|
|
}
|
2020-10-31 14:09:02 +00:00
|
|
|
});
|
2020-10-13 18:17:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2020-10-14 20:27:20 +00:00
|
|
|
// combine Links of Plugins from source to target
|
2020-10-31 14:09:02 +00:00
|
|
|
function combineMainLinks(
|
|
|
|
target: FG_Plugin.PluginMainLink[],
|
|
|
|
source: FG_Plugin.PluginRouteConfig
|
|
|
|
): FG_Plugin.PluginMainLink[] {
|
2020-11-24 17:35:37 +00:00
|
|
|
const targetPluginMainLink: FG_Plugin.PluginMainLink | undefined = target.find(
|
2020-10-31 14:09:02 +00:00
|
|
|
(targetPluginMainLink: FG_Plugin.PluginMainLink) => {
|
2020-10-13 18:17:00 +00:00
|
|
|
console.log(targetPluginMainLink.title, source.title);
|
|
|
|
return targetPluginMainLink.title == source.title;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (targetPluginMainLink) {
|
2020-11-24 17:35:37 +00:00
|
|
|
source.children?.forEach((sourcePluginChildLink: FG_Plugin.PluginRouteConfig) => {
|
|
|
|
targetPluginMainLink.children.push(<FG_Plugin.PluginChildLink>{
|
|
|
|
title: sourcePluginChildLink.title,
|
|
|
|
icon: sourcePluginChildLink.icon,
|
2021-01-30 03:19:30 +00:00
|
|
|
link: sourcePluginChildLink.route.name,
|
|
|
|
name: sourcePluginChildLink.route.name,
|
|
|
|
permissions: sourcePluginChildLink.permissions,
|
2020-11-24 17:35:37 +00:00
|
|
|
});
|
|
|
|
});
|
2020-10-13 18:17:00 +00:00
|
|
|
} else {
|
2020-10-31 14:09:02 +00:00
|
|
|
const mainLink: FG_Plugin.PluginMainLink = <FG_Plugin.PluginMainLink>{
|
|
|
|
title: source.title,
|
|
|
|
icon: source.icon,
|
2021-01-30 03:19:30 +00:00
|
|
|
link: source.route.name,
|
|
|
|
name: source.route.name,
|
|
|
|
permissions: source.permissions,
|
2020-10-31 14:09:02 +00:00
|
|
|
};
|
2021-03-18 16:23:57 +00:00
|
|
|
source.children?.forEach((child) => {
|
2020-10-31 14:09:02 +00:00
|
|
|
if (mainLink.children === undefined) {
|
|
|
|
mainLink.children = [];
|
|
|
|
}
|
|
|
|
mainLink.children.push(<FG_Plugin.PluginChildLink>{
|
|
|
|
title: child.title,
|
|
|
|
icon: child.icon,
|
2021-01-30 03:19:30 +00:00
|
|
|
link: child.route.name,
|
|
|
|
name: child.route.name,
|
|
|
|
permissions: child.permissions,
|
2020-10-31 14:09:02 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
target.push(mainLink);
|
2020-10-13 18:17:00 +00:00
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:09:02 +00:00
|
|
|
function loadShortCuts(
|
|
|
|
target: FG_Plugin.ShortCutLink[],
|
|
|
|
source: FG_Plugin.PluginRouteConfig[]
|
|
|
|
): FG_Plugin.ShortCutLink[] {
|
2021-03-18 16:23:57 +00:00
|
|
|
source.forEach((route) => {
|
2020-10-31 14:09:02 +00:00
|
|
|
if (route.shortcut) {
|
|
|
|
target.push(<FG_Plugin.ShortCutLink>{
|
2021-01-30 03:19:30 +00:00
|
|
|
link: route.route.name,
|
2020-10-31 16:33:09 +00:00
|
|
|
icon: route.icon,
|
2021-01-30 03:19:30 +00:00
|
|
|
permissions: route.permissions,
|
2020-10-31 14:09:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (route.children) {
|
|
|
|
target = loadShortCuts(target, route.children);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2020-10-14 20:27:20 +00:00
|
|
|
// loade plugins
|
2020-10-31 14:09:02 +00:00
|
|
|
function loadPlugin(
|
|
|
|
loadedPlugins: FG_Plugin.LoadedPlugins,
|
2020-10-14 20:27:20 +00:00
|
|
|
modules: string[],
|
2020-11-13 12:42:15 +00:00
|
|
|
backendpromise: Promise<Backend | null>,
|
2020-11-14 09:58:21 +00:00
|
|
|
plugins: FG_Plugin.Plugin[],
|
2021-01-30 04:45:24 +00:00
|
|
|
store: Store<UserSessionState>,
|
2021-01-30 03:19:30 +00:00
|
|
|
router: Router
|
2020-10-31 14:09:02 +00:00
|
|
|
): FG_Plugin.LoadedPlugins {
|
2021-03-18 16:23:57 +00:00
|
|
|
modules.forEach((requiredModule) => {
|
|
|
|
const plugin = plugins.find((plugin) => {
|
2020-10-13 18:17:00 +00:00
|
|
|
return plugin.name == requiredModule;
|
|
|
|
});
|
|
|
|
if (plugin) {
|
2020-10-31 14:09:02 +00:00
|
|
|
if (plugin.mainRoutes) {
|
2020-11-24 17:35:37 +00:00
|
|
|
loadedPlugins.routes = combineRoutes(loadedPlugins.routes, plugin.mainRoutes, '/main');
|
2021-03-18 16:23:57 +00:00
|
|
|
plugin.mainRoutes.forEach((route) => {
|
2020-11-24 17:35:37 +00:00
|
|
|
loadedPlugins.mainLinks = combineMainLinks(loadedPlugins.mainLinks, route);
|
2020-10-31 14:09:02 +00:00
|
|
|
});
|
2020-11-24 17:35:37 +00:00
|
|
|
loadedPlugins.shortcuts = loadShortCuts(loadedPlugins.shortcuts, plugin.mainRoutes);
|
2021-01-30 07:38:44 +00:00
|
|
|
console.log(loadedPlugins);
|
2020-10-31 14:09:02 +00:00
|
|
|
}
|
|
|
|
if (plugin.outRoutes) {
|
2020-11-24 17:35:37 +00:00
|
|
|
loadedPlugins.routes = combineRoutes(loadedPlugins.routes, plugin.outRoutes);
|
|
|
|
loadedPlugins.shortcutsOut = loadShortCuts(loadedPlugins.shortcutsOut, plugin.outRoutes);
|
2020-10-31 14:09:02 +00:00
|
|
|
}
|
2020-11-13 03:01:53 +00:00
|
|
|
if (plugin.widgets.length > 0) {
|
2021-03-18 16:23:57 +00:00
|
|
|
plugin.widgets.forEach((widget) => (widget.name = plugin.name + '_' + widget.name));
|
2020-11-13 03:01:53 +00:00
|
|
|
Array.prototype.push.apply(loadedPlugins.widgets, plugin.widgets);
|
2020-11-10 00:33:55 +00:00
|
|
|
}
|
2020-10-13 18:17:00 +00:00
|
|
|
if (plugin.store) {
|
2020-10-16 06:45:40 +00:00
|
|
|
plugin.store.forEach((store_plugin, store_namespace) => {
|
|
|
|
store.registerModule(store_namespace, store_plugin);
|
2020-10-13 18:17:00 +00:00
|
|
|
});
|
|
|
|
}
|
2020-10-14 20:27:20 +00:00
|
|
|
loadedPlugins.plugins.push({
|
|
|
|
name: plugin.name,
|
2021-03-18 16:23:57 +00:00
|
|
|
version: plugin.version,
|
2020-10-14 20:27:20 +00:00
|
|
|
});
|
2020-10-13 18:17:00 +00:00
|
|
|
} else {
|
2020-11-10 00:33:55 +00:00
|
|
|
console.exception(`Could not find required Plugin ${requiredModule}`);
|
2021-03-18 16:23:57 +00:00
|
|
|
router.push({ name: 'error' }).catch((e) => {
|
2020-11-24 16:35:11 +00:00
|
|
|
console.warn(e);
|
|
|
|
});
|
2020-10-13 18:17:00 +00:00
|
|
|
}
|
|
|
|
});
|
2020-10-14 20:27:20 +00:00
|
|
|
return loadedPlugins;
|
|
|
|
}
|
2020-10-13 18:17:00 +00:00
|
|
|
|
2020-11-13 12:42:15 +00:00
|
|
|
async function getBackend(): Promise<Backend | null> {
|
|
|
|
let backend: Backend | null = null;
|
|
|
|
try {
|
2021-01-30 07:38:44 +00:00
|
|
|
const response: AxiosResponse<Backend> = await api.get('/');
|
2020-11-13 12:42:15 +00:00
|
|
|
backend = response.data;
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
return null;
|
|
|
|
} finally {
|
|
|
|
return backend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 20:27:20 +00:00
|
|
|
// "async" is optional;
|
|
|
|
// more info on params: https://quasar.dev/quasar-cli/cli-documentation/boot-files#Anatomy-of-a-boot-file
|
2021-01-30 04:45:24 +00:00
|
|
|
export default boot<UserSessionState>(({ router, store, app }) => {
|
2020-11-14 09:58:21 +00:00
|
|
|
const plugins: FG_Plugin.Plugin[] = [];
|
2020-11-13 12:42:15 +00:00
|
|
|
|
|
|
|
const backendPromise = getBackend();
|
|
|
|
|
2020-10-31 14:09:02 +00:00
|
|
|
let loadedPlugins: FG_Plugin.LoadedPlugins = {
|
|
|
|
routes,
|
2020-10-14 20:27:20 +00:00
|
|
|
plugins: [],
|
|
|
|
mainLinks: [],
|
|
|
|
shortcuts: [],
|
2020-11-10 00:33:55 +00:00
|
|
|
shortcutsOut: [],
|
2021-03-18 16:23:57 +00:00
|
|
|
widgets: [],
|
2020-10-14 20:27:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// get all plugins
|
|
|
|
const pluginsContext = require.context('src/plugins', true, /.+\/plugin.ts$/);
|
|
|
|
pluginsContext.keys().forEach((fileName: string) => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
|
|
plugins.push(pluginsContext(fileName).default);
|
2020-10-12 21:49:05 +00:00
|
|
|
});
|
2020-10-14 20:27:20 +00:00
|
|
|
|
2020-11-14 09:58:21 +00:00
|
|
|
// check dependencies
|
2020-11-24 16:35:11 +00:00
|
|
|
backendPromise
|
2021-03-18 16:23:57 +00:00
|
|
|
.then((backend) => {
|
2020-11-24 16:35:11 +00:00
|
|
|
console.log(backend);
|
|
|
|
if (backend) {
|
|
|
|
plugins.forEach((plugin: FG_Plugin.Plugin) => {
|
|
|
|
plugin.requiredModules.forEach((requiredModule: string) => {
|
|
|
|
if (
|
|
|
|
!(
|
|
|
|
config.requiredModules.includes(requiredModule) ||
|
|
|
|
config.loadModules.includes(requiredModule)
|
|
|
|
)
|
|
|
|
) {
|
2020-11-24 17:35:37 +00:00
|
|
|
console.error(`Plugin ${plugin.name} need Plugin ${requiredModule}`);
|
2021-03-18 16:23:57 +00:00
|
|
|
router.push({ name: 'error' }).catch((e) => {
|
2020-11-24 16:35:11 +00:00
|
|
|
console.warn(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-11-24 17:35:37 +00:00
|
|
|
plugin.requiredBackendModules.forEach((requiredBackendModule: string) => {
|
|
|
|
if (!(requiredBackendModule in backend.plugins)) {
|
|
|
|
console.error(
|
|
|
|
`Plugin ${plugin.name} need Plugin ${requiredBackendModule} in backend.`
|
|
|
|
);
|
2021-03-18 16:23:57 +00:00
|
|
|
router.push({ name: 'error' }).catch((err) => {
|
2020-11-24 17:35:37 +00:00
|
|
|
console.warn(err);
|
|
|
|
});
|
2020-11-24 16:35:11 +00:00
|
|
|
}
|
2020-11-24 17:35:37 +00:00
|
|
|
});
|
2020-11-24 16:35:11 +00:00
|
|
|
});
|
|
|
|
}
|
2020-11-14 09:58:21 +00:00
|
|
|
})
|
2021-03-18 16:23:57 +00:00
|
|
|
.catch((e) => {
|
2020-11-24 16:35:11 +00:00
|
|
|
console.error(e);
|
|
|
|
});
|
2020-11-14 09:58:21 +00:00
|
|
|
|
2020-10-14 20:27:20 +00:00
|
|
|
// load plugins
|
|
|
|
loadedPlugins = loadPlugin(
|
|
|
|
loadedPlugins,
|
|
|
|
config.requiredModules,
|
2020-11-13 12:42:15 +00:00
|
|
|
backendPromise,
|
2020-10-14 20:27:20 +00:00
|
|
|
plugins,
|
2020-11-13 17:48:50 +00:00
|
|
|
store,
|
|
|
|
router
|
2020-10-14 20:27:20 +00:00
|
|
|
);
|
2020-11-24 16:35:11 +00:00
|
|
|
loadedPlugins = loadPlugin(
|
|
|
|
loadedPlugins,
|
|
|
|
config.loadModules,
|
|
|
|
backendPromise,
|
|
|
|
plugins,
|
|
|
|
store,
|
|
|
|
router
|
|
|
|
);
|
2020-10-14 20:27:20 +00:00
|
|
|
|
2020-11-10 00:33:55 +00:00
|
|
|
loadedPlugins.widgets.sort((a, b) => b.priority - a.priority);
|
2020-10-14 20:27:20 +00:00
|
|
|
|
2021-01-30 03:19:30 +00:00
|
|
|
loadedPlugins.routes.forEach((route) => router.addRoute(route));
|
2020-10-14 20:27:20 +00:00
|
|
|
|
|
|
|
// save plugins in VM-variable
|
2021-01-30 03:19:30 +00:00
|
|
|
app.provide('flaschengeistPlugins', loadedPlugins);
|
2020-10-12 21:49:05 +00:00
|
|
|
});
|