vue-vben-admin/src/layouts/default/tabs/index.vue

141 lines
4.0 KiB
Vue
Raw Normal View History

2020-12-13 22:05:34 +08:00
<template>
<div :class="getWrapClass">
<Tabs
type="editable-card"
size="small"
:animated="false"
:hideAdd="true"
:tabBarGutter="3"
:activeKey="activeKeyRef"
@change="handleChange"
@edit="handleEdit"
>
<template v-for="item in getTabsState" :key="item.query ? item.fullPath : item.path">
<TabPane :closable="!(item && item.meta && item.meta.affix)">
<template #tab>
<TabContent :tabItem="item" />
</template>
</TabPane>
</template>
2020-12-15 00:13:23 +08:00
<template #tabBarExtraContent v-if="getShowRedo || getShowQuick">
<TabRedo v-if="getShowRedo" />
<QuickButton v-if="getShowQuick" />
2021-01-06 20:10:16 +08:00
<FoldButton v-if="getShowFold" />
2020-12-13 22:05:34 +08:00
</template>
</Tabs>
</div>
</template>
<script lang="ts">
2020-12-15 00:13:23 +08:00
import { defineComponent, computed, unref, ref } from 'vue';
2020-12-13 22:05:34 +08:00
import { Tabs } from 'ant-design-vue';
import TabContent from './components/TabContent.vue';
2021-03-10 22:23:19 +08:00
import QuickButton from './components/QuickButton.vue';
import FoldButton from './components/FoldButton.vue';
import TabRedo from './components/TabRedo.vue';
import type { RouteLocationNormalized } from 'vue-router';
2020-12-13 22:05:34 +08:00
import { useGo } from '/@/hooks/web/usePage';
import { tabStore } from '/@/store/modules/tab';
import { userStore } from '/@/store/modules/user';
import { initAffixTabs, useTabsDrag } from './useMultipleTabs';
import { useDesign } from '/@/hooks/web/useDesign';
2020-12-15 00:13:23 +08:00
import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
2020-12-13 22:05:34 +08:00
2021-03-10 22:23:19 +08:00
import { REDIRECT_NAME } from '/@/router/constant';
2021-03-27 17:25:37 +08:00
import { listenerRouteChange } from '/@/logics/mitt/routeChange';
2021-03-10 22:23:19 +08:00
import router from '/@/router';
2020-12-13 22:05:34 +08:00
export default defineComponent({
name: 'MultipleTabs',
components: {
2021-03-10 22:23:19 +08:00
QuickButton,
TabRedo: TabRedo,
FoldButton,
2020-12-13 22:05:34 +08:00
Tabs,
TabPane: Tabs.TabPane,
TabContent,
},
setup() {
const affixTextList = initAffixTabs();
const activeKeyRef = ref('');
useTabsDrag(affixTextList);
const { prefixCls } = useDesign('multiple-tabs');
const go = useGo();
2021-01-06 20:10:16 +08:00
const { getShowQuick, getShowRedo, getShowFold } = useMultipleTabSetting();
2020-12-13 22:05:34 +08:00
const getTabsState = computed(() => {
return tabStore.getTabsState.filter((item) => !item.meta?.hideTab);
});
2020-12-13 22:05:34 +08:00
2020-12-15 00:13:23 +08:00
const unClose = computed(() => unref(getTabsState).length === 1);
2020-12-13 22:05:34 +08:00
const getWrapClass = computed(() => {
return [
prefixCls,
{
2020-12-15 00:13:23 +08:00
[`${prefixCls}--hide-close`]: unref(unClose),
2020-12-13 22:05:34 +08:00
},
];
});
2021-03-27 17:25:37 +08:00
listenerRouteChange((route) => {
2020-12-15 00:13:23 +08:00
const { name } = route;
if (name === REDIRECT_NAME || !route || !userStore.getTokenState) return;
const { path, fullPath, meta = {} } = route;
2020-12-15 00:13:23 +08:00
const { currentActiveMenu, hideTab } = meta;
const isHide = !hideTab ? null : currentActiveMenu;
const p = isHide || fullPath || path;
2020-12-15 00:13:23 +08:00
if (activeKeyRef.value !== p) {
2021-02-28 23:05:37 +08:00
activeKeyRef.value = p as string;
2020-12-13 22:05:34 +08:00
}
if (isHide) {
const findParentRoute = router
.getRoutes()
.find((item) => item.path === currentActiveMenu);
findParentRoute &&
tabStore.addTabAction((findParentRoute as unknown) as RouteLocationNormalized);
} else {
tabStore.addTabAction(unref(route));
}
2020-12-15 00:13:23 +08:00
});
2020-12-13 22:05:34 +08:00
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.closeTabByKeyAction(targetKey);
}
return {
prefixCls,
unClose,
getWrapClass,
handleEdit,
handleChange,
activeKeyRef,
getTabsState,
2020-12-15 00:13:23 +08:00
getShowQuick,
getShowRedo,
2021-01-06 20:10:16 +08:00
getShowFold,
2020-12-13 22:05:34 +08:00
};
},
});
</script>
<style lang="less">
@import './index.less';
</style>