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