index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. <TabContent isExtra :tabItem="$route" v-if="getShowQuick" />
  23. <FoldButton v-if="getShowFold" />
  24. </template>
  25. </Tabs>
  26. </div>
  27. </template>
  28. <script lang="ts">
  29. import type { RouteLocationNormalized } from 'vue-router'
  30. import { defineComponent, computed, unref, ref } from 'vue'
  31. import { Tabs } from 'ant-design-vue'
  32. import TabContent from './components/TabContent.vue'
  33. import FoldButton from './components/FoldButton.vue'
  34. import TabRedo from './components/TabRedo.vue'
  35. import { useGo } from '/@/hooks/web/usePage'
  36. import { useMultipleTabStore } from '/@/store/modules/multipleTab'
  37. import { useUserStore } from '/@/store/modules/user'
  38. import { initAffixTabs, useTabsDrag } from './useMultipleTabs'
  39. import { useDesign } from '/@/hooks/web/useDesign'
  40. import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting'
  41. import { REDIRECT_NAME } from '/@/router/constant'
  42. import { listenerRouteChange } from '/@/logics/mitt/routeChange'
  43. import { useRouter } from 'vue-router'
  44. export default defineComponent({
  45. name: 'MultipleTabs',
  46. components: {
  47. TabRedo,
  48. FoldButton,
  49. Tabs,
  50. TabPane: Tabs.TabPane,
  51. TabContent
  52. },
  53. setup() {
  54. const affixTextList = initAffixTabs()
  55. const activeKeyRef = ref('')
  56. useTabsDrag(affixTextList)
  57. const tabStore = useMultipleTabStore()
  58. const userStore = useUserStore()
  59. const router = useRouter()
  60. const { prefixCls } = useDesign('multiple-tabs')
  61. const go = useGo()
  62. const { getShowQuick, getShowRedo, getShowFold } = useMultipleTabSetting()
  63. const getTabsState = computed(() => {
  64. return tabStore.getTabList.filter(item => !item.meta?.hideTab)
  65. })
  66. const unClose = computed(() => unref(getTabsState).length === 1)
  67. const getWrapClass = computed(() => {
  68. return [
  69. prefixCls,
  70. {
  71. [`${prefixCls}--hide-close`]: unref(unClose)
  72. }
  73. ]
  74. })
  75. listenerRouteChange(route => {
  76. const { name } = route
  77. if (name === REDIRECT_NAME || !route || !userStore.getToken) {
  78. return
  79. }
  80. const { path, fullPath, meta = {} } = route
  81. const { currentActiveMenu, hideTab } = meta
  82. const isHide = !hideTab ? null : currentActiveMenu
  83. const p = isHide || fullPath || path
  84. if (activeKeyRef.value !== p) {
  85. activeKeyRef.value = p as string
  86. }
  87. if (isHide) {
  88. const findParentRoute = router.getRoutes().find(item => item.path === currentActiveMenu)
  89. findParentRoute && tabStore.addTab(findParentRoute as unknown as RouteLocationNormalized)
  90. } else {
  91. tabStore.addTab(unref(route))
  92. }
  93. })
  94. function handleChange(activeKey: any) {
  95. activeKeyRef.value = activeKey
  96. go(activeKey, false)
  97. }
  98. // Close the current tab
  99. function handleEdit(targetKey: string) {
  100. // Added operation to hide, currently only use delete operation
  101. if (unref(unClose)) {
  102. return
  103. }
  104. tabStore.closeTabByKey(targetKey, router)
  105. }
  106. return {
  107. prefixCls,
  108. unClose,
  109. getWrapClass,
  110. handleEdit,
  111. handleChange,
  112. activeKeyRef,
  113. getTabsState,
  114. getShowQuick,
  115. getShowRedo,
  116. getShowFold
  117. }
  118. }
  119. })
  120. </script>
  121. <style lang="less">
  122. @import './index.less';
  123. </style>