Tracks and routes as containers of points: listed (collapsed) under them in the tree, added via a point's popup, a new note or the route planner's navigation pins, and written as course points into the FIT course
CI / check (push) Successful in 18s
CI / deploy (push) Successful in 18s

This commit is contained in:
randy committed 2026-11-18 12:39:04 +01:00
1 parent 6aa7b34f92
commit 4265421457
10 files changed
+354 -20

No files matched your search

+55 -5
View File
@@ -36,7 +36,7 @@ export class RoutePanelControl {
.then((r) => r.json())
.then((d) => d.available)
.catch(() => false);
this._points = []; // [{ lngLat: [lon, lat], name, marker }]
this._points = []; // [{ lngLat: [lon, lat], name, nav, marker }]; nav: saved as a point the device shows
this._route = null; // the last /api/route Feature
this._request = 0; // so a slow older response can't overwrite a newer one
this._editing = null; // { key, id, name } of a saved route being edited
@@ -111,11 +111,13 @@ export class RoutePanelControl {
// --- points ---------------------------------------------------------------
_addPoint(lngLat, name, { quiet = false } = {}) {
// A named place (a saved point, a search result) is shown in navigation
// by default; a plain map click only shapes the route.
_addPoint(lngLat, name, { quiet = false, nav = !!name && name !== "My position" } = {}) {
const element = document.createElement("div");
element.className = "route-marker";
const marker = new Marker({ element, draggable: true }).setLngLat(lngLat).addTo(this._map);
const point = { lngLat, name, marker };
const point = { lngLat, name, nav, marker };
marker.on("dragend", () => {
const { lng, lat } = marker.getLngLat();
point.lngLat = [lng, lat];
@@ -153,7 +155,8 @@ export class RoutePanelControl {
this._clear();
// Back to the vehicle it was planned for, if that profile still exists.
if (this._vehicles?.some((v) => v.id === feature.properties.vehicle?.id)) this._selectVehicle(feature.properties.vehicle.id);
feature.properties.via.forEach((lngLat) => this._addPoint(lngLat, null, { quiet: true }));
const named = feature.properties.via_points || [];
feature.properties.via.forEach((lngLat, i) => this._addPoint(lngLat, named[i]?.name || null, { quiet: true, nav: !!named[i]?.nav }));
this._editing = { key, id: feature.properties.id, name: feature.properties.name };
this._setOpen(true);
this._changed();
@@ -188,6 +191,7 @@ export class RoutePanelControl {
text.textContent = `${label} · ${point.name || `${point.lngLat[1].toFixed(5)}, ${point.lngLat[0].toFixed(5)}`}`;
li.append(
text,
this._navButton(point, label),
this._pointButton("↑", "Move up", index > 0, () => this._move(point, -1)),
this._pointButton("↓", "Move down", index < this._points.length - 1, () => this._move(point, 1)),
this._pointButton("×", "Remove", true, () => this._removePoint(point)),
@@ -202,6 +206,24 @@ export class RoutePanelControl {
this._saveButton.textContent = this._editing ? "Save changes" : "Save to library";
}
// Toggles whether the point is saved into the route as a navigation
// point (a course point on a Garmin); an unnamed one asks for a name.
_navButton(point, label) {
const button = this._pointButton("📍", "Show in navigation", true, () => {
if (!point.nav && !point.name) {
const name = window.prompt("Name shown in navigation:", label);
if (!name?.trim()) return;
point.name = name.trim();
}
point.nav = !point.nav;
this._renderPoints();
});
button.classList.add("route-panel__nav");
button.classList.toggle("active", !!point.nav);
button.setAttribute("aria-pressed", !!point.nav);
return button;
}
_pointButton(text, title, enabled, onClick) {
const button = document.createElement("button");
button.type = "button";
@@ -372,7 +394,14 @@ export class RoutePanelControl {
type: "Feature",
geometry: this._route.geometry,
// The elevation profile too, so the library shows it like a track's.
properties: { ...properties, ...this._profileProperties(), name: name.trim(), via: this._points.map((p) => p.lngLat), vehicle: this._vehicle },
properties: {
...properties,
...this._profileProperties(),
name: name.trim(),
via: this._points.map((p) => p.lngLat),
via_points: this._points.map((p) => ({ name: p.name || null, nav: !!p.nav })),
vehicle: this._vehicle,
},
};
const editing = this._editing;
const response = await fetch(NOTES_URL, {
@@ -393,12 +422,33 @@ export class RoutePanelControl {
return;
}
const path = editing ? editing.key : (await response.json()).path;
await this._saveNavPoints(path);
notifyGpxStoreChanged(path);
this._editing = { key: path, id: editing?.id ?? (await this._savedId(path)), name: name.trim() };
this._renderPoints();
this._summary.textContent += " · saved";
}
// The route's points marked 📍 as points inside the route item (marked
// from_route, so points added to it by hand stay untouched): missing
// ones added, unmarked or moved ones removed.
async _saveNavPoints(key) {
const url = `/api/library/node/${encodeURIComponent(key)}`;
const id = (p) => `${p.lngLat[0].toFixed(6)},${p.lngLat[1].toFixed(6)},${p.name}`;
const { features } = await (await fetch(url)).json();
const existing = new Map(features.filter((f) => f.geometry.type === "Point" && f.properties.from_route).map((f) => [id({ lngLat: f.geometry.coordinates, name: f.properties.name }), f]));
const wanted = this._points.map((p, i) => ({ ...p, name: p.name || `Via ${i}` })).filter((p) => p.nav);
const wantedIds = new Set(wanted.map(id));
for (const [key2, f] of existing) {
if (!wantedIds.has(key2)) await fetch(`/api/library/notes?${new URLSearchParams({ path: key, id: f.properties.id })}`, { method: "DELETE" });
}
for (const p of wanted) {
if (existing.has(id(p))) continue;
const feature = { type: "Feature", geometry: { type: "Point", coordinates: p.lngLat }, properties: { name: p.name, from_route: true } };
await fetch(`${url}/points`, { method: "POST", body: new URLSearchParams({ feature: JSON.stringify(feature) }) });
}
}
// The id of the route just saved, so a second Save updates it instead of
// adding another copy.
async _savedId(key) {