salix/front/core/services/modules.js

56 lines
1.5 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
});
}
reset() {
this.modules = null;
}
get() {
if (this.modules)
return this.modules;
2020-03-11 11:20:15 +00:00
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) {
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-03-11 11:20:15 +00:00
modules.push({
name: mod.name || mod.module,
icon: mod.icon || null,
route,
keyBind
});
}
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-03-11 11:20:15 +00:00
2020-03-11 12:36:56 +00:00
return this.modules;
}
}
2020-03-11 11:20:15 +00:00
Modules.$inject = ['aclService', '$window', '$translate'];
ngModule.service('vnModules', Modules);