menuHelper.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { AppRouteModule } from '/@/router/types';
  2. import type { MenuModule, Menu, AppRouteRecordRaw } from '/@/router/types';
  3. import { findPath, treeMap } from '/@/utils/helper/treeHelper';
  4. import { cloneDeep } from 'lodash-es';
  5. import { isUrl } from '/@/utils/is';
  6. import { RouteParams } from 'vue-router';
  7. import { toRaw } from 'vue';
  8. export function getAllParentPath<T = Recordable>(treeData: T[], path: string) {
  9. const menuList = findPath(treeData, (n) => n.path === path) as Menu[];
  10. return (menuList || []).map((item) => item.path);
  11. }
  12. function joinParentPath(menus: Menu[], parentPath = '') {
  13. for (let index = 0; index < menus.length; index++) {
  14. const menu = menus[index];
  15. // https://next.router.vuejs.org/guide/essentials/nested-routes.html
  16. // Note that nested paths that start with / will be treated as a root path.
  17. // This allows you to leverage the component nesting without having to use a nested URL.
  18. if (!(menu.path.startsWith('/') || isUrl(menu.path))) {
  19. // path doesn't start with /, nor is it a url, join parent path
  20. menu.path = `${parentPath}/${menu.path}`;
  21. }
  22. if (menu?.children?.length) {
  23. joinParentPath(menu.children, menu.meta?.hidePathForChildren ? parentPath : menu.path);
  24. }
  25. }
  26. }
  27. // Parsing the menu module
  28. export function transformMenuModule(menuModule: MenuModule): Menu {
  29. const { menu } = menuModule;
  30. const menuList = [menu];
  31. joinParentPath(menuList);
  32. return menuList[0];
  33. }
  34. export function transformRouteToMenu(routeModList: AppRouteModule[], routerMapping = false) {
  35. const cloneRouteModList = cloneDeep(routeModList);
  36. const routeList: AppRouteRecordRaw[] = [];
  37. cloneRouteModList.forEach((item) => {
  38. if (routerMapping && item.meta.hideChildrenInMenu && typeof item.redirect === 'string') {
  39. item.path = item.redirect;
  40. }
  41. if (item.meta?.single) {
  42. const realItem = item?.children?.[0];
  43. realItem && routeList.push(realItem);
  44. } else {
  45. routeList.push(item);
  46. }
  47. });
  48. const list = treeMap(routeList, {
  49. conversion: (node: AppRouteRecordRaw) => {
  50. const { meta: { title, hideMenu = false } = {} } = node;
  51. return {
  52. ...(node.meta || {}),
  53. meta: node.meta,
  54. name: title,
  55. hideMenu,
  56. alwaysShow: node.alwaysShow || false,
  57. path: node.path,
  58. ver: node.ver,
  59. ...(node.redirect ? { redirect: node.redirect } : {}),
  60. };
  61. },
  62. });
  63. joinParentPath(list);
  64. return cloneDeep(list);
  65. }
  66. /**
  67. * config menu with given params
  68. */
  69. const menuParamRegex = /(?::)([\s\S]+?)((?=\/)|$)/g;
  70. export function configureDynamicParamsMenu(menu: Menu, params: RouteParams) {
  71. const { path, paramPath } = toRaw(menu);
  72. let realPath = paramPath ? paramPath : path;
  73. const matchArr = realPath.match(menuParamRegex);
  74. matchArr?.forEach((it) => {
  75. const realIt = it.substr(1);
  76. if (params[realIt]) {
  77. realPath = realPath.replace(`:${realIt}`, params[realIt] as string);
  78. }
  79. });
  80. // save original param path.
  81. if (!paramPath && matchArr && matchArr.length > 0) {
  82. menu.paramPath = path;
  83. }
  84. menu.path = realPath;
  85. // children
  86. menu.children?.forEach((item) => configureDynamicParamsMenu(item, params));
  87. }