2017-12-20 12:33:33 +01:00
|
|
|
import coreModule from 'app/core/core_module';
|
|
|
|
|
import config from 'app/core/config';
|
|
|
|
|
import _ from 'lodash';
|
2019-06-18 08:17:27 -07:00
|
|
|
import { NavModel } from '@grafana/data';
|
2017-06-02 14:00:42 +02:00
|
|
|
|
|
|
|
|
export class NavModelSrv {
|
2017-08-15 17:52:52 +02:00
|
|
|
navItems: any;
|
2017-06-02 14:00:42 +02:00
|
|
|
|
|
|
|
|
/** @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
|
|
|
}
|
|
|
|
|
|
2019-06-05 15:18:31 +02:00
|
|
|
getNav(...args: Array<string | number>) {
|
2018-08-30 08:58:43 +02:00
|
|
|
let children = this.navItems;
|
2019-04-30 07:46:46 -07:00
|
|
|
const nav = {
|
|
|
|
|
breadcrumbs: [],
|
|
|
|
|
} as NavModel;
|
2017-08-15 20:24:16 +02:00
|
|
|
|
2018-08-26 17:14:40 +02:00
|
|
|
for (const 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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-15 12:11:52 +02:00
|
|
|
const node: any = _.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-06-02 14:00:42 +02:00
|
|
|
|
2017-11-30 15:37:03 +01:00
|
|
|
if (nav.main.children) {
|
2018-08-26 17:14:40 +02:00
|
|
|
for (const item of nav.main.children) {
|
2017-11-30 15:37:03 +01:00
|
|
|
item.active = false;
|
|
|
|
|
|
|
|
|
|
if (item.url === nav.node.url) {
|
|
|
|
|
item.active = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 20:24:16 +02:00
|
|
|
return nav;
|
2017-06-02 14:00:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getNotFoundNav() {
|
2019-05-02 10:15:39 -07:00
|
|
|
return getNotFoundNav(); // the exported function
|
2017-06-02 14:00:42 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 10:15:39 -07:00
|
|
|
export function getNotFoundNav(): NavModel {
|
|
|
|
|
return getWarningNav('Page not found', '404 Error');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getWarningNav(text: string, subTitle?: string): NavModel {
|
|
|
|
|
const node = {
|
|
|
|
|
text,
|
|
|
|
|
subTitle,
|
2020-04-12 22:20:02 +02:00
|
|
|
icon: 'exclamation-triangle',
|
2019-05-02 10:15:39 -07:00
|
|
|
};
|
|
|
|
|
return {
|
|
|
|
|
breadcrumbs: [node],
|
|
|
|
|
node: node,
|
|
|
|
|
main: node,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
coreModule.service('navModelSrv', NavModelSrv);
|