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