Vehicle profiles (Car, Camper, ...), avoid tolls/motorways, per-country vignette and toll km for a route
This commit is contained in:
1 parent
58ca871662
commit
69aa284e36
8 files changed
+328
-80
No files matched your search
+109
-21
@@ -10,7 +10,12 @@ const ISOCHRONE_URL = "/api/isochrone";
|
||||
const REACH_SOURCE = "route-reach";
|
||||
const PROFILE_SAMPLES = 400; // elevation lookups per route, at least 50 m apart
|
||||
const GRADE_WINDOW_M = 500; // "max grade" is sustained over this distance, not one noisy pixel
|
||||
const VEHICLE_URL = "/api/vehicle";
|
||||
const VEHICLES_URL = "/api/vehicles";
|
||||
const VEHICLE_KEY = "maptool.routeVehicle";
|
||||
const AVOID_KEY = "maptool.routeAvoid";
|
||||
// Motorways paid by a time-based vignette rather than per section. Not
|
||||
// reliably tagged as toll in OSM, so every motorway km there counts.
|
||||
const VIGNETTE_COUNTRIES = new Set(["AT", "BG", "CH", "CZ", "HU", "MD", "RO", "SI", "SK"]);
|
||||
const NOTES_URL = "/api/library/notes";
|
||||
const SOURCE = "route";
|
||||
const COLOR = "#1d6fdc";
|
||||
@@ -62,6 +67,8 @@ export class RoutePanelControl {
|
||||
this._list = this._panel.querySelector(".route-panel__points");
|
||||
this._summary = this._panel.querySelector(".route-panel__summary");
|
||||
this._directions = this._panel.querySelector(".route-panel__directions");
|
||||
this._countries = this._panel.querySelector(".route-panel__countries");
|
||||
this._setupAvoid();
|
||||
this._saveButton = this._panel.querySelector('[data-action="save"]');
|
||||
this._panel.querySelector('[data-action="reverse"]').addEventListener("click", () => {
|
||||
this._points.reverse();
|
||||
@@ -141,6 +148,8 @@ export class RoutePanelControl {
|
||||
// A saved route's points back in the panel; Save then updates that item.
|
||||
_edit(key, feature) {
|
||||
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 }));
|
||||
this._editing = { key, id: feature.properties.id, name: feature.properties.name };
|
||||
this._setOpen(true);
|
||||
@@ -219,7 +228,7 @@ export class RoutePanelControl {
|
||||
const response = await fetch(ROUTE_URL, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ locations: this._points.map((p) => p.lngLat) }),
|
||||
body: JSON.stringify({ locations: this._points.map((p) => p.lngLat), vehicle: this._vehicleId, avoid: this._avoid }),
|
||||
});
|
||||
data = await response.json().catch(() => ({}));
|
||||
if (!response.ok) error = data.error || `routing failed (${response.status})`;
|
||||
@@ -237,6 +246,7 @@ export class RoutePanelControl {
|
||||
this._saveButton.hidden = !route || !isLoggedIn();
|
||||
this._directions.hidden = !route;
|
||||
this._profileButton.hidden = true;
|
||||
this._showCountries(route?.properties.countries);
|
||||
if (!route) {
|
||||
this._summary.textContent = message;
|
||||
return;
|
||||
@@ -287,7 +297,7 @@ export class RoutePanelControl {
|
||||
const response = await fetch(ISOCHRONE_URL, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ center, minutes }),
|
||||
body: JSON.stringify({ center, minutes, vehicle: this._vehicleId, avoid: this._avoid }),
|
||||
});
|
||||
const data = await response.json().catch(() => ({}));
|
||||
if (!response.ok) throw new Error(data.error || `failed (${response.status})`);
|
||||
@@ -392,31 +402,109 @@ export class RoutePanelControl {
|
||||
return features.find((f) => f.geometry.type === "LineString")?.properties.id;
|
||||
}
|
||||
|
||||
// Where the route needs a vignette or pays tolls, country by country.
|
||||
_showCountries(countries) {
|
||||
const rows = (countries || []).flatMap(({ code, motorway_km: motorway, toll_km: toll }) => {
|
||||
if (VIGNETTE_COUNTRIES.has(code) && motorway > 0) {
|
||||
return [`${code}: vignette, ${motorway.toFixed(0)} km motorway${toll > 0 ? ` (${toll.toFixed(0)} km tolled)` : ""}`];
|
||||
}
|
||||
return toll > 0 ? [`${code}: toll, ${toll.toFixed(0)} km`] : [];
|
||||
});
|
||||
this._countries.replaceChildren(...rows.map((text) => Object.assign(document.createElement("li"), { textContent: text })));
|
||||
this._countries.hidden = rows.length === 0;
|
||||
}
|
||||
|
||||
// Avoid tolls / motorways for the whole route; this browser's choice.
|
||||
_setupAvoid() {
|
||||
try {
|
||||
this._avoid = JSON.parse(localStorage.getItem(AVOID_KEY)) || {};
|
||||
} catch {
|
||||
this._avoid = {};
|
||||
}
|
||||
this._panel.querySelectorAll(".route-panel__avoid input").forEach((input) => {
|
||||
input.checked = Boolean(this._avoid[input.name]);
|
||||
input.addEventListener("change", () => {
|
||||
this._avoid[input.name] = input.checked;
|
||||
try {
|
||||
localStorage.setItem(AVOID_KEY, JSON.stringify(this._avoid));
|
||||
} catch {}
|
||||
this._fetchRoute();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// --- vehicle --------------------------------------------------------------
|
||||
|
||||
// Readable by anyone, saved only when logged in; a change re-routes.
|
||||
// Profiles (Car, Camper, ...) are shared and edited only when logged in;
|
||||
// which one is in use is this browser's own choice. A change re-routes.
|
||||
async _setupVehicle() {
|
||||
const inputs = [...this._panel.querySelectorAll(".route-panel__vehicle input")];
|
||||
const select = this._panel.querySelector(".route-panel__vehicle-select");
|
||||
const editButton = this._panel.querySelector('[data-action="vehicle-edit"]');
|
||||
const fieldset = this._panel.querySelector(".route-panel__vehicle");
|
||||
const inputs = [...fieldset.querySelectorAll("input")];
|
||||
try {
|
||||
this._vehicle = await (await fetch(VEHICLE_URL)).json();
|
||||
this._vehicles = await (await fetch(VEHICLES_URL)).json();
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
inputs.forEach((input) => {
|
||||
input.value = this._vehicle[input.name];
|
||||
input.disabled = !isLoggedIn();
|
||||
input.addEventListener("change", async () => {
|
||||
const body = new URLSearchParams(inputs.map((i) => [i.name, i.value]));
|
||||
const response = await fetch(VEHICLE_URL, { method: "PUT", body });
|
||||
const data = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
window.alert(data.error || "Could not save the vehicle.");
|
||||
input.value = this._vehicle[input.name];
|
||||
return;
|
||||
}
|
||||
this._vehicle = data;
|
||||
this._fetchRoute();
|
||||
});
|
||||
const current = () => this._vehicles.find((v) => v.id === this._vehicleId) || this._vehicles[0];
|
||||
const fill = () => {
|
||||
select.replaceChildren(...this._vehicles.map((v) => new Option(v.name, v.id)));
|
||||
this._vehicle = current();
|
||||
this._vehicleId = select.value = this._vehicle.id;
|
||||
inputs.forEach((input) => (input.value = this._vehicle[input.name] ?? ""));
|
||||
};
|
||||
this._selectVehicle = (id) => {
|
||||
this._vehicleId = id;
|
||||
try {
|
||||
localStorage.setItem(VEHICLE_KEY, id);
|
||||
} catch {}
|
||||
fill();
|
||||
};
|
||||
const save = async () => {
|
||||
const response = await fetch(VEHICLES_URL, { method: "PUT", body: JSON.stringify(this._vehicles) });
|
||||
const data = await response.json().catch(() => ({}));
|
||||
if (response.ok) {
|
||||
this._vehicles = data;
|
||||
} else {
|
||||
window.alert(data.error || "Could not save the vehicles.");
|
||||
this._vehicles = await (await fetch(VEHICLES_URL)).json();
|
||||
}
|
||||
fill();
|
||||
this._fetchRoute();
|
||||
};
|
||||
|
||||
try {
|
||||
this._vehicleId = localStorage.getItem(VEHICLE_KEY);
|
||||
} catch {}
|
||||
fill();
|
||||
select.addEventListener("change", () => {
|
||||
this._selectVehicle(select.value);
|
||||
this._fetchRoute();
|
||||
});
|
||||
|
||||
editButton.hidden = !isLoggedIn();
|
||||
editButton.addEventListener("click", () => (fieldset.hidden = !fieldset.hidden));
|
||||
inputs.forEach((input) =>
|
||||
input.addEventListener("change", () => {
|
||||
const value = input.value.trim();
|
||||
if (input.name === "name") current().name = value;
|
||||
else if (value === "") delete current()[input.name];
|
||||
else current()[input.name] = value;
|
||||
save();
|
||||
}),
|
||||
);
|
||||
fieldset.querySelector('[data-action="vehicle-add"]').addEventListener("click", () => {
|
||||
const id = `v${Date.now().toString(36)}`;
|
||||
this._vehicles.push({ ...current(), id, name: "New vehicle" });
|
||||
this._selectVehicle(id);
|
||||
save();
|
||||
});
|
||||
fieldset.querySelector('[data-action="vehicle-delete"]').addEventListener("click", () => {
|
||||
if (this._vehicles.length < 2 || !window.confirm(`Delete the vehicle "${current().name}"?`)) return;
|
||||
this._vehicles = this._vehicles.filter((v) => v !== current());
|
||||
this._selectVehicle(this._vehicles[0].id);
|
||||
save();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user