Blame view

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

import { tabStore } from '/@/store/modules/tab';
import { appStore } from '/@/store/modules/app';
10
import { unique } from '/@/utils';
陈文彬 authored
11
12
13
14
15

export function useFrameKeepAlive() {
  const { currentRoute } = useRouter();

  function getAllFramePages(routes: AppRouteRecordRaw[]): AppRouteRecordRaw[] {
16
    let res: AppRouteRecordRaw[] = [];
陈文彬 authored
17
18
19
20
21
22
23
24
25
    for (const route of routes) {
      const { meta: { frameSrc } = {}, children } = route;
      if (frameSrc) {
        res.push(route);
      }
      if (children && children.length) {
        res.push(...getAllFramePages(children));
      }
    }
26
    res = unique(res, 'name');
陈文彬 authored
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    return res;
  }

  function showIframe(item: AppRouteRecordRaw) {
    return item.path === unref(currentRoute).path;
  }
  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
42
        prev.push(next.path!);
陈文彬 authored
43
44
45
46
47
48
49
50
51
52
53
54
55
      }
      return prev;
    }, []);
  });

  function hasRenderFrame(path: string) {
    const {
      multiTabsSetting: { show },
    } = appStore.getProjectConfig;
    return show ? unref(getOpenTabList).includes(path) : true;
  }
  return { hasRenderFrame, getFramePages, showIframe, getAllFramePages };
}