]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/directions-endpoint.js
Put map key presence in js layer definitions
[rails.git] / app / assets / javascripts / index / directions-endpoint.js
index 90e95857ff9dcd5c50a21df6c4999e80ecf83605..97b841f1443c6dceae67daeb7845a167708159e2 100644 (file)
@@ -1,36 +1,22 @@
-OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, changeCallback) {
+OSM.DirectionsEndpoint = function Endpoint(map, input, marker, dragCallback, changeCallback) {
   const endpoint = {};
 
   endpoint.marker = L.marker([0, 0], {
-    icon: L.icon({
-      iconUrl: iconUrl,
-      iconSize: [25, 41],
-      iconAnchor: [12, 41],
-      popupAnchor: [1, -34],
-      shadowUrl: OSM.MARKER_SHADOW,
-      shadowSize: [41, 41]
-    }),
+    icon: OSM.getMarker(marker),
     draggable: true,
     autoPan: true
   });
 
-  endpoint.enable = function () {
+  endpoint.enableListeners = function () {
     endpoint.marker.on("drag dragend", markerDragListener);
     input.on("keydown", inputKeydownListener);
     input.on("change", inputChangeListener);
   };
 
-  endpoint.disable = function () {
+  endpoint.disableListeners = function () {
     endpoint.marker.off("drag dragend", markerDragListener);
     input.off("keydown", inputKeydownListener);
     input.off("change", inputChangeListener);
-
-    if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
-    delete endpoint.geocodeRequest;
-    removeLatLng();
-    delete endpoint.value;
-    input.val("");
-    map.removeLayer(endpoint.marker);
   };
 
   function markerDragListener(e) {
@@ -91,6 +77,15 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
     }
   };
 
+  endpoint.clearValue = function () {
+    if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
+    delete endpoint.geocodeRequest;
+    removeLatLng();
+    delete endpoint.value;
+    input.val("");
+    map.removeLayer(endpoint.marker);
+  };
+
   endpoint.swapCachedReverseGeocodes = function (otherEndpoint) {
     const g0 = endpoint.cachedReverseGeocode;
     const g1 = otherEndpoint.cachedReverseGeocode;
@@ -104,12 +99,18 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
     const viewbox = map.getBounds().toBBoxString(), // <sw lon>,<sw lat>,<ne lon>,<ne lat>
           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");
         // eslint-disable-next-line no-alert
-        alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
+        alert(OSM.i18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
         return;
       }
 
@@ -119,7 +120,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
       input.val(json[0].display_name);
 
       changeCallback();
-    });
+    }
   }
 
   function getReverseGeocode() {
@@ -127,7 +128,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 +144,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) {