X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/f32aea6d1429ef55b7e5ec60bf83576cf76d85ed..a6622e7d2c7adb59491efa0d1f21914b2a0a362f:/app/assets/javascripts/leaflet.map.js?ds=inline diff --git a/app/assets/javascripts/leaflet.map.js b/app/assets/javascripts/leaflet.map.js index e0c2ee60b..10ce94715 100644 --- a/app/assets/javascripts/leaflet.map.js +++ b/app/assets/javascripts/leaflet.map.js @@ -32,12 +32,17 @@ L.OSM.Map = L.Map.extend({ layerOptions.apikey = OSM[value]; } else if (property === "leafletOsmId") { layerConstructor = L.OSM[value]; + } else if (property === "leafletOsmDarkId" && OSM.isDarkMap() && L.OSM[value]) { + layerConstructor = L.OSM[value]; } else { layerOptions[property] = value; } } const layer = new layerConstructor(layerOptions); + layer.on("add", () => { + this.fire("baselayerchange", { layer: layer }); + }); this.baseLayers.push(layer); } @@ -51,8 +56,14 @@ L.OSM.Map = L.Map.extend({ pane: "overlayPane", code: "G" }); + this.gpsLayer.on("add", () => { + this.fire("overlayadd", { layer: this.gpsLayer }); + }).on("remove", () => { + this.fire("overlayremove", { layer: this.gpsLayer }); + }); - this.on("layeradd", function (event) { + + this.on("baselayerchange", function (event) { if (this.baseLayers.indexOf(event.layer) >= 0) { this.setMaxZoom(event.layer.options.maxZoom); } @@ -104,19 +115,19 @@ L.OSM.Map = L.Map.extend({ }, updateLayers: function (layerParam) { - var layers = layerParam || "M", - layersAdded = ""; - - for (var i = this.baseLayers.length - 1; i >= 0; i--) { - if (layers.indexOf(this.baseLayers[i].options.code) >= 0) { - this.addLayer(this.baseLayers[i]); - layersAdded = layersAdded + this.baseLayers[i].options.code; - } else if (i === 0 && layersAdded === "") { - this.addLayer(this.baseLayers[i]); - } else { - this.removeLayer(this.baseLayers[i]); + const oldBaseLayer = this.getMapBaseLayer(); + let newBaseLayer; + + for (const layer of this.baseLayers) { + if (!newBaseLayer || layerParam.includes(layer.options.code)) { + newBaseLayer = layer; } } + + if (newBaseLayer !== oldBaseLayer) { + if (oldBaseLayer) this.removeLayer(oldBaseLayer); + if (newBaseLayer) this.addLayer(newBaseLayer); + } }, getLayersCode: function () { @@ -295,12 +306,13 @@ L.OSM.Map = L.Map.extend({ } if (callback) callback(this._objectLayer.getBounds()); + this.fire("overlayadd", { layer: this._objectLayer }); } else { // element handled by L.OSM.DataLayer var map = this; this._objectLoader = $.ajax({ url: OSM.apiUrl(object), - dataType: "xml", - success: function (xml) { + dataType: "json", + success: function (data) { map._object = object; map._objectLayer = new L.OSM.DataLayer(null, { @@ -322,10 +334,11 @@ L.OSM.Map = L.Map.extend({ } }; - map._objectLayer.addData(xml); + map._objectLayer.addData(data); map._objectLayer.addTo(map); if (callback) callback(map._objectLayer.getBounds()); + map.fire("overlayadd", { layer: map._objectLayer }); } }); } @@ -335,6 +348,7 @@ L.OSM.Map = L.Map.extend({ this._object = null; if (this._objectLoader) this._objectLoader.abort(); if (this._objectLayer) this.removeLayer(this._objectLayer); + this.fire("overlayremove", { layer: this._objectLayer }); }, getState: function () { @@ -386,6 +400,14 @@ L.extend(L.Icon.Default.prototype, { } }); +OSM.isDarkMap = function () { + var mapTheme = $("body").attr("data-map-theme"); + if (mapTheme) return mapTheme === "dark"; + var siteTheme = $("html").attr("data-bs-theme"); + if (siteTheme) return siteTheme === "dark"; + return window.matchMedia("(prefers-color-scheme: dark)").matches; +}; + OSM.getUserIcon = function (url) { return L.icon({ iconUrl: url || OSM.MARKER_RED,