const sidemenuOptions = ref<SidemenuOptions>();
const router = useRouter();
const route = useRoute();
const { t } = useI18n();
onServerPrefetch(updateSidemenu);
onBeforeMount(updateSidemenu);
watch(route, updateSidemenu);
/**
* Scans the current route and walks up the parents for a sidemenu definition in
* the Page Metadata and return it if found.
* @returns The sidebar definition or undefined if not found.
*/
function scanForSidemenu() {
let pathSegment = route.fullPath;
while (pathSegment.length > 0) {
const match = router.resolve(pathSegment);
if (match?.meta?.sidemenu) {
return match.meta.sidemenu;
}
const lastSlash = pathSegment.lastIndexOf('/');
if (lastSlash === -1) {
break;
}
pathSegment = pathSegment.substring(0, lastSlash).trim();
}
}
function updateSidemenu() {
const metaSidemenu = scanForSidemenu();
if (metaSidemenu) {
const sidemenu = typeof metaSidemenu === 'function' ? metaSidemenu({ t, route }) : metaSidemenu;
sidemenuOptions.value = sidemenu;
}
}