]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/query.js
Merge remote-tracking branch 'upstream/pull/5452'
[rails.git] / app / assets / javascripts / index / query.js
index 672f650fa2145ee8832beead533b5a9161a61765..3673da5db85838b2b72b6bf6667e9196a6124562 100644 (file)
@@ -1,5 +1,3 @@
-//= require qs/dist/qs
-
 OSM.Query = function (map) {
   var url = OSM.OVERPASS_URL,
       credentials = OSM.OVERPASS_CREDENTIALS,
 OSM.Query = function (map) {
   var url = OSM.OVERPASS_URL,
       credentials = OSM.OVERPASS_CREDENTIALS,
@@ -111,23 +109,22 @@ OSM.Query = function (map) {
     var tags = feature.tags,
         locales = OSM.preferred_languages;
 
     var tags = feature.tags,
         locales = OSM.preferred_languages;
 
-    for (var i = 0; i < locales.length; i++) {
-      if (tags["name:" + locales[i]]) {
-        return tags["name:" + locales[i]];
+    for (const locale of locales) {
+      if (tags["name:" + locale]) {
+        return tags["name:" + locale];
+      }
+    }
+
+    for (const key of ["name", "ref", "addr:housename"]) {
+      if (tags[key]) {
+        return tags[key];
       }
     }
 
       }
     }
 
-    if (tags.name) {
-      return tags.name;
-    } else if (tags.ref) {
-      return tags.ref;
-    } else if (tags["addr:housename"]) {
-      return tags["addr:housename"];
-    } else if (tags["addr:housenumber"] && tags["addr:street"]) {
+    if (tags["addr:housenumber"] && tags["addr:street"]) {
       return tags["addr:housenumber"] + " " + tags["addr:street"];
       return tags["addr:housenumber"] + " " + tags["addr:street"];
-    } else {
-      return "#" + feature.id;
     }
     }
+    return "#" + feature.id;
   }
 
   function featureGeometry(feature) {
   }
 
   function featureGeometry(feature) {
@@ -160,16 +157,17 @@ OSM.Query = function (map) {
       $section.data("ajax").abort();
     }
 
       $section.data("ajax").abort();
     }
 
-    $section.data("ajax", $.ajax({
-      url: url,
+    $section.data("ajax", new AbortController());
+    fetch(url, {
       method: "POST",
       method: "POST",
-      data: {
+      body: new URLSearchParams({
         data: "[timeout:10][out:json];" + query
         data: "[timeout:10][out:json];" + query
-      },
-      xhrFields: {
-        withCredentials: credentials
-      },
-      success: function (results) {
+      }),
+      credentials: credentials ? "include" : "same-origin",
+      signal: $section.data("ajax").signal
+    })
+      .then(response => response.json())
+      .then(function (results) {
         var elements;
 
         $section.find(".loader").hide();
         var elements;
 
         $section.find(".loader").hide();
@@ -195,22 +193,20 @@ OSM.Query = function (map) {
           elements = elements.sort(compare);
         }
 
           elements = elements.sort(compare);
         }
 
-        for (var i = 0; i < elements.length; i++) {
-          var element = elements[i];
-
-          if (interestingFeature(element)) {
-            var $li = $("<li>")
-              .addClass("list-group-item list-group-item-action")
-              .text(featurePrefix(element) + " ")
-              .appendTo($ul);
-
-            $("<a>")
-              .addClass("stretched-link")
-              .attr("href", "/" + element.type + "/" + element.id)
-              .data("geometry", featureGeometry(element))
-              .text(featureName(element))
-              .appendTo($li);
-          }
+        for (const element of elements) {
+          if (!interestingFeature(element)) continue;
+
+          var $li = $("<li>")
+            .addClass("list-group-item list-group-item-action")
+            .text(featurePrefix(element) + " ")
+            .appendTo($ul);
+
+          $("<a>")
+            .addClass("stretched-link")
+            .attr("href", "/" + element.type + "/" + element.id)
+            .data("geometry", featureGeometry(element))
+            .text(featureName(element))
+            .appendTo($li);
         }
 
         if (results.remark) {
         }
 
         if (results.remark) {
@@ -226,16 +222,17 @@ OSM.Query = function (map) {
             .text(I18n.t("javascripts.query.nothing_found"))
             .appendTo($ul);
         }
             .text(I18n.t("javascripts.query.nothing_found"))
             .appendTo($ul);
         }
-      },
-      error: function (xhr, status, error) {
+      })
+      .catch(function (error) {
+        if (error.name === "AbortError") return;
+
         $section.find(".loader").hide();
 
         $("<li>")
           .addClass("list-group-item")
         $section.find(".loader").hide();
 
         $("<li>")
           .addClass("list-group-item")
-          .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
+          .text(I18n.t("javascripts.query.error", { server: url, error: error.message }))
           .appendTo($ul);
           .appendTo($ul);
-      }
-    }));
+      });
   }
 
   function compareSize(feature1, feature2) {
   }
 
   function compareSize(feature1, feature2) {
@@ -289,10 +286,11 @@ OSM.Query = function (map) {
       .hide();
 
     if (marker) map.removeLayer(marker);
       .hide();
 
     if (marker) map.removeLayer(marker);
-    marker = L.circle(latlng, Object.assign({
+    marker = L.circle(latlng, {
       radius: radius,
       radius: radius,
-      className: "query-marker"
-    }, featureStyle)).addTo(map);
+      className: "query-marker",
+      ...featureStyle
+    }).addTo(map);
 
     runQuery(latlng, radius, nearby, $("#query-nearby"), false);
     runQuery(latlng, radius, isin, $("#query-isin"), true, compareSize);
 
     runQuery(latlng, radius, nearby, $("#query-nearby"), false);
     runQuery(latlng, radius, isin, $("#query-isin"), true, compareSize);
@@ -301,7 +299,7 @@ OSM.Query = function (map) {
   function clickHandler(e) {
     const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
 
   function clickHandler(e) {
     const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
 
-    OSM.router.route("/query?" + Qs.stringify({ lat, lon }));
+    OSM.router.route("/query?" + new URLSearchParams({ lat, lon }));
   }
 
   function enableQueryMode() {
   }
 
   function enableQueryMode() {
@@ -326,8 +324,8 @@ OSM.Query = function (map) {
   };
 
   page.load = function (path, noCentre) {
   };
 
   page.load = function (path, noCentre) {
-    var params = Qs.parse(path.substring(path.indexOf("?") + 1)),
-        latlng = L.latLng(params.lat, params.lon);
+    const params = new URLSearchParams(path.substring(path.indexOf("?"))),
+          latlng = L.latLng(params.get("lat"), params.get("lon"));
 
     if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
       OSM.router.withoutMoveListener(function () {
 
     if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
       OSM.router.withoutMoveListener(function () {
@@ -335,7 +333,7 @@ OSM.Query = function (map) {
       });
     }
 
       });
     }
 
-    queryOverpass(params.lat, params.lon);
+    queryOverpass(params.get("lat"), params.get("lon"));
   };
 
   page.unload = function (sameController) {
   };
 
   page.unload = function (sameController) {