vben
authored
5 years ago
1
import { AppRouteModule } from '/@/router/types';
2
import type { MenuModule, Menu, AppRouteRecordRaw } from '/@/router/types';
Vben
authored
4 years ago
3
import { findPath, treeMap } from '/@/utils/helper/treeHelper';
4
import { cloneDeep } from 'lodash-es';
vben
authored
5 years ago
5
import { isUrl } from '/@/utils/is';
6
7
import { RouteParams } from 'vue-router';
import { toRaw } from 'vue';
8
Vben
authored
4 years ago
9
export function getAllParentPath<T = Recordable>(treeData: T[], path: string) {
10
11
12
13
const menuList = findPath(treeData, (n) => n.path === path) as Menu[];
return (menuList || []).map((item) => item.path);
}
Vben
authored
4 years ago
14
15
16
function joinParentPath(menus: Menu[], parentPath = '') {
for (let index = 0; index < menus.length; index++) {
const menu = menus[index];
Vben
authored
4 years ago
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}`;
}
Vben
authored
4 years ago
24
if (menu?.children?.length) {
25
joinParentPath(menu.children, menu.meta?.hidePathForChildren ? parentPath : menu.path);
Vben
authored
4 years ago
26
}
27
28
29
}
}
Vben
authored
4 years ago
30
// Parsing the menu module
31
32
33
34
export function transformMenuModule(menuModule: MenuModule): Menu {
const { menu } = menuModule;
const menuList = [menu];
vben
authored
5 years ago
35
Vben
authored
4 years ago
36
joinParentPath(menuList);
37
38
39
return menuList[0];
}
Vben
authored
4 years ago
40
export function transformRouteToMenu(routeModList: AppRouteModule[], routerMapping = false) {
41
42
const cloneRouteModList = cloneDeep(routeModList);
const routeList: AppRouteRecordRaw[] = [];
vben
authored
4 years ago
43
44
cloneRouteModList.forEach((item) => {
Vben
authored
4 years ago
45
46
47
if (routerMapping && item.meta.hideChildrenInMenu && typeof item.redirect === 'string') {
item.path = item.redirect;
}
vben
authored
5 years ago
48
49
50
if (item.meta?.single) {
const realItem = item?.children?.[0];
realItem && routeList.push(realItem);
vben
authored
5 years ago
51
} else {
vben
authored
5 years ago
52
routeList.push(item);
vben
authored
5 years ago
53
}
54
});
Vben
authored
4 years ago
55
const list = treeMap(routeList, {
56
conversion: (node: AppRouteRecordRaw) => {
Vben
authored
4 years ago
57
const { meta: { title, hideMenu = false } = {} } = node;
Vben
authored
4 years ago
58
59
return {
Vben
authored
4 years ago
60
...(node.meta || {}),
Vben
authored
4 years ago
61
meta: node.meta,
62
name: title,
vben
authored
4 years ago
63
hideMenu,
Vben
authored
4 years ago
64
path: node.path,
65
...(node.redirect ? { redirect: node.redirect } : {}),
66
67
68
};
},
});
Vben
authored
4 years ago
69
joinParentPath(list);
Vben
authored
4 years ago
70
return cloneDeep(list);
71
}
72
73
74
75
/**
* config menu with given params
*/
Vben
authored
4 years ago
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);
Vben
authored
4 years ago
81
82
matchArr?.forEach((it) => {
Vben
authored
4 years ago
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));
}