1
2
3
4
5
import type { PropType } from 'vue';
import type { Menu } from '/@/router/types';
import { computed, defineComponent, unref, ref, onMounted, watch } from 'vue';
import { BasicMenu } from '/@/components/Menu/index';
vben
authored
5 years ago
6
import Logo from '/@/layouts/logo/index.vue';
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { MenuModeEnum, MenuSplitTyeEnum, MenuTypeEnum } from '/@/enums/menuEnum';
// store
import { appStore } from '/@/store/modules/app';
import { menuStore } from '/@/store/modules/menu';
import {
getMenus,
getFlatMenus,
getShallowMenus,
getChildrenMenus,
getFlatChildrenMenus,
getCurrentParentPath,
} from '/@/router/menus/index';
import { useRouter } from 'vue-router';
import { useThrottle } from '/@/hooks/core/useThrottle';
import { permissionStore } from '/@/store/modules/permission';
vben
authored
5 years ago
26
import './index.less';
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
export default defineComponent({
name: 'DefaultLayoutMenu',
props: {
theme: {
type: String as PropType<string>,
default: '',
},
splitType: {
type: Number as PropType<MenuSplitTyeEnum>,
default: MenuSplitTyeEnum.NONE,
},
parentMenuPath: {
type: String as PropType<string>,
default: '',
},
showSearch: {
type: Boolean as PropType<boolean>,
default: true,
},
vben
authored
5 years ago
46
47
48
49
isTop: {
type: Boolean as PropType<boolean>,
default: false,
},
50
51
52
53
54
55
menuMode: {
type: [String] as PropType<MenuModeEnum | null>,
default: '',
},
},
setup(props) {
vben
authored
5 years ago
56
// Menu array
57
const menusRef = ref<Menu[]>([]);
vben
authored
5 years ago
58
// flat menu array
59
const flatMenusRef = ref<Menu[]>([]);
vben
authored
5 years ago
60
const { currentRoute, push } = useRouter();
61
vben
authored
5 years ago
62
// get app config
63
64
65
66
const getProjectConfigRef = computed(() => {
return appStore.getProjectConfig;
});
vben
authored
5 years ago
67
// get is Horizontal
68
69
70
71
72
73
const getIsHorizontalRef = computed(() => {
return unref(getProjectConfigRef).menuSetting.mode === MenuModeEnum.HORIZONTAL;
});
const [throttleHandleSplitLeftMenu] = useThrottle(handleSplitLeftMenu, 50);
vben
authored
5 years ago
74
// Route change split menu
75
76
77
78
79
80
81
82
83
84
85
watch(
[() => unref(currentRoute).path, () => props.splitType],
async ([path, splitType]: [string, MenuSplitTyeEnum]) => {
if (splitType !== MenuSplitTyeEnum.LEFT && !unref(getIsHorizontalRef)) return;
const parentPath = await getCurrentParentPath(path);
parentPath && throttleHandleSplitLeftMenu(parentPath);
},
{
immediate: true,
}
);
vben
authored
5 years ago
86
vben
authored
5 years ago
87
// Menu changes
88
watch(
vben
authored
5 years ago
89
[() => permissionStore.getLastBuildMenuTimeState, () => permissionStore.getBackMenuListState],
90
91
92
93
94
() => {
genMenus();
}
);
vben
authored
5 years ago
95
// split Menu changes
96
97
98
99
100
watch([() => appStore.getProjectConfig.menuSetting.split], () => {
if (props.splitType !== MenuSplitTyeEnum.LEFT && !unref(getIsHorizontalRef)) return;
genMenus();
});
vben
authored
5 years ago
101
// Handle left menu split
102
103
104
105
async function handleSplitLeftMenu(parentPath: string) {
const isSplitMenu = unref(getProjectConfigRef).menuSetting.split;
if (!isSplitMenu) return;
const { splitType } = props;
vben
authored
5 years ago
106
// spilt mode left
107
108
if (splitType === MenuSplitTyeEnum.LEFT) {
const children = await getChildrenMenus(parentPath);
vben
authored
5 years ago
109
110
111
if (!children) {
appStore.commitProjectConfigState({
menuSetting: {
vben
authored
5 years ago
112
hidden: false,
vben
authored
5 years ago
113
114
115
116
117
118
},
});
flatMenusRef.value = [];
menusRef.value = [];
return;
}
119
const flatChildren = await getFlatChildrenMenus(children);
vben
authored
5 years ago
120
121
appStore.commitProjectConfigState({
menuSetting: {
vben
authored
5 years ago
122
hidden: true,
vben
authored
5 years ago
123
124
},
});
125
126
127
128
129
flatMenusRef.value = flatChildren;
menusRef.value = children;
}
}
vben
authored
5 years ago
130
// get menus
131
132
133
async function genMenus() {
const isSplitMenu = unref(getProjectConfigRef).menuSetting.split;
vben
authored
5 years ago
134
// normal mode
135
136
137
138
139
140
141
const { splitType } = props;
if (splitType === MenuSplitTyeEnum.NONE || !isSplitMenu) {
flatMenusRef.value = await getFlatMenus();
menusRef.value = await getMenus();
return;
}
vben
authored
5 years ago
142
// split-top
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
if (splitType === MenuSplitTyeEnum.TOP) {
const parentPath = await getCurrentParentPath(unref(currentRoute).path);
menuStore.commitCurrentTopSplitMenuPathState(parentPath);
const shallowMenus = await getShallowMenus();
flatMenusRef.value = shallowMenus;
menusRef.value = shallowMenus;
return;
}
}
function handleMenuClick(menu: Menu) {
const { path } = menu;
if (path) {
const { splitType } = props;
vben
authored
5 years ago
158
// split mode top
159
160
161
if (splitType === MenuSplitTyeEnum.TOP) {
menuStore.commitCurrentTopSplitMenuPathState(path);
}
vben
authored
5 years ago
162
push(path);
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
}
}
async function beforeMenuClickFn(menu: Menu) {
const { meta: { externalLink } = {} } = menu;
if (externalLink) {
window.open(externalLink, '_blank');
return false;
}
return true;
}
function handleClickSearchInput() {
if (menuStore.getCollapsedState) {
menuStore.commitCollapsedState(false);
}
}
const showSearchRef = computed(() => {
const { showSearch, type, mode } = unref(getProjectConfigRef).menuSetting;
return (
showSearch &&
props.showSearch &&
!(type === MenuTypeEnum.MIX && mode === MenuModeEnum.HORIZONTAL)
);
});
vben
authored
5 years ago
192
193
194
195
onMounted(() => {
genMenus();
});
196
197
198
return () => {
const {
showLogo,
vben
authored
5 years ago
199
200
201
202
203
204
205
menuSetting: {
type: menuType,
mode,
theme,
collapsed,
collapsedShowTitle,
collapsedShowSearch,
vben
authored
5 years ago
206
accordion,
vben
authored
5 years ago
207
},
208
209
210
211
212
213
214
215
216
217
218
219
} = unref(getProjectConfigRef);
const isSidebarType = menuType === MenuTypeEnum.SIDEBAR;
const isShowLogo = showLogo && isSidebarType;
const themeData = props.theme || theme;
return (
<BasicMenu
beforeClickFn={beforeMenuClickFn}
onMenuClick={handleMenuClick}
type={menuType}
mode={props.menuMode || mode}
class="layout-menu"
vben
authored
5 years ago
220
collapsedShowTitle={collapsedShowTitle}
221
222
theme={themeData}
showLogo={isShowLogo}
vben
authored
5 years ago
223
search={unref(showSearchRef) && (collapsedShowSearch ? true : !collapsed)}
224
225
226
227
items={unref(menusRef)}
flatItems={unref(flatMenusRef)}
onClickSearchInput={handleClickSearchInput}
appendClass={props.splitType === MenuSplitTyeEnum.TOP}
vben
authored
5 years ago
228
isTop={props.isTop}
vben
authored
5 years ago
229
accordion={accordion}
230
231
232
233
>
{{
header: () =>
isShowLogo && (
vben
authored
5 years ago
234
235
236
237
238
<Logo
showTitle={!collapsed}
class={[`layout-menu__logo`, themeData]}
theme={themeData}
/>
239
240
241
242
243
244
245
),
}}
</BasicMenu>
);
};
},
});