vue-vben-admin/src/layouts/default/tabs/useMultipleTabs.ts

79 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-12-29 23:37:40 +08:00
import { toRaw, ref, nextTick } from 'vue';
2021-04-10 19:25:49 +08:00
import type { RouteLocationNormalized } from 'vue-router';
2020-12-13 22:05:34 +08:00
import { useDesign } from '/@/hooks/web/useDesign';
2020-12-29 23:37:40 +08:00
import { useSortable } from '/@/hooks/web/useSortable';
2021-04-10 19:25:49 +08:00
import { useMultipleTabStore } from '/@/store/modules/multipleTab';
2020-12-03 21:49:32 +08:00
import { isNullAndUnDef } from '/@/utils/is';
import projectSetting from '/@/settings/projectSetting';
2021-04-10 19:25:49 +08:00
import { useRouter } from 'vue-router';
2020-11-25 21:26:10 +08:00
2020-12-03 21:49:32 +08:00
export function initAffixTabs(): string[] {
const affixList = ref<RouteLocationNormalized[]>([]);
2021-04-10 19:25:49 +08:00
const tabStore = useMultipleTabStore();
const router = useRouter();
2020-11-25 21:26:10 +08:00
/**
* @description: Filter all fixed routes
*/
2020-12-03 21:49:32 +08:00
function filterAffixTabs(routes: RouteLocationNormalized[]) {
const tabs: RouteLocationNormalized[] = [];
2020-11-25 21:26:10 +08:00
routes &&
routes.forEach((route) => {
if (route.meta && route.meta.affix) {
2020-12-03 21:49:32 +08:00
tabs.push(toRaw(route));
2020-11-25 21:26:10 +08:00
}
});
return tabs;
}
/**
* @description: Set fixed tabs
*/
function addAffixTabs(): void {
2021-06-09 22:36:30 +08:00
const affixTabs = filterAffixTabs(router.getRoutes() as unknown as RouteLocationNormalized[]);
2020-11-25 22:28:58 +08:00
affixList.value = affixTabs;
2020-11-25 21:26:10 +08:00
for (const tab of affixTabs) {
2021-06-09 22:36:30 +08:00
tabStore.addTab({
2020-12-03 21:49:32 +08:00
meta: tab.meta,
name: tab.name,
path: tab.path,
2021-06-09 22:36:30 +08:00
} as unknown as RouteLocationNormalized);
2020-11-25 21:26:10 +08:00
}
}
2020-11-25 22:28:58 +08:00
2020-11-25 21:26:10 +08:00
let isAddAffix = false;
2021-04-10 19:25:49 +08:00
2020-11-25 21:26:10 +08:00
if (!isAddAffix) {
addAffixTabs();
isAddAffix = true;
}
2021-02-25 20:17:08 +08:00
return affixList.value.map((item) => item.meta?.title).filter(Boolean) as string[];
2020-11-25 21:26:10 +08:00
}
2020-12-03 21:49:32 +08:00
export function useTabsDrag(affixTextList: string[]) {
2021-04-10 19:25:49 +08:00
const tabStore = useMultipleTabStore();
const { multiTabsSetting } = projectSetting;
2020-12-13 22:05:34 +08:00
const { prefixCls } = useDesign('multiple-tabs');
2020-12-29 23:37:40 +08:00
nextTick(() => {
2020-12-03 21:49:32 +08:00
if (!multiTabsSetting.canDrag) return;
2020-12-29 23:37:40 +08:00
const el = document.querySelectorAll(`.${prefixCls} .ant-tabs-nav > div`)?.[0] as HTMLElement;
const { initSortable } = useSortable(el, {
filter: (e: ChangeEvent) => {
const text = e?.target?.innerText;
if (!text) return false;
return affixTextList.includes(text);
},
onEnd: (evt) => {
const { oldIndex, newIndex } = evt;
2020-12-03 21:49:32 +08:00
2020-12-29 23:37:40 +08:00
if (isNullAndUnDef(oldIndex) || isNullAndUnDef(newIndex) || oldIndex === newIndex) {
return;
}
2020-12-03 21:49:32 +08:00
2021-04-10 19:25:49 +08:00
tabStore.sortTabs(oldIndex, newIndex);
2020-12-29 23:37:40 +08:00
},
2020-12-03 21:49:32 +08:00
});
2020-12-29 23:37:40 +08:00
initSortable();
2020-12-03 21:49:32 +08:00
});
}