vben
authored
5 years ago
1
2
3
4
5
6
7
8
9
10
import type { Menu } from '/@/router/types';
import type { Ref } from 'vue';
import { watch, unref, ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import { MenuSplitTyeEnum } from '/@/enums/menuEnum';
import { useThrottle } from '/@/hooks/core/useThrottle';
import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
vben
authored
5 years ago
11
import { getChildrenMenus, getCurrentParentPath, getMenus, getShallowMenus } from '/@/router/menus';
vben
authored
5 years ago
12
import { permissionStore } from '/@/store/modules/permission';
vben
authored
5 years ago
13
import { useAppInject } from '/@/hooks/web/useAppInject';
vben
authored
5 years ago
14
15
16
17
18
export function useSplitMenu(splitType: Ref<MenuSplitTyeEnum>) {
// Menu array
const menusRef = ref<Menu[]>([]);
const { currentRoute } = useRouter();
vben
authored
5 years ago
19
const { getIsMobile } = useAppInject();
vben
authored
5 years ago
20
21
22
23
24
25
26
27
const { setMenuSetting, getIsHorizontal, getSplit } = useMenuSetting();
const [throttleHandleSplitLeftMenu] = useThrottle(handleSplitLeftMenu, 50);
const splitNotLeft = computed(
() => unref(splitType) !== MenuSplitTyeEnum.LEFT && !unref(getIsHorizontal)
);
vben
authored
5 years ago
28
29
30
const getSplitLeft = computed(
() => !unref(getSplit) || unref(splitType) !== MenuSplitTyeEnum.LEFT
);
vben
authored
5 years ago
31
vben
authored
5 years ago
32
const getSpiltTop = computed(() => unref(splitType) === MenuSplitTyeEnum.TOP);
vben
authored
5 years ago
33
34
35
36
37
38
39
40
const normalType = computed(() => {
return unref(splitType) === MenuSplitTyeEnum.NONE || !unref(getSplit);
});
watch(
[() => unref(currentRoute).path, () => unref(splitType)],
async ([path]: [string, MenuSplitTyeEnum]) => {
vben
authored
5 years ago
41
if (unref(splitNotLeft) || unref(getIsMobile)) return;
vben
authored
5 years ago
42
vben
authored
4 years ago
43
44
45
46
47
48
const { meta } = unref(currentRoute);
const currentActiveMenu = meta.currentActiveMenu;
let parentPath = await getCurrentParentPath(path);
if (!parentPath) {
parentPath = await getCurrentParentPath(currentActiveMenu);
}
vben
authored
5 years ago
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
parentPath && throttleHandleSplitLeftMenu(parentPath);
},
{
immediate: true,
}
);
// Menu changes
watch(
[() => permissionStore.getLastBuildMenuTimeState, () => permissionStore.getBackMenuListState],
() => {
genMenus();
},
{
immediate: true,
}
);
// split Menu changes
watch([() => getSplit.value], () => {
if (unref(splitNotLeft)) return;
genMenus();
});
// Handle left menu split
async function handleSplitLeftMenu(parentPath: string) {
vben
authored
5 years ago
75
if (unref(getSplitLeft) || unref(getIsMobile)) return;
vben
authored
5 years ago
76
77
78
// spilt mode left
const children = await getChildrenMenus(parentPath);
vben
authored
4 years ago
79
80
if (!children || !children.length) {
vben
authored
5 years ago
81
setMenuSetting({ hidden: true });
vben
authored
5 years ago
82
83
84
85
menusRef.value = [];
return;
}
vben
authored
5 years ago
86
setMenuSetting({ hidden: false });
vben
authored
5 years ago
87
88
89
90
91
92
menusRef.value = children;
}
// get menus
async function genMenus() {
// normal mode
vben
authored
5 years ago
93
if (unref(normalType) || unref(getIsMobile)) {
vben
authored
5 years ago
94
95
96
97
98
menusRef.value = await getMenus();
return;
}
// split-top
vben
authored
5 years ago
99
if (unref(getSpiltTop)) {
vben
authored
5 years ago
100
101
102
103
104
105
const shallowMenus = await getShallowMenus();
menusRef.value = shallowMenus;
return;
}
}
vben
authored
5 years ago
106
vben
authored
5 years ago
107
return { menusRef };
vben
authored
5 years ago
108
}