Files
breadcrumb-the-shire/web/js/components/app-nav-history.js
2026-02-04 23:31:53 +01:00

49 lines
1.3 KiB
JavaScript

const updateHistoryButtons = () => {
const back = document.querySelector('#global-back');
const forward = document.querySelector('#global-forward');
if (!back && !forward) return;
const hasHistory = window.history.length > 1;
if (back) {
back.setAttribute('aria-disabled', hasHistory ? 'false' : 'true');
back.classList.toggle('is-disabled', !hasHistory);
}
if (forward) {
forward.setAttribute('aria-disabled', hasHistory ? 'false' : 'true');
forward.classList.toggle('is-disabled', !hasHistory);
}
};
const initHistoryButtons = () => {
const back = document.querySelector('#global-back');
const forward = document.querySelector('#global-forward');
if (back) {
back.addEventListener('click', (event) => {
if (window.history.length > 1) {
event.preventDefault();
window.history.back();
}
});
}
if (forward) {
forward.addEventListener('click', (event) => {
if (window.history.length > 1) {
event.preventDefault();
window.history.forward();
}
});
}
updateHistoryButtons();
window.addEventListener('popstate', updateHistoryButtons);
};
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initHistoryButtons);
} else {
initHistoryButtons();
}