2020-10-12 21:49:05 +00:00
|
|
|
import { boot } from 'quasar/wrappers';
|
|
|
|
import { RouteConfig } from 'vue-router';
|
2020-10-16 06:45:40 +00:00
|
|
|
import { Module, Store } from 'vuex';
|
2020-10-12 21:49:05 +00:00
|
|
|
|
2020-10-14 20:27:20 +00:00
|
|
|
const config = {
|
|
|
|
// Do not change required Modules !!
|
|
|
|
requiredModules: ['user'],
|
|
|
|
// here you can import plugins.
|
2020-10-16 06:45:40 +00:00
|
|
|
loadModules: []
|
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 !!
|
|
|
|
|
|
|
|
interface ShortCutLink {
|
2020-10-12 21:49:05 +00:00
|
|
|
link: string;
|
2020-10-14 20:27:20 +00:00
|
|
|
icon: string;
|
2020-10-12 21:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Plugin {
|
2020-10-13 18:17:00 +00:00
|
|
|
name: string;
|
2020-10-14 20:27:20 +00:00
|
|
|
routes: RouteConfig[];
|
2020-10-16 06:45:40 +00:00
|
|
|
store?: Map<string, Module<any, any>>;
|
2020-10-13 09:27:27 +00:00
|
|
|
mainLink: PluginMainLink;
|
2020-10-13 18:17:00 +00:00
|
|
|
requiredModules: string[];
|
2020-10-14 20:41:50 +00:00
|
|
|
shortcuts: ShortCutLink[];
|
|
|
|
shortcutsOut: ShortCutLink[];
|
2020-10-14 20:27:20 +00:00
|
|
|
version: string;
|
2020-10-12 21:49:05 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 09:27:27 +00:00
|
|
|
interface PluginMainLink extends PluginChildLink {
|
2020-10-13 18:17:00 +00:00
|
|
|
children: PluginChildLink[];
|
2020-10-13 09:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface PluginChildLink {
|
|
|
|
name: string;
|
|
|
|
title: string;
|
|
|
|
link: string;
|
|
|
|
icon: string;
|
|
|
|
}
|
|
|
|
|
2020-10-14 20:27:20 +00:00
|
|
|
interface LoadedPlugin {
|
|
|
|
name: string;
|
|
|
|
version: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface LoadedPlugins {
|
|
|
|
plugins: LoadedPlugin[];
|
|
|
|
routes: RouteConfig[];
|
|
|
|
mainLinks: PluginMainLink[];
|
|
|
|
shortcuts: ShortCutLink[];
|
|
|
|
shortcutsOut: ShortCutLink[];
|
|
|
|
}
|
2020-10-13 18:17:00 +00:00
|
|
|
|
2020-10-14 20:27:20 +00:00
|
|
|
export {
|
|
|
|
Plugin,
|
|
|
|
PluginChildLink,
|
|
|
|
PluginMainLink,
|
|
|
|
ShortCutLink,
|
|
|
|
LoadedPlugins,
|
|
|
|
LoadedPlugin
|
|
|
|
};
|
2020-10-12 21:49:05 +00:00
|
|
|
|
2020-10-14 20:27:20 +00:00
|
|
|
// combine routes from source to target
|
2020-10-13 18:17:00 +00:00
|
|
|
function combineRoutes(
|
|
|
|
target: RouteConfig[],
|
|
|
|
source: RouteConfig[]
|
|
|
|
): RouteConfig[] {
|
2020-10-16 06:45:40 +00:00
|
|
|
// iterate first layer e.g. /main, / etc.
|
2020-10-13 18:17:00 +00:00
|
|
|
source.forEach((sourceRouteConfig: RouteConfig) => {
|
|
|
|
const targetRouteConfig: RouteConfig | undefined = target.find(
|
|
|
|
(routeConfig: RouteConfig) => {
|
|
|
|
return sourceRouteConfig.path == routeConfig.path;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (targetRouteConfig) {
|
2020-10-14 20:27:20 +00:00
|
|
|
// if exists first layer in target exist iterate through 2nd layer e.g. /main/user, /main/about
|
2020-10-13 18:17:00 +00:00
|
|
|
sourceRouteConfig.children?.forEach(
|
|
|
|
(sourcePluginChildRouteConfig: RouteConfig) => {
|
|
|
|
const targetPluginRouteConfig:
|
|
|
|
| RouteConfig
|
|
|
|
| undefined = targetRouteConfig.children?.find(
|
|
|
|
(routeConfig: RouteConfig) => {
|
|
|
|
return sourcePluginChildRouteConfig.path == routeConfig.path;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (targetPluginRouteConfig) {
|
2020-10-14 20:27:20 +00:00
|
|
|
// if 2nd layer in target exist check if target has children path.
|
2020-10-13 18:17:00 +00:00
|
|
|
if (targetPluginRouteConfig.children) {
|
2020-10-14 20:27:20 +00:00
|
|
|
// if target has children path, add children from source.
|
2020-10-13 18:17:00 +00:00
|
|
|
targetPluginRouteConfig.children = Object.assign(
|
|
|
|
targetPluginRouteConfig.children,
|
|
|
|
sourcePluginChildRouteConfig.children
|
|
|
|
);
|
|
|
|
} else {
|
2020-10-14 20:27:20 +00:00
|
|
|
// if not set children of targen from children of source
|
2020-10-13 18:17:00 +00:00
|
|
|
targetPluginRouteConfig.children =
|
|
|
|
sourcePluginChildRouteConfig.children;
|
|
|
|
}
|
|
|
|
} else {
|
2020-10-14 20:27:20 +00:00
|
|
|
// if target not exists in 2nd layer, add source to children of first targen
|
2020-10-13 18:17:00 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
targetRouteConfig.children.push(sourcePluginChildRouteConfig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
2020-10-14 20:27:20 +00:00
|
|
|
// if target not exists in first layer, add source
|
2020-10-13 18:17:00 +00:00
|
|
|
target.push(sourceRouteConfig);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2020-10-14 20:27:20 +00:00
|
|
|
// combine Links of Plugins from source to target
|
2020-10-13 18:17:00 +00:00
|
|
|
function combineMainLinks(
|
|
|
|
target: PluginMainLink[],
|
|
|
|
source: PluginMainLink
|
|
|
|
): PluginMainLink[] {
|
|
|
|
const targetPluginMainLink: PluginMainLink | undefined = target.find(
|
|
|
|
(targetPluginMainLink: PluginMainLink) => {
|
|
|
|
console.log(targetPluginMainLink.title, source.title);
|
|
|
|
return targetPluginMainLink.title == source.title;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (targetPluginMainLink) {
|
|
|
|
source.children.forEach((sourcePluginChildLink: PluginChildLink) => {
|
|
|
|
targetPluginMainLink.children.push(sourcePluginChildLink);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
target.push(source);
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2020-10-14 20:27:20 +00:00
|
|
|
// loade plugins
|
|
|
|
function loadPlugin(
|
|
|
|
loadedPlugins: LoadedPlugins,
|
|
|
|
modules: string[],
|
|
|
|
plugins: Plugin[],
|
2020-10-16 06:45:40 +00:00
|
|
|
store: Store<any>
|
2020-10-14 20:27:20 +00:00
|
|
|
): LoadedPlugins {
|
|
|
|
modules.forEach(requiredModule => {
|
2020-10-13 18:17:00 +00:00
|
|
|
const plugin = plugins.find(plugin => {
|
|
|
|
return plugin.name == requiredModule;
|
|
|
|
});
|
|
|
|
if (plugin) {
|
2020-10-14 20:27:20 +00:00
|
|
|
loadedPlugins.routes = combineRoutes(loadedPlugins.routes, plugin.routes);
|
2020-10-13 18:17:00 +00:00
|
|
|
if (plugin.store) {
|
2020-10-16 06:45:40 +00:00
|
|
|
console.log(plugin.store);
|
|
|
|
console.log(plugin.store.keys());
|
|
|
|
plugin.store.forEach((store_plugin, store_namespace) => {
|
|
|
|
store.registerModule(store_namespace, store_plugin);
|
2020-10-13 18:17:00 +00:00
|
|
|
});
|
2020-10-16 06:45:40 +00:00
|
|
|
//.forEach(store_key => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
|
|
|
|
//store.registerModule(store_key, plugin.store.get(store_key));
|
|
|
|
//});
|
2020-10-13 18:17:00 +00:00
|
|
|
}
|
2020-10-14 20:27:20 +00:00
|
|
|
loadedPlugins.mainLinks = combineMainLinks(
|
|
|
|
loadedPlugins.mainLinks,
|
|
|
|
plugin.mainLink
|
|
|
|
);
|
2020-10-14 20:41:50 +00:00
|
|
|
loadedPlugins.shortcuts = loadedPlugins.shortcuts.concat(
|
|
|
|
plugin.shortcuts
|
|
|
|
);
|
|
|
|
loadedPlugins.shortcutsOut = loadedPlugins.shortcutsOut.concat(
|
|
|
|
plugin.shortcutsOut
|
|
|
|
);
|
2020-10-14 20:27:20 +00:00
|
|
|
loadedPlugins.plugins.push({
|
|
|
|
name: plugin.name,
|
|
|
|
version: plugin.version
|
|
|
|
});
|
2020-10-13 18:17:00 +00:00
|
|
|
} else {
|
|
|
|
console.exception(`Don't find required Plugin ${requiredModule}`);
|
|
|
|
}
|
|
|
|
});
|
2020-10-14 20:27:20 +00:00
|
|
|
return loadedPlugins;
|
|
|
|
}
|
2020-10-13 18:17:00 +00:00
|
|
|
|
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
|
|
|
|
export default boot(({ Vue, router, store }) => {
|
|
|
|
const plugins: Plugin[] = [];
|
|
|
|
let loadedPlugins: LoadedPlugins = {
|
|
|
|
routes: [],
|
|
|
|
plugins: [],
|
|
|
|
mainLinks: [],
|
|
|
|
shortcuts: [],
|
|
|
|
shortcutsOut: []
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// load plugins
|
|
|
|
loadedPlugins = loadPlugin(
|
|
|
|
loadedPlugins,
|
|
|
|
config.requiredModules,
|
|
|
|
plugins,
|
|
|
|
store
|
|
|
|
);
|
|
|
|
loadedPlugins = loadPlugin(loadedPlugins, config.loadModules, plugins, store);
|
|
|
|
|
|
|
|
console.log(loadedPlugins.routes);
|
|
|
|
|
|
|
|
// add new routes for plugins
|
|
|
|
router.addRoutes(loadedPlugins.routes);
|
|
|
|
|
|
|
|
// save plugins in VM-variable
|
2020-10-13 21:13:42 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
2020-10-14 20:27:20 +00:00
|
|
|
Vue.prototype.$flaschengeistPlugins = loadedPlugins;
|
|
|
|
console.log(loadedPlugins);
|
2020-10-14 20:41:50 +00:00
|
|
|
console.log(Vue.prototype.$flaschengeistPlugins);
|
2020-10-12 21:49:05 +00:00
|
|
|
});
|