drupal-caravane/web/themes/custom/caravane/assets/js/utils/handle-navigation.js

52 lines
1.8 KiB
JavaScript
Raw Normal View History

import { setActiveNavItem } from "./set-active-nav-item";
2024-10-19 03:49:16 +02:00
import { useLayoutStore } from '../stores/layout';
export async function initFirstLoadRouting(store, router, baseUrl, siteName) {
const decoupled_origin = JSON.parse(window.localStorage.getItem('decoupled_origin'));
if(decoupled_origin) {
await store.fetchContentData(baseUrl + decoupled_origin.url);
router.push(decoupled_origin.url);
window.localStorage.removeItem("decoupled_origin");
document.title = store.pageTitle;
setActiveNavItem(store.contentType, decoupled_origin.url);
} else {
document.title = siteName;
}
}
export function handleClickableElements(clickableElements, store, router, baseUrl, siteName, mapStore) {
for (const link of clickableElements) {
let href = link.href || link.dataset.href;
if (href.startsWith(baseUrl)) href = href.replace(baseUrl, '');
link.onclick = async function (e) {
router.push(href);
if (href !== window.location.pathname) {
2024-11-14 04:33:27 +01:00
pageChange(href, store, siteName, mapStore, baseUrl);
}
}
}
}
2024-11-14 04:28:38 +01:00
export async function handleBrowserNavigation(store, baseUrl, siteName, mapStore) {
let href = window.location.pathname;
if (href.startsWith(baseUrl)) href = href.replace(baseUrl, '');
pageChange(href, store, siteName, mapStore, baseUrl)
}
2024-11-14 04:33:27 +01:00
async function pageChange(href, store, siteName, mapStore, baseUrl) {
2024-11-14 04:28:38 +01:00
if (href === '/') {
store.resetStore(true);
document.title = siteName;
mapStore.resetMap();
} else {
await store.fetchContentData(baseUrl + href);
document.title = store.pageTitle;
}
setActiveNavItem(store.contentType, href);
const listeEtape = document.querySelector('#etapes-liste');
if (!useLayoutStore().isDesktop) useLayoutStore().collapseEtapeListe(listeEtape);
}