feat(layout):增加关闭掉全部或者指定 tab 的方法、重整 tab 相关方法

This commit is contained in:
妙码生花 2024-03-31 23:15:47 +08:00
parent e7ed19d7bc
commit f7bafabf74
2 changed files with 100 additions and 26 deletions

View File

@ -103,12 +103,12 @@ const toLastTab = () => {
}
const closeTab = (route: RouteLocationNormalized) => {
navTabs.closeTab(route)
navTabs._closeTab(route)
proxy.eventBus.emit('onTabViewClose', route)
if (navTabs.state.activeRoute?.path === route.path) {
toLastTab()
} else {
navTabs.setActiveRoute(navTabs.state.activeRoute!)
navTabs._setActiveRoute(navTabs.state.activeRoute!)
nextTick(() => {
selectNavTab(tabsRefs.value[navTabs.state.activeIndex])
})
@ -118,22 +118,26 @@ const closeTab = (route: RouteLocationNormalized) => {
}
const closeOtherTab = (menu: RouteLocationNormalized) => {
navTabs.closeTabs(menu)
navTabs.setActiveRoute(menu)
navTabs._closeTabs(menu)
navTabs._setActiveRoute(menu)
if (navTabs.state.activeRoute?.path !== route.path) {
router.push(menu!.path)
}
}
const closeAllTab = (menu: RouteLocationNormalized) => {
/**
* 关闭所有tab等同于 navTabs.closeAllTab
* @param menu 需要保留的标签否则关闭全部标签
*/
const closeAllTab = (menu?: RouteLocationNormalized) => {
let firstRoute = getFirstRoute(navTabs.state.tabsViewRoutes)
if (firstRoute && firstRoute.path == menu.path) {
if (menu && firstRoute && firstRoute.path == menu.path) {
return closeOtherTab(menu)
}
if (firstRoute && firstRoute.path == navTabs.state.activeRoute?.path) {
return closeOtherTab(navTabs.state.activeRoute)
}
navTabs.closeTabs(false)
navTabs._closeTabs(false)
if (firstRoute) routePush(firstRoute.path)
}
@ -164,9 +168,9 @@ const onContextmenuItem = async (item: ContextmenuItemClickEmitArg) => {
const updateTab = function (newRoute: RouteLocationNormalized) {
// tab
navTabs.addTab(newRoute)
navTabs._addTab(newRoute)
// tab
navTabs.setActiveRoute(newRoute)
navTabs._setActiveRoute(newRoute)
nextTick(() => {
selectNavTab(tabsRefs.value[navTabs.state.activeIndex])
@ -181,6 +185,24 @@ onMounted(() => {
updateTab(router.currentRoute.value)
new horizontalScroll(tabScrollbarRef.value)
})
/**
* 通过路由路径关闭tab等同于 navTabs.closeTabByPath
* @param path 需要关闭的 tab 的路径
*/
const closeTabByPath = (path: string) => {
for (const key in navTabs.state.tabsView) {
if (navTabs.state.tabsView[key].path == path) {
closeTab(navTabs.state.tabsView[key])
break
}
}
}
defineExpose({
closeAllTab,
closeTabByPath,
})
</script>
<style scoped lang="scss">

View File

@ -6,6 +6,7 @@ import { STORE_TAB_VIEW_CONFIG } from '/@/stores/constant/cacheKey'
import type { NavTabs } from '/@/stores/interface/index'
import type { RouteLocationNormalized, RouteRecordRaw } from 'vue-router'
import { adminBaseRoutePath } from '/@/router/static/adminBase'
import { layoutNavTabsRef } from '/@/stores/refs'
export const useNavTabs = defineStore(
'navTabs',
@ -25,7 +26,27 @@ export const useNavTabs = defineStore(
authNode: new Map(),
})
function addTab(route: RouteLocationNormalized) {
/**
* tab
* @param path tab
*/
const closeTabByPath = (path: string) => {
layoutNavTabsRef.value?.closeTabByPath(path)
}
/**
* tab
* @param menu
*/
const closeAllTab = (menu?: RouteLocationNormalized) => {
layoutNavTabsRef.value?.closeAllTab(menu)
}
/**
* tab
* router.push tab
*/
function _addTab(route: RouteLocationNormalized) {
if (!route.meta.addtab) return
for (const key in state.tabsView) {
if (state.tabsView[key].path === route.path) {
@ -40,7 +61,23 @@ export const useNavTabs = defineStore(
state.tabsView.push(route)
}
function closeTab(route: RouteLocationNormalized) {
/**
* tab
* router.push tab
*/
const _setActiveRoute = (route: RouteLocationNormalized): void => {
const currentRouteIndex: number = state.tabsView.findIndex((item: RouteLocationNormalized) => {
return item.path === route.path
})
if (currentRouteIndex === -1) return
state.activeRoute = route
state.activeIndex = currentRouteIndex
}
/**
* tab
*/
function _closeTab(route: RouteLocationNormalized) {
state.tabsView.map((v, k) => {
if (v.path == route.path) {
state.tabsView.splice(k, 1)
@ -50,10 +87,9 @@ export const useNavTabs = defineStore(
}
/**
*
* @param retainMenu
*
*/
const closeTabs = (retainMenu: RouteLocationNormalized | false = false) => {
const _closeTabs = (retainMenu: RouteLocationNormalized | false = false) => {
if (retainMenu) {
state.tabsView = [retainMenu]
} else {
@ -61,32 +97,48 @@ export const useNavTabs = defineStore(
}
}
const setActiveRoute = (route: RouteLocationNormalized): void => {
const currentRouteIndex: number = state.tabsView.findIndex((item: RouteLocationNormalized) => {
return item.path === route.path
})
if (currentRouteIndex === -1) return
state.activeRoute = route
state.activeIndex = currentRouteIndex
}
/**
*
*/
const setTabsViewRoutes = (data: RouteRecordRaw[]): void => {
state.tabsViewRoutes = encodeRoutesURI(data)
}
/**
* key设置权限节点
*/
const setAuthNode = (key: string, data: string[]) => {
state.authNode.set(key, data)
}
/**
*
*/
const fillAuthNode = (data: Map<string, string[]>) => {
state.authNode = data
}
const setFullScreen = (fullScreen: boolean): void => {
state.tabFullScreen = fullScreen
/**
* tab
* @param status
*/
const setFullScreen = (status: boolean): void => {
state.tabFullScreen = status
}
return { state, addTab, closeTab, closeTabs, setActiveRoute, setTabsViewRoutes, setAuthNode, fillAuthNode, setFullScreen }
return {
state,
closeAllTab,
closeTabByPath,
setTabsViewRoutes,
setAuthNode,
fillAuthNode,
setFullScreen,
_addTab,
_closeTab,
_closeTabs,
_setActiveRoute,
}
},
{
persist: {