Trip log: a trip is a library folder (Trips/<date> <name>) with the planned route and its stops; the car's GNSS recorded into its track there (10 m steps, written every minute and at the end), GET/POST/DELETE /api/trip/log; the Trip menu's Start / End and the line really driven (--route-driven); along-route POI mode for the plugins in TODO
CI / check (push) Successful in 21s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-12-01 02:25:20 +01:00
1 parent 809e88ab1a
commit 7d72e9eb63
7 files changed
+279 -1

No files matched your search

+59
View File
@@ -2,6 +2,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";
import { isLoggedIn } from "/js/auth.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
@@ -13,6 +14,8 @@ 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
const LOG_POLL_MS = 5000; // the trip log's state and new points
const DRIVEN_SOURCE = "trip-driven";
const escapeHtml = (text) => String(text).replace(/[&<>"]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);
@@ -66,6 +69,7 @@ export function setupDriving(map, nightButton, routePanel) {
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__log"><button type="button" data-action="log"></button><span class="drive-trip__log-state"></span></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">
@@ -169,6 +173,61 @@ export function setupDriving(map, nightButton, routePanel) {
});
tripPanel.querySelector('[data-action="start"]').addEventListener("click", () => routePanel?.startHere());
routePanel?.onStopsChanged(renderTrip);
// The trip log (lib/maptool/trip_log.rb): Start makes the trip's library
// folder and records the car's GNSS into it, End closes it; the line
// really driven drawn in --route-driven, grown by polling ?since=N.
const logButton = tripPanel.querySelector('[data-action="log"]');
const logState = tripPanel.querySelector(".drive-trip__log-state");
let log = null;
let driven = [];
const drawDriven = () => {
const data = { type: "Feature", geometry: { type: "LineString", coordinates: driven }, properties: {} };
const source = map.getSource(DRIVEN_SOURCE);
if (source) return source.setData(data);
if (!map.isStyleLoaded()) return;
map.addSource(DRIVEN_SOURCE, { type: "geojson", data });
map.addLayer({ id: DRIVEN_SOURCE, type: "line", source: DRIVEN_SOURCE, layout: { "line-cap": "round", "line-join": "round" },
paint: { "line-color": getComputedStyle(document.documentElement).getPropertyValue("--route-driven").trim() || "#c2410c", "line-width": 4 } });
};
map.on("style.load", () => driven.length && drawDriven());
const refreshLog = async () => {
try {
const response = await fetch(`/api/trip/log?since=${driven.length}`, { cache: "no-store" });
if (response.ok) {
log = await response.json();
if (!log.active) driven = [];
else driven = driven.concat(log.coordinates || []);
drawDriven();
}
} catch { /* offline for a moment: the next poll */ }
renderLog();
};
const pollLog = async () => {
await refreshLog();
setTimeout(pollLog, LOG_POLL_MS);
};
const renderLog = () => {
logButton.textContent = log?.active ? "End the trip" : "Start the trip (record it)";
logButton.classList.toggle("drive-trip__log--on", !!log?.active);
logState.textContent = log?.active ? `● ${log.folder.replace(/^Trips\//, "")} · ${(log.distance_m / 1000).toFixed(1)} km` : "";
};
logButton.addEventListener("click", async () => {
if (log?.active) {
if (!window.confirm(`End the trip "${log.folder}"? Its track stays in the library.`)) return;
await fetch("/api/trip/log", { method: "DELETE" });
} else {
const ends = routePanel?.stops().at(-1)?.name || "";
const name = window.prompt("The trip's name:", ends);
if (name === null) return;
const response = await fetch("/api/trip/log", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name }) });
if (!response.ok) window.alert((await response.json().catch(() => ({}))).error || `HTTP ${response.status}`);
}
driven = [];
refreshLog();
});
if (isLoggedIn()) pollLog();
else tripPanel.querySelector(".drive-trip__log").hidden = true;
window.addEventListener("resize", placeCrosshair);
map.getContainer().append(bar, recenter, tripPanel, crosshair);