Vben
2021-04-10 215d8bab380728164d7fe2958c2d2d1151fce892
提交 | 用户 | age
215d8b 1 import type { RouteLocationNormalized, Router } from 'vue-router';
beb4c3 2
215d8b 3 import { useRouter } from 'vue-router';
V 4 import { unref } from 'vue';
5
6 import { useMultipleTabStore } from '/@/store/modules/multipleTab';
7 import { useAppStore } from '/@/store/modules/app';
8
9 enum TableActionEnum {
10   REFRESH,
11   CLOSE_ALL,
12   CLOSE_LEFT,
13   CLOSE_RIGHT,
14   CLOSE_OTHER,
15   CLOSE_CURRENT,
16   CLOSE,
17 }
18
19 export function useTabs(_router: Router) {
20   const appStore = useAppStore();
21
22   function canIUseTabs(): boolean {
23     const { show } = appStore.getMultiTabsSetting;
2f6253 24     if (!show) {
c303ec 25       throw new Error('The multi-tab page is currently not open, please open it in the settings!');
2f6253 26     }
27     return !!show;
28   }
cedba3 29
215d8b 30   const tabStore = useMultipleTabStore();
V 31   const router = _router || useRouter();
32
33   const { currentRoute } = router;
34
35   function getCurrentTab() {
36     const route = unref(currentRoute);
37     return tabStore.getTabList.find((item) => item.path === route.path)!;
38   }
39
40   async function handleTabAction(action: TableActionEnum, tab?: RouteLocationNormalized) {
41     const canIUse = canIUseTabs;
42     if (!canIUse) {
43       return;
44     }
45     const currentTab = getCurrentTab();
46     switch (action) {
47       case TableActionEnum.REFRESH:
48         await tabStore.refreshPage(router);
49         break;
50
51       case TableActionEnum.CLOSE_ALL:
52         await tabStore.closeAllTab(router);
53         break;
54
55       case TableActionEnum.CLOSE_LEFT:
56         await tabStore.closeLeftTabs(currentTab, router);
57         break;
58
59       case TableActionEnum.CLOSE_RIGHT:
60         await tabStore.closeRightTabs(currentTab, router);
61         break;
62
63       case TableActionEnum.CLOSE_OTHER:
64         await tabStore.closeOtherTabs(currentTab, router);
65         break;
66
67       case TableActionEnum.CLOSE_CURRENT:
68       case TableActionEnum.CLOSE:
69         await tabStore.closeTab(tab || currentTab, router);
70         break;
71     }
72   }
73
2f6253 74   return {
215d8b 75     refreshPage: () => handleTabAction(TableActionEnum.REFRESH),
V 76     closeAll: () => handleTabAction(TableActionEnum.CLOSE_ALL),
77     closeLeft: () => handleTabAction(TableActionEnum.CLOSE_LEFT),
78     closeRight: () => handleTabAction(TableActionEnum.CLOSE_RIGHT),
79     closeOther: () => handleTabAction(TableActionEnum.CLOSE_OTHER),
80     closeCurrent: () => handleTabAction(TableActionEnum.CLOSE_CURRENT),
81     close: (tab?: RouteLocationNormalized) => {
82       handleTabAction(TableActionEnum.CLOSE, tab);
a65ad9 83     },
2f6253 84   };
85 }