Blame view

src/layouts/default/tabs/useTabDropdown.ts 4.09 KB
vben authored
1
import type { TabContentProps } from './types';
陈文彬 authored
2
3
import type { DropMenu } from '/@/components/Dropdown';
vben authored
4
5
import { computed, unref, reactive } from 'vue';
import { TabContentEnum, MenuEventEnum } from './types';
陈文彬 authored
6
7
import { tabStore } from '/@/store/modules/tab';
import router from '/@/router';
vben authored
8
9
10
import { RouteLocationNormalized } from 'vue-router';
import { useTabs } from '/@/hooks/web/useTabs';
import { useI18n } from '/@/hooks/web/useI18n';
陈文彬 authored
11
vben authored
12
const { t } = useI18n();
13
陈文彬 authored
14
export function useTabDropdown(tabContentProps: TabContentProps) {
vben authored
15
16
17
  const state = reactive({
    current: null as Nullable<RouteLocationNormalized>,
    currentIndex: 0,
陈文彬 authored
18
19
  });
vben authored
20
21
  const { currentRoute } = router;
vben authored
22
  const isTabs = computed(() => tabContentProps.type === TabContentEnum.TAB_TYPE);
vben authored
23
24
25
26
27
28
29

  const getCurrentTab = computed(
    (): RouteLocationNormalized => {
      return unref(isTabs) ? tabContentProps.tabItem : unref(currentRoute);
    }
  );
陈文彬 authored
30
  /**
31
   * @description: drop-down list
陈文彬 authored
32
33
   */
  const getDropMenuList = computed(() => {
34
    if (!unref(getCurrentTab)) return;
vben authored
35
36
    const { meta } = unref(getCurrentTab);
    const { path } = unref(currentRoute);
陈文彬 authored
37
38
    // Refresh button
vben authored
39
40
    const curItem = state.current;
    const index = state.currentIndex;
陈文彬 authored
41
    const refreshDisabled = curItem ? curItem.path !== path : true;
42
    // Close left
陈文彬 authored
43
44
    const closeLeftDisabled = index === 0;
vben authored
45
46
    const disabled = tabStore.getTabsState.length === 1;
47
    // Close right
vben authored
48
49
50
51
    const closeRightDisabled =
      index === tabStore.getTabsState.length - 1 && tabStore.getLastDragEndIndexState >= 0;
    const dropMenuList: DropMenu[] = [
      {
陈小婷 authored
52
        icon: 'ion:reload-sharp',
vben authored
53
        event: MenuEventEnum.REFRESH_PAGE,
vben authored
54
        text: t('layout.multipleTab.reload'),
vben authored
55
56
57
        disabled: refreshDisabled,
      },
      {
陈小婷 authored
58
        icon: 'clarity:close-line',
vben authored
59
60
61
62
63
64
        event: MenuEventEnum.CLOSE_CURRENT,
        text: t('layout.multipleTab.close'),
        disabled: meta?.affix || disabled,
        divider: true,
      },
      {
陈小婷 authored
65
        icon: 'line-md:arrow-close-left',
vben authored
66
67
68
69
70
71
        event: MenuEventEnum.CLOSE_LEFT,
        text: t('layout.multipleTab.closeLeft'),
        disabled: closeLeftDisabled,
        divider: false,
      },
      {
陈小婷 authored
72
        icon: 'line-md:arrow-close-right',
vben authored
73
74
75
76
77
78
        event: MenuEventEnum.CLOSE_RIGHT,
        text: t('layout.multipleTab.closeRight'),
        disabled: closeRightDisabled,
        divider: true,
      },
      {
陈小婷 authored
79
        icon: 'dashicons:align-center',
vben authored
80
81
82
83
84
        event: MenuEventEnum.CLOSE_OTHER,
        text: t('layout.multipleTab.closeOther'),
        disabled: disabled,
      },
      {
陈小婷 authored
85
        icon: 'clarity:minus-line',
vben authored
86
87
88
89
90
91
        event: MenuEventEnum.CLOSE_ALL,
        text: t('layout.multipleTab.closeAll'),
        disabled: disabled,
      },
    ];
陈文彬 authored
92
93
94
    return dropMenuList;
  });
vben authored
95
96
97
  const getTrigger = computed(() => {
    return unref(isTabs) ? ['contextmenu'] : ['click'];
  });
98
vben authored
99
100
101
102
103
104
105
106
  function handleContextMenu(tabItem: RouteLocationNormalized) {
    return (e: Event) => {
      if (!tabItem) return;
      e?.preventDefault();
      const index = tabStore.getTabsState.findIndex((tab) => tab.path === tabItem.path);
      state.current = tabItem;
      state.currentIndex = index;
    };
陈文彬 authored
107
  }
108
109

  // Handle right click event
陈文彬 authored
110
  function handleMenuEvent(menu: DropMenu): void {
vben authored
111
    const { refreshPage, closeAll, close, closeLeft, closeOther, closeRight } = useTabs();
陈文彬 authored
112
113
114
115
116
117
    const { event } = menu;
    switch (event) {
      case MenuEventEnum.SCALE:
        scaleScreen();
        break;
      case MenuEventEnum.REFRESH_PAGE:
118
        // refresh page
陈文彬 authored
119
120
        refreshPage();
        break;
121
      // Close current
陈文彬 authored
122
      case MenuEventEnum.CLOSE_CURRENT:
vben authored
123
        close(tabContentProps.tabItem);
陈文彬 authored
124
        break;
125
      // Close left
陈文彬 authored
126
127
128
      case MenuEventEnum.CLOSE_LEFT:
        closeLeft();
        break;
129
      // Close right
陈文彬 authored
130
131
132
      case MenuEventEnum.CLOSE_RIGHT:
        closeRight();
        break;
133
      // Close other
陈文彬 authored
134
135
136
      case MenuEventEnum.CLOSE_OTHER:
        closeOther();
        break;
137
      // Close all
陈文彬 authored
138
139
140
141
142
      case MenuEventEnum.CLOSE_ALL:
        closeAll();
        break;
    }
  }
vben authored
143
  return { getDropMenuList, handleMenuEvent, handleContextMenu, getTrigger, isTabs };
陈文彬 authored
144
}