X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/1ca2c2837ef0e637d145c22ccf346deb9fdfd035..475a3fd83615fec2a1b79d04f3323ae45c917ccd:/app/assets/javascripts/leaflet.map.js diff --git a/app/assets/javascripts/leaflet.map.js b/app/assets/javascripts/leaflet.map.js index e0c2ee60b..99d5a6050 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); } @@ -107,14 +118,18 @@ L.OSM.Map = L.Map.extend({ var layers = layerParam || "M", layersAdded = ""; - for (var i = this.baseLayers.length - 1; i >= 0; i--) { + for (let i = this.baseLayers.length - 1; i >= 0; i--) { + if (layers.indexOf(this.baseLayers[i].options.code) === -1) { + this.removeLayer(this.baseLayers[i]); + } + } + + for (let 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]); } } }, @@ -295,6 +310,7 @@ 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({ @@ -326,6 +342,7 @@ L.OSM.Map = L.Map.extend({ map._objectLayer.addTo(map); if (callback) callback(map._objectLayer.getBounds()); + map.fire("overlayadd", { layer: map._objectLayer }); } }); } @@ -335,6 +352,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 +404,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,