X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/699e73a22ad85124cef4a69f6a1848729cf87b32..847d2c544b84d54de074213241ab813165395261:/app/assets/javascripts/index/directions-endpoint.js diff --git a/app/assets/javascripts/index/directions-endpoint.js b/app/assets/javascripts/index/directions-endpoint.js index 11a70c62f..0b52d3d4c 100644 --- a/app/assets/javascripts/index/directions-endpoint.js +++ b/app/assets/javascripts/index/directions-endpoint.js @@ -1,5 +1,5 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, changeCallback) { - var endpoint = {}; + const endpoint = {}; endpoint.marker = L.marker([0, 0], { icon: L.icon({ @@ -52,7 +52,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch function inputChangeListener(e) { // make text the same in both text boxes - var value = e.target.value; + const value = e.target.value; endpoint.setValue(value); } @@ -61,8 +61,8 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch delete endpoint.geocodeRequest; input.removeClass("is-invalid"); - var coordinatesMatch = value.match(/^\s*([+-]?\d+(?:\.\d*)?)(?:\s+|\s*[/,]\s*)([+-]?\d+(?:\.\d*)?)\s*$/); - var latlng = coordinatesMatch && L.latLng(coordinatesMatch[1], coordinatesMatch[2]); + const coordinatesMatch = value.match(/^\s*([+-]?\d+(?:\.\d*)?)(?:\s+|\s*[/,]\s*)([+-]?\d+(?:\.\d*)?)\s*$/); + const latlng = coordinatesMatch && L.latLng(coordinatesMatch[1], coordinatesMatch[2]); if (latlng && endpoint.cachedReverseGeocode && endpoint.cachedReverseGeocode.latlng.equals(latlng)) { setLatLng(latlng); @@ -92,8 +92,8 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch }; endpoint.swapCachedReverseGeocodes = function (otherEndpoint) { - var g0 = endpoint.cachedReverseGeocode; - var g1 = otherEndpoint.cachedReverseGeocode; + const g0 = endpoint.cachedReverseGeocode; + const g1 = otherEndpoint.cachedReverseGeocode; delete endpoint.cachedReverseGeocode; delete otherEndpoint.cachedReverseGeocode; if (g0) otherEndpoint.cachedReverseGeocode = g0; @@ -104,7 +104,13 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch const viewbox = map.getBounds().toBBoxString(), // ,,, geocodeUrl = OSM.NOMINATIM_URL + "search?" + new URLSearchParams({ q: endpoint.value, format: "json", viewbox }); - endpoint.geocodeRequest = $.getJSON(geocodeUrl, function (json) { + endpoint.geocodeRequest = new AbortController(); + fetch(geocodeUrl, { signal: endpoint.geocodeRequest.signal }) + .then(r => r.json()) + .then(success) + .catch(() => {}); + + function success(json) { delete endpoint.geocodeRequest; if (json.length === 0) { input.addClass("is-invalid"); @@ -119,7 +125,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch input.val(json[0].display_name); changeCallback(); - }); + } } function getReverseGeocode() { @@ -127,7 +133,13 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch { lat, lng } = latlng, reverseGeocodeUrl = OSM.NOMINATIM_URL + "reverse?" + new URLSearchParams({ lat, lon: lng, format: "json" }); - endpoint.geocodeRequest = $.getJSON(reverseGeocodeUrl, function (json) { + endpoint.geocodeRequest = new AbortController(); + fetch(reverseGeocodeUrl, { signal: endpoint.geocodeRequest.signal }) + .then(r => r.json()) + .then(success) + .catch(() => {}); + + function success(json) { delete endpoint.geocodeRequest; if (!json || !json.display_name) { endpoint.cachedReverseGeocode = { latlng: latlng, notFound: true }; @@ -137,7 +149,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch endpoint.value = json.display_name; input.val(json.display_name); endpoint.cachedReverseGeocode = { latlng: latlng, value: endpoint.value }; - }); + } } function setLatLng(ll) {