Blame view

src/layouts/iframe/useFrameKeepAlive.ts 1.72 KB
vben authored
1
2
import type { AppRouteRecordRaw } from '/@/router/types';
陈文彬 authored
3
4
import { computed, toRaw, unref } from 'vue';
Vben authored
5
import { useMultipleTabStore } from '/@/store/modules/multipleTab';
陈文彬 authored
6
vben authored
7
import { uniqBy } from 'lodash-es';
陈文彬 authored
8
vben authored
9
10
import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
Vben authored
11
import { useRouter } from 'vue-router';
vben authored
12
陈文彬 authored
13
export function useFrameKeepAlive() {
Vben authored
14
  const router = useRouter();
vben authored
15
  const { currentRoute } = router;
vben authored
16
  const { getShowMultipleTab } = useMultipleTabSetting();
Vben authored
17
  const tabStore = useMultipleTabStore();
vben authored
18
  const getFramePages = computed(() => {
Vben authored
19
    const ret = getAllFramePages(toRaw(router.getRoutes()) as unknown as AppRouteRecordRaw[]) || [];
vben authored
20
21
22
23
    return ret;
  });

  const getOpenTabList = computed((): string[] => {
Vben authored
24
    return tabStore.getTabList.reduce((prev: string[], next) => {
vben authored
25
      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));
      }
    }
vben authored
43
    res = uniqBy(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
  function hasRenderFrame(name: string) {
    if (!unref(getShowMultipleTab)) {
53
      return router.currentRoute.value.name === name;
vben authored
54
55
    }
    return unref(getOpenTabList).includes(name);
陈文彬 authored
56
  }
57
陈文彬 authored
58
59
  return { hasRenderFrame, getFramePages, showIframe, getAllFramePages };
}