menuHelper.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 && !(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. path: node.path,
  57. ...(node.redirect ? { redirect: node.redirect } : {}),
  58. };
  59. },
  60. });
  61. joinParentPath(list);
  62. return cloneDeep(list);
  63. }
  64. /**
  65. * config menu with given params
  66. */
  67. const menuParamRegex = /(?::)([\s\S]+?)((?=\/)|$)/g;
  68. export function configureDynamicParamsMenu(menu: Menu, params: RouteParams) {
  69. const { path, paramPath } = toRaw(menu);
  70. let realPath = paramPath ? paramPath : path;
  71. const matchArr = realPath.match(menuParamRegex);
  72. matchArr?.forEach((it) => {
  73. const realIt = it.substr(1);
  74. if (params[realIt]) {
  75. realPath = realPath.replace(`:${realIt}`, params[realIt] as string);
  76. }
  77. });
  78. // save original param path.
  79. if (!paramPath && matchArr && matchArr.length > 0) {
  80. menu.paramPath = path;
  81. }
  82. menu.path = realPath;
  83. // children
  84. menu.children?.forEach((item) => configureDynamicParamsMenu(item, params));
  85. }