feat(regions): show checker regions on a map
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m48s
ci / test (pull_request) Successful in 12m42s
ci / build (pull_request) Canceled after 30s
ci / image (pull_request) Canceled after 0s

Regions get an optional location. Platform admins set it for platform
regions and org admins for their own, by clicking the map on the regions
page or entering the coordinates; PATCH /org/regions/:id and
/admin/regions/:id set or remove it.

The monitor form shows the located regions on a map from the
self-hosted ZB-Server Maps: a click on a marker ticks or unticks the
region, and the checkbox list stays the source of truth. MapLibre loads
only on pages with a map, and the map stays hidden if the map server
cannot be reached.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-28 20:17:27 +01:00
1 parent 3f260c0712
commit 3562f8224e
24 files changed
+3081 -1806

No files matched your search

+130
View File
@@ -588,4 +588,134 @@ for (const el of document.querySelectorAll("input[data-tz-default]")) {
};
for (const t of ed.querySelectorAll("[data-md-tab]")) t.addEventListener("click", () => show(t.dataset.mdTab));
}
// Region maps show the [data-lat][data-lon] elements inside the element
// [data-region-items] selects, on the map server's tiles. MapLibre loads
// only on pages with a map, and the map stays hidden when it cannot.
let maplibre;
const loadMaplibre = (server) =>
(maplibre ??= window.maplibregl
? Promise.resolve(window.maplibregl)
: new Promise((resolve, reject) => {
const css = Object.assign(document.createElement("link"), { rel: "stylesheet", href: `${server}/vendor/maplibre-gl.css` });
const js = Object.assign(document.createElement("script"), { src: `${server}/vendor/maplibre-gl.js` });
js.onload = () => (window.maplibregl ? resolve(window.maplibregl) : reject(new Error("no MapLibre")));
js.onerror = reject;
document.head.append(css, js);
}));
const darkScheme = window.matchMedia?.("(prefers-color-scheme: dark)");
const mapStyle = (server) => `${server}/style-${darkScheme?.matches ? "dark" : "light"}.json`;
// A click on the map fills the coordinates of the location form last
// opened or focused, else the one marked [data-location-default].
let locationTarget = document.querySelector("[data-location-default]");
document.addEventListener("focusin", (ev) => {
const form = ev.target.closest?.("[data-location-form]");
if (form) locationTarget = form;
});
document.addEventListener("click", (ev) => {
const toggle = ev.target.closest("[data-location-toggle]");
if (toggle) {
const row = document.getElementById(toggle.dataset.locationToggle);
row.hidden = !row.hidden;
const form = row.querySelector("[data-location-form]");
if (!row.hidden) {
locationTarget = form;
row.querySelector("[data-location-field]")?.focus();
} else if (locationTarget === form) {
locationTarget = document.querySelector("[data-location-default]");
}
}
const clear = ev.target.closest("[data-location-clear]");
if (clear) {
const form = clear.closest("form");
for (const f of form.querySelectorAll("[data-location-field]")) f.value = "";
form.requestSubmit();
}
});
const initRegionMap = async (box) => {
const server = box.dataset.mapServer;
let gl;
try {
gl = await loadMaplibre(server);
} catch {
return;
}
const items = [...document.querySelectorAll(`${box.dataset.regionItems} [data-lat][data-lon]`)];
const lngLat = (item) => [Number(item.dataset.lon), Number(item.dataset.lat)];
box.hidden = false;
let map;
try {
map = new gl.Map({ container: box, style: mapStyle(server), center: [10.45, 51.17], zoom: 3 });
} catch {
box.hidden = true;
return;
}
let styled = false;
map.on("styledata", () => (styled = true));
map.on("error", () => {
if (!styled) box.hidden = true;
});
map.addControl(new gl.NavigationControl({ showCompass: false }), "top-right");
darkScheme?.addEventListener("change", () => map.setStyle(mapStyle(server)));
// The map may start hidden, as with the regions of passive monitor
// types, so it fits the markers once it has a size.
let fitted = false;
const fit = () => {
map.resize();
if (fitted || !box.clientWidth) return;
fitted = true;
const points = items.map(lngLat);
if (points.length > 1) {
const bounds = points.slice(1).reduce((b, p) => b.extend(p), new gl.LngLatBounds(points[0], points[0]));
map.fitBounds(bounds, { padding: 40, maxZoom: 6, duration: 0 });
} else if (points.length === 1) {
map.jumpTo({ center: points[0], zoom: 5 });
}
};
new ResizeObserver(fit).observe(box);
fit();
const selecting = box.dataset.regionMap === "select";
for (const item of items) {
const el = Object.assign(document.createElement("button"), { type: "button", className: "region-marker", title: item.dataset.name });
el.setAttribute("aria-label", item.dataset.name);
el.dataset.live = item.dataset.live;
new gl.Marker({ element: el }).setLngLat(lngLat(item)).addTo(map);
if (selecting) {
// The checkbox stays the source of truth; its change event keeps
// the form's own behaviour.
const paint = () => el.classList.toggle("is-checked", item.checked);
paint();
item.addEventListener("change", paint);
el.addEventListener("click", (ev) => {
ev.stopPropagation();
if (!item.matches(":disabled")) item.click();
});
} else {
el.classList.add("is-checked");
el.addEventListener("click", (ev) => {
ev.stopPropagation();
item.querySelector("[data-location-toggle]")?.click();
});
}
}
if (selecting) return;
let pick;
map.on("click", (ev) => {
if (!locationTarget) return;
const at = ev.lngLat.wrap ? ev.lngLat.wrap() : ev.lngLat;
const round = (v) => String(Math.round(v * 10000) / 10000);
locationTarget.querySelector('[data-location-field="latitude"]').value = round(at.lat);
locationTarget.querySelector('[data-location-field="longitude"]').value = round(at.lng);
const point = [at.lng, at.lat];
pick ??= new gl.Marker({ element: Object.assign(document.createElement("div"), { className: "region-marker is-pick" }) })
.setLngLat(point)
.addTo(map);
pick.setLngLat(point);
});
};
for (const box of document.querySelectorAll("[data-region-map]")) initRegionMap(box);
})();