salix/front/core/services/modules.js

69 lines
1.8 KiB
JavaScript
Raw Normal View History

import ngModule from '../module';
import getMainRoute from '../lib/get-main-route';
export default class Modules {
2020-03-11 11:20:15 +00:00
constructor(aclService, $window, $translate) {
Object.assign(this, {
aclService,
2020-03-11 11:20:15 +00:00
$window,
$translate
});
}
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
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;
if (mod.keybindings) {
2019-01-24 08:25:51 +00:00
let res = mod.keybindings.find(i => i.state == route.state);
if (res) keyBind = res.key.toUpperCase();
}
2020-05-06 12:38:09 +00:00
const module = {
name: mod.name || mod.module,
2022-12-14 12:49:49 +00:00
code: mod.module,
icon: mod.icon || null,
route,
keyBind
2020-05-06 12:38:09 +00:00
};
modules.push(module);
map[mod.module] = mod;
}
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;
}
2020-05-06 12:38:09 +00:00
getMap() {
if (!this.map) this.fetch();
return this.map;
}
}
2020-03-11 11:20:15 +00:00
Modules.$inject = ['aclService', '$window', '$translate'];
ngModule.service('vnModules', Modules);