admin-vben5/packages/business/layouts/src/iframe/iframe-router-view.vue

78 lines
1.9 KiB
Vue
Raw Normal View History

2024-05-19 21:20:42 +08:00
<script lang="ts" setup>
import { computed, ref } from 'vue';
import { type RouteLocationNormalized, useRoute } from 'vue-router';
2024-06-08 19:49:06 +08:00
import { Spinner } from '@vben/common-ui';
import { preferences } from '@vben-core/preferences';
import { useTabsStore } from '@vben-core/stores';
2024-05-19 21:20:42 +08:00
defineOptions({ name: 'IFrameRouterView' });
const spinning = ref(true);
const tabsStore = useTabsStore();
const route = useRoute();
2024-06-01 23:15:29 +08:00
const enableTabbar = computed(() => preferences.tabbar.enable);
2024-05-19 21:20:42 +08:00
const iframeRoutes = computed(() => {
2024-06-01 23:15:29 +08:00
if (!enableTabbar.value) {
2024-05-19 21:20:42 +08:00
return route.meta.iframeSrc ? [route] : [];
}
2024-06-02 23:46:18 +08:00
return tabsStore.getTabs.filter((tab) => !!tab.meta?.iframeSrc);
2024-05-19 21:20:42 +08:00
});
2024-06-02 23:46:18 +08:00
const tabNames = computed(
() => new Set(iframeRoutes.value.map((item) => item.name as string)),
);
2024-05-19 21:20:42 +08:00
const showIframe = computed(() => iframeRoutes.value.length > 0);
function routeShow(tabItem: RouteLocationNormalized) {
2024-06-02 23:46:18 +08:00
return tabItem.name === route.name;
2024-05-19 21:20:42 +08:00
}
function canRender(tabItem: RouteLocationNormalized) {
const { meta, name } = tabItem;
2024-06-02 23:46:18 +08:00
if (!name || !tabsStore.renderRouteView) {
2024-05-19 21:20:42 +08:00
return false;
}
2024-06-01 23:15:29 +08:00
if (!enableTabbar.value) {
2024-05-19 21:20:42 +08:00
return routeShow(tabItem);
}
// 跟随 keepAlive 状态,与其他tab页保持一致
if (
!meta?.keepAlive &&
tabNames.value.has(name as string) &&
name !== route.name
) {
return false;
}
2024-06-02 23:46:18 +08:00
return tabsStore.getTabs.some((tab) => tab.name === name);
2024-05-19 21:20:42 +08:00
}
function hideLoading() {
spinning.value = false;
}
</script>
<template>
<template v-if="showIframe">
<template v-for="item in iframeRoutes" :key="item.fullPath">
<div
v-show="routeShow(item)"
v-if="canRender(item)"
class="relative size-full"
>
<Spinner :spinning="spinning" />
<iframe
:src="item.meta.iframeSrc as string"
class="size-full"
@load="hideLoading"
></iframe>
</div>
</template>
</template>
</template>