index.vue 4.1 KB
Newer Older
V
vben 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<template>
  <div :class="getWrapClass">
    <Tabs
      type="editable-card"
      size="small"
      :animated="false"
      :hideAdd="true"
      :tabBarGutter="3"
      :activeKey="activeKeyRef"
      @change="handleChange"
      @edit="handleEdit"
    >
      <template v-for="item in getTabsState" :key="item.query ? item.fullPath : item.path">
        <TabPane :closable="!(item && item.meta && item.meta.affix)">
          <template #tab>
            <TabContent :tabItem="item" />
          </template>
        </TabPane>
      </template>
V
vben 已提交
20

V
vben 已提交
21
      <template #rightExtra v-if="getShowRedo || getShowQuick">
V
vben 已提交
22
        <TabRedo v-if="getShowRedo" />
V
Vben 已提交
23
        <TabContent isExtra :tabItem="$route" v-if="getShowQuick" />
V
vben 已提交
24
        <FoldButton v-if="getShowFold" />
V
vben 已提交
25 26 27 28 29
      </template>
    </Tabs>
  </div>
</template>
<script lang="ts">
30
  import type { RouteLocationNormalized, RouteMeta } from 'vue-router';
V
Vben 已提交
31

V
vben 已提交
32
  import { defineComponent, computed, unref, ref } from 'vue';
V
vben 已提交
33 34 35

  import { Tabs } from 'ant-design-vue';
  import TabContent from './components/TabContent.vue';
V
Vben 已提交
36 37
  import FoldButton from './components/FoldButton.vue';
  import TabRedo from './components/TabRedo.vue';
V
vben 已提交
38 39 40

  import { useGo } from '/@/hooks/web/usePage';

V
Vben 已提交
41 42
  import { useMultipleTabStore } from '/@/store/modules/multipleTab';
  import { useUserStore } from '/@/store/modules/user';
V
vben 已提交
43 44 45

  import { initAffixTabs, useTabsDrag } from './useMultipleTabs';
  import { useDesign } from '/@/hooks/web/useDesign';
V
vben 已提交
46
  import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
V
vben 已提交
47

V
Vben 已提交
48
  import { REDIRECT_NAME } from '/@/router/constant';
V
Vben 已提交
49
  import { listenerRouteChange } from '/@/logics/mitt/routeChange';
V
Vben 已提交
50

V
Vben 已提交
51
  import { useRouter } from 'vue-router';
V
vben 已提交
52

V
vben 已提交
53 54 55
  export default defineComponent({
    name: 'MultipleTabs',
    components: {
V
Vben 已提交
56
      TabRedo,
V
Vben 已提交
57
      FoldButton,
V
vben 已提交
58 59 60 61 62 63 64 65 66
      Tabs,
      TabPane: Tabs.TabPane,
      TabContent,
    },
    setup() {
      const affixTextList = initAffixTabs();
      const activeKeyRef = ref('');

      useTabsDrag(affixTextList);
V
Vben 已提交
67 68 69 70
      const tabStore = useMultipleTabStore();
      const userStore = useUserStore();
      const router = useRouter();

V
vben 已提交
71 72
      const { prefixCls } = useDesign('multiple-tabs');
      const go = useGo();
V
vben 已提交
73
      const { getShowQuick, getShowRedo, getShowFold } = useMultipleTabSetting();
V
vben 已提交
74

V
vben 已提交
75
      const getTabsState = computed(() => {
V
Vben 已提交
76
        return tabStore.getTabList.filter((item) => !item.meta?.hideTab);
V
vben 已提交
77
      });
V
vben 已提交
78

V
vben 已提交
79
      const unClose = computed(() => unref(getTabsState).length === 1);
V
vben 已提交
80 81 82 83 84

      const getWrapClass = computed(() => {
        return [
          prefixCls,
          {
V
vben 已提交
85
            [`${prefixCls}--hide-close`]: unref(unClose),
V
vben 已提交
86 87 88 89
          },
        ];
      });

V
Vben 已提交
90
      listenerRouteChange((route) => {
V
vben 已提交
91
        const { name } = route;
V
Vben 已提交
92 93 94
        if (name === REDIRECT_NAME || !route || !userStore.getToken) {
          return;
        }
V
vben 已提交
95

V
vben 已提交
96
        const { path, fullPath, meta = {} } = route;
97
        const { currentActiveMenu, hideTab } = meta as RouteMeta;
V
vben 已提交
98 99
        const isHide = !hideTab ? null : currentActiveMenu;
        const p = isHide || fullPath || path;
V
vben 已提交
100
        if (activeKeyRef.value !== p) {
V
Vben 已提交
101
          activeKeyRef.value = p as string;
V
vben 已提交
102
        }
V
vben 已提交
103 104 105 106 107

        if (isHide) {
          const findParentRoute = router
            .getRoutes()
            .find((item) => item.path === currentActiveMenu);
V
Vben 已提交
108

V
Vben 已提交
109
          findParentRoute && tabStore.addTab(findParentRoute as unknown as RouteLocationNormalized);
V
vben 已提交
110
        } else {
V
Vben 已提交
111
          tabStore.addTab(unref(route));
V
vben 已提交
112
        }
V
vben 已提交
113
      });
V
vben 已提交
114 115 116 117 118 119 120 121 122

      function handleChange(activeKey: any) {
        activeKeyRef.value = activeKey;
        go(activeKey, false);
      }

      // Close the current tab
      function handleEdit(targetKey: string) {
        // Added operation to hide, currently only use delete operation
V
Vben 已提交
123 124 125
        if (unref(unClose)) {
          return;
        }
V
vben 已提交
126

V
Vben 已提交
127
        tabStore.closeTabByKey(targetKey, router);
V
vben 已提交
128 129 130 131 132 133 134
      }
      return {
        getWrapClass,
        handleEdit,
        handleChange,
        activeKeyRef,
        getTabsState,
V
vben 已提交
135 136
        getShowQuick,
        getShowRedo,
V
vben 已提交
137
        getShowFold,
V
vben 已提交
138 139 140 141 142 143 144
      };
    },
  });
</script>
<style lang="less">
  @import './index.less';
</style>