vben
authored
|
1
2
3
4
|
import Sortable from 'sortablejs';
import { toRaw, ref, nextTick, onMounted } from 'vue';
import { RouteLocationNormalized } from 'vue-router';
import { useProjectSetting } from '/@/hooks/setting';
|
vben
authored
|
5
|
import router from '/@/router';
|
vben
authored
|
6
7
|
import { tabStore } from '/@/store/modules/tab';
import { isNullAndUnDef } from '/@/utils/is';
|
vben
authored
|
8
|
|
vben
authored
|
9
10
|
export function initAffixTabs(): string[] {
const affixList = ref<RouteLocationNormalized[]>([]);
|
vben
authored
|
11
12
13
|
/**
* @description: Filter all fixed routes
*/
|
vben
authored
|
14
15
|
function filterAffixTabs(routes: RouteLocationNormalized[]) {
const tabs: RouteLocationNormalized[] = [];
|
vben
authored
|
16
17
18
|
routes &&
routes.forEach((route) => {
if (route.meta && route.meta.affix) {
|
vben
authored
|
19
|
tabs.push(toRaw(route));
|
vben
authored
|
20
21
22
23
24
25
26
27
28
|
}
});
return tabs;
}
/**
* @description: Set fixed tabs
*/
function addAffixTabs(): void {
|
vben
authored
|
29
|
const affixTabs = filterAffixTabs((router.getRoutes() as unknown) as RouteLocationNormalized[]);
|
vben
authored
|
30
|
affixList.value = affixTabs;
|
vben
authored
|
31
|
for (const tab of affixTabs) {
|
vben
authored
|
32
33
34
35
36
|
tabStore.addTabAction(({
meta: tab.meta,
name: tab.name,
path: tab.path,
} as unknown) as RouteLocationNormalized);
|
vben
authored
|
37
38
|
}
}
|
vben
authored
|
39
|
|
vben
authored
|
40
41
42
43
44
|
let isAddAffix = false;
if (!isAddAffix) {
addAffixTabs();
isAddAffix = true;
}
|
vben
authored
|
45
|
return affixList.value.map((item) => item.meta?.title).filter(Boolean);
|
vben
authored
|
46
|
}
|
vben
authored
|
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
export function useTabsDrag(affixTextList: string[]) {
const { multiTabsSetting } = useProjectSetting();
function initSortableTabs() {
if (!multiTabsSetting.canDrag) return;
nextTick(() => {
const el = document.querySelectorAll(
'.multiple-tabs .ant-tabs-nav > div'
)?.[0] as HTMLElement;
if (!el) return;
Sortable.create(el, {
animation: 500,
delay: 400,
delayOnTouchOnly: true,
filter: (e: ChangeEvent) => {
const text = e?.target?.innerText;
if (!text) return false;
return affixTextList.includes(text);
},
onEnd: (evt) => {
const { oldIndex, newIndex } = evt;
if (isNullAndUnDef(oldIndex) || isNullAndUnDef(newIndex) || oldIndex === newIndex) {
return;
}
tabStore.commitSortTabs({ oldIndex, newIndex });
},
});
});
}
onMounted(() => {
initSortableTabs();
});
}
|