NavStore.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { types } from 'mobx-state-tree';
  2. import config from 'app/core/config';
  3. import _ from 'lodash';
  4. export const NavItem = types.model('NavItem', {
  5. id: types.identifier(types.string),
  6. text: types.string,
  7. url: types.optional(types.string, ''),
  8. description: types.optional(types.string, ''),
  9. icon: types.optional(types.string, ''),
  10. img: types.optional(types.string, ''),
  11. active: types.optional(types.boolean, false),
  12. children: types.optional(types.array(types.late(() => NavItem)), []),
  13. });
  14. export const NavStore = types
  15. .model('NavStore', {
  16. main: types.maybe(NavItem),
  17. node: types.maybe(NavItem),
  18. breadcrumbs: types.optional(types.array(NavItem), []),
  19. })
  20. .actions(self => ({
  21. load(...args) {
  22. var children = config.bootData.navTree;
  23. let main, node;
  24. let breadcrumbs = [];
  25. for (let id of args) {
  26. // if its a number then it's the index to use for main
  27. if (_.isNumber(id)) {
  28. main = breadcrumbs[id];
  29. break;
  30. }
  31. let current = _.find(children, { id: id });
  32. breadcrumbs.push(current);
  33. main = node;
  34. node = current;
  35. children = node.children;
  36. }
  37. if (main.children) {
  38. for (let item of main.children) {
  39. item.active = false;
  40. if (item.url === node.url) {
  41. item.active = true;
  42. }
  43. }
  44. }
  45. self.main = NavItem.create(main);
  46. self.node = NavItem.create(node);
  47. for (let item of breadcrumbs) {
  48. self.breadcrumbs.push(NavItem.create(item));
  49. }
  50. // self.main = NavItem.create({
  51. // id: 'test',
  52. // text: 'test',
  53. // url: '/test';
  54. // children: [
  55. // {
  56. // id: 'test',
  57. // text: 'text',
  58. // url: '/test',
  59. // active: true,
  60. // children: []
  61. // }
  62. // ]
  63. // });
  64. },
  65. }));