12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import type { AppRouteRecordRaw } from '/@/router/types';
- import { computed, toRaw, unref } from 'vue';
- import { useMultipleTabStore } from '/@/store/modules/multipleTab';
- import { uniqBy } from 'lodash-es';
- import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
- import { useRouter } from 'vue-router';
- export function useFrameKeepAlive() {
- const router = useRouter();
- const { currentRoute } = router;
- const { getShowMultipleTab } = useMultipleTabSetting();
- const tabStore = useMultipleTabStore();
- const getFramePages = computed(() => {
- const ret = getAllFramePages(toRaw(router.getRoutes()) as unknown as AppRouteRecordRaw[]) || [];
- return ret;
- });
- const getOpenTabList = computed((): string[] => {
- return tabStore.getTabList.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 = uniqBy(res, 'name');
- return res;
- }
- function showIframe(item: AppRouteRecordRaw) {
- return item.name === unref(currentRoute).name;
- }
- function hasRenderFrame(name: string) {
- if (!unref(getShowMultipleTab)) {
- return unref(router.currentRoute).name === name;
- }
- if (!unref(getOpenTabList)) {
- return unref(getOpenTabList).includes(name);
- }
- if (unref(router.currentRoute).meta.frameSrc) {
- return true;
- }
- }
- return { hasRenderFrame, getFramePages, showIframe, getAllFramePages };
- }
|