Files
maptool/public/js/driving.js
T

75 lines
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { LngLatBounds } from "/vendor/maplibre-gl/maplibre-gl.mjs";
import { requestGnssFollow, onRouteChanged } from "/js/panels.js";
import { pauseFollow, resumeFollow } from "/js/gnss.js";
// Driving mode: the few things needed on the road as big buttons down the
// side of the map, the planning sidebar and panels out of the way (TODO:
// tiny icons can't be hit while driving). Per device (localStorage).
const KEY = "drivingMode";
const PITCH_3D = 60; // as map.js's 3D toggle
export function setupDriving(map, nightButton) {
let route = onRouteChanged((r) => (route = r));
const toggle = document.createElement("button");
toggle.type = "button";
toggle.className = "topbar__drive";
toggle.title = "Driving mode: big buttons, no planning panels";
toggle.textContent = "Drive";
nightButton.before(toggle);
const bar = document.createElement("div");
bar.className = "drive-bar";
bar.hidden = true;
const buttons = {
follow: ["Follow", "Follow the position, heading up in 3D", () => {
requestGnssFollow();
resumeFollow(map);
if (map.getPitch() === 0) map.easeTo({ pitch: PITCH_3D, duration: 300 });
}],
zoomIn: ["+", "Zoom in", () => map.zoomIn()],
zoomOut: ["−", "Zoom out", () => map.zoomOut()],
pitch: ["2D", "2D / 3D", () => map.easeTo({ pitch: map.getPitch() > 0 ? 0 : PITCH_3D, ...(map.getPitch() > 0 && { bearing: 0 }), duration: 300 })],
night: ["", "Night mode", () => nightButton.click()],
overview: ["Route", "The whole route (the follow pauses)", () => {
const coords = route?.geometry?.coordinates;
if (!coords?.length) return;
pauseFollow(map);
map.fitBounds(coords.reduce((b, c) => b.extend(c), new LngLatBounds(coords[0], coords[0])), { padding: 80, pitch: 0, bearing: 0 });
}],
exit: ["✕", "Leave driving mode", () => set(false)],
};
for (const [name, [label, title, action]] of Object.entries(buttons)) {
const button = document.createElement("button");
button.type = "button";
button.className = `drive-bar__${name}`;
button.textContent = label;
button.title = title;
button.setAttribute("aria-label", title);
button.addEventListener("click", action);
bar.appendChild(button);
}
map.getContainer().appendChild(bar);
const render = () => {
bar.querySelector(".drive-bar__pitch").textContent = map.getPitch() > 0 ? "2D" : "3D";
bar.querySelector(".drive-bar__night").textContent = nightButton.textContent;
bar.querySelector(".drive-bar__overview").disabled = !route;
};
map.on("pitchend", render);
nightButton.addEventListener("click", render);
onRouteChanged(render);
function set(on) {
document.documentElement.classList.toggle("driving", on);
bar.hidden = !on;
toggle.classList.toggle("active", on);
try { localStorage.setItem(KEY, on ? "on" : "off"); } catch { /* private window: not kept */ }
render();
map.resize(); // the sidebar came or went
}
toggle.addEventListener("click", () => set(bar.hidden));
let saved = null;
try { saved = localStorage.getItem(KEY); } catch { /* nothing kept */ }
set(saved === "on");
}