diff --git a/internal/web/assets/app.css b/internal/web/assets/app.css index f03fea0..40c5b26 100644 --- a/internal/web/assets/app.css +++ b/internal/web/assets/app.css @@ -164,6 +164,22 @@ font-size: 12px; } } +/* Pins whose label would cover another one turn it to the left of their + point, or show only their dot until hovered. */ +.is-flipped > .region-pin { + flex-direction: row-reverse; + padding: 3px 5px 3px 9px; + translate: calc(16px - 100%); +} +.maplibregl-marker:is(.is-flipped, .is-compact):not(.is-hover, :hover) { z-index: 2; } +.is-compact > .region-pin { gap: 0; padding: 3px 5px; } +.is-compact:not(.is-hover, :hover) > .region-pin > :last-child { display: none; } +.is-compact.is-hover > .region-pin, +.is-compact:hover > .region-pin { gap: 6px; padding: 3px 9px 3px 5px; } +@media (pointer: coarse) { + .is-flipped > .region-pin { padding: 6px 8px 6px 11px; } + .is-compact > .region-pin { padding: 6px 8px; } +} .region-pin:focus-visible { outline: 2px solid var(--color-maintenance); outline-offset: 2px; } .region-pick { width: 14px; diff --git a/internal/web/assets/app.js b/internal/web/assets/app.js index 829da5a..51a8081 100644 --- a/internal/web/assets/app.js +++ b/internal/web/assets/app.js @@ -698,6 +698,7 @@ for (const el of document.querySelectorAll("input[data-tz-default]")) { fit(); const selecting = box.dataset.regionMap === "select"; + const markers = []; for (const item of items) { // The list row (a label or table row) and the marker light up together. const row = item.closest("label, tr") ?? item; @@ -708,6 +709,7 @@ for (const el of document.querySelectorAll("input[data-tz-default]")) { const holder = document.createElement("div"); holder.append(pin); const marker = new gl.Marker({ element: holder, anchor: "left", offset: [-8, 0] }).setLngLat(lngLat(item)).addTo(map); + markers.push({ holder, pin, marker }); const hover = (on) => { holder.classList.toggle("is-hover", on); row.classList.toggle("is-hover", on); @@ -743,6 +745,48 @@ for (const el of document.querySelectorAll("input[data-tz-default]")) { }); } } + + // Labels run to the right of their point, or to the left where that + // would cover another pin or leave the map, as with regions a few + // kilometres apart. A pin with room on neither side shows only its dot + // and its name on hover; the list names every region. No label covers + // the dot of another pin. + const place = () => { + const width = box.clientWidth; + const height = box.clientHeight; + if (!width) return; + for (const m of markers) m.holder.classList.remove("is-flipped", "is-compact"); + const spots = markers.map((m) => { + const at = map.project(m.marker.getLngLat()); + const h = m.pin.offsetHeight; + return { m, at, w: m.pin.offsetWidth, h, dot: { left: at.x - 8, right: at.x + 12, top: at.y - h / 2, bottom: at.y + h / 2 } }; + }); + // The rounded ends of two pills may touch. + const hits = (a, b) => a.left + 4 < b.right - 4 && b.left + 4 < a.right - 4 && a.top < b.bottom && b.top < a.bottom; + const labels = []; + const fits = (a, self) => + a.left >= 0 && a.right <= width && a.top >= 0 && a.bottom <= height && + !labels.some((b) => hits(a, b)) && + // Dots stacked on its own one stay beside it either way. + !spots.some((s) => s !== self && !hits(s.dot, self.dot) && hits(a, s.dot)); + for (const s of spots.sort((a, b) => b.at.x - a.at.x)) { + const right = { ...s.dot, right: s.at.x - 8 + s.w }; + const left = { ...s.dot, left: s.at.x + 8 - s.w, right: s.at.x + 8 }; + if (fits(right, s)) { + labels.push(right); + } else if (fits(left, s)) { + s.m.holder.classList.add("is-flipped"); + labels.push(left); + } else { + s.m.holder.classList.add("is-compact"); + } + } + }; + map.on("load", place); + map.on("zoom", place); + map.on("moveend", place); + map.on("resize", place); + if (selecting) return; let pick; map.on("click", (ev) => { diff --git a/internal/web/testdata/regionmap.html b/internal/web/testdata/regionmap.html index c0df9cc..99a634a 100644 --- a/internal/web/testdata/regionmap.html +++ b/internal/web/testdata/regionmap.html @@ -20,6 +20,12 @@ + + + + + +