提交 | 用户 | 时间
|
820397
|
1 |
import type { RouteMeta } from 'vue-router' |
H |
2 |
import { findPath } from '@/utils/tree' |
|
3 |
|
|
4 |
type OnlyOneChildType = AppRouteRecordRaw & { noShowingChildren?: boolean } |
|
5 |
|
|
6 |
interface HasOneShowingChild { |
|
7 |
oneShowingChild?: boolean |
|
8 |
onlyOneChild?: OnlyOneChildType |
|
9 |
} |
|
10 |
|
|
11 |
export const getAllParentPath = <T = Recordable>(treeData: T[], path: string) => { |
|
12 |
const menuList = findPath(treeData, (n) => n.path === path) as AppRouteRecordRaw[] |
|
13 |
return (menuList || []).map((item) => item.path) |
|
14 |
} |
|
15 |
|
|
16 |
export const hasOneShowingChild = ( |
|
17 |
children: AppRouteRecordRaw[] = [], |
|
18 |
parent: AppRouteRecordRaw |
|
19 |
): HasOneShowingChild => { |
|
20 |
const onlyOneChild = ref<OnlyOneChildType>() |
|
21 |
|
|
22 |
const showingChildren = children.filter((v) => { |
|
23 |
const meta = (v.meta ?? {}) as RouteMeta |
|
24 |
if (meta.hidden) { |
|
25 |
return false |
|
26 |
} else { |
|
27 |
// Temp set(will be used if only has one showing child) |
|
28 |
onlyOneChild.value = v |
|
29 |
return true |
|
30 |
} |
|
31 |
}) |
|
32 |
|
|
33 |
// When there is only one child router, the child router is displayed by default |
|
34 |
if (showingChildren.length === 1) { |
|
35 |
return { |
|
36 |
oneShowingChild: true, |
|
37 |
onlyOneChild: unref(onlyOneChild) |
|
38 |
} |
|
39 |
} |
|
40 |
|
|
41 |
// Show parent if there are no child router to display |
|
42 |
if (!showingChildren.length) { |
|
43 |
onlyOneChild.value = { ...parent, path: '', noShowingChildren: true } |
|
44 |
return { |
|
45 |
oneShowingChild: true, |
|
46 |
onlyOneChild: unref(onlyOneChild) |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
return { |
|
51 |
oneShowingChild: false, |
|
52 |
onlyOneChild: unref(onlyOneChild) |
|
53 |
} |
|
54 |
} |