Blame view

src/layouts/iframe/useFrameKeepAlive.ts 1.73 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
19
20
21
22
23
24
  const getFramePages = computed(() => {
    const ret =
      getAllFramePages((toRaw(router.getRoutes()) as unknown) as AppRouteRecordRaw[]) || [];
    return ret;
  });

  const getOpenTabList = computed((): string[] => {
Vben authored
25
    return tabStore.getTabList.reduce((prev: string[], next) => {
vben authored
26
      if (next.meta && Reflect.has(next.meta, 'frameSrc')) {
vben authored
27
        prev.push(next.name as string);
vben authored
28
29
30
31
      }
      return prev;
    }, []);
  });
陈文彬 authored
32
33

  function getAllFramePages(routes: AppRouteRecordRaw[]): AppRouteRecordRaw[] {
34
    let res: AppRouteRecordRaw[] = [];
陈文彬 authored
35
36
37
38
39
40
41
42
43
    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
44
    res = uniqBy(res, 'name');
陈文彬 authored
45
46
47
48
    return res;
  }

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