salix/front/core/services/modules.js

69 lines
1.8 KiB
JavaScript

import ngModule from '../module';
import getMainRoute from '../lib/get-main-route';
export default class Modules {
constructor(aclService, $window, $translate) {
Object.assign(this, {
aclService,
$window,
$translate
});
}
fetch() {
const map = {};
const modules = [];
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) {
let res = mod.keybindings.find(i => i.state == route.state);
if (res) keyBind = res.key.toUpperCase();
}
const module = {
name: mod.name || mod.module,
code: mod.module,
icon: mod.icon || null,
route,
keyBind
};
modules.push(module);
map[mod.module] = mod;
}
const sortedModules = modules.sort((a, b) => {
const translatedNameA = this.$translate.instant(a.name);
const translatedNameB = this.$translate.instant(b.name);
return translatedNameA.localeCompare(translatedNameB);
});
this.modules = sortedModules;
this.map = map;
}
reset() {
this.modules = null;
this.map = null;
}
get() {
if (!this.modules) this.fetch();
return this.modules;
}
getMap() {
if (!this.map) this.fetch();
return this.map;
}
}
Modules.$inject = ['aclService', '$window', '$translate'];
ngModule.service('vnModules', Modules);