149 lines
7.5 KiB
JavaScript
149 lines
7.5 KiB
JavaScript
import { PLUGIN_ICON } from "/js/icons.js";
|
|
import { notifyGpxStoreChanged, notifySidePanelOpen, onlyOneSidePanelOpen } from "/js/panels.js";
|
|
|
|
const CORRIDORS_KM = [1, 2, 5, 10, 20];
|
|
const CORRIDOR_KEY = "maptool.pluginCorridorKm";
|
|
|
|
// A side panel (#plugins-panel) listing whatever import plugins are on disk (see
|
|
// lib/maptool/plugins.rb for the contract) — each one just a "Run for
|
|
// this area" button using the current map viewport as the bbox, so there's
|
|
// no separate area-picker to build: pan/zoom to where you want, click Run.
|
|
// Only ever mounted when logged in (see map.js) — same "hide the
|
|
// write-only affordance" story as NotesControl, since the only thing this
|
|
// panel does is a login-gated write.
|
|
export class PluginsControl {
|
|
constructor(map) {
|
|
this._map = map;
|
|
}
|
|
|
|
mount(sidebar) {
|
|
this._container = document.createElement("div");
|
|
this._container.className = "sidebar-item";
|
|
this._container.innerHTML = `<button type="button" class="sidebar-icon" title="Import plugins" aria-label="Import plugins">${PLUGIN_ICON}</button>`;
|
|
this._icon = this._container.querySelector(".sidebar-icon");
|
|
this._icon.addEventListener("click", () => this._togglePanel());
|
|
|
|
this._panel = document.getElementById("plugins-panel");
|
|
this._list = this._panel.querySelector(".plugins-panel__list");
|
|
this._panel.querySelector(".basemap-panel__close").addEventListener("click", () => this._togglePanel(false));
|
|
onlyOneSidePanelOpen(this._panel, () => this._icon.classList.remove("active"));
|
|
|
|
sidebar.appendChild(this._container);
|
|
}
|
|
|
|
_togglePanel(force) {
|
|
const open = force === undefined ? this._panel.hidden : force;
|
|
this._panel.hidden = !open;
|
|
this._icon.classList.toggle("active", open);
|
|
if (open) {
|
|
notifySidePanelOpen(this._panel);
|
|
this._loadPlugins();
|
|
}
|
|
}
|
|
|
|
async _loadPlugins() {
|
|
this._list.innerHTML = `<li class="plugins-panel__empty">Loading…</li>`;
|
|
let plugins;
|
|
try {
|
|
plugins = await (await fetch("/api/plugins")).json();
|
|
} catch (err) {
|
|
this._list.innerHTML = `<li class="plugins-panel__empty">Could not load plugins</li>`;
|
|
return;
|
|
}
|
|
|
|
if (plugins.length === 0) {
|
|
this._list.innerHTML = `<li class="plugins-panel__empty">No plugins installed — see docs/development.adoc</li>`;
|
|
return;
|
|
}
|
|
this._list.innerHTML = "";
|
|
plugins.forEach((plugin) => this._list.appendChild(this._renderItem(plugin)));
|
|
}
|
|
|
|
// Per plugin: "Run for this area"; with "route" in its plugin.json also
|
|
// "Along the trip's route" N km either side (the corridor remembered); with
|
|
// "settings" (a login cookie) their fields — a value set shows as "set",
|
|
// never sent back from the server.
|
|
_renderItem(plugin) {
|
|
const li = document.createElement("li");
|
|
li.className = "plugins-panel__item";
|
|
li.innerHTML = `
|
|
<span class="plugins-panel__label"></span>
|
|
<button type="button" class="plugins-panel__run">Run for this area</button>
|
|
${plugin.route ? `<span class="plugins-panel__route"><button type="button" class="plugins-panel__run plugins-panel__run--route">Along the trip's route</button>
|
|
<select class="plugins-panel__corridor" aria-label="How far either side of the route">${CORRIDORS_KM.map((km) => `<option value="${km}">± ${km} km</option>`).join("")}</select></span>` : ""}
|
|
<span class="plugins-panel__status"></span>
|
|
${plugin.settings?.length ? `<details class="plugins-panel__settings"><summary>Settings</summary>${plugin.settings.map(() => `<label class="plugins-panel__setting"><span></span><input type="password" autocomplete="off" spellcheck="false"><small></small></label>`).join("")}
|
|
<button type="button" class="plugins-panel__save">Save</button></details>` : ""}
|
|
`;
|
|
li.querySelector(".plugins-panel__label").textContent = plugin.label;
|
|
|
|
const button = li.querySelector(".plugins-panel__run");
|
|
const status = li.querySelector(".plugins-panel__status");
|
|
button.addEventListener("click", () => this._run(plugin, button, status));
|
|
const routeButton = li.querySelector(".plugins-panel__run--route");
|
|
if (routeButton) {
|
|
const corridor = li.querySelector(".plugins-panel__corridor");
|
|
corridor.value = localStorage.getItem(CORRIDOR_KEY) || "5";
|
|
corridor.addEventListener("change", () => localStorage.setItem(CORRIDOR_KEY, corridor.value));
|
|
routeButton.addEventListener("click", () => this._run(plugin, routeButton, status, { route: "1", corridor_km: corridor.value }));
|
|
}
|
|
const fields = [...li.querySelectorAll(".plugins-panel__setting")];
|
|
fields.forEach((label, i) => {
|
|
const field = plugin.settings[i];
|
|
label.querySelector("span").textContent = field.label || field.key;
|
|
label.querySelector("small").textContent = field.help || "";
|
|
label.querySelector("input").placeholder = plugin.set?.includes(field.key) ? "set — paste to replace" : "not set";
|
|
});
|
|
li.querySelector(".plugins-panel__save")?.addEventListener("click", async () => {
|
|
const values = Object.fromEntries(plugin.settings.map((f, i) => [f.key, fields[i].querySelector("input").value]).filter(([, v]) => v));
|
|
try {
|
|
const response = await fetch(`/api/plugins/${encodeURIComponent(plugin.name)}/settings`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(values) });
|
|
const data = await response.json().catch(() => ({}));
|
|
if (!response.ok) throw new Error(data.error || `save failed: ${response.status}`);
|
|
plugin.set = data.set;
|
|
fields.forEach((label, i) => { const input = label.querySelector("input"); input.value = ""; input.placeholder = data.set.includes(plugin.settings[i].key) ? "set — paste to replace" : "not set"; });
|
|
status.textContent = "Settings saved";
|
|
status.classList.remove("plugins-panel__status--error");
|
|
} catch (err) {
|
|
status.textContent = err.message;
|
|
status.classList.add("plugins-panel__status--error");
|
|
}
|
|
});
|
|
return li;
|
|
}
|
|
|
|
async _run(plugin, button, status, along = null) {
|
|
button.disabled = true;
|
|
status.textContent = "Running…";
|
|
try {
|
|
const bounds = this._map.getBounds();
|
|
const body = new URLSearchParams(along || {
|
|
south: bounds.getSouth(),
|
|
west: bounds.getWest(),
|
|
north: bounds.getNorth(),
|
|
east: bounds.getEast(),
|
|
});
|
|
const response = await fetch(`/api/plugins/${encodeURIComponent(plugin.name)}/run`, { method: "POST", body });
|
|
// Every app.rb response on this route is JSON, success or error —
|
|
// the catch below is only for an infra-level failure in front of it
|
|
// (a proxy's own HTML error page, a dropped connection) that never
|
|
// reached app.rb at all.
|
|
const data = await response.json().catch(() => ({}));
|
|
if (!response.ok) throw new Error(data.error || `run failed: ${response.status}`);
|
|
notifyGpxStoreChanged(data.path);
|
|
status.textContent = `Done · ${data.count} found`;
|
|
status.classList.remove("plugins-panel__status--error");
|
|
} catch (err) {
|
|
// Plugins::RunError's message (e.g. "plugin exited with status 1")
|
|
// reaches here as `data.error` above — nothing about a plugin run
|
|
// failure gets logged server-side (app.rb turns it into a plain 502
|
|
// JSON response, not an exception), so showing that message directly
|
|
// is the only way to see why, short of SSHing in to reproduce it.
|
|
status.textContent = err.message;
|
|
status.classList.add("plugins-panel__status--error");
|
|
} finally {
|
|
button.disabled = false;
|
|
}
|
|
}
|
|
}
|