Blame view

src/layouts/default/tabs/index.vue 4.01 KB
vben authored
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>
vben authored
20
21
22
23

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

  import { Tabs } from 'ant-design-vue';
  import TabContent from './components/TabContent.vue';
Vben authored
34
35
36
  import QuickButton from './components/QuickButton.vue';
  import FoldButton from './components/FoldButton.vue';
  import TabRedo from './components/TabRedo.vue';
37
  import type { RouteLocationNormalized } from 'vue-router';
vben authored
38
39
40
41
42
43
44
45

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

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

  import { initAffixTabs, useTabsDrag } from './useMultipleTabs';
  import { useDesign } from '/@/hooks/web/useDesign';
vben authored
46
  import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
vben authored
47
Vben authored
48
49
50
  import { REDIRECT_NAME } from '/@/router/constant';
  import { listenerLastChangeTab } from '/@/logics/mitt/tabChange';
51
52
  import router from '/@/router';
vben authored
53
54
55
  export default defineComponent({
    name: 'MultipleTabs',
    components: {
Vben authored
56
57
58
      QuickButton,
      TabRedo: TabRedo,
      FoldButton,
vben authored
59
60
61
62
63
64
65
66
67
68
69
      Tabs,
      TabPane: Tabs.TabPane,
      TabContent,
    },
    setup() {
      const affixTextList = initAffixTabs();
      const activeKeyRef = ref('');

      useTabsDrag(affixTextList);
      const { prefixCls } = useDesign('multiple-tabs');
      const go = useGo();
vben authored
70
      const { getShowQuick, getShowRedo, getShowFold } = useMultipleTabSetting();
vben authored
71
72
73
74
      const getTabsState = computed(() => {
        return tabStore.getTabsState.filter((item) => !item.meta?.hideTab);
      });
vben authored
75
vben authored
76
      const unClose = computed(() => unref(getTabsState).length === 1);
vben authored
77
78
79
80
81

      const getWrapClass = computed(() => {
        return [
          prefixCls,
          {
vben authored
82
            [`${prefixCls}--hide-close`]: unref(unClose),
vben authored
83
84
85
86
          },
        ];
      });
vben authored
87
88
89
90
      listenerLastChangeTab((route) => {
        const { name } = route;
        if (name === REDIRECT_NAME || !route || !userStore.getTokenState) return;
91
        const { path, fullPath, meta = {} } = route;
vben authored
92
93
94
95
        const { currentActiveMenu, hideTab } = meta;
        const isHide = !hideTab ? null : currentActiveMenu;
        const p = isHide || fullPath || path;
vben authored
96
        if (activeKeyRef.value !== p) {
Vben authored
97
          activeKeyRef.value = p as string;
vben authored
98
        }
99
100
101
102
103
104
105
106
107
108

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

      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,
vben authored
131
132
        getShowQuick,
        getShowRedo,
vben authored
133
        getShowFold,
vben authored
134
135
136
137
138
139
140
      };
    },
  });
</script>
<style lang="less">
  @import './index.less';
</style>