grafana/public/app/core/nav_model_srv.ts

78 lines
1.6 KiB
TypeScript
Raw Normal View History

2017-12-20 05:33:33 -06:00
import coreModule from 'app/core/core_module';
import config from 'app/core/config';
import { find, isNumber } from 'lodash';
import { NavModel } from '@grafana/data';
export class NavModelSrv {
2017-08-15 10:52:52 -05:00
navItems: any;
2017-12-13 06:16:44 -06:00
constructor() {
2017-08-15 10:52:52 -05:00
this.navItems = config.bootData.navTree;
}
getCfgNode() {
return find(this.navItems, { id: 'cfg' });
2017-08-15 10:52:52 -05:00
}
getNav(...args: Array<string | number>) {
let children = this.navItems;
const nav = {
breadcrumbs: [],
} as any;
2017-08-15 13:24:16 -05:00
for (const id of args) {
2017-11-30 08:37:03 -06:00
// if its a number then it's the index to use for main
if (isNumber(id)) {
2017-11-30 08:37:03 -06:00
nav.main = nav.breadcrumbs[id];
break;
}
const node: any = find(children, { id: id });
2017-08-15 13:24:16 -05:00
nav.breadcrumbs.push(node);
nav.node = node;
2017-11-30 08:37:03 -06:00
nav.main = node;
2017-08-15 13:24:16 -05:00
children = node.children;
}
2017-11-30 08:37:03 -06:00
if (nav.main.children) {
for (const item of nav.main.children) {
2017-11-30 08:37:03 -06:00
item.active = false;
if (item.url === nav.node.url) {
item.active = true;
}
}
}
2017-08-15 13:24:16 -05:00
return nav;
}
getNotFoundNav() {
return getNotFoundNav(); // the exported function
}
}
export function getExceptionNav(error: any): NavModel {
console.error(error);
return getWarningNav('Exception thrown', 'See console for details');
}
export function getNotFoundNav(): NavModel {
return getWarningNav('Page not found', '404 Error');
}
export function getWarningNav(text: string, subTitle?: string): NavModel {
const node = {
text,
subTitle,
icon: 'exclamation-triangle',
};
return {
breadcrumbs: [node],
node: node,
main: node,
};
}
2017-12-20 05:33:33 -06:00
coreModule.service('navModelSrv', NavModelSrv);