Blame view

src/layouts/iframe/useFrameKeepAlive.ts 1.6 KB
vben authored
1
2
import type { AppRouteRecordRaw } from '/@/router/types';
陈文彬 authored
3
4
5
6
import { computed, toRaw, unref } from 'vue';

import { tabStore } from '/@/store/modules/tab';
7
import { unique } from '/@/utils';
陈文彬 authored
8
vben authored
9
10
import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
vben authored
11
12
import router from '/@/router';
陈文彬 authored
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;
    }, []);
  });
陈文彬 authored
31
32

  function getAllFramePages(routes: AppRouteRecordRaw[]): AppRouteRecordRaw[] {
33
    let res: AppRouteRecordRaw[] = [];
陈文彬 authored
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));
      }
    }
43
    res = unique(res, 'name');
陈文彬 authored
44
45
46
47
    return res;
  }

  function showIframe(item: AppRouteRecordRaw) {
vben authored
48
    return item.name === unref(currentRoute).name;
陈文彬 authored
49
50
  }
vben authored
51
52
53
54
55
  function hasRenderFrame(name: string) {
    if (!unref(getShowMultipleTab)) {
      return true;
    }
    return unref(getOpenTabList).includes(name);
陈文彬 authored
56
57
58
  }
  return { hasRenderFrame, getFramePages, showIframe, getAllFramePages };
}