(() => { 'use strict'; window.PagesWikiCityNavigation = ({svg, parent}) => { const selector = '.city-quartier-tile,.city-street-tile,.city-spot-entry[role="button"],.city-estate-more,.city-map-page-link,.city-portal-link'; const directions = {ArrowLeft:[-1,0], a:[-1,0], h:[-1,0], ArrowRight:[1,0], d:[1,0], l:[1,0], ArrowUp:[0,-1], w:[0,-1], k:[0,-1], ArrowDown:[0,1], s:[0,1], j:[0,1]}; let focusPending = false, preferred = null; const blocked = () => !!document.querySelector('dialog[open],.city-context-menu') || [...document.querySelectorAll('[role="dialog"][aria-modal="true"]:not(dialog):not([hidden])')].some(element => element.getClientRects().length && getComputedStyle(element).visibility !== 'hidden'); const candidates = () => { const bounds = svg.getBoundingClientRect(); return [...svg.querySelectorAll(selector)].map(element => ({element, rect: element.getBoundingClientRect()})).filter(({element, rect}) => { const style = getComputedStyle(element); return rect.width > 0 && rect.height > 0 && rect.right > bounds.left && rect.left < bounds.right && rect.bottom > bounds.top && rect.top < bounds.bottom && style.display !== 'none' && style.visibility !== 'hidden' && !element.closest('[hidden],[inert]'); }).map(item => ({...item, x: item.rect.left + item.rect.width/2, y: item.rect.top + item.rect.height/2})); }; const focus = item => item?.element.focus({preventScroll:true}); const refresh = () => { if (!focusPending || blocked()) return; const items = candidates(); const wanted = preferred && items.find(item => item.element.closest('[data-city-uuid]')?.dataset.cityUuid === preferred); focus(wanted || items.sort((a,b) => a.y-b.y || a.x-b.x)[0]); if (items.length) { focusPending = false; preferred = null; } }; const requestFocus = id => { focusPending = true; preferred = id || null; }; const keydown = event => { if (event.defaultPrevented || event.isComposing || event.keyCode === 229 || event.ctrlKey || event.metaKey || event.altKey || blocked()) return; const target = event.target; if (target.closest?.('input,textarea,select,[contenteditable]:not([contenteditable="false"]),[role="textbox"]')) return; if (!svg.contains(target) && target !== document.body && target !== document.documentElement) return; const direction = directions[event.key] || directions[event.key.toLowerCase()]; if (event.key === 'Backspace') { event.preventDefault(); event.stopPropagation(); parent(); return; } if (!direction && event.key !== 'Enter' && event.key !== ' ') return; const items = candidates(); const current = items.find(item => item.element === document.activeElement || item.element === target); if (event.key === 'Enter' || event.key === ' ') { if (!current) return; event.preventDefault(); event.stopPropagation(); requestFocus(); if (event.repeat) return; current.element.dispatchEvent(new MouseEvent('click', {bubbles:true,cancelable:true,view:window})); return; } if (!direction || !items.length) return; event.preventDefault(); event.stopPropagation(); if (!current) { focus(items.sort((a,b) => a.y-b.y || a.x-b.x)[0]); return; } const [vx, vy] = direction; const next = items.filter(item => item !== current).map(item => { const dx = item.x-current.x, dy = item.y-current.y, forward = dx*vx+dy*vy, sideways = Math.abs(dx*vy-dy*vx); return {item, forward, score: Math.hypot(dx,dy)*(1+sideways/Math.max(1,forward))}; }).filter(item => item.forward > 1).sort((a,b) => a.score-b.score)[0]; focus(next?.item); }; document.addEventListener('keydown', keydown, true); return {refresh, requestFocus, destroy: () => document.removeEventListener('keydown', keydown, true)}; }; })();