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

65 lines
3.0 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;
document.dispatchEvent(new CustomEvent("plotdirector:book-context-changed", {detail:{bookId:Number(id)||0}}));
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');
});
document.querySelectorAll('[data-review-url]').forEach(link => {
if (!link.dataset.reviewUrl) return;
if (selected) {
link.href = link.dataset.reviewUrl.replace('/0/', `/${id}/`);
link.removeAttribute('aria-disabled');
link.title = 'Review Centre';
} else {
link.removeAttribute('href');
link.setAttribute('aria-disabled', 'true');
link.title = 'Select a book to open Review Centre';
}
});
navigation.querySelectorAll('[data-ribbon-book-url]').forEach(link => {
if (!link.dataset.ribbonBookUrl) return;
if (selected) {
const url = new URL(link.dataset.ribbonBookUrl, location.origin);
url.searchParams.set(link.dataset.bookParam, id);
link.href = url.pathname + url.search;
link.removeAttribute('aria-disabled');
link.title = link.textContent.trim();
} else {
link.removeAttribute('href'); link.setAttribute('aria-disabled', 'true');
link.title = 'Select a book to use this command';
}
});
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 });
})();