nav_model_srv.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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) {
  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. const node = {
  43. text: 'Page not found',
  44. icon: 'fa fa-fw fa-warning',
  45. subTitle: '404 Error',
  46. };
  47. return {
  48. breadcrumbs: [node],
  49. node: node,
  50. main: node,
  51. };
  52. }
  53. }
  54. coreModule.service('navModelSrv', NavModelSrv);