Blame view

src/layouts/default/multitabs/useTabDropdown.ts 5.17 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
11
12
13
import { RouteLocationNormalized } from 'vue-router';
import { useTabs } from '/@/hooks/web/useTabs';
import { useI18n } from '/@/hooks/web/useI18n';
import { useHeaderSetting } from '/@/hooks/setting/useHeaderSetting';
import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
陈文彬 authored
14
vben authored
15
const { t } = useI18n();
16
陈文彬 authored
17
export function useTabDropdown(tabContentProps: TabContentProps) {
vben authored
18
19
20
  const state = reactive({
    current: null as Nullable<RouteLocationNormalized>,
    currentIndex: 0,
陈文彬 authored
21
22
  });
vben authored
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  const { currentRoute } = router;

  const { getShowMenu, setMenuSetting } = useMenuSetting();
  const { getShowHeader, setHeaderSetting } = useHeaderSetting();
  const { getShowQuick } = useMultipleTabSetting();

  const isTabs = computed(() =>
    !unref(getShowQuick) ? true : tabContentProps.type === TabContentEnum.TAB_TYPE
  );

  const getCurrentTab = computed(
    (): RouteLocationNormalized => {
      return unref(isTabs) ? tabContentProps.tabItem : unref(currentRoute);
    }
  );

  const getIsScale = computed(() => {
    return !unref(getShowMenu) && !unref(getShowHeader);
  });
陈文彬 authored
42
43

  /**
44
   * @description: drop-down list
陈文彬 authored
45
46
   */
  const getDropMenuList = computed(() => {
47
    if (!unref(getCurrentTab)) return;
vben authored
48
49
    const { meta } = unref(getCurrentTab);
    const { path } = unref(currentRoute);
陈文彬 authored
50
51
    // Refresh button
vben authored
52
53
    const curItem = state.current;
    const index = state.currentIndex;
陈文彬 authored
54
    const refreshDisabled = curItem ? curItem.path !== path : true;
55
    // Close left
陈文彬 authored
56
57
    const closeLeftDisabled = index === 0;
vben authored
58
59
    const disabled = tabStore.getTabsState.length === 1;
60
    // Close right
vben authored
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    const closeRightDisabled =
      index === tabStore.getTabsState.length - 1 && tabStore.getLastDragEndIndexState >= 0;
    const dropMenuList: DropMenu[] = [
      {
        icon: 'ant-design:reload-outlined',
        event: MenuEventEnum.REFRESH_PAGE,
        text: t('layout.multipleTab.redo'),
        disabled: refreshDisabled,
      },
      {
        icon: 'ant-design:close-outlined',
        event: MenuEventEnum.CLOSE_CURRENT,
        text: t('layout.multipleTab.close'),
        disabled: meta?.affix || disabled,
        divider: true,
      },
      {
        icon: 'ant-design:pic-left-outlined',
        event: MenuEventEnum.CLOSE_LEFT,
        text: t('layout.multipleTab.closeLeft'),
        disabled: closeLeftDisabled,
        divider: false,
      },
      {
        icon: 'ant-design:pic-right-outlined',
        event: MenuEventEnum.CLOSE_RIGHT,
        text: t('layout.multipleTab.closeRight'),
        disabled: closeRightDisabled,
        divider: true,
      },
      {
        icon: 'ant-design:pic-center-outlined',
        event: MenuEventEnum.CLOSE_OTHER,
        text: t('layout.multipleTab.closeOther'),
        disabled: disabled,
      },
      {
        icon: 'ant-design:line-outlined',
        event: MenuEventEnum.CLOSE_ALL,
        text: t('layout.multipleTab.closeAll'),
        disabled: disabled,
      },
    ];

    if (!unref(isTabs)) {
      const isScale = unref(getIsScale);
      dropMenuList.unshift({
        icon: isScale ? 'codicon:screen-normal' : 'codicon:screen-full',
        event: MenuEventEnum.SCALE,
        text: isScale ? t('layout.multipleTab.putAway') : t('layout.multipleTab.unfold'),
        disabled: false,
      });
陈文彬 authored
113
114
115
116
117
    }

    return dropMenuList;
  });
vben authored
118
119
120
  const getTrigger = computed(() => {
    return unref(isTabs) ? ['contextmenu'] : ['click'];
  });
121
vben authored
122
123
124
125
126
127
128
129
  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
130
  }
131
陈文彬 authored
132
  function scaleScreen() {
vben authored
133
134
135
    const isScale = !unref(getShowMenu) && !unref(getShowHeader);
    setMenuSetting({
      show: isScale,
陈文彬 authored
136
    });
vben authored
137
138
    setHeaderSetting({
      show: isScale,
陈文彬 authored
139
140
141
    });
  }
142
  // Handle right click event
陈文彬 authored
143
  function handleMenuEvent(menu: DropMenu): void {
vben authored
144
    const { refreshPage, closeAll, closeCurrent, closeLeft, closeOther, closeRight } = useTabs();
陈文彬 authored
145
146
147
148
149
150
    const { event } = menu;
    switch (event) {
      case MenuEventEnum.SCALE:
        scaleScreen();
        break;
      case MenuEventEnum.REFRESH_PAGE:
151
        // refresh page
陈文彬 authored
152
153
        refreshPage();
        break;
154
      // Close current
陈文彬 authored
155
156
157
      case MenuEventEnum.CLOSE_CURRENT:
        closeCurrent();
        break;
158
      // Close left
陈文彬 authored
159
160
161
      case MenuEventEnum.CLOSE_LEFT:
        closeLeft();
        break;
162
      // Close right
陈文彬 authored
163
164
165
      case MenuEventEnum.CLOSE_RIGHT:
        closeRight();
        break;
166
      // Close other
陈文彬 authored
167
168
169
      case MenuEventEnum.CLOSE_OTHER:
        closeOther();
        break;
170
      // Close all
陈文彬 authored
171
172
173
174
175
      case MenuEventEnum.CLOSE_ALL:
        closeAll();
        break;
    }
  }
vben authored
176
  return { getDropMenuList, handleMenuEvent, handleContextMenu, getTrigger, isTabs };
陈文彬 authored
177
}