X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/4b6b49801c63424b08c582d349511a6b1e05354f..5047ec403db94b0d371a9a10b56801bc0a3fcf0a:/app/assets/javascripts/leaflet.map.js?ds=sidebyside diff --git a/app/assets/javascripts/leaflet.map.js b/app/assets/javascripts/leaflet.map.js index c45761d4e..1b92edf99 100644 --- a/app/assets/javascripts/leaflet.map.js +++ b/app/assets/javascripts/leaflet.map.js @@ -1,5 +1,3 @@ -//= require qs/dist/qs - L.extend(L.LatLngBounds.prototype, { getSize: function () { return (this._northEast.lat - this._southWest.lat) * @@ -108,26 +106,24 @@ L.OSM.Map = L.Map.extend({ link.attr("target", "_blank"); } return link.prop("outerHTML"); - } else { - return text; } + return text; } }, updateLayers: function (layerParam) { - var layers = layerParam || "M"; + const oldBaseLayer = this.getMapBaseLayer(); + let newBaseLayer; - 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 (const layer of this.baseLayers) { + if (!newBaseLayer || layerParam.includes(layer.options.code)) { + newBaseLayer = layer; } } - for (let i = this.baseLayers.length - 1; i >= 0; i--) { - if (layers.indexOf(this.baseLayers[i].options.code) >= 0 || i === 0) { - this.addLayer(this.baseLayers[i]); - return; - } + if (newBaseLayer !== oldBaseLayer) { + if (oldBaseLayer) this.removeLayer(oldBaseLayer); + if (newBaseLayer) this.addLayer(newBaseLayer); } }, @@ -160,7 +156,7 @@ L.OSM.Map = L.Map.extend({ } var url = window.location.protocol + "//" + OSM.SERVER_URL + "/", - query = Qs.stringify(params), + query = new URLSearchParams(params), hash = OSM.formatHash(this); if (query) url += "?" + query; @@ -172,16 +168,15 @@ L.OSM.Map = L.Map.extend({ getShortUrl: function (marker) { var zoom = this.getZoom(), latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(), - str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ? - window.location.protocol + "//osm.org/go/" : - window.location.protocol + "//" + window.location.hostname + "/go/", + str = window.location.protocol + "//" + window.location.hostname.replace(/^www\.openstreetmap\.org/i, "osm.org") + "/go/", char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~", x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)), y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)), // JavaScript only has to keep 32 bits of bitwise operators, so this has to be // done in two parts. each of the parts c1/c2 has 30 bits of the total in it // and drops the last 4 bits of the full 64 bit Morton code. - c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff), + c1 = interlace(x >>> 17, y >>> 17), + c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff), digit, i; @@ -210,22 +205,22 @@ L.OSM.Map = L.Map.extend({ return (interlaced_x << 1) | interlaced_y; } - var params = {}; + const params = new URLSearchParams(); var layers = this.getLayersCode().replace("M", ""); if (layers) { - params.layers = layers; + params.set("layers", layers); } if (marker && this.hasLayer(marker)) { - params.m = ""; + params.set("m", ""); } if (this._object) { - params[this._object.type] = this._object.id; + params.set(this._object.type, this._object.id); } - var query = Qs.stringify(params); + const query = params.toString(); if (query) { str += "?" + query; } @@ -270,9 +265,7 @@ L.OSM.Map = L.Map.extend({ this.removeObject(); if (object.type === "note" || object.type === "changeset") { - this._objectLoader = { - abort: function () {} - }; + this._objectLoader = { abort: () => {} }; this._object = object; this._objectLayer = L.featureGroup().addTo(this); @@ -300,10 +293,13 @@ L.OSM.Map = L.Map.extend({ this.fire("overlayadd", { layer: this._objectLayer }); } else { // element handled by L.OSM.DataLayer var map = this; - this._objectLoader = $.ajax({ - url: OSM.apiUrl(object), - dataType: "json", - success: function (data) { + this._objectLoader = new AbortController(); + fetch(OSM.apiUrl(object), { + headers: { accept: "application/json" }, + signal: this._objectLoader.signal + }) + .then(response => response.json()) + .then(function (data) { map._object = object; map._objectLayer = new L.OSM.DataLayer(null, { @@ -316,13 +312,8 @@ L.OSM.Map = L.Map.extend({ }); map._objectLayer.interestingNode = function (node, wayNodes, relationNodes) { - if (object.type === "node") { - return true; - } else if (object.type === "relation") { - return Boolean(relationNodes[node.id]); - } else { - return false; - } + return object.type === "node" || + (object.type === "relation" && Boolean(relationNodes[node.id])); }; map._objectLayer.addData(data); @@ -330,8 +321,8 @@ L.OSM.Map = L.Map.extend({ if (callback) callback(map._objectLayer.getBounds()); map.fire("overlayadd", { layer: map._objectLayer }); - } - }); + }) + .catch(() => {}); } }, @@ -356,15 +347,22 @@ L.OSM.Map = L.Map.extend({ }, setSidebarOverlaid: function (overlaid) { - var sidebarWidth = 350; + var mediumDeviceWidth = window.getComputedStyle(document.documentElement).getPropertyValue("--bs-breakpoint-md"); + var isMediumDevice = window.matchMedia(`(max-width: ${mediumDeviceWidth})`).matches; + var sidebarWidth = $("#sidebar").width(); + var sidebarHeight = $("#sidebar").height(); if (overlaid && !$("#content").hasClass("overlay-sidebar")) { $("#content").addClass("overlay-sidebar"); this.invalidateSize({ pan: false }); - if ($("html").attr("dir") !== "rtl") { + if (isMediumDevice) { + this.panBy([0, -sidebarHeight], { animate: false }); + } else if ($("html").attr("dir") !== "rtl") { this.panBy([-sidebarWidth, 0], { animate: false }); } } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) { - if ($("html").attr("dir") !== "rtl") { + if (isMediumDevice) { + this.panBy([0, $("#map").height() / 2], { animate: false }); + } else if ($("html").attr("dir") !== "rtl") { this.panBy([sidebarWidth, 0], { animate: false }); } $("#content").removeClass("overlay-sidebar");