DRY duplicated helpers (escapeAttr, belongsToForm, isSubmitControl, isEditableTarget) into web/js/core/app-form-utils.js and update 7 consumers. Add aria-live="polite" to flash messages, async messages, and search results for screen reader announcements. Add prefers-reduced-motion support to CSS tooltips. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
77 lines
2.0 KiB
JavaScript
77 lines
2.0 KiB
JavaScript
/**
|
|
* Browser history integration — handles back/forward navigation state.
|
|
*/
|
|
import { isEditableTarget } from '../core/app-form-utils.js';
|
|
|
|
const updateHistoryButtons = () => {
|
|
const back = document.querySelector('#global-back');
|
|
const forward = document.querySelector('#global-forward');
|
|
if (!back && !forward) {return;}
|
|
|
|
const hasHistory = window.history.length > 1;
|
|
const setDisabledState = (link, disabled) => {
|
|
link.setAttribute('aria-disabled', disabled ? 'true' : 'false');
|
|
link.classList.toggle('is-disabled', disabled);
|
|
if (disabled) {
|
|
link.setAttribute('tabindex', '-1');
|
|
} else {
|
|
link.removeAttribute('tabindex');
|
|
}
|
|
};
|
|
|
|
if (back) {
|
|
setDisabledState(back, !hasHistory);
|
|
}
|
|
if (forward) {
|
|
setDisabledState(forward, !hasHistory);
|
|
}
|
|
};
|
|
|
|
const initHistoryButtons = () => {
|
|
const back = document.querySelector('#global-back');
|
|
const forward = document.querySelector('#global-forward');
|
|
|
|
document.addEventListener('keydown', (event) => {
|
|
if (!event.altKey || event.metaKey || event.ctrlKey) {return;}
|
|
if (isEditableTarget(event.target)) {return;}
|
|
if (event.key === 'ArrowLeft') {
|
|
if (window.history.length > 1) {
|
|
event.preventDefault();
|
|
window.history.back();
|
|
}
|
|
} else if (event.key === 'ArrowRight') {
|
|
if (window.history.length > 1) {
|
|
event.preventDefault();
|
|
window.history.forward();
|
|
}
|
|
}
|
|
});
|
|
|
|
if (back) {
|
|
back.addEventListener('click', (event) => {
|
|
event.preventDefault();
|
|
if (window.history.length > 1) {
|
|
window.history.back();
|
|
}
|
|
});
|
|
}
|
|
|
|
if (forward) {
|
|
forward.addEventListener('click', (event) => {
|
|
event.preventDefault();
|
|
if (window.history.length > 1) {
|
|
window.history.forward();
|
|
}
|
|
});
|
|
}
|
|
|
|
updateHistoryButtons();
|
|
window.addEventListener('popstate', updateHistoryButtons);
|
|
};
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', initHistoryButtons);
|
|
} else {
|
|
initHistoryButtons();
|
|
}
|