115 lines
4.6 KiB
JavaScript
115 lines
4.6 KiB
JavaScript
// Keeps at most one full-height side panel (base map, GPX, ...) open at a
|
|
// time — they all dock to the same spot next to the sidebar, so two open
|
|
// together would just stack on top of each other.
|
|
const OPEN_EVENT = "side-panel-open";
|
|
|
|
export function notifySidePanelOpen(panel) {
|
|
document.dispatchEvent(new CustomEvent(OPEN_EVENT, { detail: panel }));
|
|
}
|
|
|
|
export function onlyOneSidePanelOpen(panel, onClosedByOther) {
|
|
document.addEventListener(OPEN_EVENT, (event) => {
|
|
if (event.detail !== panel && !panel.hidden) {
|
|
panel.hidden = true;
|
|
onClosedByOther();
|
|
}
|
|
});
|
|
}
|
|
|
|
// Two ways a side panel sits next to the map, switched by the pin button
|
|
// every panel's header gets (remembered per browser):
|
|
// - pinned (default): stays open and the map shrinks beside it (CSS on
|
|
// body.side-pinned, see style.css; MapLibre resizes itself on that);
|
|
// - overlay: floats over the map and closes once the mouse has been in it
|
|
// and left — not while typing in one of its fields, and never on touch.
|
|
const PANELS = ".basemap-panel, .gpx-panel";
|
|
const MODE_KEY = "sidePanelMode";
|
|
const HIDE_DELAY_MS = 400;
|
|
|
|
export function setupSidePanelModes() {
|
|
let pinned = true;
|
|
try {
|
|
pinned = localStorage.getItem(MODE_KEY) !== "overlay";
|
|
} catch {}
|
|
const apply = () => {
|
|
document.body.classList.toggle("side-pinned", pinned);
|
|
document.querySelectorAll(".side-panel-pin").forEach((btn) => {
|
|
btn.classList.toggle("active", pinned);
|
|
btn.title = pinned ? "Pinned beside the map (click: float over it, hide on mouse out)" : "Floats over the map (click: pin beside it)";
|
|
btn.setAttribute("aria-pressed", pinned);
|
|
});
|
|
};
|
|
|
|
document.querySelectorAll(PANELS).forEach((panel) => {
|
|
const close = panel.querySelector(".basemap-panel__close, .gpx-panel__close");
|
|
const pin = document.createElement("button");
|
|
pin.type = "button";
|
|
pin.className = "side-panel-pin";
|
|
pin.setAttribute("aria-label", "Pin panel");
|
|
pin.innerHTML = `<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 17v5M9 3h6l-1 6 3 3v2H7v-2l3-3z"/></svg>`;
|
|
pin.addEventListener("click", () => {
|
|
pinned = !pinned;
|
|
try {
|
|
localStorage.setItem(MODE_KEY, pinned ? "pinned" : "overlay");
|
|
} catch {}
|
|
apply();
|
|
});
|
|
close.before(pin);
|
|
|
|
let entered = false;
|
|
let timer = null;
|
|
new MutationObserver(() => (entered = false)).observe(panel, { attributes: true, attributeFilter: ["hidden"] });
|
|
panel.addEventListener("pointerenter", (event) => {
|
|
if (event.pointerType !== "mouse") return;
|
|
entered = true;
|
|
clearTimeout(timer);
|
|
});
|
|
panel.addEventListener("pointerleave", (event) => {
|
|
if (pinned || !entered || event.pointerType !== "mouse") return;
|
|
timer = setTimeout(() => {
|
|
const typing = panel.contains(document.activeElement) && document.activeElement.matches("input:not([type=checkbox]):not([type=radio]), textarea, select");
|
|
if (!pinned && !panel.hidden && !typing) close.click();
|
|
}, HIDE_DELAY_MS);
|
|
});
|
|
});
|
|
apply();
|
|
}
|
|
|
|
// Lets a control that writes into the GPX store (e.g. notes.js saving a
|
|
// drawn point) tell GpxPanelControl its tree is stale, without either one
|
|
// holding a reference to the other. path (the .geojson file just written
|
|
// to) lets the listener also auto-show that file instead of just refreshing
|
|
// the tree — the point/shape just saved is otherwise invisible until the
|
|
// user finds and checks it by hand.
|
|
const STORE_CHANGED_EVENT = "gpx-store-changed";
|
|
|
|
export function notifyGpxStoreChanged(path) {
|
|
document.dispatchEvent(new CustomEvent(STORE_CHANGED_EVENT, { detail: path }));
|
|
}
|
|
|
|
export function onGpxStoreChanged(callback) {
|
|
document.addEventListener(STORE_CHANGED_EVENT, (event) => callback(event.detail));
|
|
}
|
|
|
|
// Anything with a location (a search result, a library point) hands it to
|
|
// the route panel (route.js) the same decoupled way.
|
|
const ROUTE_POINT_EVENT = "route-point";
|
|
const ROUTE_EDIT_EVENT = "route-edit";
|
|
|
|
export function addRoutePoint(lngLat, name) {
|
|
document.dispatchEvent(new CustomEvent(ROUTE_POINT_EVENT, { detail: { lngLat, name } }));
|
|
}
|
|
|
|
export function onRoutePoint(callback) {
|
|
document.addEventListener(ROUTE_POINT_EVENT, (event) => callback(event.detail));
|
|
}
|
|
|
|
// A saved route (its library node key and feature) back into the panel.
|
|
export function editRoute(key, feature) {
|
|
document.dispatchEvent(new CustomEvent(ROUTE_EDIT_EVENT, { detail: { key, feature } }));
|
|
}
|
|
|
|
export function onRouteEdit(callback) {
|
|
document.addEventListener(ROUTE_EDIT_EVENT, (event) => callback(event.detail));
|
|
}
|