vben
authored
|
1
2
|
import type { AppRouteRecordRaw } from '/@/router/types';
|
|
3
4
5
6
|
import { computed, toRaw, unref } from 'vue';
import { tabStore } from '/@/store/modules/tab';
|
nebv
authored
|
7
|
import { unique } from '/@/utils';
|
|
8
|
|
vben
authored
|
9
10
|
import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
|
vben
authored
|
11
12
|
import router from '/@/router';
|
|
13
|
export function useFrameKeepAlive() {
|
vben
authored
|
14
|
const { currentRoute } = router;
|
vben
authored
|
15
|
const { getShowMultipleTab } = useMultipleTabSetting();
|
vben
authored
|
16
17
18
19
20
21
22
23
24
25
|
const getFramePages = computed(() => {
const ret =
getAllFramePages((toRaw(router.getRoutes()) as unknown) as AppRouteRecordRaw[]) || [];
return ret;
});
const getOpenTabList = computed((): string[] => {
return tabStore.getTabsState.reduce((prev: string[], next) => {
if (next.meta && Reflect.has(next.meta, 'frameSrc')) {
|
vben
authored
|
26
|
prev.push(next.name as string);
|
vben
authored
|
27
28
29
30
|
}
return prev;
}, []);
});
|
|
31
32
|
function getAllFramePages(routes: AppRouteRecordRaw[]): AppRouteRecordRaw[] {
|
nebv
authored
|
33
|
let res: AppRouteRecordRaw[] = [];
|
|
34
35
36
37
38
39
40
41
42
|
for (const route of routes) {
const { meta: { frameSrc } = {}, children } = route;
if (frameSrc) {
res.push(route);
}
if (children && children.length) {
res.push(...getAllFramePages(children));
}
}
|
nebv
authored
|
43
|
res = unique(res, 'name');
|
|
44
45
46
47
|
return res;
}
function showIframe(item: AppRouteRecordRaw) {
|
vben
authored
|
48
|
return item.name === unref(currentRoute).name;
|
|
49
50
|
}
|
vben
authored
|
51
52
53
54
55
|
function hasRenderFrame(name: string) {
if (!unref(getShowMultipleTab)) {
return true;
}
return unref(getOpenTabList).includes(name);
|
|
56
57
58
|
}
return { hasRenderFrame, getFramePages, showIframe, getAllFramePages };
}
|