2019-01-23 12:11:44 +00:00
|
|
|
import ngModule from '../module';
|
2019-01-23 16:49:28 +00:00
|
|
|
import getMainRoute from '../lib/get-main-route';
|
2019-01-23 12:11:44 +00:00
|
|
|
|
|
|
|
export default class Modules {
|
2020-03-11 11:20:15 +00:00
|
|
|
constructor(aclService, $window, $translate) {
|
2019-01-23 12:11:44 +00:00
|
|
|
Object.assign(this, {
|
|
|
|
aclService,
|
2020-03-11 11:20:15 +00:00
|
|
|
$window,
|
|
|
|
$translate
|
2019-01-23 12:11:44 +00:00
|
|
|
});
|
|
|
|
}
|
2019-11-01 12:02:47 +00:00
|
|
|
|
2020-05-06 12:38:09 +00:00
|
|
|
fetch() {
|
|
|
|
const map = {};
|
2020-03-11 11:20:15 +00:00
|
|
|
const modules = [];
|
2020-05-06 12:38:09 +00:00
|
|
|
|
2019-01-23 12:11:44 +00:00
|
|
|
for (let mod of this.$window.routes) {
|
|
|
|
if (!mod || !mod.routes) continue;
|
|
|
|
|
|
|
|
let route = getMainRoute(mod.routes);
|
|
|
|
if (!route || (route.acl && !this.aclService.hasAny(route.acl)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
let keyBind;
|
2019-01-23 16:49:28 +00:00
|
|
|
if (mod.keybindings) {
|
2019-01-24 08:25:51 +00:00
|
|
|
let res = mod.keybindings.find(i => i.state == route.state);
|
2019-01-23 12:11:44 +00:00
|
|
|
if (res) keyBind = res.key.toUpperCase();
|
|
|
|
}
|
|
|
|
|
2020-05-06 12:38:09 +00:00
|
|
|
const module = {
|
2019-01-23 12:11:44 +00:00
|
|
|
name: mod.name || mod.module,
|
2022-12-14 12:49:49 +00:00
|
|
|
code: mod.module,
|
2019-01-23 12:11:44 +00:00
|
|
|
icon: mod.icon || null,
|
|
|
|
route,
|
|
|
|
keyBind
|
2020-05-06 12:38:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
modules.push(module);
|
|
|
|
map[mod.module] = mod;
|
2019-01-23 12:11:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 11:20:15 +00:00
|
|
|
const sortedModules = modules.sort((a, b) => {
|
|
|
|
const translatedNameA = this.$translate.instant(a.name);
|
|
|
|
const translatedNameB = this.$translate.instant(b.name);
|
|
|
|
return translatedNameA.localeCompare(translatedNameB);
|
|
|
|
});
|
2020-03-11 12:36:56 +00:00
|
|
|
this.modules = sortedModules;
|
2020-05-06 12:38:09 +00:00
|
|
|
this.map = map;
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.modules = null;
|
|
|
|
this.map = null;
|
|
|
|
}
|
2020-03-11 11:20:15 +00:00
|
|
|
|
2020-05-06 12:38:09 +00:00
|
|
|
get() {
|
|
|
|
if (!this.modules) this.fetch();
|
2020-03-11 12:36:56 +00:00
|
|
|
return this.modules;
|
2019-01-23 12:11:44 +00:00
|
|
|
}
|
2020-05-06 12:38:09 +00:00
|
|
|
|
|
|
|
getMap() {
|
|
|
|
if (!this.map) this.fetch();
|
|
|
|
return this.map;
|
|
|
|
}
|
2019-01-23 12:11:44 +00:00
|
|
|
}
|
2020-03-11 11:20:15 +00:00
|
|
|
Modules.$inject = ['aclService', '$window', '$translate'];
|
2019-01-23 12:11:44 +00:00
|
|
|
|
|
|
|
ngModule.service('vnModules', Modules);
|