Blame view

src/layouts/default/tabs/useTabDropdown.ts 3.94 KB
vben authored
1
import type { TabContentProps } from './types';
陈文彬 authored
2
import type { DropMenu } from '/@/components/Dropdown';
Vben authored
3
import type { ComputedRef } from 'vue';
陈文彬 authored
4
vben authored
5
import { computed, unref, reactive } from 'vue';
Vben authored
6
7
8
import { MenuEventEnum } from './types';
import { useMultipleTabStore } from '/@/store/modules/multipleTab';
import { RouteLocationNormalized, useRouter } from 'vue-router';
vben authored
9
10
import { useTabs } from '/@/hooks/web/useTabs';
import { useI18n } from '/@/hooks/web/useI18n';
陈文彬 authored
11
Vben authored
12
export function useTabDropdown(tabContentProps: TabContentProps, getIsTabs: ComputedRef<boolean>) {
vben authored
13
14
15
  const state = reactive({
    current: null as Nullable<RouteLocationNormalized>,
    currentIndex: 0,
陈文彬 authored
16
17
  });
Vben authored
18
19
20
21
  const { t } = useI18n();
  const tabStore = useMultipleTabStore();
  const { currentRoute } = useRouter();
  const { refreshPage, closeAll, close, closeLeft, closeOther, closeRight } = useTabs();
vben authored
22
Vben authored
23
24
25
  const getTargetTab = computed((): RouteLocationNormalized => {
    return unref(getIsTabs) ? tabContentProps.tabItem : unref(currentRoute);
  });
vben authored
26
陈文彬 authored
27
  /**
28
   * @description: drop-down list
陈文彬 authored
29
30
   */
  const getDropMenuList = computed(() => {
Vben authored
31
32
33
34
    if (!unref(getTargetTab)) {
      return;
    }
    const { meta } = unref(getTargetTab);
vben authored
35
    const { path } = unref(currentRoute);
陈文彬 authored
36
37
    // Refresh button
vben authored
38
39
    const curItem = state.current;
    const index = state.currentIndex;
陈文彬 authored
40
    const refreshDisabled = curItem ? curItem.path !== path : true;
41
    // Close left
陈文彬 authored
42
43
    const closeLeftDisabled = index === 0;
Vben authored
44
    const disabled = tabStore.getTabList.length === 1;
vben authored
45
46
    // Close right
vben authored
47
    const closeRightDisabled =
Vben authored
48
      index === tabStore.getTabList.length - 1 && tabStore.getLastDragEndIndex >= 0;
vben authored
49
50
    const dropMenuList: DropMenu[] = [
      {
陈小婷 authored
51
        icon: 'ion:reload-sharp',
vben authored
52
        event: MenuEventEnum.REFRESH_PAGE,
vben authored
53
        text: t('layout.multipleTab.reload'),
vben authored
54
55
56
        disabled: refreshDisabled,
      },
      {
陈小婷 authored
57
        icon: 'clarity:close-line',
vben authored
58
59
        event: MenuEventEnum.CLOSE_CURRENT,
        text: t('layout.multipleTab.close'),
Vben authored
60
        disabled: !!meta?.affix || disabled,
vben authored
61
62
63
        divider: true,
      },
      {
陈小婷 authored
64
        icon: 'line-md:arrow-close-left',
vben authored
65
66
67
68
69
70
        event: MenuEventEnum.CLOSE_LEFT,
        text: t('layout.multipleTab.closeLeft'),
        disabled: closeLeftDisabled,
        divider: false,
      },
      {
陈小婷 authored
71
        icon: 'line-md:arrow-close-right',
vben authored
72
73
74
75
76
77
        event: MenuEventEnum.CLOSE_RIGHT,
        text: t('layout.multipleTab.closeRight'),
        disabled: closeRightDisabled,
        divider: true,
      },
      {
陈小婷 authored
78
        icon: 'dashicons:align-center',
vben authored
79
80
81
82
83
        event: MenuEventEnum.CLOSE_OTHER,
        text: t('layout.multipleTab.closeOther'),
        disabled: disabled,
      },
      {
陈小婷 authored
84
        icon: 'clarity:minus-line',
vben authored
85
86
87
88
89
90
        event: MenuEventEnum.CLOSE_ALL,
        text: t('layout.multipleTab.closeAll'),
        disabled: disabled,
      },
    ];
陈文彬 authored
91
92
93
    return dropMenuList;
  });
vben authored
94
95
  function handleContextMenu(tabItem: RouteLocationNormalized) {
    return (e: Event) => {
Vben authored
96
97
98
      if (!tabItem) {
        return;
      }
vben authored
99
      e?.preventDefault();
Vben authored
100
      const index = tabStore.getTabList.findIndex((tab) => tab.path === tabItem.path);
vben authored
101
102
103
      state.current = tabItem;
      state.currentIndex = index;
    };
陈文彬 authored
104
  }
105
106

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