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