nav_model_srv.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { NavModel, NavModelItem } from '@grafana/data';
  2. import coreModule from 'app/angular/core_module';
  3. import config from 'app/core/config';
  4. interface Nav {
  5. breadcrumbs: NavModelItem[];
  6. node?: NavModelItem;
  7. main?: NavModelItem;
  8. }
  9. export class NavModelSrv {
  10. navItems: NavModelItem[];
  11. constructor() {
  12. this.navItems = config.bootData.navTree;
  13. }
  14. getCfgNode() {
  15. return this.navItems.find((navItem) => navItem.id === 'cfg');
  16. }
  17. getNav(...args: Array<string | number>) {
  18. let children = this.navItems;
  19. const nav: Nav = {
  20. breadcrumbs: [],
  21. };
  22. for (const id of args) {
  23. // if its a number then it's the index to use for main
  24. if (typeof id === 'number') {
  25. nav.main = nav.breadcrumbs[id];
  26. break;
  27. }
  28. const node = children.find((child) => child.id === id);
  29. if (node) {
  30. nav.breadcrumbs.push(node);
  31. nav.node = node;
  32. nav.main = node;
  33. children = node.children ?? [];
  34. }
  35. }
  36. if (nav.main?.children) {
  37. for (const item of nav.main.children) {
  38. item.active = item.url === nav.node?.url;
  39. }
  40. }
  41. return nav;
  42. }
  43. getNotFoundNav() {
  44. return getNotFoundNav(); // the exported function
  45. }
  46. }
  47. export function getExceptionNav(error: any): NavModel {
  48. console.error(error);
  49. return getWarningNav('Exception thrown', 'See console for details');
  50. }
  51. export function getNotFoundNav(): NavModel {
  52. return getWarningNav('Page not found', '404 Error');
  53. }
  54. export function getWarningNav(text: string, subTitle?: string): NavModel {
  55. const node = {
  56. text,
  57. subTitle,
  58. icon: 'exclamation-triangle',
  59. };
  60. return {
  61. breadcrumbs: [node],
  62. node: node,
  63. main: node,
  64. };
  65. }
  66. coreModule.service('navModelSrv', NavModelSrv);