2022-04-21 18:20:39 +08:00
|
|
|
<script lang="ts" setup>
|
2023-06-20 18:40:18 +08:00
|
|
|
import { computed } from "vue"
|
2022-10-18 15:07:42 +08:00
|
|
|
import { type RouteRecordRaw } from "vue-router"
|
2022-08-25 16:26:28 +08:00
|
|
|
import SidebarItemLink from "./SidebarItemLink.vue"
|
2022-04-22 01:16:02 +08:00
|
|
|
import { isExternal } from "@/utils/validate"
|
2022-04-22 12:47:04 +08:00
|
|
|
import path from "path-browserify"
|
2022-04-21 18:20:39 +08:00
|
|
|
|
2023-06-20 18:40:18 +08:00
|
|
|
interface Props {
|
|
|
|
item: RouteRecordRaw
|
|
|
|
isCollapse?: boolean
|
2023-07-18 11:14:07 +08:00
|
|
|
isTop?: boolean
|
2023-06-20 18:40:18 +08:00
|
|
|
isFirstLevel?: boolean
|
|
|
|
basePath?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
|
|
isCollapse: false,
|
2023-07-18 11:14:07 +08:00
|
|
|
isTop: false,
|
2023-06-20 18:40:18 +08:00
|
|
|
isFirstLevel: true,
|
|
|
|
basePath: ""
|
2022-04-21 18:20:39 +08:00
|
|
|
})
|
2022-04-22 12:47:04 +08:00
|
|
|
|
2023-06-15 13:42:58 +08:00
|
|
|
/** 是否始终显示根菜单 */
|
|
|
|
const alwaysShowRootMenu = computed(() => props.item.meta?.alwaysShow)
|
|
|
|
|
|
|
|
/** 显示的子菜单 */
|
|
|
|
const showingChildren = computed(() => {
|
|
|
|
return props.item.children?.filter((child) => !child.meta?.hidden) ?? []
|
2022-04-21 18:20:39 +08:00
|
|
|
})
|
2022-08-25 16:26:28 +08:00
|
|
|
|
2023-06-15 13:42:58 +08:00
|
|
|
/** 显示的子菜单数量 */
|
2022-04-21 18:20:39 +08:00
|
|
|
const showingChildNumber = computed(() => {
|
2023-06-15 13:42:58 +08:00
|
|
|
return showingChildren.value.length
|
2022-04-21 18:20:39 +08:00
|
|
|
})
|
2022-08-25 16:26:28 +08:00
|
|
|
|
2023-06-15 13:42:58 +08:00
|
|
|
/** 唯一的子菜单项 */
|
2022-04-21 18:20:39 +08:00
|
|
|
const theOnlyOneChild = computed(() => {
|
2023-06-15 13:42:58 +08:00
|
|
|
const number = showingChildNumber.value
|
|
|
|
switch (true) {
|
|
|
|
case number > 1:
|
|
|
|
return null
|
|
|
|
case number === 1:
|
|
|
|
return showingChildren.value[0]
|
|
|
|
default:
|
|
|
|
return { ...props.item, path: "" }
|
2022-04-21 18:20:39 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-06-15 13:42:58 +08:00
|
|
|
/** 解析路径 */
|
2022-04-21 18:20:39 +08:00
|
|
|
const resolvePath = (routePath: string) => {
|
2023-06-15 13:42:58 +08:00
|
|
|
switch (true) {
|
|
|
|
case isExternal(routePath):
|
|
|
|
return routePath
|
|
|
|
case isExternal(props.basePath):
|
|
|
|
return props.basePath
|
|
|
|
default:
|
|
|
|
return path.resolve(props.basePath, routePath)
|
2022-04-21 18:20:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2022-04-22 12:47:04 +08:00
|
|
|
<template>
|
2023-07-18 11:14:07 +08:00
|
|
|
<div
|
|
|
|
v-if="!props.item.meta?.hidden"
|
|
|
|
:class="{ 'simple-mode': props.isCollapse && !isTop, 'first-level': props.isFirstLevel }"
|
|
|
|
>
|
2022-04-22 12:47:04 +08:00
|
|
|
<template v-if="!alwaysShowRootMenu && theOnlyOneChild && !theOnlyOneChild.children">
|
|
|
|
<SidebarItemLink v-if="theOnlyOneChild.meta" :to="resolvePath(theOnlyOneChild.path)">
|
|
|
|
<el-menu-item :index="resolvePath(theOnlyOneChild.path)">
|
2023-06-21 10:28:04 +08:00
|
|
|
<SvgIcon v-if="theOnlyOneChild.meta.svgIcon" :name="theOnlyOneChild.meta.svgIcon" />
|
2022-10-08 15:19:36 +08:00
|
|
|
<component v-else-if="theOnlyOneChild.meta.elIcon" :is="theOnlyOneChild.meta.elIcon" class="el-icon" />
|
2022-04-22 12:47:04 +08:00
|
|
|
<template v-if="theOnlyOneChild.meta.title" #title>
|
|
|
|
{{ theOnlyOneChild.meta.title }}
|
|
|
|
</template>
|
|
|
|
</el-menu-item>
|
|
|
|
</SidebarItemLink>
|
|
|
|
</template>
|
2023-02-20 14:51:34 +08:00
|
|
|
<el-sub-menu v-else :index="resolvePath(props.item.path)" teleported>
|
2022-04-22 12:47:04 +08:00
|
|
|
<template #title>
|
2023-06-21 10:28:04 +08:00
|
|
|
<SvgIcon v-if="props.item.meta?.svgIcon" :name="props.item.meta.svgIcon" />
|
2023-06-15 13:42:58 +08:00
|
|
|
<component v-else-if="props.item.meta?.elIcon" :is="props.item.meta.elIcon" class="el-icon" />
|
|
|
|
<span v-if="props.item.meta?.title">{{ props.item.meta.title }}</span>
|
2022-04-22 12:47:04 +08:00
|
|
|
</template>
|
2022-08-25 16:26:28 +08:00
|
|
|
<template v-if="props.item.children">
|
2022-04-22 12:47:04 +08:00
|
|
|
<sidebar-item
|
2022-08-25 16:26:28 +08:00
|
|
|
v-for="child in props.item.children"
|
2022-04-22 12:47:04 +08:00
|
|
|
:key="child.path"
|
|
|
|
:item="child"
|
2022-08-25 16:26:28 +08:00
|
|
|
:is-collapse="props.isCollapse"
|
2022-04-22 12:47:04 +08:00
|
|
|
:is-first-level="false"
|
|
|
|
:base-path="resolvePath(child.path)"
|
|
|
|
/>
|
|
|
|
</template>
|
|
|
|
</el-sub-menu>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-04-21 18:20:39 +08:00
|
|
|
<style lang="scss" scoped>
|
|
|
|
.svg-icon {
|
|
|
|
min-width: 1em;
|
2022-10-08 15:19:36 +08:00
|
|
|
margin-right: 12px;
|
|
|
|
font-size: 18px;
|
2022-04-21 18:20:39 +08:00
|
|
|
}
|
2022-10-08 15:19:36 +08:00
|
|
|
|
2022-09-30 17:41:47 +08:00
|
|
|
.el-icon {
|
|
|
|
width: 1em;
|
2022-10-08 15:19:36 +08:00
|
|
|
margin-right: 12px;
|
|
|
|
font-size: 18px;
|
2022-09-30 17:41:47 +08:00
|
|
|
}
|
2022-10-08 15:19:36 +08:00
|
|
|
|
2022-04-21 18:20:39 +08:00
|
|
|
.simple-mode {
|
|
|
|
&.first-level {
|
2022-09-28 17:17:24 +08:00
|
|
|
:deep(.el-sub-menu) {
|
2022-04-21 18:20:39 +08:00
|
|
|
.el-sub-menu__icon-arrow {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
span {
|
|
|
|
visibility: hidden;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|