import { Map as MaplibreMap, NavigationControl, Popup, ScaleControl, addProtocol, } from "/vendor/maplibre-gl/maplibre-gl.mjs"; import { GpxPanelControl } from "/js/gpx.js"; import { OverlaysControl } from "/js/overlays.js"; import { GnssControl } from "/js/gnss.js"; import { LiveTrackPanelControl } from "/js/livetrack.js"; import { TrackProfilePanel } from "/js/track_profile.js"; import { MeasureControl } from "/js/measure.js"; import { NotesControl } from "/js/notes.js"; import { PluginsControl } from "/js/plugins.js"; import { LocateControl } from "/js/locate.js"; import { SearchControl } from "/js/search.js"; import { OfflineDataControl } from "/js/offline_data.js"; import { RoutePanelControl } from "/js/route.js"; import { LAYERS_ICON, WAYPOINT_ICON, SUNRISE_ICON, SUNSET_ICON, PENDING_ICON, LOCK_ICON, UNLOCK_ICON } from "/js/icons.js"; import { formatCoords, formatSunInfo, sunChartHtml } from "/js/sun.js"; import { notifySidePanelOpen, onlyOneSidePanelOpen, setupSidePanelModes } from "/js/panels.js"; import { isLoggedIn } from "/js/auth.js"; import { DebugControl } from "/js/debug.js"; import { patchOnlineStyle } from "/js/map_themes.js"; import { offerSharedFiles } from "/js/share_import.js"; import HomeButton from "/vendor/npmap-home-button/home-button.esm.js"; import OverviewMap from "/vendor/npmap-overview/overview.esm.js"; import * as maplibregl from "/vendor/maplibre-gl/maplibre-gl.mjs"; const CENTER = [17.1077, 48.1486]; const ZOOM = 13; const ONLINE_STYLE_URL = "https://tiles.openfreemap.org/styles/liberty"; // Fetched (not just referenced by URL) so its colors can be patched before // the map ever sees it — MapLibre accepts a style object anywhere it'd // accept a style URL, so every use of onlineStyle() (applyStyle's // map.setStyle) needs no change. This runs before the map — and the rest // of the page's setup — is constructed (see the top-level await below), so // it falls back to the plain URL, letting MapLibre fetch and render it // unpatched, both when the fetch fails outright and when it's just slow // (a 3s timeout): this app is meant to work offline in the field, and // blocking the entire page on a remote style server would undermine that // even though this particular fetch is only for the online map. // Kept unpatched; onlineStyle() patches a fresh copy for each setStyle. async function loadOnlineStyle() { try { const response = await fetch(ONLINE_STYLE_URL, { signal: AbortSignal.timeout(3000) }); return await response.json(); } catch (err) { return ONLINE_STYLE_URL; } } const ONLINE_STYLE_TEMPLATE = await loadOnlineStyle(); function onlineStyle() { if (typeof ONLINE_STYLE_TEMPLATE === "string") return ONLINE_STYLE_TEMPLATE; return patchOnlineStyle(structuredClone(ONLINE_STYLE_TEMPLATE)); } // A vendored copy of the same Liberty style ONLINE_STYLE_TEMPLATE fetches live // (public/vendor/liberty-style/liberty.json — a point-in-time snapshot, not // auto-updated) that offlineStyle() below patches+repoints the exact same // way. Offline mode can't depend on ONLINE_STYLE_URL actually being // reachable — that fetch failing/timing out is the normal case in the // field, not an edge case — so this is a local, same-origin fetch instead // (fast and always available, since it's served by this app itself). const OFFLINE_STYLE_TEMPLATE = await fetch("/vendor/liberty-style/liberty.json").then((response) => response.json()); const protocol = new pmtiles.Protocol(); addProtocol("pmtiles", protocol.tile); // Builds the offline style from OFFLINE_STYLE_TEMPLATE — the identical // Liberty JSON + patchOnlineStyle recolor (map_themes.js) as the online map, just // repointed at a local PMTiles source instead of tiles.openfreemap.org. // This works because data/offline.pmtiles is now built by Planetiler's own // OpenMapTiles profile (see docs/development.adoc), the same vector schema // Liberty expects — before that, offline tiles used a different schema // (protomaps' own basemaps) needing a completely separate hand-tuned // theme (protomaps-themes-base + OFFLINE_THEME) that could never be made // to match the online look exactly, no matter how much either // side got tuned. // // glyphs/sprite point at vendored local copies (public/vendor/fonts, // public/vendor/liberty-sprite) rather than tiles.openfreemap.org's — // glyphs are Latin-only (see docs/architecture.adoc: labels in other // scripts won't render offline), sprite is a one-time snapshot like // OFFLINE_STYLE_TEMPLATE itself. ne2_shaded (Liberty's low-zoom shaded- // relief backdrop, z0-6 only) is left pointing at its remote URL // unchanged — offline usage is regional, not whole-earth zoomed out, so // this only ever silently fails to load at a zoom level offline users // aren't in, the same graceful-degradation story as this app's other // online-only extras (Wikipedia, live radar). function offlineStyle() { const style = patchOnlineStyle(structuredClone(OFFLINE_STYLE_TEMPLATE)); style.sources.openmaptiles.url = `pmtiles://${window.location.origin}/tiles/offline.pmtiles`; style.glyphs = `${window.location.origin}/vendor/fonts/{fontstack}/{range}.pbf`; style.sprite = `${window.location.origin}/vendor/liberty-sprite/ofm`; return style; } // A sidebar icon that opens a full-height side panel (like the GPX one) // with the Online/Offline choice — modelled after OsmAnd's "Configure map" // panel, rather than a small dropdown flyout. class LayerToggleControl { constructor(onSelect) { this._onSelect = onSelect; } mount(sidebar) { this._container = document.createElement("div"); this._container.className = "sidebar-item"; this._container.innerHTML = ``; this._icon = this._container.querySelector(".sidebar-icon"); this._icon.addEventListener("click", () => this._togglePanel()); this._panel = document.getElementById("basemap-panel"); this._panel .querySelector(".basemap-panel__close") .addEventListener("click", () => this._togglePanel(false)); this._panel.querySelectorAll(".basemap-panel__option").forEach((button) => { button.addEventListener("click", () => { this._onSelect(button.dataset.style); 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); } // Public wrapper so a second entry point (MapLayersButton, a map corner // control) can open/close the same panel without reaching into a // "private" method by convention only. toggle() { this._togglePanel(); } setActive(name) { this._panel.querySelectorAll(".basemap-panel__option").forEach((button) => { button.classList.toggle("active", button.dataset.style === name); }); } // Called once it's known there's no offline map to switch to (see the // /api/offline-tiles check below) — an Online/Offline switch doesn't make // sense to show at all when Offline was never going to work. The panel // itself stays for its legend link. hideSources() { this._panel.querySelector(".basemap-panel__sources").hidden = true; } } // A small map-corner button (grouped with Home/zoom, see below) that's a // second entry point to the same panel LayerToggleControl already manages // in the page's own sidebar — matching the National Park Service's own // map control layout, which keeps zoom/home/base-map together on the map // rather than split between the map and a separate side rail. class MapLayersButton { constructor(layerToggle) { this._layerToggle = layerToggle; } onAdd() { this._container = document.createElement("div"); this._container.className = "maplibregl-ctrl maplibregl-ctrl-group"; const button = document.createElement("button"); button.type = "button"; button.title = "Base map"; button.setAttribute("aria-label", "Base map"); button.innerHTML = LAYERS_ICON; button.addEventListener("click", () => this._layerToggle.toggle()); this._container.appendChild(button); return this._container; } onRemove() { this._container.remove(); } getDefaultPosition() { return "top-left"; } } // A passive zoom-level readout, sitting in the same stack as the buttons // around it — the National Park Service's own control layout has one (the // small numbered square next to their zoom buttons). Not a button: no // click behavior, just a live display, so it's a plain div rather than a //