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

+21
View File
@@ -118,3 +118,24 @@
font-size: max(16px, 1em);
}
}
/* Region maps: a dot per region, filled when it is selected and grey
when no checker of the region is live. */
.region-marker {
width: 14px;
height: 14px;
padding: 0;
border: 2px solid var(--color-ink);
border-radius: 9999px;
background: var(--color-paper);
cursor: pointer;
}
.region-marker.is-checked { background: var(--color-ink); }
.region-marker[data-live="0"] { border-color: var(--color-unknown); }
.region-marker.is-checked[data-live="0"] { background: var(--color-unknown); }
.region-marker.is-pick {
border-color: var(--color-down);
background: var(--color-down);
cursor: default;
}
.region-map .maplibregl-ctrl-attrib { font-size: 10px; }
+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);
})();
+46 -15
View File
@@ -84,8 +84,8 @@ type DashboardView struct {
// ArrangeURL turns it on or off, empty when not offered.
Arrange bool
ArrangeURL string
Range string
Ranges []RangeOption
Range string
Ranges []RangeOption
// Unchecked counts the monitors no region has reported for recently.
Unchecked int
// DeadRegions are regions of the monitors without a live checker.
@@ -721,6 +721,19 @@ type RegionOption struct {
Own bool
Checked bool
Live int64
// Lat and Lon place the region on the map; both are nil without a
// location.
Lat, Lon *float64
}
// RegionsMapped reports whether any region of the form has a location.
func (f MonitorForm) RegionsMapped() bool {
for _, r := range f.Regions {
if r.HasLocation() {
return true
}
}
return false
}
// MonitorForm is the data of the monitor editor; ID is empty for new ones.
@@ -1119,20 +1132,38 @@ templ MonitorEditor(p Page, me Me, f MonitorForm) {
<fieldset class="mb-4" data-hide-for="heartbeat alertmanager" hidden?={ passiveType(f.Type) } disabled?={ f.Type == "heartbeat" }>
<legend class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "monitors.regions") }</legend>
<p class="mb-2 text-xs text-muted">{ i18n.T(ctx, "monitors.regions_hint") }</p>
for _, r := range f.Regions {
<label class="mb-1.5 flex items-center gap-2 text-sm">
<input type="checkbox" name="regions" value={ r.ID } data-multi checked?={ r.Checked } class="accent-ink"/>
{ r.Name }
<span class="font-mono text-xs text-muted">
if r.Own {
{ i18n.T(ctx, "regions.own") }
} else {
{ i18n.T(ctx, "regions.platform") }
}
· { i18n.T(ctx, "regions.live", "Count", int(r.Live)) }
</span>
</label>
if f.RegionsMapped() {
@regionMap("select", "#monitor-regions")
}
<div id="monitor-regions">
for _, r := range f.Regions {
<label class="mb-1.5 flex items-center gap-2 text-sm">
<input
type="checkbox"
name="regions"
value={ r.ID }
data-multi
checked?={ r.Checked }
class="accent-ink"
if r.HasLocation() {
data-lat={ coord(r.Lat) }
data-lon={ coord(r.Lon) }
}
data-name={ r.Name }
data-live={ strconv.FormatInt(r.Live, 10) }
/>
{ r.Name }
<span class="font-mono text-xs text-muted">
if r.Own {
{ i18n.T(ctx, "regions.own") }
} else {
{ i18n.T(ctx, "regions.platform") }
}
· { i18n.T(ctx, "regions.live", "Count", int(r.Live)) }
</span>
</label>
}
</div>
</fieldset>
@checkbox(i18n.T(ctx, "sso.field.enabled"), "enabled", f.Enabled)
@submit(i18n.T(ctx, "common.save"))
File diff suppressed because it is too large. Load diff
+68
View File
@@ -0,0 +1,68 @@
package web
import (
"strconv"
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
)
// MapServer serves the map tiles, styles and the MapLibre library the
// region maps load.
const MapServer = "https://maps.zb-server.de"
// coord formats a coordinate for an attribute or input, empty for none.
func coord(f *float64) string {
if f == nil {
return ""
}
return strconv.FormatFloat(*f, 'f', -1, 64)
}
// HasLocation reports whether r has a place on the map.
func (r RegionOption) HasLocation() bool { return r.Lat != nil && r.Lon != nil }
// HasLocation reports whether r has a place on the map.
func (r Region) HasLocation() bool { return r.Lat != nil && r.Lon != nil }
// regionMap is a map of the regions marked with data-lat and data-lon
// inside the element items selects. In "select" mode a click on a marker
// toggles the region's checkbox; in "edit" mode a click on the map fills
// the active [data-location-form]. The script shows it once the map loads.
templ regionMap(mode, items string) {
<div
data-region-map={ mode }
data-region-items={ items }
data-map-server={ MapServer }
hidden
class="region-map mb-3 h-72 w-full overflow-hidden border border-rule"
></div>
}
// locationFields are the coordinate inputs of a region; empty ones remove
// the location.
templ locationFields(lat, lon *float64) {
<div class="flex flex-wrap gap-3">
for _, f := range []struct {
label, name, value, min, max string
}{
{i18n.T(ctx, "regions.latitude"), "latitude", coord(lat), "-90", "90"},
{i18n.T(ctx, "regions.longitude"), "longitude", coord(lon), "-180", "180"},
} {
<label class="mb-4 block">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ f.label }</span>
<input
type="number"
step="any"
name={ f.name }
data-number
data-nullable
data-location-field={ f.name }
value={ f.value }
min={ f.min }
max={ f.max }
class="w-36 border border-rule bg-transparent px-2.5 py-1.5 font-mono text-sm outline-none focus:border-ink"
/>
</label>
}
</div>
}
+224
View File
@@ -0,0 +1,224 @@
// Code generated by templ - DO NOT EDIT.
// templ: version: v0.3.1118
package web
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
import (
"strconv"
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
)
// MapServer serves the map tiles, styles and the MapLibre library the
// region maps load.
const MapServer = "https://maps.zb-server.de"
// coord formats a coordinate for an attribute or input, empty for none.
func coord(f *float64) string {
if f == nil {
return ""
}
return strconv.FormatFloat(*f, 'f', -1, 64)
}
// HasLocation reports whether r has a place on the map.
func (r RegionOption) HasLocation() bool { return r.Lat != nil && r.Lon != nil }
// HasLocation reports whether r has a place on the map.
func (r Region) HasLocation() bool { return r.Lat != nil && r.Lon != nil }
// regionMap is a map of the regions marked with data-lat and data-lon
// inside the element items selects. In "select" mode a click on a marker
// toggles the region's checkbox; in "edit" mode a click on the map fills
// the active [data-location-form]. The script shows it once the map loads.
func regionMap(mode, items string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div data-region-map=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templruntime.NewAttributeExpression(mode).Resolve(ctx)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/regionmap.templ`, Line: 33, Col: 24}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var2)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\" data-region-items=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templruntime.NewAttributeExpression(items).Resolve(ctx)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/regionmap.templ`, Line: 34, Col: 27}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var3)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\" data-map-server=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templruntime.NewAttributeExpression(MapServer).Resolve(ctx)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/regionmap.templ`, Line: 35, Col: 29}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var4)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "\" hidden class=\"region-map mb-3 h-72 w-full overflow-hidden border border-rule\"></div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
// locationFields are the coordinate inputs of a region; empty ones remove
// the location.
func locationFields(lat, lon *float64) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "<div class=\"flex flex-wrap gap-3\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
for _, f := range []struct {
label, name, value, min, max string
}{
{i18n.T(ctx, "regions.latitude"), "latitude", coord(lat), "-90", "90"},
{i18n.T(ctx, "regions.longitude"), "longitude", coord(lon), "-180", "180"},
} {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "<label class=\"mb-4 block\"><span class=\"mb-1 block text-xs font-medium uppercase tracking-wider text-muted\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var6 string
templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(f.label)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/regionmap.templ`, Line: 52, Col: 94}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "</span> <input type=\"number\" step=\"any\" name=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var7 string
templ_7745c5c3_Var7, templ_7745c5c3_Err = templruntime.NewAttributeExpression(f.name).Resolve(ctx)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/regionmap.templ`, Line: 56, Col: 18}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var7)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "\" data-number data-nullable data-location-field=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var8 string
templ_7745c5c3_Var8, templ_7745c5c3_Err = templruntime.NewAttributeExpression(f.name).Resolve(ctx)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/regionmap.templ`, Line: 59, Col: 33}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var8)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "\" value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var9 string
templ_7745c5c3_Var9, templ_7745c5c3_Err = templruntime.NewAttributeExpression(f.value).Resolve(ctx)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/regionmap.templ`, Line: 60, Col: 20}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var9)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "\" min=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var10 string
templ_7745c5c3_Var10, templ_7745c5c3_Err = templruntime.NewAttributeExpression(f.min).Resolve(ctx)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/regionmap.templ`, Line: 61, Col: 16}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var10)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "\" max=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var11 string
templ_7745c5c3_Var11, templ_7745c5c3_Err = templruntime.NewAttributeExpression(f.max).Resolve(ctx)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/regionmap.templ`, Line: 62, Col: 16}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var11)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "\" class=\"w-36 border border-rule bg-transparent px-2.5 py-1.5 font-mono text-sm outline-none focus:border-ink\"></label>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, "</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
+52 -3
View File
@@ -1,6 +1,10 @@
package web
import "git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
import (
"strconv"
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
)
// Region is a row in a region list.
type Region struct {
@@ -12,6 +16,9 @@ type Region struct {
// Manageable regions can be deleted on the current page.
Manageable bool
Instances int64
// Lat and Lon place the region on the map; both are nil without a
// location.
Lat, Lon *float64
}
// Agent is a checker or status page server.
@@ -65,6 +72,8 @@ type RegionsView struct {
templ regionsBody(v RegionsView) {
<section class="mb-12">
@sectionHeading("regions.list")
<p class="mb-2 text-xs text-muted">{ i18n.T(ctx, "regions.location_pick_hint") }</p>
@regionMap("edit", "#region-list")
<div class="overflow-x-auto">
<table class="w-full text-left text-sm">
<thead class="border-b border-rule text-xs uppercase tracking-wider text-muted">
@@ -73,12 +82,20 @@ templ regionsBody(v RegionsView) {
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "regions.key") }</th>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "regions.owner") }</th>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "regions.instances") }</th>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "regions.location") }</th>
<th class="py-2"></th>
</tr>
</thead>
<tbody class="divide-y divide-rule">
<tbody id="region-list" class="divide-y divide-rule">
for _, r := range v.Regions {
<tr>
<tr
if r.HasLocation() {
data-lat={ coord(r.Lat) }
data-lon={ coord(r.Lon) }
}
data-name={ r.Name }
data-live={ strconv.FormatInt(r.Instances, 10) }
>
<td class="py-2.5 pr-4">{ r.Name }</td>
<td class="py-2.5 pr-4 font-mono text-xs">{ r.Key }</td>
<td class="py-2.5 pr-4">
@@ -91,12 +108,41 @@ templ regionsBody(v RegionsView) {
<td class="py-2.5 pr-4 font-mono text-xs">
<span class={ templ.KV("text-up", r.Instances > 0), templ.KV("text-unknown", r.Instances == 0) }>●</span> { i18n.T(ctx, "regions.live", "Count", int(r.Instances)) }
</td>
<td class="py-2.5 pr-4 font-mono text-xs">
if r.HasLocation() {
{ coord(r.Lat) }, { coord(r.Lon) }
} else {
<span class="text-muted">–</span>
}
if r.Manageable {
<button type="button" data-location-toggle={ "location-" + r.ID } class="ml-2 font-sans text-muted hover:text-ink">{ i18n.T(ctx, "regions.location_set") }</button>
}
</td>
<td class="py-2.5 text-right">
if r.Manageable {
<button type="button" data-delete={ v.APIBase + "/regions/" + r.ID } data-confirm={ i18n.T(ctx, "regions.delete_confirm") } class="text-muted hover:text-down">{ i18n.T(ctx, "regions.delete") }</button>
}
</td>
</tr>
if r.Manageable {
<tr id={ "location-" + r.ID } hidden>
<td colspan="6" class="py-3">
<form data-api={ v.APIBase + "/regions/" + r.ID } data-method="patch" data-next={ v.Next } data-location-form>
@formError()
<p class="mb-2 text-xs text-muted">{ i18n.T(ctx, "regions.location_for", "Name", r.Name) }</p>
<div class="flex flex-wrap items-end gap-4">
@locationFields(r.Lat, r.Lon)
<div class="mb-4 flex gap-3">
<button type="submit" class="bg-ink px-3 py-1.5 text-sm font-medium text-paper hover:opacity-90 disabled:opacity-50">{ i18n.T(ctx, "common.save") }</button>
if r.HasLocation() {
<button type="button" data-location-clear class="text-sm text-muted hover:text-down">{ i18n.T(ctx, "regions.location_clear") }</button>
}
</div>
</div>
</form>
</td>
</tr>
}
}
</tbody>
</table>
@@ -109,6 +155,9 @@ templ regionsBody(v RegionsView) {
@formError()
@valueField(i18n.T(ctx, "regions.key"), "key", "", "office-berlin", true)
@valueField(i18n.T(ctx, "sso.field.name"), "name", "", "Office Berlin", false)
<div data-location-form data-location-default>
@locationFields(nil, nil)
</div>
@submit(i18n.T(ctx, "regions.create"))
</form>
</section>
File diff suppressed because it is too large. Load diff
+98
View File
@@ -0,0 +1,98 @@
<!doctype html><html><body data-msg-network="net">
<form id="monitor">
<div id="select-map" data-region-map="select" data-region-items="#monitor-regions" data-map-server="stub" hidden style="width:300px;height:200px"></div>
<div id="monitor-regions">
<label><input type="checkbox" name="regions" value="r1" data-multi checked data-lat="50.11" data-lon="8.68" data-name="Frankfurt" data-live="2"></label>
<label><input type="checkbox" name="regions" value="r2" data-multi data-lat="40.71" data-lon="-74.0" data-name="New York" data-live="0"></label>
<label><input type="checkbox" name="regions" value="r3" data-multi data-name="Office" data-live="1"></label>
</div>
</form>
<div id="edit-map" data-region-map="edit" data-region-items="#region-list" data-map-server="stub" hidden style="width:300px;height:200px"></div>
<table><tbody id="region-list">
<tr data-lat="50.11" data-lon="8.68" data-name="Frankfurt" data-live="2"><td><button type="button" id="toggle" data-location-toggle="location-r1">set</button></td></tr>
<tr id="location-r1" hidden><td><form data-api="/org/regions/r1" data-method="patch" data-next="#saved" data-location-form id="row-form">
<input name="latitude" data-number data-nullable data-location-field="latitude" value="50.11">
<input name="longitude" data-number data-nullable data-location-field="longitude" value="8.68">
<button type="submit">save</button><button type="button" id="clear" data-location-clear>clear</button>
</form></td></tr>
</tbody></table>
<form data-api="/org/regions" id="create"><div data-location-form data-location-default>
<input name="latitude" data-number data-nullable data-location-field="latitude">
<input name="longitude" data-number data-nullable data-location-field="longitude">
</div></form>
<pre id="out"></pre>
<script>
const sent = [];
window.axios = { create: () => ({ interceptors: { request: { use() {} } }, request: async (cfg) => { sent.push(cfg); return { data: {} }; } }) };
const maps = [];
window.maplibregl = {
Map: class {
constructor(opts) { this.container = opts.container; this.handlers = {}; this.style = opts.style; maps.push(this); }
on(ev, fn) { (this.handlers[ev] ??= []).push(fn); }
fire(ev, arg) { for (const fn of this.handlers[ev] ?? []) fn(arg); }
addControl() {}
resize() {}
fitBounds(b) { this.bounds = b.points; }
jumpTo(o) { this.center = o.center; }
setStyle(s) { this.style = s; }
},
Marker: class {
constructor(o) { this.el = o.element; }
setLngLat(p) { this.at = p; this.el.dataset.at = p.join(","); return this; }
addTo(map) { map.container.append(this.el); return this; }
},
NavigationControl: class {},
LngLatBounds: class {
constructor(a) { this.points = [a]; }
extend(p) { this.points.push(p); return this; }
},
};
</script>
<script src="../assets/app.js"></script>
<script>
(async () => {
const out = [];
const check = (name, got, want) => {
const g = JSON.stringify(got), w = JSON.stringify(want);
out.push((g === w ? "PASS " : "FAIL ") + name + (g === w ? "" : " got=" + g + " want=" + w));
};
await new Promise((r) => setTimeout(r, 50));
const sel = document.getElementById("select-map");
const markers = () => [...sel.querySelectorAll(".region-marker")];
const box = (v) => document.querySelector(`[value="${v}"]`);
check("select map shown", sel.hidden, false);
check("light style", maps[0].style, "stub/style-light.json");
check("markers for located regions", markers().map((m) => m.title), ["Frankfurt", "New York"]);
check("fits all markers", maps[0].bounds, [[8.68, 50.11], [-74, 40.71]]);
check("checked marker filled", markers().map((m) => m.classList.contains("is-checked")), [true, false]);
check("idle region marked", markers()[1].dataset.live, "0");
markers()[1].click();
check("marker click ticks checkbox", box("r2").checked, true);
check("marker follows", markers()[1].classList.contains("is-checked"), true);
box("r1").click();
check("checkbox untick clears marker", markers()[0].classList.contains("is-checked"), false);
const edit = maps[1];
const click = (lat, lng) => edit.fire("click", { lngLat: { lat, lng, wrap() { return { lat, lng: lng > 180 ? lng - 360 : lng }; } } });
click(48.137154, 11.576124);
const create = document.getElementById("create");
check("map click fills create form", [create.elements.latitude.value, create.elements.longitude.value], ["48.1372", "11.5761"]);
check("pick marker shown", document.querySelector("#edit-map .is-pick")?.dataset.at, "11.576124,48.137154");
document.querySelector("#edit-map .region-marker:not(.is-pick)").click();
check("marker click opens its row", document.getElementById("location-r1").hidden, false);
click(52.52, 373.4);
const row = document.getElementById("row-form");
check("map click fills open row", [row.elements.latitude.value, row.elements.longitude.value], ["52.52", "13.4"]);
check("create form untouched", create.elements.latitude.value, "48.1372");
document.getElementById("clear").click();
await new Promise((r) => setTimeout(r, 50));
check("clear sends nulls", sent.at(-1) && [sent.at(-1).method, sent.at(-1).url, sent.at(-1).data], ["patch", "/org/regions/r1", { latitude: null, longitude: null }]);
document.getElementById("toggle").click();
check("toggle closes row", document.getElementById("location-r1").hidden, true);
click(1, 2);
check("closed row falls back to create form", create.elements.latitude.value, "1");
document.getElementById("out").textContent = out.join("\n");
})();
</script>
</body></html>
+15
View File
@@ -0,0 +1,15 @@
<!doctype html><html><body data-msg-network="net">
<div id="map" data-region-map="select" data-region-items="#items" data-map-server="./missing" hidden></div>
<div id="items"><input type="checkbox" data-lat="1" data-lon="2" data-name="x" data-live="1"></div>
<pre id="out"></pre>
<script>
window.axios = { create: () => ({ interceptors: { request: { use() {} } } }) };
</script>
<script src="../assets/app.js"></script>
<script>
setTimeout(() => {
const ok = document.getElementById("map").hidden && !document.querySelector(".region-marker");
document.getElementById("out").textContent = (ok ? "PASS" : "FAIL") + " map stays hidden when MapLibre cannot load";
}, 500);
</script>
</body></html>