127 lines
3.4 KiB
Vue
Raw Normal View History

<script lang="ts" setup>
2022-10-18 15:07:42 +08:00
import { type PropType, computed } from "vue"
import { type RouteRecordRaw } from "vue-router"
2022-08-25 16:26:28 +08:00
import SidebarItemLink from "./SidebarItemLink.vue"
import { isExternal } from "@/utils/validate"
import path from "path-browserify"
const props = defineProps({
item: {
type: Object as PropType<RouteRecordRaw>,
required: true
},
isCollapse: {
type: Boolean,
2022-08-25 16:26:28 +08:00
default: false
},
isFirstLevel: {
type: Boolean,
default: true
},
basePath: {
type: String,
2022-08-25 16:26:28 +08:00
default: ""
}
})
const alwaysShowRootMenu = computed(() => {
2022-08-25 16:26:28 +08:00
return props.item.meta && props.item.meta.alwaysShow
})
2022-08-25 16:26:28 +08:00
const showingChildNumber = computed(() => {
if (props.item.children) {
const showingChildren = props.item.children.filter((item) => {
return !(item.meta && item.meta.hidden)
})
return showingChildren.length
}
return 0
})
2022-08-25 16:26:28 +08:00
const theOnlyOneChild = computed(() => {
if (showingChildNumber.value > 1) {
return null
}
if (props.item.children) {
for (const child of props.item.children) {
if (!child.meta || !child.meta.hidden) {
return child
}
}
}
// If there is no children, return itself with path removed,
// because this.basePath already contains item's path information
return { ...props.item, path: "" }
})
const resolvePath = (routePath: string) => {
if (isExternal(routePath)) {
return routePath
}
if (isExternal(props.basePath)) {
return props.basePath
}
return path.resolve(props.basePath, routePath)
}
</script>
<template>
2022-08-25 16:26:28 +08:00
<div v-if="!props.item.meta?.hidden" :class="{ 'simple-mode': props.isCollapse, 'first-level': props.isFirstLevel }">
<template v-if="!alwaysShowRootMenu && theOnlyOneChild && !theOnlyOneChild.children">
<SidebarItemLink v-if="theOnlyOneChild.meta" :to="resolvePath(theOnlyOneChild.path)">
<el-menu-item :index="resolvePath(theOnlyOneChild.path)">
<svg-icon 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" />
<template v-if="theOnlyOneChild.meta.title" #title>
{{ theOnlyOneChild.meta.title }}
</template>
</el-menu-item>
</SidebarItemLink>
</template>
2022-08-25 16:26:28 +08:00
<el-sub-menu v-else :index="resolvePath(props.item.path)" popper-append-to-body>
<template #title>
<svg-icon v-if="props.item.meta && props.item.meta.svgIcon" :name="props.item.meta.svgIcon" />
2022-10-08 15:19:36 +08:00
<component v-else-if="props.item.meta && props.item.meta.elIcon" :is="props.item.meta.elIcon" class="el-icon" />
2022-08-25 16:26:28 +08:00
<span v-if="props.item.meta && props.item.meta.title">{{ props.item.meta.title }}</span>
</template>
2022-08-25 16:26:28 +08:00
<template v-if="props.item.children">
<sidebar-item
2022-08-25 16:26:28 +08:00
v-for="child in props.item.children"
:key="child.path"
:item="child"
2022-08-25 16:26:28 +08:00
:is-collapse="props.isCollapse"
:is-first-level="false"
:base-path="resolvePath(child.path)"
/>
</template>
</el-sub-menu>
</div>
</template>
<style lang="scss" scoped>
.svg-icon {
min-width: 1em;
2022-10-08 15:19:36 +08:00
margin-right: 12px;
font-size: 18px;
}
2022-10-08 15:19:36 +08:00
.el-icon {
width: 1em;
2022-10-08 15:19:36 +08:00
margin-right: 12px;
font-size: 18px;
}
2022-10-08 15:19:36 +08:00
.simple-mode {
&.first-level {
:deep(.el-sub-menu) {
.el-sub-menu__icon-arrow {
display: none;
}
span {
visibility: hidden;
}
}
}
}
</style>