Library phase 2: rename/recolor nodes, rename and move folders, split a layer into items; offline routing design

This commit is contained in:
randy committed 2026-11-13 21:16:35 +01:00
1 parent 4d838395b8
commit 6f5f11cb4d
9 files changed
+467 -39

No files matched your search

+129 -29
View File
@@ -30,6 +30,7 @@ const BADGE_IMAGE_PREFIX = "badge-"; // one registered image per distinct badge
// folders, as opposed to an OS file drag (type "Files") for uploading a new
// one — both land on the same drop targets, told apart by this.
const MOVE_MIME = "application/x-gpx-path";
const FOLDER_DRAG_PREFIX = "folder:"; // a dragged folder's MOVE_MIME payload, vs. a node key
// A sidebar icon that opens a side panel for uploading GPX files (via
// drag & drop, stored server-side in a folder hierarchy the user can add to
@@ -278,7 +279,16 @@ export class GpxPanelControl {
const name = document.createElement("span");
name.textContent = folder.name;
row.appendChild(name);
if (isLoggedIn()) row.appendChild(this._deleteButton(() => this._deleteFolder(folder.path)));
if (isLoggedIn()) {
row.appendChild(this._menuButton("⋯", "Edit", [{ label: "Rename…", run: () => this._renameFolder(folder) }]));
row.appendChild(this._deleteButton(() => this._deleteFolder(folder.path)));
// Dragged onto another folder (or the tree's blank space) moves it.
summary.draggable = true;
summary.addEventListener("dragstart", (event) => {
event.dataTransfer.setData(MOVE_MIME, `${FOLDER_DRAG_PREFIX}${folder.path}`);
event.dataTransfer.effectAllowed = "move";
});
}
summary.appendChild(row);
this._wireFolderDropTarget(summary, folder.path);
@@ -314,6 +324,8 @@ export class GpxPanelControl {
// rather than shown-but-failing-on-click.
if (loggedIn) {
row.appendChild(this._downloadButton(file));
// A folder's "Points (N)" node has no name or color of its own.
if (file.path.startsWith("item:")) row.appendChild(this._editButton(file));
row.appendChild(this._deleteButton(() => this._deleteFile(file)));
}
@@ -338,35 +350,60 @@ export class GpxPanelControl {
// One button, one obvious place for both download kinds — clicking it
// opens a small menu (Original / GeoJSON) instead of splitting them
// across two differently-shaped click targets (the file name used to be
// a GeoJSON-only download link; easy to miss it was a link at all, and
// it only ever covered half the story). Costs a click for the common
// case of downloading several files in a row, in exchange for both
// formats actually being reachable from one, discoverable place.
// across two differently-shaped click targets.
_downloadButton(file) {
const geojsonUrl = `${gpxFileUrl(file.path)}?${new URLSearchParams({ download: "geojson", name: file.name })}`;
return this._menuButton(DOWNLOAD_ICON, "Download", [
// A folder's loose points have no single file they came from.
file.original && { label: "Original file", run: () => this._download(`${gpxFileUrl(file.path)}?download=original`, file.name) },
{ label: "GeoJSON", run: () => this._download(geojsonUrl, `${file.name}.geojson`) },
]);
}
_editButton(file) {
return this._menuButton("⋯", "Edit", [
{ label: "Rename…", run: () => this._renameFile(file) },
{ label: "Color…", run: () => this._pickColor(file) },
file.color && { label: "Default color", run: () => this._patchNode(file.path, { color: "" }) },
file.layer && { label: "Split into items", run: () => this._splitLayer(file) },
]);
}
// A button opening a small menu of `entries` ({ label, run }, falsy ones
// skipped).
_menuButton(content, title, entries) {
const wrapper = document.createElement("span");
wrapper.className = "gpx-tree__download-wrapper";
const button = document.createElement("button");
button.type = "button";
button.className = "gpx-tree__download";
button.title = "Download";
button.setAttribute("aria-label", "Download");
button.innerHTML = DOWNLOAD_ICON;
button.title = title;
button.setAttribute("aria-label", title);
button.innerHTML = content;
const menu = document.createElement("div");
menu.className = "gpx-tree__download-menu";
menu.hidden = true;
// A folder's loose points have no single file they came from.
if (file.original) menu.appendChild(this._downloadMenuItem("Original file", `${gpxFileUrl(file.path)}?download=original`, file.name));
const geojsonUrl = `${gpxFileUrl(file.path)}?${new URLSearchParams({ download: "geojson", name: file.name })}`;
menu.appendChild(this._downloadMenuItem("GeoJSON", geojsonUrl, `${file.name}.geojson`));
entries.filter(Boolean).forEach(({ label, run }) => {
const item = document.createElement("button");
item.type = "button";
item.className = "gpx-tree__download-menu-item";
item.textContent = label;
item.addEventListener("click", (event) => {
event.preventDefault();
event.stopPropagation();
this._closeDownloadMenu();
run();
});
menu.appendChild(item);
});
// stopPropagation so this click doesn't immediately reach the
// document-level listener (mount()) that closes whichever menu is
// open on an outside click — otherwise it would close right back up
// the instant it opened.
// open on an outside click, nor toggle a folder's <details>.
button.addEventListener("click", (event) => {
event.preventDefault();
event.stopPropagation();
this._toggleDownloadMenu(menu);
});
@@ -376,19 +413,6 @@ export class GpxPanelControl {
return wrapper;
}
_downloadMenuItem(label, url, filename) {
const item = document.createElement("a");
item.className = "gpx-tree__download-menu-item";
item.href = url;
item.textContent = label;
item.addEventListener("click", (event) => {
event.preventDefault();
this._closeDownloadMenu();
this._download(url, filename);
});
return item;
}
// Only one menu open at a time — opening a second one closes whichever
// was already open, same as onlyOneSidePanelOpen does for the sidebar's
// own panels (panels.js), just scoped to this one control instead of
@@ -557,7 +581,7 @@ export class GpxPanelControl {
source: linesId,
filter: ["any", ["!has", "category"], ["!=", ["get", "category"], "Note"]],
layout: { "line-cap": "round", "line-join": "round" },
paint: { "line-color": TRACK_COLOR, "line-width": 3 },
paint: { "line-color": ["coalesce", ["get", "color"], TRACK_COLOR], "line-width": 3 },
});
this._registerLineInteractivity(`${linesId}-line`);
@@ -1025,6 +1049,10 @@ export class GpxPanelControl {
}
async _moveFile(from, toFolder) {
if (from.startsWith(FOLDER_DRAG_PREFIX)) {
await this._patchFolder(from.slice(FOLDER_DRAG_PREFIX.length), { to_folder: toFolder });
return;
}
const response = await fetch(MOVE_URL, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
@@ -1043,6 +1071,78 @@ export class GpxPanelControl {
this._renderTree(tree);
}
async _renameFolder(folder) {
const name = window.prompt("Rename folder:", folder.name);
if (name && name.trim() && name.trim() !== folder.name) await this._patchFolder(folder.path, { name: name.trim() });
}
// Node keys are ids, so nothing shown on the map changes with its folder.
async _patchFolder(path, fields) {
const response = await fetch(`${FOLDER_URL}/${path.split("/").map(encodeURIComponent).join("/")}`, {
method: "PATCH",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams(fields),
});
if (redirectIfUnauthorized(response)) return;
if (response.status === 409) window.alert("A folder with that name already exists there.");
if (!response.ok) return;
this._renderTree((await response.json()).tree);
}
async _renameFile(file) {
const name = window.prompt("Rename:", file.name);
if (name && name.trim() && name.trim() !== file.name) await this._patchNode(file.path, { name: name.trim() });
}
// The native color picker; one hidden input reused for every node.
_pickColor(file) {
if (!this._colorInput) {
this._colorInput = document.createElement("input");
this._colorInput.type = "color";
this._colorInput.hidden = true;
this._panel.appendChild(this._colorInput);
this._colorInput.addEventListener("change", () => this._patchNode(this._colorInput.dataset.path, { color: this._colorInput.value }));
}
this._colorInput.dataset.path = file.path;
this._colorInput.value = file.color || TRACK_COLOR;
this._colorInput.click();
}
async _patchNode(path, fields) {
const response = await fetch(gpxFileUrl(path), {
method: "PATCH",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams(fields),
});
if (redirectIfUnauthorized(response)) return;
if (!response.ok) return;
this._renderTree((await response.json()).tree);
if (this._visible.has(path)) {
this._removeLayers(path);
await this._addLayers(path);
}
}
async _splitLayer(file) {
if (!window.confirm(`Split "${file.name}" into separate points and tracks? This can't be undone.`)) return;
this._setStatus("Splitting…");
const response = await fetch(`${gpxFileUrl(file.path)}/split`, { method: "POST" });
if (redirectIfUnauthorized(response)) return;
if (!response.ok) {
this._setStatus("Split failed.");
return;
}
const { paths, tree } = await response.json();
this._renderTree(tree);
this._setStatus("Split.");
if (this._visible.has(file.path)) {
this._visible.delete(file.path);
this._removeLayers(file.path);
await Promise.all(paths.map((path) => this.show(path)));
}
}
_setStatus(text) {
this._uploadStatus.hidden = false;
this._uploadStatus.textContent = text;