index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <div :class="getWrapClass">
  3. <Tabs
  4. type="editable-card"
  5. size="small"
  6. :animated="false"
  7. :hideAdd="true"
  8. :tabBarGutter="3"
  9. :activeKey="activeKeyRef"
  10. @change="handleChange"
  11. @edit="handleEdit"
  12. >
  13. <template v-for="item in getTabsState" :key="item.query ? item.fullPath : item.path">
  14. <TabPane :closable="!(item && item.meta && item.meta.affix)">
  15. <template #tab>
  16. <TabContent :tabItem="item" />
  17. </template>
  18. </TabPane>
  19. </template>
  20. <template #tabBarExtraContent v-if="getShowRedo || getShowQuick">
  21. <TabRedo v-if="getShowRedo" />
  22. <QuickButton v-if="getShowQuick" />
  23. <FoldButton v-if="getShowFold" />
  24. </template>
  25. </Tabs>
  26. </div>
  27. </template>
  28. <script lang="ts">
  29. import { defineComponent, computed, unref, ref } from 'vue';
  30. import { Tabs } from 'ant-design-vue';
  31. import TabContent from './components/TabContent.vue';
  32. import type { RouteLocationNormalized } from 'vue-router';
  33. import { useGo } from '/@/hooks/web/usePage';
  34. import { tabStore } from '/@/store/modules/tab';
  35. import { userStore } from '/@/store/modules/user';
  36. import { initAffixTabs, useTabsDrag } from './useMultipleTabs';
  37. import { REDIRECT_NAME } from '/@/router/constant';
  38. import { useDesign } from '/@/hooks/web/useDesign';
  39. import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
  40. import { listenerLastChangeTab } from '/@/logics/mitt/tabChange';
  41. import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
  42. import router from '/@/router';
  43. export default defineComponent({
  44. name: 'MultipleTabs',
  45. components: {
  46. QuickButton: createAsyncComponent(() => import('./components/QuickButton.vue')),
  47. TabRedo: createAsyncComponent(() => import('./components/TabRedo.vue')),
  48. FoldButton: createAsyncComponent(() => import('./components/FoldButton.vue')),
  49. Tabs,
  50. TabPane: Tabs.TabPane,
  51. TabContent,
  52. },
  53. setup() {
  54. const affixTextList = initAffixTabs();
  55. const activeKeyRef = ref('');
  56. useTabsDrag(affixTextList);
  57. const { prefixCls } = useDesign('multiple-tabs');
  58. const go = useGo();
  59. const { getShowQuick, getShowRedo, getShowFold } = useMultipleTabSetting();
  60. const getTabsState = computed(() => {
  61. return tabStore.getTabsState.filter((item) => !item.meta?.hideTab);
  62. });
  63. const unClose = computed(() => unref(getTabsState).length === 1);
  64. const getWrapClass = computed(() => {
  65. return [
  66. prefixCls,
  67. {
  68. [`${prefixCls}--hide-close`]: unref(unClose),
  69. },
  70. ];
  71. });
  72. listenerLastChangeTab((route) => {
  73. const { name } = route;
  74. if (name === REDIRECT_NAME || !route || !userStore.getTokenState) return;
  75. const { path, fullPath, meta = {} } = route;
  76. const { currentActiveMenu, hideTab } = meta;
  77. const isHide = !hideTab ? null : currentActiveMenu;
  78. const p = isHide || fullPath || path;
  79. if (activeKeyRef.value !== p) {
  80. activeKeyRef.value = p as string;
  81. }
  82. if (isHide) {
  83. const findParentRoute = router
  84. .getRoutes()
  85. .find((item) => item.path === currentActiveMenu);
  86. findParentRoute &&
  87. tabStore.addTabAction((findParentRoute as unknown) as RouteLocationNormalized);
  88. } else {
  89. tabStore.addTabAction(unref(route));
  90. }
  91. });
  92. function handleChange(activeKey: any) {
  93. activeKeyRef.value = activeKey;
  94. go(activeKey, false);
  95. }
  96. // Close the current tab
  97. function handleEdit(targetKey: string) {
  98. // Added operation to hide, currently only use delete operation
  99. if (unref(unClose)) return;
  100. tabStore.closeTabByKeyAction(targetKey);
  101. }
  102. return {
  103. prefixCls,
  104. unClose,
  105. getWrapClass,
  106. handleEdit,
  107. handleChange,
  108. activeKeyRef,
  109. getTabsState,
  110. getShowQuick,
  111. getShowRedo,
  112. getShowFold,
  113. };
  114. },
  115. });
  116. </script>
  117. <style lang="less">
  118. @import './index.less';
  119. </style>