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
This commit is contained in:
1 parent
809e88ab1a
commit
7d72e9eb63
7 files changed
+279
-1
No files matched your search
@@ -30,6 +30,7 @@
|
||||
--inst-active: #a21caf; /* magenta: the route / waypoint / lane being followed */
|
||||
--inst-selected: #00799a; /* cyan: a value set by hand */
|
||||
--route-planned: #1d6fdc;
|
||||
--route-driven: #c2410c; /* the recorded trip log: really driven */
|
||||
--route-active: var(--inst-active);
|
||||
/* road signs: still the sign (white disc, red ring), dimmed by night */
|
||||
--sign-bg: #ffffff;
|
||||
@@ -57,6 +58,7 @@
|
||||
--inst-active: #cc45e0;
|
||||
--inst-selected: #12bedf;
|
||||
--route-planned: #3f86e0;
|
||||
--route-driven: #ea7a3c; /* the recorded trip log: really driven */
|
||||
--sign-bg: #e4e1d6;
|
||||
--sign-ring: #c42b26;
|
||||
}
|
||||
@@ -87,6 +89,7 @@
|
||||
--sign-bg: #6b675e;
|
||||
--sign-ring: #8a231b;
|
||||
--sign-fg: #0c0c0b;
|
||||
--route-driven: #9a5a32;
|
||||
}
|
||||
|
||||
/* The palette follows the night mode (map.js sets data-palette); the
|
||||
@@ -3683,3 +3686,18 @@ html.follow-3d .maplibregl-ctrl-overview-map { display: none; }
|
||||
.drive-trip__stops .drive-bar__stop-done { width: 68px; font-size: 24px; border-left: 1px solid var(--ui-line); color: var(--ui-muted); }
|
||||
.drive-trip__stops .drive-bar__stop-done.active { color: var(--inst-ok); font-weight: 700; }
|
||||
.route-marker--passed { opacity: 0.45; }
|
||||
.drive-trip__log { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-bottom: 1px solid var(--ui-line); }
|
||||
.drive-trip__log[hidden] { display: none; }
|
||||
.drive-trip__log button {
|
||||
min-height: 60px;
|
||||
padding: 0 18px;
|
||||
border: 2px solid var(--route-driven);
|
||||
border-radius: 10px;
|
||||
background: none;
|
||||
color: var(--ui-fg);
|
||||
font-size: 18px;
|
||||
font-weight: 700;
|
||||
cursor: pointer;
|
||||
}
|
||||
.drive-trip__log button.drive-trip__log--on { background: var(--route-driven); color: #fff; }
|
||||
.drive-trip__log-state { flex: 1; color: var(--route-driven); font-weight: 600; font-size: 15px; }
|
||||
@@ -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) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[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);
|
||||
|
||||
|
||||
Reference in new issue
Block a user