1
import type { AppRouteRecordRaw } from '/@/router/types';
vben
authored
5 years ago
2
import type { TabContentProps } from './data';
3
4
5
6
7
import type { Ref } from 'vue';
import type { TabItem } from '/@/store/modules/tab';
import type { DropMenu } from '/@/components/Dropdown';
import { computed, unref } from 'vue';
vben
authored
5 years ago
8
import { TabContentEnum, MenuEventEnum, getActions } from './data';
9
10
11
12
13
14
import { tabStore } from '/@/store/modules/tab';
import { appStore } from '/@/store/modules/app';
import { PageEnum } from '/@/enums/pageEnum';
import { useGo, useRedo } from '/@/hooks/web/usePage';
import router from '/@/router';
import { useTabs, isInitUseTab } from '/@/hooks/web/useTabs';
vben
authored
5 years ago
15
import { RouteLocationRaw } from 'vue-router';
16
17
const { initTabFn } = useTabs();
vben
authored
5 years ago
18
19
20
21
22
23
24
25
26
27
28
29
30
export function useTabDropdown(tabContentProps: TabContentProps) {
const { currentRoute } = router;
const redo = useRedo();
const go = useGo();
const isTabsRef = computed(() => tabContentProps.type === TabContentEnum.TAB_TYPE);
const getCurrentTab: Ref<TabItem | AppRouteRecordRaw> = computed(() => {
return unref(isTabsRef)
? tabContentProps.tabItem
: ((unref(currentRoute) as any) as AppRouteRecordRaw);
});
vben
authored
5 years ago
31
32
// Current tab list
const getTabsState = computed(() => tabStore.getTabsState);
33
34
/**
vben
authored
5 years ago
35
* @description: drop-down list
36
37
38
*/
const getDropMenuList = computed(() => {
const dropMenuList = getActions();
vben
authored
5 years ago
39
// Reset to initial state
40
41
42
43
for (const item of dropMenuList) {
item.disabled = false;
}
vben
authored
5 years ago
44
// No tab
45
46
47
if (!unref(getTabsState) || unref(getTabsState).length <= 0) {
return dropMenuList;
} else if (unref(getTabsState).length === 1) {
vben
authored
5 years ago
48
// Only one tab
49
50
51
52
53
54
55
for (const item of dropMenuList) {
if (item.event !== MenuEventEnum.REFRESH_PAGE) {
item.disabled = true;
}
}
return dropMenuList;
}
vben
authored
5 years ago
56
if (!unref(getCurrentTab)) return;
57
58
const { meta, path } = unref(getCurrentTab);
vben
authored
5 years ago
59
// Refresh button
60
61
62
const curItem = tabStore.getCurrentContextMenuState;
const index = tabStore.getCurrentContextMenuIndexState;
const refreshDisabled = curItem ? curItem.path !== path : true;
vben
authored
5 years ago
63
// Close left
64
65
const closeLeftDisabled = index === 0;
vben
authored
5 years ago
66
// Close right
67
const closeRightDisabled = index === unref(getTabsState).length - 1;
vben
authored
5 years ago
68
69
// Currently fixed tab
// TODO PERf
70
71
72
73
74
75
76
77
78
79
80
dropMenuList[0].disabled = unref(isTabsRef) ? refreshDisabled : false;
if (meta && meta.affix) {
dropMenuList[1].disabled = true;
}
dropMenuList[2].disabled = closeLeftDisabled;
dropMenuList[3].disabled = closeRightDisabled;
return dropMenuList;
});
/**
vben
authored
5 years ago
81
* @description: Jump to page when closing all pages
82
83
84
85
86
87
88
89
*/
function gotoPage() {
const len = unref(getTabsState).length;
const { path } = unref(currentRoute);
let toPath: PageEnum | string = PageEnum.BASE_HOME;
if (len > 0) {
vben
authored
5 years ago
90
91
92
93
94
const page = unref(getTabsState)[len - 1];
const p = page.fullPath || page.path;
if (p) {
toPath = p;
}
95
}
vben
authored
5 years ago
96
// Jump to the current page and report an error
97
98
99
100
101
102
path !== toPath && go(toPath as PageEnum, true);
}
function isGotoPage(currentTab?: TabItem) {
const { path } = unref(currentRoute);
const currentPath = (currentTab || unref(getCurrentTab)).path;
vben
authored
5 years ago
103
// Not the current tab, when you close the left/right side, you need to jump to the page
104
105
106
107
108
109
110
111
112
113
if (path !== currentPath) {
go(currentPath as PageEnum, true);
}
}
function refreshPage(tabItem?: TabItem) {
try {
tabStore.commitCloseTabKeepAlive(tabItem || unref(getCurrentTab));
} catch (error) {}
redo();
}
vben
authored
5 years ago
114
115
116
117
118
function closeAll() {
tabStore.commitCloseAllTab();
gotoPage();
}
vben
authored
5 years ago
119
120
121
122
123
function closeLeft(tabItem?: TabItem) {
tabStore.closeLeftTabAction(tabItem || unref(getCurrentTab));
isGotoPage(tabItem);
}
vben
authored
5 years ago
124
125
126
127
128
function closeRight(tabItem?: TabItem) {
tabStore.closeRightTabAction(tabItem || unref(getCurrentTab));
isGotoPage(tabItem);
}
vben
authored
5 years ago
129
130
131
132
133
function closeOther(tabItem?: TabItem) {
tabStore.closeOtherTabAction(tabItem || unref(getCurrentTab));
isGotoPage(tabItem);
}
vben
authored
5 years ago
134
135
136
137
function closeCurrent(tabItem?: TabItem) {
closeTab(unref(tabItem || unref(getCurrentTab)));
}
vben
authored
5 years ago
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
function scaleScreen() {
const {
headerSetting: { show: showHeader },
menuSetting: { show: showMenu },
} = appStore.getProjectConfig;
const isScale = !showHeader && !showMenu;
appStore.commitProjectConfigState({
headerSetting: { show: isScale },
menuSetting: { show: isScale },
});
}
if (!isInitUseTab) {
initTabFn({
refreshPageFn: refreshPage,
closeAllFn: closeAll,
closeCurrentFn: closeCurrent,
closeLeftFn: closeLeft,
closeOtherFn: closeOther,
closeRightFn: closeRight,
});
}
vben
authored
5 years ago
162
// Handle right click event
163
164
165
166
167
168
169
170
function handleMenuEvent(menu: DropMenu): void {
const { event } = menu;
switch (event) {
case MenuEventEnum.SCALE:
scaleScreen();
break;
case MenuEventEnum.REFRESH_PAGE:
vben
authored
5 years ago
171
// refresh page
172
173
refreshPage();
break;
vben
authored
5 years ago
174
// Close current
175
176
177
case MenuEventEnum.CLOSE_CURRENT:
closeCurrent();
break;
vben
authored
5 years ago
178
// Close left
179
180
181
case MenuEventEnum.CLOSE_LEFT:
closeLeft();
break;
vben
authored
5 years ago
182
// Close right
183
184
185
case MenuEventEnum.CLOSE_RIGHT:
closeRight();
break;
vben
authored
5 years ago
186
// Close other
187
188
189
case MenuEventEnum.CLOSE_OTHER:
closeOther();
break;
vben
authored
5 years ago
190
// Close all
191
192
193
194
195
196
197
case MenuEventEnum.CLOSE_ALL:
closeAll();
break;
}
}
return { getDropMenuList, handleMenuEvent };
}
vben
authored
5 years ago
198
199
200
201
202
203
204
205
206
207
export function getObj(tabItem: TabItem) {
const { params, path, query } = tabItem;
return {
params: params || {},
path,
query: query || {},
};
}
vben
authored
5 years ago
208
export function closeTab(closedTab: TabItem | AppRouteRecordRaw) {
209
const { currentRoute, replace } = router;
vben
authored
5 years ago
210
211
// Current tab list
const getTabsState = computed(() => tabStore.getTabsState);
vben
authored
5 years ago
212
213
214
const { path } = unref(currentRoute);
if (path !== closedTab.path) {
vben
authored
5 years ago
215
// Closed is not the activation tab
216
217
218
tabStore.commitCloseTab(closedTab);
return;
}
vben
authored
5 years ago
219
220
// Closed is activated atb
vben
authored
5 years ago
221
let toObj: RouteLocationRaw = {};
vben
authored
5 years ago
222
223
224
const index = unref(getTabsState).findIndex((item) => item.path === path);
vben
authored
5 years ago
225
// If the current is the leftmost tab
226
if (index === 0) {
vben
authored
5 years ago
227
// There is only one tab, then jump to the homepage, otherwise jump to the right tab
228
if (unref(getTabsState).length === 1) {
vben
authored
5 years ago
229
toObj = PageEnum.BASE_HOME;
230
} else {
vben
authored
5 years ago
231
// Jump to the right tab
vben
authored
5 years ago
232
const page = unref(getTabsState)[index + 1];
vben
authored
5 years ago
233
toObj = getObj(page);
234
235
}
} else {
vben
authored
5 years ago
236
// Close the current tab
vben
authored
5 years ago
237
const page = unref(getTabsState)[index - 1];
vben
authored
5 years ago
238
toObj = getObj(page);
239
240
241
}
const route = (unref(currentRoute) as unknown) as AppRouteRecordRaw;
tabStore.commitCloseTab(route);
vben
authored
5 years ago
242
replace(toObj);
243
}