]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/directions-endpoint.js
Move close button out of left sidebar content
[rails.git] / app / assets / javascripts / index / directions-endpoint.js
index 3fdae7c5413560139ada1e4150957d84523da2c5..e3f6afa46d4a49a4984a94097347878411b854ff 100644 (file)
@@ -1,5 +1,5 @@
 OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, changeCallback) {
 OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, changeCallback) {
-  var endpoint = {};
+  const endpoint = {};
 
   endpoint.marker = L.marker([0, 0], {
     icon: L.icon({
 
   endpoint.marker = L.marker([0, 0], {
     icon: L.icon({
@@ -14,23 +14,16 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
     autoPan: true
   });
 
     autoPan: true
   });
 
-  endpoint.enable = function () {
+  endpoint.enableListeners = function () {
     endpoint.marker.on("drag dragend", markerDragListener);
     input.on("keydown", inputKeydownListener);
     input.on("change", inputChangeListener);
   };
 
     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);
     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) {
   };
 
   function markerDragListener(e) {
@@ -52,7 +45,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
 
   function inputChangeListener(e) {
     // make text the same in both text boxes
 
   function inputChangeListener(e) {
     // make text the same in both text boxes
-    var value = e.target.value;
+    const value = e.target.value;
     endpoint.setValue(value);
   }
 
     endpoint.setValue(value);
   }
 
@@ -61,8 +54,8 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
     delete endpoint.geocodeRequest;
     input.removeClass("is-invalid");
 
     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);
 
     if (latlng && endpoint.cachedReverseGeocode && endpoint.cachedReverseGeocode.latlng.equals(latlng)) {
       setLatLng(latlng);
@@ -91,9 +84,18 @@ 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) {
   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;
     delete endpoint.cachedReverseGeocode;
     delete otherEndpoint.cachedReverseGeocode;
     if (g0) otherEndpoint.cachedReverseGeocode = g0;
@@ -101,10 +103,16 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
   };
 
   function getGeocode() {
   };
 
   function getGeocode() {
-    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;
+    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");
       delete endpoint.geocodeRequest;
       if (json.length === 0) {
         input.addClass("is-invalid");
@@ -119,14 +127,21 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
       input.val(json[0].display_name);
 
       changeCallback();
       input.val(json[0].display_name);
 
       changeCallback();
-    });
+    }
   }
 
   function getReverseGeocode() {
   }
 
   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) {
+    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 };
       delete endpoint.geocodeRequest;
       if (!json || !json.display_name) {
         endpoint.cachedReverseGeocode = { latlng: latlng, notFound: true };
@@ -136,7 +151,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 };
       endpoint.value = json.display_name;
       input.val(json.display_name);
       endpoint.cachedReverseGeocode = { latlng: latlng, value: endpoint.value };
-    });
+    }
   }
 
   function setLatLng(ll) {
   }
 
   function setLatLng(ll) {