NavStore.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { types, getEnv } from 'mobx-state-tree';
  2. import _ from 'lodash';
  3. export const NavItem = types.model('NavItem', {
  4. id: types.identifier(types.string),
  5. text: types.string,
  6. url: types.optional(types.string, ''),
  7. subTitle: types.optional(types.string, ''),
  8. icon: types.optional(types.string, ''),
  9. img: types.optional(types.string, ''),
  10. active: types.optional(types.boolean, false),
  11. children: types.optional(types.array(types.late(() => NavItem)), []),
  12. });
  13. export const NavStore = types
  14. .model('NavStore', {
  15. main: types.maybe(NavItem),
  16. node: types.maybe(NavItem),
  17. })
  18. .actions(self => ({
  19. load(...args) {
  20. var children = getEnv(self).navTree;
  21. let main, node;
  22. let parents = [];
  23. for (let id of args) {
  24. node = _.find(children, { id: id });
  25. children = node.children;
  26. parents.push(node);
  27. }
  28. main = parents[parents.length - 2];
  29. if (main.children) {
  30. for (let item of main.children) {
  31. item.active = false;
  32. if (item.url === node.url) {
  33. item.active = true;
  34. }
  35. }
  36. }
  37. self.main = NavItem.create(main);
  38. self.node = NavItem.create(node);
  39. },
  40. }));