(async () => { const holder = document.querySelector('[data-world-map]'); if (!holder) return; const status = document.querySelector('[data-map-status]'); const ns = 'http://www.w3.org/2000/svg'; const countries = new Map(JSON.parse(document.querySelector('#country-traffic').textContent) .filter(row => row.country).map(row => [row.country, row.views])); const names = typeof Intl.DisplayNames === 'function' ? new Intl.DisplayNames(['en'],{type:'region'}) : null; document.querySelectorAll('[data-country-name]').forEach(cell => { if (cell.dataset.countryName && names) cell.textContent = names.of(cell.dataset.countryName); }); const number = new Intl.NumberFormat(); try { const result = await fetch(holder.dataset.mapUrl); if (!result.ok) throw Error('Map unavailable'); const shapes = await result.json(); const svg = document.createElementNS(ns,'svg'); svg.setAttribute('viewBox','0 0 900 365'); svg.setAttribute('role','group'); svg.setAttribute('aria-label','Interactive world map of readership by country'); let selected; const max = Math.max(1, ...countries.values()); for (const [code, name, outline] of shapes) { const shape = document.createElementNS(ns,'path'); shape.setAttribute('d',outline); shape.setAttribute('fill-rule','evenodd'); const count = countries.get(code) || 0; const label = `${name}: ${number.format(count)} pageviews`; const title = document.createElementNS(ns,'title'); title.textContent = label; shape.append(title); if (count) { shape.classList.add('has-traffic'); shape.style.setProperty('--intensity',(0.46 + 0.54 * Math.sqrt(count/max)).toFixed(3)); shape.setAttribute('tabindex','0'); shape.setAttribute('role','button'); shape.setAttribute('aria-label',label); shape.setAttribute('aria-pressed','false'); const pick = () => { if (selected) selected.setAttribute('aria-pressed','false'); selected = shape; shape.setAttribute('aria-pressed','true'); status.textContent = label; }; shape.addEventListener('click',pick); shape.addEventListener('keydown',event => { if (event.key === 'Enter' || event.key === ' ') { event.preventDefault(); pick(); } }); } svg.append(shape); } holder.append(svg); } catch { status.textContent = 'The map is unavailable. Country totals are listed below.'; } })();