]> git.openstreetmap.org Git - rails.git/commitdiff
Merge remote-tracking branch 'upstream/pull/5095'
authorTom Hughes <tom@compton.nu>
Mon, 19 Aug 2024 18:05:44 +0000 (19:05 +0100)
committerTom Hughes <tom@compton.nu>
Mon, 19 Aug 2024 18:05:44 +0000 (19:05 +0100)
app/assets/javascripts/index/directions-endpoint.js
app/assets/javascripts/index/directions.js

index d111eca35f2682729053f9b1862cb0404aaa5e02..8489dd6df1780d50ea281e3acfdf01e1866ce0a6 100644 (file)
@@ -14,24 +14,40 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
     autoPan: true
   });
 
-  endpoint.marker.on("drag dragend", function (e) {
+  endpoint.enable = function () {
+    endpoint.marker.on("drag dragend", markerDragListener);
+    input.on("keydown", inputKeydownListener);
+    input.on("change", inputChangeListener);
+  };
+
+  endpoint.disable = function () {
+    endpoint.marker.off("drag dragend", markerDragListener);
+    input.off("keydown", inputKeydownListener);
+    input.off("change", inputChangeListener);
+
+    if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
+    delete endpoint.geocodeRequest;
+    map.removeLayer(endpoint.marker);
+  };
+
+  function markerDragListener(e) {
     var latlng = e.target.getLatLng();
 
     setLatLng(latlng);
     setInputValueFromLatLng(latlng);
     endpoint.value = input.val();
     dragCallback(e.type === "drag");
-  });
+  }
 
-  input.on("keydown", function () {
+  function inputKeydownListener() {
     input.removeClass("is-invalid");
-  });
+  }
 
-  input.on("change", function (e) {
+  function inputChangeListener(e) {
     // make text the same in both text boxes
     var value = e.target.value;
     endpoint.setValue(value);
-  });
+  }
 
   endpoint.setValue = function (value, latlng) {
     endpoint.value = value;
@@ -49,12 +65,11 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
   };
 
   function getGeocode() {
-    endpoint.awaitingGeocode = true;
-
     var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
+    var geocodeUrl = OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox;
 
-    $.getJSON(OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox, function (json) {
-      endpoint.awaitingGeocode = false;
+    endpoint.geocodeRequest = $.getJSON(geocodeUrl, function (json) {
+      delete endpoint.geocodeRequest;
       if (json.length === 0) {
         input.addClass("is-invalid");
         alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
index ff76f40086d3834f8c3d60b59b3206b497740d51..7e8c18fb8cdaafd6d0d0ace0542a8a780d5eadb3 100644 (file)
@@ -290,6 +290,9 @@ OSM.Directions = function (map) {
       endpoints[type === "from" ? 0 : 1].setValue(value, ll);
     });
 
+    endpoints[0].enable();
+    endpoints[1].enable();
+
     var params = Qs.parse(location.search.substring(1)),
         route = (params.route || "").split(";"),
         from = route[0] && L.latLng(route[0].split(",")),
@@ -318,11 +321,12 @@ OSM.Directions = function (map) {
     $(".directions_form").hide();
     $("#map").off("dragend dragover drop");
 
+    endpoints[0].disable();
+    endpoints[1].disable();
+
     map
       .removeLayer(popup)
-      .removeLayer(polyline)
-      .removeLayer(endpoints[0].marker)
-      .removeLayer(endpoints[1].marker);
+      .removeLayer(polyline);
   };
 
   return page;