AppLayoutNavigation.vue
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<template>
<div class="flex gap-2">
<VaIconMenuCollapsed
class="cursor-pointer"
:class="{ 'x-flip': !isSidebarMinimized }"
:color="collapseIconColor"
@click="isSidebarMinimized = !isSidebarMinimized"
/>
<nav class="flex items-center">
<VaBreadcrumbs>
<VaBreadcrumbsItem label="Home" :to="{ name: 'dashboard' }" />
<VaBreadcrumbsItem
v-for="item in items"
:key="item.label"
:label="item.label"
@click="handleBreadcrumbClick(item)"
/>
</VaBreadcrumbs>
</nav>
</div>
</template>
<script setup lang="ts">
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { useColors } from 'vuestic-ui'
import VaIconMenuCollapsed from '../icons/VaIconMenuCollapsed.vue'
import { storeToRefs } from 'pinia'
import { useGlobalStore } from '../../stores/global-store'
import NavigationRoutes from '../sidebar/NavigationRoutes'
const { isSidebarMinimized } = storeToRefs(useGlobalStore())
const router = useRouter()
const route = useRoute()
const { t } = useI18n()
type BreadcrumbNavigationItem = {
label: string
to: string
hasChildren: boolean
}
const findRouteName = (name: string) => {
const traverse = (routers: any[]): string => {
for (const router of routers) {
if (router.name === name) {
return router.displayName
}
if (router.children) {
const result = traverse(router.children)
if (result) {
return result
}
}
}
return ''
}
return traverse(NavigationRoutes.routes)
}
const items = computed(() => {
const result: { label: string; to: string; hasChildren: boolean }[] = []
route.matched.forEach((route) => {
const labelKey = findRouteName(route.name as string)
if (!labelKey) {
return
}
result.push({
label: t(labelKey),
to: route.path,
hasChildren: route.children && route.children.length > 0,
})
})
return result
})
const { getColor } = useColors()
const collapseIconColor = computed(() => getColor('secondary'))
const handleBreadcrumbClick = (item: BreadcrumbNavigationItem) => {
if (!item.hasChildren) {
router.push(item.to)
}
}
</script>
<style lang="scss" scoped>
.x-flip {
transform: scaleX(-100%);
}
</style>