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