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 21 22 23

      <template #tabBarExtraContent v-if="getShowRedo || getShowQuick">
        <TabRedo v-if="getShowRedo" />
        <QuickButton v-if="getShowQuick" />
V
vben 已提交
24
        <FoldButton v-if="getShowFold" />
V
vben 已提交
25 26 27 28 29
      </template>
    </Tabs>
  </div>
</template>
<script lang="ts">
V
vben 已提交
30
  import { defineComponent, computed, unref, ref } from 'vue';
V
vben 已提交
31 32 33

  import { Tabs } from 'ant-design-vue';
  import TabContent from './components/TabContent.vue';
V
vben 已提交
34
  import type { RouteLocationNormalized } from 'vue-router';
V
vben 已提交
35 36 37 38 39 40 41 42 43 44

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

  import { tabStore } from '/@/store/modules/tab';
  import { userStore } from '/@/store/modules/user';

  import { initAffixTabs, useTabsDrag } from './useMultipleTabs';
  import { REDIRECT_NAME } from '/@/router/constant';
  import { useDesign } from '/@/hooks/web/useDesign';
  import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
V
vben 已提交
45 46
  import { listenerLastChangeTab } from '/@/logics/mitt/tabChange';
  import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
V
vben 已提交
47

V
vben 已提交
48 49
  import router from '/@/router';

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

      useTabsDrag(affixTextList);
      const { prefixCls } = useDesign('multiple-tabs');
      const go = useGo();
V
vben 已提交
67
      const { getShowQuick, getShowRedo, getShowFold } = useMultipleTabSetting();
V
vben 已提交
68

V
vben 已提交
69 70 71
      const getTabsState = computed(() => {
        return tabStore.getTabsState.filter((item) => !item.meta?.hideTab);
      });
V
vben 已提交
72

V
vben 已提交
73
      const unClose = computed(() => unref(getTabsState).length === 1);
V
vben 已提交
74 75 76 77 78

      const getWrapClass = computed(() => {
        return [
          prefixCls,
          {
V
vben 已提交
79
            [`${prefixCls}--hide-close`]: unref(unClose),
V
vben 已提交
80 81 82 83
          },
        ];
      });

V
vben 已提交
84 85 86 87
      listenerLastChangeTab((route) => {
        const { name } = route;
        if (name === REDIRECT_NAME || !route || !userStore.getTokenState) return;

V
vben 已提交
88
        const { path, fullPath, meta = {} } = route;
V
vben 已提交
89

V
vben 已提交
90 91 92
        const { currentActiveMenu, hideTab } = meta;
        const isHide = !hideTab ? null : currentActiveMenu;
        const p = isHide || fullPath || path;
V
vben 已提交
93 94
        if (activeKeyRef.value !== p) {
          activeKeyRef.value = p;
V
vben 已提交
95
        }
V
vben 已提交
96 97 98 99 100 101 102 103 104 105

        if (isHide) {
          const findParentRoute = router
            .getRoutes()
            .find((item) => item.path === currentActiveMenu);
          findParentRoute &&
            tabStore.addTabAction((findParentRoute as unknown) as RouteLocationNormalized);
        } else {
          tabStore.addTabAction(unref(route));
        }
V
vben 已提交
106
      });
V
vben 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

      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
        if (unref(unClose)) return;

        tabStore.closeTabByKeyAction(targetKey);
      }
      return {
        prefixCls,
        unClose,
        getWrapClass,
        handleEdit,
        handleChange,
        activeKeyRef,
        getTabsState,
V
vben 已提交
128 129
        getShowQuick,
        getShowRedo,
V
vben 已提交
130
        getShowFold,
V
vben 已提交
131 132 133 134 135 136 137
      };
    },
  });
</script>
<style lang="less">
  @import './index.less';
</style>