187 lines
7.0 KiB
JavaScript
187 lines
7.0 KiB
JavaScript
// Small browser-only interactions shared by the hydrated views.
|
|
const scrollPositions = new Map();
|
|
const dialogs = new Map();
|
|
const watchers = new Map();
|
|
let nextHandle = 1;
|
|
let scrollLock = null;
|
|
let restoreGeneration = 0;
|
|
|
|
export function saveScroll(key) {
|
|
restoreGeneration += 1;
|
|
scrollPositions.set(key, window.scrollY);
|
|
}
|
|
export function restoreScroll(destination) {
|
|
const target = new URL(destination, location.href);
|
|
const top = scrollPositions.get(target.pathname) || 0;
|
|
const route = target.pathname + target.search;
|
|
const previousRoute = location.pathname + location.search;
|
|
const generation = ++restoreGeneration;
|
|
let attempts = 0;
|
|
const afterCommit = () => {
|
|
if (generation !== restoreGeneration) return;
|
|
const currentRoute = location.pathname + location.search;
|
|
// Leptos constructs the destination before committing the browser URL.
|
|
// Wait for that commit, but abandon a canceled or superseded navigation.
|
|
if (currentRoute !== route) {
|
|
if (currentRoute === previousRoute && ++attempts < 60) requestAnimationFrame(afterCommit);
|
|
return;
|
|
}
|
|
requestAnimationFrame(() => {
|
|
if (generation === restoreGeneration && location.pathname + location.search === route) {
|
|
window.scrollTo({ top, behavior: "instant" });
|
|
}
|
|
});
|
|
};
|
|
requestAnimationFrame(afterCommit);
|
|
}
|
|
export function focusSearch() {
|
|
document.querySelector('input[name="q"]')?.focus({ preventScroll: true });
|
|
}
|
|
export function blurActive() {
|
|
if (document.activeElement instanceof HTMLElement) document.activeElement.blur();
|
|
}
|
|
export function closeMenu(id) { document.getElementById(id)?.removeAttribute("open"); }
|
|
export function clearJobQuery() {
|
|
const url = new URL(location.href);
|
|
if (url.searchParams.has("job")) {
|
|
url.searchParams.delete("job");
|
|
history.replaceState(history.state, "", url);
|
|
}
|
|
}
|
|
export function revealSelectedPhoto() {
|
|
requestAnimationFrame(() => {
|
|
if (isCompact() || document.querySelector('dialog:modal')) return;
|
|
const panel = document.getElementById('info-panel');
|
|
if (panel) panel.scrollTop = 0;
|
|
panel?.querySelector('.selected-zone-panel')?.scrollIntoView({ block: 'nearest', behavior: 'instant' });
|
|
});
|
|
}
|
|
export function isCompact() {
|
|
return matchMedia("(max-width: 639px), (max-height: 499px)").matches;
|
|
}
|
|
export function watchCompact(onChange) {
|
|
const handle = nextHandle++;
|
|
const media = matchMedia("(max-width: 639px), (max-height: 499px)");
|
|
const listener = () => onChange(media.matches);
|
|
media.addEventListener("change", listener);
|
|
watchers.set(handle, { media, listener });
|
|
listener();
|
|
return handle;
|
|
}
|
|
export function releaseWatch(handle) {
|
|
const watcher = watchers.get(handle);
|
|
if (watcher) watcher.media.removeEventListener("change", watcher.listener);
|
|
watchers.delete(handle);
|
|
}
|
|
|
|
function unlockScroll() {
|
|
if (scrollLock && ![...dialogs.values()].some(record => !record.finished)) {
|
|
document.documentElement.style.overflow = scrollLock.html;
|
|
document.body.style.overflow = scrollLock.body;
|
|
scrollLock = null;
|
|
}
|
|
}
|
|
function finishDialog(record, notify = true) {
|
|
if (record.finished) return;
|
|
record.finished = true;
|
|
record.dialog.removeEventListener("cancel", record.cancel);
|
|
record.dialog.removeEventListener("close", record.closed);
|
|
record.dialog.removeEventListener("click", record.backdrop);
|
|
record.dialog.removeAttribute("aria-modal");
|
|
if (record.dialog.open) record.dialog.close();
|
|
if (record.inline && record.dialog.isConnected) record.dialog.show();
|
|
unlockScroll();
|
|
if (location.pathname + location.search === record.route) {
|
|
window.scrollTo({ top: record.scrollY, behavior: "instant" });
|
|
if (record.focus?.isConnected) record.focus.focus({ preventScroll: true });
|
|
}
|
|
if (notify) queueMicrotask(() => {
|
|
if (dialogs.get(record.handle) === record) record.onClose();
|
|
});
|
|
}
|
|
|
|
function requestClose(record) {
|
|
if (record.finished || record.pending) return;
|
|
if (history.state?.spaceOverlay === record.token) {
|
|
record.pending = true;
|
|
history.back();
|
|
} else {
|
|
finishDialog(record);
|
|
}
|
|
}
|
|
|
|
window.addEventListener("popstate", () => {
|
|
for (const record of [...dialogs.values()].reverse()) {
|
|
if (!record.finished && history.state?.spaceOverlay !== record.token) finishDialog(record);
|
|
else if (!record.finished) break;
|
|
}
|
|
});
|
|
|
|
export function openDialog(id, onClose) {
|
|
const dialog = document.getElementById(id);
|
|
if (!(dialog instanceof HTMLDialogElement)) return 0;
|
|
const existing = [...dialogs.values()].find(record => record.dialog === dialog && !record.finished);
|
|
if (existing) {
|
|
// The Rust bridge replaces its stored Closure for a reused handle.
|
|
existing.onClose = onClose;
|
|
return existing.handle;
|
|
}
|
|
const handle = nextHandle++;
|
|
const record = {
|
|
handle, dialog, onClose, token: `space-${handle}-${Date.now()}`,
|
|
focus: document.activeElement instanceof HTMLElement ? document.activeElement : null,
|
|
inline: dialog.hasAttribute("data-inline-dialog"),
|
|
scrollY: window.scrollY, route: location.pathname + location.search,
|
|
finished: false, pending: false,
|
|
};
|
|
record.cancel = event => { event.preventDefault(); requestClose(record); };
|
|
record.closed = () => {
|
|
// close() before showModal() queues an event; ignore it while the new modal is open.
|
|
if (!dialog.open && !record.finished) requestClose(record);
|
|
};
|
|
record.backdrop = event => {
|
|
if (event.target !== dialog) return;
|
|
const rect = dialog.getBoundingClientRect();
|
|
if (event.clientX < rect.left || event.clientX > rect.right || event.clientY < rect.top || event.clientY > rect.bottom) requestClose(record);
|
|
};
|
|
if (!scrollLock) {
|
|
scrollLock = { html: document.documentElement.style.overflow, body: document.body.style.overflow };
|
|
document.documentElement.style.overflow = "hidden";
|
|
document.body.style.overflow = "hidden";
|
|
}
|
|
dialogs.set(handle, record);
|
|
if (dialog.open) dialog.close();
|
|
try {
|
|
dialog.showModal();
|
|
} catch {
|
|
dialogs.delete(handle);
|
|
if (record.inline && dialog.isConnected) dialog.show();
|
|
unlockScroll();
|
|
return 0;
|
|
}
|
|
dialog.setAttribute("aria-modal", "true");
|
|
dialog.addEventListener("cancel", record.cancel);
|
|
dialog.addEventListener("close", record.closed);
|
|
dialog.addEventListener("click", record.backdrop);
|
|
history.pushState({ ...history.state, spaceOverlay: record.token }, "");
|
|
const focusTarget = dialog.querySelector("[autofocus], button:not([disabled]), [href], [tabindex='0']");
|
|
focusTarget?.focus({ preventScroll: true });
|
|
return handle;
|
|
}
|
|
export function closeDialog(id) {
|
|
const record = [...dialogs.values()].reverse().find(entry => entry.dialog.id === id && !entry.finished);
|
|
if (record) requestClose(record);
|
|
}
|
|
export function releaseDialog(handle) {
|
|
const record = dialogs.get(handle);
|
|
if (!record) return;
|
|
// Route disposal must not initiate a second navigation. Explicit close consumes history.
|
|
finishDialog(record, false);
|
|
dialogs.delete(handle);
|
|
}
|
|
|
|
export function copyText(value, onResult) {
|
|
if (!navigator.clipboard?.writeText) { onResult(false); return; }
|
|
navigator.clipboard.writeText(value).then(() => onResult(true), () => onResult(false));
|
|
}
|