/* Modal visibility, keyboard containment and focus restoration. */ (function () { 'use strict'; const selector = '.auth-modal,.bell-modal,.return-modal,.qr-modal,.token-expired-modal,.return-confirm-modal,.unlock-confirm-modal,.lost-modal,#bike-card'; const focusable = 'button:not([disabled]),a[href],input:not([disabled]),select:not([disabled]),textarea:not([disabled]),[tabindex="0"]'; const previous = new WeakMap(); function containBackground() { const open = [...document.querySelectorAll(selector)].filter(el => el.classList.contains('show')); const modal = open.at(-1), settings = document.getElementById('settings-page'); const active = modal || (settings && !settings.hidden ? settings : null); for (const child of document.body.children) { if (['SCRIPT','STYLE','LINK'].includes(child.tagName)) continue; if (active) child.inert = !(child === active || child.contains(active) || (modal?.id === 'bike-card' && child.matches('.card-overlay.show'))); else if (!child.matches(selector)) child.inert = false; } } function sync(el) { const open = el.classList.contains('show'); const activeElement = document.activeElement, hadFocus = el.contains(activeElement); el.inert = !open; el.setAttribute('aria-hidden', String(!open)); if (!el.hasAttribute('role')) el.setAttribute('role', 'dialog'); el.setAttribute('aria-modal', 'true'); if (!el.hasAttribute('aria-label') && !el.hasAttribute('aria-labelledby')) { const title = el.querySelector('.auth-header,.unlock-confirm-title,.return-confirm-title,.bike-card-title,.qr-header'); el.setAttribute('aria-label', title?.textContent.trim().slice(0, 60) || '操作窗口'); } if (open) { previous.set(el, activeElement); containBackground(); queueMicrotask(() => { if (el.classList.contains('show')) [...el.querySelectorAll(focusable)].find(node => node.getClientRects().length)?.focus({ preventScroll: true }); }); } else if (hadFocus) { containBackground(); const target = previous.get(el); if (target?.isConnected) target.focus({ preventScroll: true }); else document.getElementById('auth-btn')?.focus({ preventScroll: true }); } containBackground(); } document.addEventListener('DOMContentLoaded', () => { document.querySelectorAll(selector).forEach(el => { sync(el); new MutationObserver(records => { if (records.some(r => r.attributeName === 'class')) sync(el); }).observe(el, { attributes: true, attributeFilter: ['class'] }); }); const settings = document.getElementById('settings-page'); if (settings) new MutationObserver(containBackground).observe(settings, { attributes:true, attributeFilter:['hidden'] }); containBackground(); document.addEventListener('keydown', event => { const dialogs = [...document.querySelectorAll(selector)].filter(el => el.classList.contains('show')); const el = dialogs.at(-1); if (!el) return; if (event.key === 'Escape') { const close = el.querySelector('.auth-close,.bike-card-close,.qr-close,#app-confirm-cancel,.unlock-confirm-btn.cancel,.return-confirm-btn.cancel,.return-confirm-cancel,[data-dialog-close]'); if (close) { event.preventDefault(); close.click(); } return; } if (event.key !== 'Tab') return; const items = [...el.querySelectorAll(focusable)].filter(n => n.getClientRects().length); if (!items.length) { event.preventDefault(); return; } const first = items[0], last = items.at(-1); if (event.shiftKey && (document.activeElement === first || !el.contains(document.activeElement))) { event.preventDefault(); last.focus({ preventScroll: true }); } else if (!event.shiftKey && (document.activeElement === last || !el.contains(document.activeElement))) { event.preventDefault(); first.focus({ preventScroll: true }); } }); }); })();