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