X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/cbf53088a2c2668077b68c62e605beb1f530d095..00108bc9b7e9e03a47d89343e7f53eb924977ff8:/app/assets/javascripts/index/directions-endpoint.js?ds=sidebyside diff --git a/app/assets/javascripts/index/directions-endpoint.js b/app/assets/javascripts/index/directions-endpoint.js index b9dc65748..90e95857f 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({ @@ -34,7 +34,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch }; function markerDragListener(e) { - var latlng = convertLatLngToZoomPrecision(e.target.getLatLng()); + const latlng = L.latLng(OSM.cropLocation(e.target.getLatLng(), map.getZoom())); if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort(); delete endpoint.geocodeRequest; @@ -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,12 +61,17 @@ 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); - endpoint.value = endpoint.cachedReverseGeocode.value; + if (endpoint.cachedReverseGeocode.notFound) { + endpoint.value = value; + input.addClass("is-invalid"); + } else { + endpoint.value = endpoint.cachedReverseGeocode.value; + } input.val(endpoint.value); changeCallback(); return; @@ -86,14 +91,24 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch } }; + endpoint.swapCachedReverseGeocodes = function (otherEndpoint) { + const g0 = endpoint.cachedReverseGeocode; + const g1 = otherEndpoint.cachedReverseGeocode; + delete endpoint.cachedReverseGeocode; + delete otherEndpoint.cachedReverseGeocode; + if (g0) otherEndpoint.cachedReverseGeocode = g0; + if (g1) endpoint.cachedReverseGeocode = g1; + }; + function getGeocode() { - var viewbox = map.getBounds().toBBoxString(); // ,,, - var geocodeUrl = OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox; + const viewbox = map.getBounds().toBBoxString(), // ,,, + geocodeUrl = OSM.NOMINATIM_URL + "search?" + new URLSearchParams({ q: endpoint.value, format: "json", viewbox }); endpoint.geocodeRequest = $.getJSON(geocodeUrl, function (json) { delete endpoint.geocodeRequest; if (json.length === 0) { input.addClass("is-invalid"); + // eslint-disable-next-line no-alert alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value })); return; } @@ -108,12 +123,14 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch } function getReverseGeocode() { - var latlng = endpoint.latlng.clone(); - var reverseGeocodeUrl = OSM.NOMINATIM_URL + "reverse?lat=" + latlng.lat + "&lon=" + latlng.lng + "&format=json"; + const latlng = endpoint.latlng.clone(), + { lat, lng } = latlng, + reverseGeocodeUrl = OSM.NOMINATIM_URL + "reverse?" + new URLSearchParams({ lat, lon: lng, format: "json" }); endpoint.geocodeRequest = $.getJSON(reverseGeocodeUrl, function (json) { delete endpoint.geocodeRequest; if (!json || !json.display_name) { + endpoint.cachedReverseGeocode = { latlng: latlng, notFound: true }; return; } @@ -144,11 +161,5 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch input.val(latlng.lat + ", " + latlng.lng); } - function convertLatLngToZoomPrecision(latlng) { - var precision = OSM.zoomPrecision(map.getZoom()); - - return L.latLng(latlng.lat.toFixed(precision), latlng.lng.toFixed(precision)); - } - return endpoint; };