Files
grafana/public/app/core/nav_model_srv.ts

87 lines
1.6 KiB
TypeScript
Raw Normal View History

2017-12-20 12:33:33 +01:00
import coreModule from 'app/core/core_module';
import config from 'app/core/config';
import _ from 'lodash';
export interface NavModelItem {
2017-11-30 11:31:38 +01:00
text: string;
url: string;
icon?: string;
2017-11-30 11:31:38 +01:00
img?: string;
2017-12-01 10:27:05 +01:00
id: string;
2017-11-30 15:37:03 +01:00
active?: boolean;
hideFromTabs?: boolean;
divider?: boolean;
2017-11-30 15:37:03 +01:00
children: NavModelItem[];
target?: string;
}
2017-11-30 11:31:38 +01:00
export class NavModel {
breadcrumbs: NavModelItem[];
2017-11-30 15:37:03 +01:00
main: NavModelItem;
2017-11-30 11:31:38 +01:00
node: NavModelItem;
constructor() {
this.breadcrumbs = [];
}
}
export class NavModelSrv {
2017-08-15 17:52:52 +02:00
navItems: any;
/** @ngInject */
2017-12-13 13:16:44 +01:00
constructor() {
2017-08-15 17:52:52 +02:00
this.navItems = config.bootData.navTree;
}
getCfgNode() {
2017-12-20 12:33:33 +01:00
return _.find(this.navItems, { id: 'cfg' });
2017-08-15 17:52:52 +02:00
}
2017-08-15 20:24:16 +02:00
getNav(...args) {
var children = this.navItems;
2017-11-30 11:31:38 +01:00
var nav = new NavModel();
2017-08-15 20:24:16 +02:00
for (let id of args) {
2017-11-30 15:37:03 +01:00
// if its a number then it's the index to use for main
if (_.isNumber(id)) {
nav.main = nav.breadcrumbs[id];
break;
}
let node = _.find(children, { id: id });
2017-08-15 20:24:16 +02:00
nav.breadcrumbs.push(node);
nav.node = node;
2017-11-30 15:37:03 +01:00
nav.main = node;
2017-08-15 20:24:16 +02:00
children = node.children;
}
2017-11-30 15:37:03 +01:00
if (nav.main.children) {
for (let item of nav.main.children) {
item.active = false;
if (item.url === nav.node.url) {
item.active = true;
}
}
}
2017-08-15 20:24:16 +02:00
return nav;
}
getNotFoundNav() {
2017-08-15 20:24:16 +02:00
var node = {
2017-12-20 12:33:33 +01:00
text: 'Page not found',
icon: 'fa fa-fw fa-warning',
subTitle: '404 Error',
2017-08-15 17:52:52 +02:00
};
return {
2017-08-15 20:24:16 +02:00
breadcrumbs: [node],
node: node,
2017-12-20 12:33:33 +01:00
main: node,
};
}
}
2017-12-20 12:33:33 +01:00
coreModule.service('navModelSrv', NavModelSrv);