Files
eventLabs/assets/maps.js
2026-11-11 21:30:20 -05:00

116 lines
3.6 KiB
JavaScript

(() => {
const selector = '[data-be-map]';
const mounted = new WeakSet();
function link(text, url) {
const anchor = document.createElement('a');
anchor.textContent = text;
anchor.href = url;
return anchor;
}
function popup(pin) {
const content = document.createElement('div');
content.className = 'be-map-popup';
for (const place of Object.values(pin.places)) {
const name = document.createElement('strong');
name.append(link(place.name, place.url));
content.append(name);
if (place.address) {
const address = document.createElement('span');
address.textContent = place.address;
content.append(address);
}
}
const events = Object.values(pin.events);
if (events.length) {
const list = document.createElement('ul');
for (const event of events) {
const item = document.createElement('li');
const date = document.createElement('span');
date.textContent = event.date;
item.append(link(event.title, event.url), date);
list.append(item);
}
content.append(list);
}
return content;
}
function mount(frame) {
if (mounted.has(frame) || !window.L) return;
const canvas = frame.querySelector('.be-map-canvas');
const pins = JSON.parse(frame.querySelector('.be-map-pins').textContent);
const map = L.map(canvas, { scrollWheelZoom: false });
const markers = new Map();
const bounds = L.latLngBounds(pins.map(pin => [pin.latitude, pin.longitude]));
map.fitBounds(bounds, { padding: [42, 42], maxZoom: 15 });
for (const pin of pins) {
const count = Object.keys(pin.events).length;
const title = Object.values(pin.places).map(place => place.name).join(' / ');
const symbol = document.createElement('span');
symbol.textContent = count || '';
const marker = L.marker([pin.latitude, pin.longitude], {
title,
icon: L.divIcon({
className: 'be-map-pin',
html: symbol,
iconSize: [36, 36],
iconAnchor: [18, 36],
popupAnchor: [0, -34],
}),
}).addTo(map);
marker.getElement().setAttribute('aria-label', title);
marker.bindPopup(popup(pin), { maxHeight: 260, maxWidth: 300 });
markers.set(pin.key, marker);
}
const status = frame.querySelector('.be-map-error');
const tiles = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
keepBuffer: 1,
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>',
});
tiles.on('tileload', () => { status.hidden = true; });
tiles.on('tileerror', () => {
status.hidden = !!canvas.querySelector('.leaflet-tile-loaded');
});
tiles.addTo(map);
mounted.add(frame);
for (const button of frame.closest('.be-map-layout')?.querySelectorAll('[data-be-map-focus]') ?? []) {
const marker = markers.get(button.dataset.beMapFocus);
if (!marker) continue;
button.hidden = false;
button.addEventListener('click', () => {
map.panTo(marker.getLatLng(), { animate: false });
marker.openPopup();
marker.getElement().focus();
});
}
}
function mountAll(scope) {
if (scope.matches?.(selector)) mount(scope);
for (const frame of scope.querySelectorAll(selector)) mount(frame);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => mountAll(document), { once: true });
} else {
mountAll(document);
}
let elementorHook = false;
function mountInElementor() {
const hooks = window.elementorFrontend?.hooks;
if (!hooks || elementorHook) return;
hooks.addAction('frontend/element_ready/global', scope => mountAll(scope[0]));
elementorHook = true;
}
mountInElementor();
if (window.jQuery) window.jQuery(window).on('elementor/frontend/init', mountInElementor);
})();