NavStore.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import _ from 'lodash';
  2. import { types, getEnv } from 'mobx-state-tree';
  3. import { NavItem } from './NavItem';
  4. export const NavStore = types
  5. .model('NavStore', {
  6. main: types.maybe(NavItem),
  7. node: types.maybe(NavItem),
  8. })
  9. .actions(self => ({
  10. load(...args) {
  11. let children = getEnv(self).navTree;
  12. let main, node;
  13. let parents = [];
  14. for (let id of args) {
  15. node = children.find(el => el.id === id);
  16. if (!node) {
  17. throw new Error(`NavItem with id ${id} not found`);
  18. }
  19. children = node.children;
  20. parents.push(node);
  21. }
  22. main = parents[parents.length - 2];
  23. if (main.children) {
  24. for (let item of main.children) {
  25. item.active = false;
  26. if (item.url === node.url) {
  27. item.active = true;
  28. }
  29. }
  30. }
  31. self.main = NavItem.create(main);
  32. self.node = NavItem.create(node);
  33. },
  34. initFolderNav(folder: any, activeChildId: string) {
  35. let main = {
  36. icon: 'fa fa-folder-open',
  37. id: 'manage-folder',
  38. subTitle: 'Manage folder dashboards & permissions',
  39. url: '',
  40. text: folder.title,
  41. breadcrumbs: [{ title: 'Dashboards', url: 'dashboards' }],
  42. children: [
  43. {
  44. active: activeChildId === 'manage-folder-dashboards',
  45. icon: 'fa fa-fw fa-th-large',
  46. id: 'manage-folder-dashboards',
  47. text: 'Dashboards',
  48. url: folder.url,
  49. },
  50. {
  51. active: activeChildId === 'manage-folder-permissions',
  52. icon: 'fa fa-fw fa-lock',
  53. id: 'manage-folder-permissions',
  54. text: 'Permissions',
  55. url: `${folder.url}/permissions`,
  56. },
  57. {
  58. active: activeChildId === 'manage-folder-settings',
  59. icon: 'fa fa-fw fa-cog',
  60. id: 'manage-folder-settings',
  61. text: 'Settings',
  62. url: `${folder.url}/settings`,
  63. },
  64. ],
  65. };
  66. self.main = NavItem.create(main);
  67. },
  68. initDatasourceEditNav(ds: any, plugin: any, currentPage: string) {
  69. let title = 'New';
  70. let subTitle = `Type: ${plugin.name}`;
  71. if (ds.id) {
  72. title = ds.name;
  73. }
  74. let main = {
  75. img: plugin.info.logos.large,
  76. id: 'ds-edit-' + plugin.id,
  77. subTitle: subTitle,
  78. url: '',
  79. text: title,
  80. breadcrumbs: [{ title: 'Data Sources', url: 'datasources' }],
  81. children: [
  82. {
  83. active: currentPage === 'datasource-settings',
  84. icon: 'fa fa-fw fa-sliders',
  85. id: 'datasource-settings',
  86. text: 'Settings',
  87. url: `datasources/edit/${ds.id}`,
  88. },
  89. ],
  90. };
  91. const hasDashboards = _.find(plugin.includes, { type: 'dashboard' }) !== undefined;
  92. if (hasDashboards && ds.id) {
  93. main.children.push({
  94. active: currentPage === 'datasource-dashboards',
  95. icon: 'fa fa-fw fa-th-large',
  96. id: 'datasource-dashboards',
  97. text: 'Dashboards',
  98. url: `datasources/edit/${ds.id}/dashboards`,
  99. });
  100. }
  101. self.main = NavItem.create(main);
  102. },
  103. }));