Blame view

src/router/helper/menuHelper.ts 3.08 KB
1
import { AppRouteModule } from '/@/router/types';
陈文彬 authored
2
import type { MenuModule, Menu, AppRouteRecordRaw } from '/@/router/types';
3
import { findPath, treeMap } from '/@/utils/helper/treeHelper';
陈文彬 authored
4
import { cloneDeep } from 'lodash-es';
5
import { isUrl } from '/@/utils/is';
6
7
import { RouteParams } from 'vue-router';
import { toRaw } from 'vue';
陈文彬 authored
8
9
export function getAllParentPath<T = Recordable>(treeData: T[], path: string) {
陈文彬 authored
10
11
12
13
  const menuList = findPath(treeData, (n) => n.path === path) as Menu[];
  return (menuList || []).map((item) => item.path);
}
14
15
16
function joinParentPath(menus: Menu[], parentPath = '') {
  for (let index = 0; index < menus.length; index++) {
    const menu = menus[index];
17
18
19
20
21
22
23
    // https://next.router.vuejs.org/guide/essentials/nested-routes.html
    // Note that nested paths that start with / will be treated as a root path.
    // This allows you to leverage the component nesting without having to use a nested URL.
    if (!(menu.path.startsWith('/') || isUrl(menu.path))) {
      // path doesn't start with /, nor is it a url, join parent path
      menu.path = `${parentPath}/${menu.path}`;
    }
24
    if (menu?.children?.length) {
25
      joinParentPath(menu.children, menu.meta?.hidePathForChildren ? parentPath : menu.path);
26
    }
陈文彬 authored
27
28
29
  }
}
30
// Parsing the menu module
陈文彬 authored
31
32
33
34
export function transformMenuModule(menuModule: MenuModule): Menu {
  const { menu } = menuModule;

  const menuList = [menu];
vben authored
35
36
  joinParentPath(menuList);
陈文彬 authored
37
38
39
  return menuList[0];
}
40
export function transformRouteToMenu(routeModList: AppRouteModule[], routerMapping = false) {
陈文彬 authored
41
42
  const cloneRouteModList = cloneDeep(routeModList);
  const routeList: AppRouteRecordRaw[] = [];
43
陈文彬 authored
44
  cloneRouteModList.forEach((item) => {
45
46
47
    if (routerMapping && item.meta.hideChildrenInMenu && typeof item.redirect === 'string') {
      item.path = item.redirect;
    }
vben authored
48
49
50
    if (item.meta?.single) {
      const realItem = item?.children?.[0];
      realItem && routeList.push(realItem);
51
    } else {
vben authored
52
      routeList.push(item);
53
    }
陈文彬 authored
54
  });
55
  const list = treeMap(routeList, {
陈文彬 authored
56
    conversion: (node: AppRouteRecordRaw) => {
57
      const { meta: { title, hideMenu = false } = {} } = node;
58
陈文彬 authored
59
      return {
60
        ...(node.meta || {}),
61
        meta: node.meta,
陈文彬 authored
62
        name: title,
63
        hideMenu,
64
        path: node.path,
65
        ...(node.redirect ? { redirect: node.redirect } : {}),
陈文彬 authored
66
67
68
      };
    },
  });
69
  joinParentPath(list);
70
  return cloneDeep(list);
陈文彬 authored
71
}
72
73
74
75

/**
 * config menu with given params
 */
76
const menuParamRegex = /(?::)([\s\S]+?)((?=\/)|$)/g;
77
78
79
80
export function configureDynamicParamsMenu(menu: Menu, params: RouteParams) {
  const { path, paramPath } = toRaw(menu);
  let realPath = paramPath ? paramPath : path;
  const matchArr = realPath.match(menuParamRegex);
81
82
  matchArr?.forEach((it) => {
83
84
85
    const realIt = it.substr(1);
    if (params[realIt]) {
      realPath = realPath.replace(`:${realIt}`, params[realIt] as string);
86
87
88
89
90
91
92
93
94
95
    }
  });
  // save original param path.
  if (!paramPath && matchArr && matchArr.length > 0) {
    menu.paramPath = path;
  }
  menu.path = realPath;
  // children
  menu.children?.forEach((item) => configureDynamicParamsMenu(item, params));
}