nav_model_srv.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import coreModule from 'app/core/core_module';
  2. import config from 'app/core/config';
  3. import _ from 'lodash';
  4. import { NavModel } from '@grafana/ui';
  5. export class NavModelSrv {
  6. navItems: any;
  7. /** @ngInject */
  8. constructor() {
  9. this.navItems = config.bootData.navTree;
  10. }
  11. getCfgNode() {
  12. return _.find(this.navItems, { id: 'cfg' });
  13. }
  14. getNav(...args: Array<string | number>) {
  15. let children = this.navItems;
  16. const nav = {
  17. breadcrumbs: [],
  18. } as NavModel;
  19. for (const id of args) {
  20. // if its a number then it's the index to use for main
  21. if (_.isNumber(id)) {
  22. nav.main = nav.breadcrumbs[id];
  23. break;
  24. }
  25. const node: any = _.find(children, { id: id });
  26. nav.breadcrumbs.push(node);
  27. nav.node = node;
  28. nav.main = node;
  29. children = node.children;
  30. }
  31. if (nav.main.children) {
  32. for (const item of nav.main.children) {
  33. item.active = false;
  34. if (item.url === nav.node.url) {
  35. item.active = true;
  36. }
  37. }
  38. }
  39. return nav;
  40. }
  41. getNotFoundNav() {
  42. return getNotFoundNav(); // the exported function
  43. }
  44. }
  45. export function getNotFoundNav(): NavModel {
  46. return getWarningNav('Page not found', '404 Error');
  47. }
  48. export function getWarningNav(text: string, subTitle?: string): NavModel {
  49. const node = {
  50. text,
  51. subTitle,
  52. icon: 'fa fa-fw fa-warning',
  53. };
  54. return {
  55. breadcrumbs: [node],
  56. node: node,
  57. main: node,
  58. };
  59. }
  60. coreModule.service('navModelSrv', NavModelSrv);