Driving menu Trip: the stops as big rows (look, ✓ passed, ↑ ↓, ✕), a stop at the crosshair, the start at the car, search of saved points and offline places; each leg's km and time (legs_s) and the total, in the route panel too; passed stops kept (not routed, dimmed, editable) — a reroute marks them instead of dropping them; /status.html: every status endpoint full width (the debug panel links it); the trip-as-plan-and-log model and planned-vs-driven colors in docs/TODO
CI / check (push) Successful in 22s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-12-01 02:21:14 +01:00
1 parent 0eb9d05cdc
commit 809e88ab1a
14 files changed
+517 -101

No files matched your search

+120 -2
View File
@@ -1,6 +1,7 @@
import { LngLatBounds } from "/vendor/maplibre-gl/maplibre-gl.mjs";
import { requestGnssFollow, onRouteChanged } from "/js/panels.js";
import { pauseFollow, resumeFollow, isFollowing } from "/js/gnss.js";
import { formatDuration } from "/js/route.js";
// Driving mode: the few things needed on the road as big buttons, the
// planning sidebar and panels out of the way. After the research in
@@ -11,8 +12,11 @@ import { pauseFollow, resumeFollow, isFollowing } from "/js/gnss.js";
const KEY = "drivingMode";
const PITCH_3D = 60; // as map.js's 3D toggle
const CHECK_MS = 1000; // how often the recenter button looks whether the map follows
const SEARCH_DELAY_MS = 300; // after the last key
export function setupDriving(map, nightButton) {
const escapeHtml = (text) => String(text).replace(/[&<>"]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);
export function setupDriving(map, nightButton, routePanel) {
let route = onRouteChanged((r) => (route = r));
const toggle = document.createElement("button");
toggle.type = "button";
@@ -38,6 +42,7 @@ export function setupDriving(map, nightButton) {
button("zoomIn", "+", "Zoom in", () => map.zoomIn()),
button("zoomOut", "−", "Zoom out", () => map.zoomOut()),
button("pitch", "3D", "2D / 3D", () => map.easeTo({ pitch: map.getPitch() > 0 ? 0 : PITCH_3D, ...(map.getPitch() > 0 && { bearing: 0 }), duration: 300 })),
button("trip", "Trip", "The trip's stops: add, move, remove", () => openTrip(tripPanel.hidden)),
button("overview", "Route", "The whole route (the follow pauses)", () => {
const coords = route?.geometry?.coordinates;
if (!coords?.length) return;
@@ -53,7 +58,119 @@ export function setupDriving(map, nightButton) {
recenter.hidden = true;
});
recenter.hidden = true;
map.getContainer().append(bar, recenter);
// The Trip menu (the navigator's, docs/design.adoc): the stops as big
// rows — tap a name to look at it, ↑ ↓ to reorder, ✕ to drop — and big
// actions: a stop where the crosshair is (pan the map there first), the
// start moved to the car. One level, no dragging, no typing.
const tripPanel = document.createElement("div");
tripPanel.className = "drive-trip";
tripPanel.hidden = true;
tripPanel.innerHTML = `<div class="drive-trip__head"><span>Trip <small class="drive-trip__total"></small></span><button type="button" class="drive-trip__close" aria-label="Close">✕</button></div>
<div class="drive-trip__search"><input type="search" placeholder="Search saved points and places…" enterkeyhint="search"></div>
<ol class="drive-trip__stops"></ol>
<div class="drive-trip__actions">
<button type="button" data-action="add">+ Stop at the crosshair</button>
<button type="button" data-action="start">Start from here (the car)</button>
</div>`;
const crosshair = document.createElement("div");
crosshair.className = "drive-crosshair";
crosshair.hidden = true;
const visibleCenter = () => {
const { clientWidth: w, clientHeight: h } = map.getContainer();
return [(w - tripPanel.offsetWidth) / 2, h / 2];
};
const placeCrosshair = () => {
const [x, y] = visibleCenter();
crosshair.style.left = `${x}px`;
crosshair.style.top = `${y}px`;
};
// Search: saved points (the library) and places (the offline POI
// database), nearest the crosshair first; a result's name shows it, + adds
// it as a stop. While there's a query the list shows results, not stops.
const input = tripPanel.querySelector(".drive-trip__search input");
let results = null;
let searchTimer = null;
let searchSeq = 0;
input.addEventListener("input", () => {
clearTimeout(searchTimer);
if (!input.value.trim()) {
results = null;
return renderTrip();
}
searchTimer = setTimeout(async () => {
const mine = ++searchSeq;
const { lng, lat } = map.unproject(visibleCenter());
const url = new URL("/api/search", window.location.origin);
url.search = new URLSearchParams({ q: input.value, sources: "layers,places", lat, lng });
let found = [];
try { found = (await (await fetch(url)).json()).results; } catch { /* offline for a moment: nothing */ }
if (mine !== searchSeq) return;
results = found.filter(({ bbox }) => bbox).map(({ name, bbox, source_label: where }) => ({ name, where, lngLat: [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2] }));
renderTrip();
}, SEARCH_DELAY_MS);
});
const lookAt = (lngLat) => {
pauseFollow(map);
map.easeTo({ center: lngLat, zoom: Math.max(map.getZoom(), 14), padding: { right: tripPanel.offsetWidth } });
};
const renderResults = (list) => {
list.replaceChildren(...results.map((r) => {
const li = document.createElement("li");
const name = button("stop-name", r.name, "Show it on the map", () => lookAt(r.lngLat));
name.innerHTML = `${escapeHtml(r.name)}<small>${escapeHtml(r.where || "")}</small>`;
const add = button("stop-add", "+", "Add as a stop", () => {
input.value = "";
results = null; // back to the stops, the new one among them
routePanel.addStop(r.lngLat, r.name);
});
li.append(name, add);
return li;
}));
if (!results.length) list.innerHTML = `<li class="drive-trip__empty">Nothing found.</li>`;
};
const renderTrip = () => {
if (tripPanel.hidden || !routePanel) return;
if (results) return renderResults(tripPanel.querySelector(".drive-trip__stops"));
const stops = routePanel.stops();
const list = tripPanel.querySelector(".drive-trip__stops");
list.replaceChildren(...stops.map((stop, i) => {
const li = document.createElement("li");
const name = button("stop-name", "", "Show it on the map", () => lookAt(stop.lngLat));
const leg = stop.leg ? `<small>+${stop.leg.km.toFixed(1)} km${stop.leg.s != null ? ` · ${formatDuration(stop.leg.s)}` : ""}</small>` : "";
name.innerHTML = `${escapeHtml(stop.label)} · ${escapeHtml(stop.name || `${stop.lngLat[1].toFixed(4)}, ${stop.lngLat[0].toFixed(4)}`)}${leg}`;
const done = button("stop-done", "✓", stop.passed ? "Passed — tap to route through it again" : "Mark as passed (not routed any more)", () => routePanel.togglePassed(i));
done.classList.toggle("active", !!stop.passed);
li.classList.toggle("drive-trip__passed", !!stop.passed || stop.behind);
const up = button("stop-up", "↑", "Earlier", () => routePanel.moveStop(i, -1));
const down = button("stop-down", "↓", "Later", () => routePanel.moveStop(i, 1));
const drop = button("stop-drop", "✕", "Remove this stop", () => routePanel.removeStop(i));
up.disabled = i === 0;
down.disabled = i === stops.length - 1;
li.append(name, done, up, down, drop);
return li;
}));
if (!stops.length) list.innerHTML = `<li class="drive-trip__empty">No stops yet — pan the map to a place and add it.</li>`;
const p = route?.properties;
tripPanel.querySelector(".drive-trip__total").textContent = p && stops.length > 1 ? `${p.distance_km.toFixed(1)} km · ${formatDuration(p.time_s)}` : "";
};
function openTrip(open) {
tripPanel.hidden = !open;
crosshair.hidden = !open;
bar.querySelector(".drive-bar__trip").classList.toggle("active", open);
if (open) {
renderTrip();
placeCrosshair();
}
}
tripPanel.querySelector(".drive-trip__close").addEventListener("click", () => openTrip(false));
tripPanel.querySelector('[data-action="add"]').addEventListener("click", () => {
const { lng, lat } = map.unproject(visibleCenter());
routePanel?.addStop([lng, lat]);
});
tripPanel.querySelector('[data-action="start"]').addEventListener("click", () => routePanel?.startHere());
routePanel?.onStopsChanged(renderTrip);
window.addEventListener("resize", placeCrosshair);
map.getContainer().append(bar, recenter, tripPanel, crosshair);
const render = () => {
bar.querySelector(".drive-bar__pitch").textContent = map.getPitch() > 0 ? "2D" : "3D";
@@ -65,6 +182,7 @@ export function setupDriving(map, nightButton) {
setInterval(render, CHECK_MS);
function set(on) {
if (!on) openTrip(false);
document.documentElement.classList.toggle("driving", on);
bar.hidden = !on;
toggle.classList.toggle("active", on);