vue-vben-admin/src/layouts/default/multitabs/index.tsx

152 lines
3.9 KiB
TypeScript
Raw Normal View History

2020-09-28 20:19:10 +08:00
import type { TabContentProps } from './tab.data';
import type { TabItem } from '/@/store/modules/tab';
import type { AppRouteRecordRaw } from '/@/router/types';
import {
defineComponent,
watch,
computed,
// ref,
unref,
2020-10-23 23:45:21 +08:00
// onMounted,
2020-10-14 21:08:07 +08:00
toRaw,
} from 'vue';
2020-09-28 20:19:10 +08:00
import { Tabs } from 'ant-design-vue';
import TabContent from './TabContent';
import { useGo } from '/@/hooks/web/usePage';
import { TabContentEnum } from './tab.data';
import { useRouter } from 'vue-router';
import { tabStore } from '/@/store/modules/tab';
import { closeTab } from './useTabDropdown';
import router from '/@/router';
import { useTabs } from '/@/hooks/web/useTabs';
2020-10-23 23:45:21 +08:00
// import { PageEnum } from '/@/enums/pageEnum';
import './index.less';
import { userStore } from '/@/store/modules/user';
2020-09-28 20:19:10 +08:00
export default defineComponent({
name: 'MultiTabs',
setup() {
let isAddAffix = false;
const go = useGo();
const { currentRoute } = useRouter();
2020-11-10 23:50:47 +08:00
const { activeKeyRef } = useTabs();
2020-09-28 20:19:10 +08:00
// 当前tab列表
const getTabsState = computed(() => {
return tabStore.getTabsState;
});
if (!isAddAffix) {
addAffixTabs();
isAddAffix = true;
}
2020-09-28 20:19:10 +08:00
watch(
() => unref(currentRoute).path,
() => {
if (!userStore.getTokenState) return;
const { path: rPath, fullPath } = unref(currentRoute);
if (activeKeyRef.value !== (fullPath || rPath)) {
activeKeyRef.value = fullPath || rPath;
2020-09-28 20:19:10 +08:00
}
tabStore.commitAddTab((unref(currentRoute) as unknown) as AppRouteRecordRaw);
2020-09-28 20:19:10 +08:00
},
{
immediate: true,
}
);
2020-09-28 20:19:10 +08:00
/**
* @description:
*/
function filterAffixTabs(routes: AppRouteRecordRaw[]) {
const tabs: TabItem[] = [];
routes &&
routes.forEach((route) => {
if (route.meta && route.meta.affix) {
2020-10-14 21:08:07 +08:00
tabs.push(toRaw(route) as TabItem);
2020-09-28 20:19:10 +08:00
}
});
return tabs;
}
2020-09-28 20:19:10 +08:00
/**
* @description: tabs
*/
function addAffixTabs(): void {
const affixTabs = filterAffixTabs((router.getRoutes() as unknown) as AppRouteRecordRaw[]);
for (const tab of affixTabs) {
tabStore.commitAddTab(tab);
}
}
// tab切换
function handleChange(activeKey: any) {
activeKeyRef.value = activeKey;
go(activeKey, false);
}
2020-09-28 20:19:10 +08:00
// 关闭当前ab
function handleEdit(targetKey: string) {
// 新增操作隐藏,目前只使用删除操作
const index = unref(getTabsState).findIndex(
(item) => (item.fullPath || item.path) === targetKey
);
2020-09-28 20:19:10 +08:00
index !== -1 && closeTab(unref(getTabsState)[index]);
}
function renderQuick() {
const tabContentProps: TabContentProps = {
tabItem: (currentRoute as unknown) as AppRouteRecordRaw,
type: TabContentEnum.EXTRA_TYPE,
trigger: ['click', 'contextmenu'],
};
return (
<span>
2020-10-14 21:08:07 +08:00
<TabContent {...(tabContentProps as any)} />
2020-09-28 20:19:10 +08:00
</span>
);
}
function renderTabs() {
return unref(getTabsState).map((item: TabItem) => {
const key = item.query ? item.fullPath : item.path;
2020-09-28 20:19:10 +08:00
return (
<Tabs.TabPane key={key} closable={!(item && item.meta && item.meta.affix)}>
2020-09-28 20:19:10 +08:00
{{
tab: () => <TabContent tabItem={item} />,
}}
</Tabs.TabPane>
);
});
}
return () => {
return (
<div class="multiple-tabs">
<Tabs
type="editable-card"
size="small"
2020-10-24 00:29:33 +08:00
animated={false}
2020-09-28 20:19:10 +08:00
hideAdd={true}
2020-10-31 19:51:24 +08:00
tabBarGutter={4}
2020-09-28 20:19:10 +08:00
activeKey={unref(activeKeyRef)}
onChange={handleChange}
onEdit={handleEdit}
>
{{
default: () => renderTabs(),
tabBarExtraContent: () => renderQuick(),
}}
</Tabs>
</div>
);
};
},
});