Blame view

src/layouts/default/multitabs/useTabDropdown.ts 6.76 KB
陈文彬 authored
1
import type { AppRouteRecordRaw } from '/@/router/types';
2
import type { TabContentProps } from './data';
陈文彬 authored
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';
8
import { TabContentEnum, MenuEventEnum, getActions } from './data';
陈文彬 authored
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';
15
import { RouteLocationRaw } from 'vue-router';
陈文彬 authored
16
17

const { initTabFn } = useTabs();
18
陈文彬 authored
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);
  });
31
32
  // Current tab list
  const getTabsState = computed(() => tabStore.getTabsState);
陈文彬 authored
33
34

  /**
35
   * @description: drop-down list
陈文彬 authored
36
37
38
   */
  const getDropMenuList = computed(() => {
    const dropMenuList = getActions();
39
    // Reset to initial state
陈文彬 authored
40
41
42
43
    for (const item of dropMenuList) {
      item.disabled = false;
    }
44
    // No tab
陈文彬 authored
45
46
47
    if (!unref(getTabsState) || unref(getTabsState).length <= 0) {
      return dropMenuList;
    } else if (unref(getTabsState).length === 1) {
48
      // Only one tab
陈文彬 authored
49
50
51
52
53
54
55
      for (const item of dropMenuList) {
        if (item.event !== MenuEventEnum.REFRESH_PAGE) {
          item.disabled = true;
        }
      }
      return dropMenuList;
    }
56
    if (!unref(getCurrentTab)) return;
陈文彬 authored
57
58
    const { meta, path } = unref(getCurrentTab);
59
    // Refresh button
陈文彬 authored
60
61
62
    const curItem = tabStore.getCurrentContextMenuState;
    const index = tabStore.getCurrentContextMenuIndexState;
    const refreshDisabled = curItem ? curItem.path !== path : true;
63
    // Close left
陈文彬 authored
64
65
    const closeLeftDisabled = index === 0;
66
    // Close right
陈文彬 authored
67
    const closeRightDisabled = index === unref(getTabsState).length - 1;
68
69
    // Currently fixed tab
    // TODO PERf
陈文彬 authored
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;
  });

  /**
81
   * @description: Jump to page when closing all pages
陈文彬 authored
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) {
90
91
92
93
94
      const page = unref(getTabsState)[len - 1];
      const p = page.fullPath || page.path;
      if (p) {
        toPath = p;
      }
陈文彬 authored
95
    }
96
    // Jump to the current page and report an error
陈文彬 authored
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;
103
    // Not the current tab, when you close the left/right side, you need to jump to the page
陈文彬 authored
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();
  }
114
陈文彬 authored
115
116
117
118
  function closeAll() {
    tabStore.commitCloseAllTab();
    gotoPage();
  }
119
陈文彬 authored
120
121
122
123
  function closeLeft(tabItem?: TabItem) {
    tabStore.closeLeftTabAction(tabItem || unref(getCurrentTab));
    isGotoPage(tabItem);
  }
124
陈文彬 authored
125
126
127
128
  function closeRight(tabItem?: TabItem) {
    tabStore.closeRightTabAction(tabItem || unref(getCurrentTab));
    isGotoPage(tabItem);
  }
129
陈文彬 authored
130
131
132
133
  function closeOther(tabItem?: TabItem) {
    tabStore.closeOtherTabAction(tabItem || unref(getCurrentTab));
    isGotoPage(tabItem);
  }
134
陈文彬 authored
135
136
137
  function closeCurrent(tabItem?: TabItem) {
    closeTab(unref(tabItem || unref(getCurrentTab)));
  }
138
陈文彬 authored
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,
    });
  }
162
  // Handle right click event
陈文彬 authored
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:
171
        // refresh page
陈文彬 authored
172
173
        refreshPage();
        break;
174
      // Close current
陈文彬 authored
175
176
177
      case MenuEventEnum.CLOSE_CURRENT:
        closeCurrent();
        break;
178
      // Close left
陈文彬 authored
179
180
181
      case MenuEventEnum.CLOSE_LEFT:
        closeLeft();
        break;
182
      // Close right
陈文彬 authored
183
184
185
      case MenuEventEnum.CLOSE_RIGHT:
        closeRight();
        break;
186
      // Close other
陈文彬 authored
187
188
189
      case MenuEventEnum.CLOSE_OTHER:
        closeOther();
        break;
190
      // Close all
陈文彬 authored
191
192
193
194
195
196
197
      case MenuEventEnum.CLOSE_ALL:
        closeAll();
        break;
    }
  }
  return { getDropMenuList, handleMenuEvent };
}
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 || {},
  };
}
208
export function closeTab(closedTab: TabItem | AppRouteRecordRaw) {
陈文彬 authored
209
  const { currentRoute, replace } = router;
210
211
  // Current tab list
  const getTabsState = computed(() => tabStore.getTabsState);
212
陈文彬 authored
213
214
  const { path } = unref(currentRoute);
  if (path !== closedTab.path) {
215
    // Closed is not the activation tab
陈文彬 authored
216
217
218
    tabStore.commitCloseTab(closedTab);
    return;
  }
219
220

  // Closed is activated atb
221
  let toObj: RouteLocationRaw = {};
222
陈文彬 authored
223
224
  const index = unref(getTabsState).findIndex((item) => item.path === path);
225
  // If the current is the leftmost tab
陈文彬 authored
226
  if (index === 0) {
227
    // There is only one tab, then jump to the homepage, otherwise jump to the right tab
陈文彬 authored
228
    if (unref(getTabsState).length === 1) {
229
      toObj = PageEnum.BASE_HOME;
陈文彬 authored
230
    } else {
231
      //  Jump to the right tab
232
      const page = unref(getTabsState)[index + 1];
233
      toObj = getObj(page);
陈文彬 authored
234
235
    }
  } else {
236
    // Close the current tab
237
    const page = unref(getTabsState)[index - 1];
238
    toObj = getObj(page);
陈文彬 authored
239
240
241
  }
  const route = (unref(currentRoute) as unknown) as AppRouteRecordRaw;
  tabStore.commitCloseTab(route);
242
  replace(toObj);
陈文彬 authored
243
}