vue-vben-admin/src/layouts/iframe/useFrameKeepAlive.ts

56 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-10-30 21:32:05 +08:00
import type { AppRouteRecordRaw } from '/@/router/types';
2020-09-28 20:19:10 +08:00
import { computed, toRaw, unref } from 'vue';
2020-10-30 21:32:05 +08:00
import { useRouter } from 'vue-router';
import router from '/@/router';
2020-09-28 20:19:10 +08:00
import { tabStore } from '/@/store/modules/tab';
2020-10-11 00:05:29 +08:00
import { unique } from '/@/utils';
2020-09-28 20:19:10 +08:00
2020-11-23 23:24:13 +08:00
import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
2020-09-28 20:19:10 +08:00
export function useFrameKeepAlive() {
const { currentRoute } = useRouter();
2020-11-24 22:59:29 +08:00
const { getShowMultipleTab } = useMultipleTabSetting();
2020-11-23 23:24:13 +08:00
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')) {
prev.push(next.path!);
}
return prev;
}, []);
});
2020-09-28 20:19:10 +08:00
function getAllFramePages(routes: AppRouteRecordRaw[]): AppRouteRecordRaw[] {
2020-10-11 00:05:29 +08:00
let res: AppRouteRecordRaw[] = [];
2020-09-28 20:19:10 +08:00
for (const route of routes) {
const { meta: { frameSrc } = {}, children } = route;
if (frameSrc) {
res.push(route);
}
if (children && children.length) {
res.push(...getAllFramePages(children));
}
}
2020-10-11 00:05:29 +08:00
res = unique(res, 'name');
2020-09-28 20:19:10 +08:00
return res;
}
function showIframe(item: AppRouteRecordRaw) {
return item.path === unref(currentRoute).path;
}
function hasRenderFrame(path: string) {
2020-11-24 22:59:29 +08:00
return unref(getShowMultipleTab) ? unref(getOpenTabList).includes(path) : true;
2020-09-28 20:19:10 +08:00
}
return { hasRenderFrame, getFramePages, showIframe, getAllFramePages };
}