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

86 lines
2.2 KiB
Vue
Raw Normal View History

2024-05-19 21:20:42 +08:00
<script lang="ts" setup>
2024-06-08 22:46:22 +08:00
import type { RouteLocationNormalized } from 'vue-router';
2024-05-19 21:20:42 +08:00
import { computed, ref } from 'vue';
2024-06-08 22:46:22 +08:00
import { useRoute } from 'vue-router';
2024-05-19 21:20:42 +08:00
2024-06-08 19:49:06 +08:00
import { preferences } from '@vben-core/preferences';
2024-06-08 22:46:22 +08:00
import { Spinner } from '@vben-core/shadcn-ui';
2024-06-08 19:49:06 +08:00
import { useTabsStore } from '@vben-core/stores';
2024-05-19 21:20:42 +08:00
defineOptions({ name: 'IFrameRouterView' });
2024-06-08 22:46:22 +08:00
const spinningList = ref<boolean[]>([]);
2024-05-19 21:20:42 +08:00
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
}
2024-06-08 22:46:22 +08:00
function hideLoading(index: number) {
spinningList.value[index] = false;
}
function showSpinning(index: number) {
const curSpinning = spinningList.value[index];
// 首次加载时显示loading
return curSpinning === undefined ? true : curSpinning;
2024-05-19 21:20:42 +08:00
}
</script>
<template>
<template v-if="showIframe">
2024-06-08 22:46:22 +08:00
<template v-for="(item, index) in iframeRoutes" :key="item.fullPath">
2024-05-19 21:20:42 +08:00
<div
v-if="canRender(item)"
2024-06-09 13:31:43 +08:00
v-show="routeShow(item)"
2024-05-19 21:20:42 +08:00
class="relative size-full"
>
2024-06-08 22:46:22 +08:00
<Spinner :spinning="showSpinning(index)" />
2024-05-19 21:20:42 +08:00
<iframe
:src="item.meta.iframeSrc as string"
class="size-full"
2024-06-08 22:46:22 +08:00
@load="hideLoading(index)"
2024-05-19 21:20:42 +08:00
></iframe>
</div>
</template>
</template>
</template>