Blame view

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

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