]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index.js
Further usage of URLSearchParams
[rails.git] / app / assets / javascripts / index.js
index 4dfc849feb5d9d24361566c50de3793d1c65ebd6..4555553ee7fb6868950d208a1b0233d2928d73f2 100644 (file)
@@ -22,7 +22,6 @@
 //= require index/changeset
 //= require index/query
 //= require router
-//= require qs/dist/qs
 
 $(document).ready(function () {
   var map = new L.OSM.Map("map", {
@@ -98,9 +97,7 @@ $(document).ready(function () {
   var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
 
   function addControlGroup(controls) {
-    controls.forEach(function (control) {
-      control.addTo(map);
-    });
+    for (const control of controls) control.addTo(map);
 
     var firstContainer = controls[0].getContainer();
     $(firstContainer).find(".control-button").first()
@@ -173,7 +170,7 @@ $(document).ready(function () {
   var expiry = new Date();
   expiry.setYear(expiry.getFullYear() + 10);
 
-  map.on("moveend layeradd layerremove", function () {
+  map.on("moveend baselayerchange overlayadd overlayremove", function () {
     updateLinks(
       map.getCenter().wrap(),
       map.getZoom(),
@@ -205,9 +202,9 @@ $(document).ready(function () {
   });
 
   if (OSM.MATOMO) {
-    map.on("layeradd", function (e) {
+    map.on("baselayerchange overlayadd", function (e) {
       if (e.layer.options) {
-        var goal = OSM.MATOMO.goals[e.layer.options.keyid];
+        var goal = OSM.MATOMO.goals[e.layer.options.layerId];
 
         if (goal) {
           $("body").trigger("matomogoal", goal);
@@ -239,36 +236,27 @@ $(document).ready(function () {
   function remoteEditHandler(bbox, object) {
     var remoteEditHost = "http://127.0.0.1:8111",
         osmHost = location.protocol + "//" + location.host,
-        query = {
+        query = new URLSearchParams({
           left: bbox.getWest() - 0.0001,
           top: bbox.getNorth() + 0.0001,
           right: bbox.getEast() + 0.0001,
           bottom: bbox.getSouth() - 0.0001
-        };
+        });
 
-    if (object && object.type !== "note") query.select = object.type + object.id; // can't select notes
-    sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + Qs.stringify(query), function () {
+    if (object && object.type !== "note") query.set("select", object.type + object.id); // can't select notes
+    sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + query, function () {
       if (object && object.type === "note") {
-        var noteQuery = { url: osmHost + OSM.apiUrl(object) };
-        sendRemoteEditCommand(remoteEditHost + "/import?" + Qs.stringify(noteQuery));
+        const noteQuery = new URLSearchParams({ url: osmHost + OSM.apiUrl(object) });
+        sendRemoteEditCommand(remoteEditHost + "/import?" + noteQuery);
       }
     });
 
     function sendRemoteEditCommand(url, callback) {
-      var iframe = $("<iframe>");
-      var timeoutId = setTimeout(function () {
-        alert(I18n.t("site.index.remote_failed"));
-        iframe.remove();
-      }, 5000);
-
-      iframe
-        .hide()
-        .appendTo("body")
-        .attr("src", url)
-        .on("load", function () {
-          clearTimeout(timeoutId);
-          iframe.remove();
-          if (callback) callback();
+      fetch(url, { mode: "no-cors", signal: AbortSignal.timeout(5000) })
+        .then(callback)
+        .catch(function () {
+          // eslint-disable-next-line no-alert
+          alert(I18n.t("site.index.remote_failed"));
         });
     }
 
@@ -304,9 +292,9 @@ $(document).ready(function () {
     };
 
     page.load = function () {
-      var params = Qs.parse(location.search.substring(1));
-      if (params.query) {
-        $("#sidebar .search_form input[name=query]").value(params.query);
+      const params = new URLSearchParams(location.search);
+      if (params.has("query")) {
+        $("#sidebar .search_form input[name=query]").value(params.get("query"));
       }
       if (!("autofocus" in document.createElement("input"))) {
         $("#sidebar .search_form input[name=query]").focus();
@@ -405,6 +393,9 @@ $(document).ready(function () {
 
     if (OSM.router.route(this.pathname + this.search + this.hash)) {
       e.preventDefault();
+      if (this.pathname !== "/directions") {
+        $("header").addClass("closed");
+      }
     }
   });