]> git.openstreetmap.org Git - rails.git/commitdiff
Merge remote-tracking branch 'upstream/pull/5278'
authorTom Hughes <tom@compton.nu>
Sun, 16 Feb 2025 13:11:45 +0000 (13:11 +0000)
committerTom Hughes <tom@compton.nu>
Sun, 16 Feb 2025 13:11:45 +0000 (13:11 +0000)
1  2 
app/assets/javascripts/index.js

index 194ed82f7451098bd318d63a661a2b35221087c0,2d943b44d641d34528f13d54144e2a42210c6bd9..4903703cd39d4635d1afdef7bbd8370091cea4ad
@@@ -1,7 -1,7 +1,7 @@@
  //= require_self
  //= require leaflet.sidebar
  //= require leaflet.sidebar-pane
 -//= require leaflet.locatecontrol/src/L.Control.Locate
 +//= require leaflet.locatecontrol/dist/L.Control.Locate.umd
  //= require leaflet.locate
  //= require leaflet.layers
  //= require leaflet.key
  //= require index/directions
  //= require index/changeset
  //= require index/query
 +//= require index/home
  //= require router
 -//= require qs/dist/qs
  
  $(document).ready(function () {
 -  var loaderTimeout;
 -
 -  var map = new L.OSM.Map("map", {
 +  const map = new L.OSM.Map("map", {
      zoomControl: false,
      layerControl: false,
      contextmenu: true,
    });
  
    OSM.loadSidebarContent = function (path, callback) {
 -    var content_path = path;
 +    let content_path = path;
  
      map.setSidebarOverlaid(false);
  
 -    clearTimeout(loaderTimeout);
 -
 -    loaderTimeout = setTimeout(function () {
 -      $("#sidebar_loader").show();
 -    }, 200);
 +    $("#sidebar_loader").show().addClass("delayed-fade-in");
  
 -    // IE<10 doesn't respect Vary: X-Requested-With header, so
 -    // prevent caching the XHR response as a full-page URL.
 +    // Prevent caching the XHR response as a full-page URL
 +    // https://github.com/openstreetmap/openstreetmap-website/issues/5663
      if (content_path.indexOf("?") >= 0) {
        content_path += "&xhr=1";
      } else {
      $("#sidebar_content")
        .empty();
  
 -    $.ajax({
 -      url: content_path,
 -      dataType: "html",
 -      complete: function (xhr) {
 -        clearTimeout(loaderTimeout);
 +    fetch(content_path, { headers: { "accept": "text/html", "x-requested-with": "XMLHttpRequest" } })
 +      .then(response => {
          $("#flash").empty();
 -        $("#sidebar_loader").hide();
 +        $("#sidebar_loader").removeClass("delayed-fade-in").hide();
  
 -        var content = $(xhr.responseText);
 +        const title = response.headers.get("X-Page-Title");
 +        if (title) document.title = decodeURIComponent(title);
  
 -        if (xhr.getResponseHeader("X-Page-Title")) {
 -          var title = xhr.getResponseHeader("X-Page-Title");
 -          document.title = decodeURIComponent(title);
 -        }
 +        return response.text();
 +      })
 +      .then(html => {
 +        const content = $(html);
  
          $("head")
            .find("link[type=\"application/atom+xml\"]")
          if (callback) {
            callback();
          }
 -      }
 -    });
 +      });
    };
  
 -  var params = OSM.mapParams();
 +  const params = OSM.mapParams();
  
    map.attributionControl.setPrefix("");
  
      }
    });
  
 -  var sidebar = L.OSM.sidebar("#map-ui")
 +  const sidebar = L.OSM.sidebar("#map-ui")
      .addTo(map);
  
 -  var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
 +  const 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();
 +    const firstContainer = controls[0].getContainer();
      $(firstContainer).find(".control-button").first()
        .addClass("control-button-first");
  
 -    var lastContainer = controls[controls.length - 1].getContainer();
 +    const lastContainer = controls[controls.length - 1].getContainer();
      $(lastContainer).find(".control-button").last()
        .addClass("control-button-last");
    }
  
    $(".leaflet-control .control-button").tooltip({ placement: "left", container: "body" });
  
 -  var expiry = new Date();
 +  const 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(),
      Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
    });
  
 -  var bannerExpiry = new Date();
 +  const bannerExpiry = new Date();
    bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
  
    $("#banner .btn-close").on("click", function (e) {
 -    var cookieId = e.target.id;
 +    const cookieId = e.target.id;
      $("#banner").hide();
      e.preventDefault();
      if (cookieId) {
    });
  
    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];
 +        const goal = OSM.MATOMO.goals[e.layer.options.layerId];
  
          if (goal) {
            $("body").trigger("matomogoal", goal);
      L.marker([params.mlat, params.mlon]).addTo(map);
    }
  
 -  $("#homeanchor").on("click", function (e) {
 -    e.preventDefault();
 -
 -    var data = $(this).data(),
 -        center = L.latLng(data.lat, data.lon);
 -
 -    map.setView(center, data.zoom);
 -    L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
 -  });
 -
    function remoteEditHandler(bbox, object) {
 -    var remoteEditHost = "http://127.0.0.1:8111",
 -        osmHost = location.protocol + "//" + location.host,
 -        query = {
 -          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 () {
 +    const remoteEditHost = "http://127.0.0.1:8111",
 +          osmHost = location.protocol + "//" + location.host,
 +          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.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"));
          });
      }
  
    }
  
    $("a[data-editor=remote]").click(function (e) {
 -    var params = OSM.mapParams(this.search);
 +    const params = OSM.mapParams(this.search);
      remoteEditHandler(map.getBounds(), params.object);
      e.preventDefault();
    });
    }
  
    OSM.Index = function (map) {
 -    var page = {};
 +    const page = {};
  
      page.pushstate = page.popstate = function () {
        map.setSidebarOverlaid(true);
      };
  
      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();
    };
  
    OSM.Browse = function (map, type) {
 -    var page = {};
 +    const page = {};
  
-     page.pushstate = page.popstate = function (path, id) {
+     page.pushstate = page.popstate = function (path, id, version) {
        OSM.loadSidebarContent(path, function () {
-         addObject(type, id);
+         addObject(type, id, version);
        });
      };
  
-     page.load = function (path, id) {
-       addObject(type, id, true);
+     page.load = function (path, id, version) {
+       addObject(type, id, version, true);
      };
  
-     function addObject(type, id, center) {
+     function addObject(type, id, version, center) {
 +      const hashParams = OSM.parseHash(window.location.hash);
-       map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
+       map.addObject({ type: type, id: parseInt(id, 10), version: version && parseInt(version, 10) }, function (bounds) {
 -        if (!window.location.hash && bounds.isValid() &&
 +        if (!hashParams.center && bounds.isValid() &&
              (center || !map.getBounds().contains(bounds))) {
            OSM.router.withoutMoveListener(function () {
              map.fitBounds(bounds);
    };
  
    OSM.OldBrowse = function () {
 -    var page = {};
 +    const page = {};
  
      page.pushstate = page.popstate = function (path) {
        OSM.loadSidebarContent(path);
      return page;
    };
  
 -  var history = OSM.History(map);
 +  const history = OSM.History(map);
  
    OSM.router = OSM.Router(map, {
      "/": OSM.Index(map),
      "/user/:display_name/history": history,
      "/note/:id": OSM.Note(map),
      "/node/:id(/history)": OSM.Browse(map, "node"),
-     "/node/:id/history/:version": OSM.OldBrowse(),
+     "/node/:id/history/:version": OSM.Browse(map, "node"),
      "/way/:id(/history)": OSM.Browse(map, "way"),
      "/way/:id/history/:version": OSM.OldBrowse(),
      "/relation/:id(/history)": OSM.Browse(map, "relation"),
      "/relation/:id/history/:version": OSM.OldBrowse(),
      "/changeset/:id": OSM.Changeset(map),
 -    "/query": OSM.Query(map)
 +    "/query": OSM.Query(map),
 +    "/account/home": OSM.Home(map)
    });
  
    if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
  
      if (OSM.router.route(this.pathname + this.search + this.hash)) {
        e.preventDefault();
 +      if (this.pathname !== "/directions") {
 +        $("header").addClass("closed");
 +      }
      }
    });