web-view-antd/src/layouts/default/tabs/index.vue

150 lines
4.1 KiB
Vue
Raw Normal View History

2024-11-11 10:34:49 +08:00
<script lang="ts" setup>
import type { RouteLocationNormalized, RouteMeta } from 'vue-router'
import { useMouse } from '@vueuse/core'
import { computed, ref, unref } from 'vue'
import { Tabs } from 'ant-design-vue'
2025-03-13 17:35:20 +08:00
import { useRoute, useRouter } from 'vue-router'
2024-11-11 10:34:49 +08:00
import TabContent from './components/TabContent.vue'
import FoldButton from './components/FoldButton.vue'
import TabRedo from './components/TabRedo.vue'
import { initAffixTabs, useTabsDrag } from './useMultipleTabs'
import { multipleTabHeight } from '@/settings/designSetting'
import { useGo } from '@/hooks/web/usePage'
import { useMultipleTabStore } from '@/store/modules/multipleTab'
import { useUserStore } from '@/store/modules/user'
import { useDesign } from '@/hooks/web/useDesign'
import { useMultipleTabSetting } from '@/hooks/setting/useMultipleTabSetting'
import { REDIRECT_NAME } from '@/router/constant'
import { listenerRouteChange } from '@/logics/mitt/routeChange'
defineOptions({ name: 'MultipleTabs' })
2025-03-13 17:35:20 +08:00
const props = defineProps<{
scoped: boolean
}>()
2024-11-11 10:34:49 +08:00
const affixTextList = initAffixTabs()
const activeKeyRef = ref('')
useTabsDrag(affixTextList)
const tabStore = useMultipleTabStore()
const userStore = useUserStore()
const router = useRouter()
2025-03-13 17:35:20 +08:00
const route = useRoute()
2024-11-11 10:34:49 +08:00
const { prefixCls } = useDesign('multiple-tabs')
const go = useGo()
const { getShowQuick, getShowRedo, getShowFold } = useMultipleTabSetting()
2025-03-13 17:35:20 +08:00
function getFirstPathSegment(path: string): string {
if (!path || typeof path !== 'string')
return '/'
const segments = path.split('/').filter(Boolean) // 移除空字符串
return segments.length > 0 ? `/${segments[0]}` : '/'
}
2024-11-11 10:34:49 +08:00
const getTabsState = computed(() => {
2025-03-13 17:35:20 +08:00
const currentPath = route.path
const currentCloud = getFirstPathSegment(currentPath)
if (props.scoped) {
return tabStore.getTabList.filter(item => !item.meta?.hideTab).filter((item) => {
const firstPath = getFirstPathSegment(item.path)
return firstPath === currentCloud
})
}
else { return tabStore.getTabList.filter(item => !item.meta?.hideTab) }
2024-11-11 10:34:49 +08:00
})
const unClose = computed(() => unref(getTabsState).length === 1)
const { y: mouseY } = useMouse()
const getWrapClass = computed(() => {
return [
prefixCls,
{
[`${prefixCls}--hide-close`]: unref(unClose),
[`${prefixCls}--hover`]: unref(mouseY) < multipleTabHeight,
},
]
})
listenerRouteChange((route) => {
const { name } = route
if (name === REDIRECT_NAME || !route || !userStore.getAccessToken)
return
const { path, fullPath, meta = {} } = route
const { currentActiveMenu, hideTab } = meta as RouteMeta
const isHide = !hideTab ? null : currentActiveMenu
const p = isHide || fullPath || path
if (activeKeyRef.value !== p)
activeKeyRef.value = p as string
if (isHide) {
const findParentRoute = router.getRoutes().find(item => item.path === currentActiveMenu)
findParentRoute && tabStore.addTab(findParentRoute as unknown as RouteLocationNormalized)
}
else {
tabStore.addTab(unref(route))
}
})
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.closeTabByKey(targetKey, router)
}
</script>
<template>
<div :class="getWrapClass">
<Tabs
type="editable-card"
:animated="false"
:hide-add="true"
:tab-bar-gutter="3"
:active-key="activeKeyRef"
@change="handleChange"
@edit="handleEdit"
>
<template v-for="item in getTabsState" :key="item.query ? item.fullPath : item.path">
<Tabs.TabPane :closable="!(item && item.meta && item.meta.affix)">
<template #tab>
<TabContent :tab-item="item" />
</template>
</Tabs.TabPane>
</template>
<template v-if="getShowRedo || getShowQuick" #rightExtra>
<TabRedo v-if="getShowRedo" />
<TabContent v-if="getShowQuick" is-extra :tab-item="$route" />
<FoldButton v-if="getShowFold" />
</template>
</Tabs>
</div>
</template>
<style lang="less">
@import './index.less';
</style>