PlotDirector/PlotLine/wwwroot/js/application-navigation.js

39 lines
1.8 KiB
JavaScript

(() => {
const navigation = document.querySelector('[data-app-navigation]');
if (!navigation) return;
const label = navigation.querySelector('[data-app-book-label]');
const heading = navigation.querySelector('[data-app-book-heading]');
const choices = [...navigation.querySelectorAll('[data-app-book-choice]')];
let currentBook;
const updateBook = () => {
// Calendar and At This Point replace their content without re-rendering the layout.
const context = document.querySelector('[data-navigation-book-id]');
const pointBook = document.querySelector('[data-point-book]');
if ((!context && !pointBook) || !label) return;
const id = pointBook?.value ?? context?.dataset.navigationBookId ?? '';
if (id === currentBook) return;
currentBook = id;
const selected = choices.find(x => x.dataset.appBookChoice === id);
label.textContent = selected?.dataset.appBookTitle || 'Book';
label.closest('button').title = label.textContent;
heading.textContent = selected?.dataset.appBookTitle || 'Books in this project';
choices.forEach(choice => {
const active = choice === selected;
choice.classList.toggle('active', active);
if (active) choice.setAttribute('aria-current', 'page');
else choice.removeAttribute('aria-current');
});
navigation.querySelectorAll('[data-app-book-action]').forEach(item => {
item.hidden = !selected;
const link = item.querySelector('[data-app-book-url]');
if (!selected || !link) return;
const url = new URL(link.dataset.appBookUrl, location.origin);
url.searchParams.set(link.dataset.appBookParam || 'id', id);
link.href = url.pathname + url.search;
});
};
updateBook();
const main = document.querySelector('.plotline-help-main');
if (main) new MutationObserver(updateBook).observe(main, { childList: true, subtree: true });
})();