diff --git a/src/trx-client/trx-frontend/trx-frontend-http/assets/web/app.js b/src/trx-client/trx-frontend/trx-frontend-http/assets/web/app.js index 645994e..df682f2 100644 --- a/src/trx-client/trx-frontend/trx-frontend-http/assets/web/app.js +++ b/src/trx-client/trx-frontend/trx-frontend-http/assets/web/app.js @@ -4469,6 +4469,29 @@ function updateTabHistory(name, replaceHistory = false) { window.history[method]({}, "", nextUrl); } +// Initialise the Leaflet map, waiting for both Leaflet (L) and map-core.js +// (window.trx.map) if they haven't loaded yet. +let _mapInitTimer = null; +function _initMapWhenReady() { + const loadingEl = document.getElementById("map-loading"); + if (window.trx.map && typeof L !== "undefined") { + if (_mapInitTimer) { clearInterval(_mapInitTimer); _mapInitTimer = null; } + if (loadingEl) loadingEl.style.display = "none"; + window.trx.map.initAprsMap(); + window.trx.map.sizeAprsMapToViewport(); + if (window.trx.map.aprsMap) setTimeout(() => window.trx.map.aprsMap.invalidateSize(), 50); + return; + } + // Not ready yet — show loading and poll until both are available. + if (loadingEl) loadingEl.style.display = ""; + if (!_mapInitTimer) { + _mapInitTimer = setInterval(() => { + if (_activeTab !== "map") { clearInterval(_mapInitTimer); _mapInitTimer = null; return; } + _initMapWhenReady(); + }, 100); + } +} + function navigateToTab(name, options = {}) { const { updateHistory = true, replaceHistory = false } = options; if (authEnabled && !authRole && name !== "main") { @@ -4499,15 +4522,7 @@ function navigateToTab(name, options = {}) { scheduleSpectrumLayout(); if (typeof window.loadPluginsForTab === "function") window.loadPluginsForTab(name); if (name === "map") { - const loadingEl = document.getElementById("map-loading"); - if (window.trx.map) { - if (loadingEl) loadingEl.style.display = "none"; - window.trx.map.initAprsMap(); - window.trx.map.sizeAprsMapToViewport(); - if (window.trx.map.aprsMap) setTimeout(() => window.trx.map.aprsMap.invalidateSize(), 50); - } else if (loadingEl) { - loadingEl.style.display = ""; - } + _initMapWhenReady(); } if (name === "statistics") { window.trx.map?.scheduleStatsRender(); diff --git a/src/trx-client/trx-frontend/trx-frontend-http/assets/web/map-core.js b/src/trx-client/trx-frontend/trx-frontend-http/assets/web/map-core.js index ff77cd5..3c18bad 100644 --- a/src/trx-client/trx-frontend/trx-frontend-http/assets/web/map-core.js +++ b/src/trx-client/trx-frontend/trx-frontend-http/assets/web/map-core.js @@ -3427,15 +3427,12 @@ } - // Auto-init map if the map tab is already visible (e.g. direct /map navigation) + // Auto-init map if the map tab is already visible (e.g. direct /map navigation). + // Delegates to _initMapWhenReady() in app.js which handles the Leaflet load race. function autoInitIfVisible() { const panel = document.getElementById("tab-map"); - if (panel && panel.style.display !== "none") { - const loadingEl = document.getElementById("map-loading"); - if (loadingEl) loadingEl.style.display = "none"; - initAprsMap(); - sizeAprsMapToViewport(); - if (aprsMap) setTimeout(() => aprsMap.invalidateSize(), 50); + if (panel && panel.style.display !== "none" && typeof _initMapWhenReady === "function") { + _initMapWhenReady(); } }