Merge remote-tracking branch 'upstream/pull/5669'

This commit is contained in:
Tom Hughes 2025-02-15 09:11:03 +00:00
commit 12920987f5
7 changed files with 85 additions and 91 deletions

View file

@ -41,19 +41,18 @@ $(document).ready(function () {
$("#sidebar_content") $("#sidebar_content")
.empty(); .empty();
$.ajax({ fetch(content_path, { headers: { "accept": "text/html", "x-requested-with": "XMLHttpRequest" } })
url: content_path, .then(response => {
dataType: "html",
complete: function (xhr) {
$("#flash").empty(); $("#flash").empty();
$("#sidebar_loader").removeClass("delayed-fade-in").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")) { return response.text();
var title = xhr.getResponseHeader("X-Page-Title"); })
document.title = decodeURIComponent(title); .then(html => {
} const content = $(html);
$("head") $("head")
.find("link[type=\"application/atom+xml\"]") .find("link[type=\"application/atom+xml\"]")
@ -67,8 +66,7 @@ $(document).ready(function () {
if (callback) { if (callback) {
callback(); callback();
} }
} });
});
}; };
var params = OSM.mapParams(); var params = OSM.mapParams();

View file

@ -56,24 +56,23 @@ OSM.History = function (map) {
} }
function update() { function update() {
var data = { list: "1" }; const data = new URLSearchParams();
if (window.location.pathname === "/history") { if (window.location.pathname === "/history") {
data.bbox = map.getBounds().wrap().toBBoxString(); data.set("bbox", map.getBounds().wrap().toBBoxString());
var feedLink = $("link[type=\"application/atom+xml\"]"), var feedLink = $("link[type=\"application/atom+xml\"]"),
feedHref = feedLink.attr("href").split("?")[0]; feedHref = feedLink.attr("href").split("?")[0];
feedLink.attr("href", feedHref + "?bbox=" + data.bbox); feedLink.attr("href", feedHref + "?" + data);
} }
$.ajax({ data.set("list", "1");
url: window.location.pathname,
method: "GET", fetch(window.location.pathname + "?" + data)
data: data, .then(response => response.text())
success: function (html) { .then(function (html) {
displayFirstChangesets(html); displayFirstChangesets(html);
updateMap(); updateMap();
} });
});
} }
function loadMore(e) { function loadMore(e) {

View file

@ -79,20 +79,27 @@ OSM.initializeDataLayer = function (map) {
function getData() { function getData() {
var bounds = map.getBounds(); var bounds = map.getBounds();
var url = "/api/" + OSM.API_VERSION + "/map?bbox=" + bounds.toBBoxString(); var url = "/api/" + OSM.API_VERSION + "/map.json?bbox=" + bounds.toBBoxString();
/* /*
* Modern browsers are quite happy showing far more than 100 features in * Modern browsers are quite happy showing far more than 100 features in
* the data browser, so increase the limit to 4000 by default. * the data browser, so increase the limit to 4000.
*/ */
const maxFeatures = 4000; const maxFeatures = 4000;
if (dataLoader) dataLoader.abort(); if (dataLoader) dataLoader.abort();
dataLoader = $.ajax({ dataLoader = new AbortController();
url: url, fetch(url, { signal: dataLoader.signal })
dataType: "json", .then(response => {
success: function (data) { if (response.ok) return response.json();
const status = response.statusText || response.status;
if (response.status !== 400) throw new Error(status);
return response.text().then(text => {
throw new Error(text || status);
});
})
.then(function (data) {
dataLayer.clearLayers(); dataLayer.clearLayers();
var features = dataLayer.buildFeatures(data); var features = dataLayer.buildFeatures(data);
@ -116,26 +123,15 @@ OSM.initializeDataLayer = function (map) {
if (map._objectLayer) { if (map._objectLayer) {
map._objectLayer.bringToFront(); map._objectLayer.bringToFront();
} }
})
.catch(function (error) {
if (error.name === "AbortError") return;
dataLoader = null; displayLoadError(error?.message, () => {
},
error: function (XMLHttpRequest, textStatus) {
dataLoader = null;
if (textStatus === "abort") return;
function closeError() {
$("#browse_status").empty(); $("#browse_status").empty();
} });
})
if (XMLHttpRequest.status === 400 && XMLHttpRequest.responseText) { .finally(() => dataLoader = null);
displayLoadError(XMLHttpRequest.responseText, closeError);
} else if (XMLHttpRequest.statusText) {
displayLoadError(XMLHttpRequest.statusText, closeError);
} else {
displayLoadError(String(XMLHttpRequest.status), closeError);
}
}
});
} }
function onSelect(layer) { function onSelect(layer) {

View file

@ -75,10 +75,12 @@ OSM.initializeNotesLayer = function (map) {
if (noteLoader) noteLoader.abort(); if (noteLoader) noteLoader.abort();
noteLoader = $.ajax({ noteLoader = new AbortController();
url: url, fetch(url, { signal: noteLoader.signal })
success: success .then(response => response.json())
}); .then(success)
.catch(() => {})
.finally(() => noteLoader = null);
} }
function success(json) { function success(json) {
@ -93,8 +95,6 @@ OSM.initializeNotesLayer = function (map) {
for (var id in oldNotes) { for (var id in oldNotes) {
noteLayer.removeLayer(oldNotes[id]); noteLayer.removeLayer(oldNotes[id]);
} }
noteLoader = null;
} }
} }
}; };

View file

@ -157,16 +157,17 @@ OSM.Query = function (map) {
$section.data("ajax").abort(); $section.data("ajax").abort();
} }
$section.data("ajax", $.ajax({ $section.data("ajax", new AbortController());
url: url, fetch(url, {
method: "POST", method: "POST",
data: { body: new URLSearchParams({
data: "[timeout:10][out:json];" + query data: "[timeout:10][out:json];" + query
}, }),
xhrFields: { credentials: credentials ? "include" : "same-origin",
withCredentials: credentials signal: $section.data("ajax").signal
}, })
success: function (results) { .then(response => response.json())
.then(function (results) {
var elements; var elements;
$section.find(".loader").hide(); $section.find(".loader").hide();
@ -221,16 +222,17 @@ OSM.Query = function (map) {
.text(I18n.t("javascripts.query.nothing_found")) .text(I18n.t("javascripts.query.nothing_found"))
.appendTo($ul); .appendTo($ul);
} }
}, })
error: function (xhr, status, error) { .catch(function (error) {
if (error.name === "AbortError") return;
$section.find(".loader").hide(); $section.find(".loader").hide();
$("<li>") $("<li>")
.addClass("list-group-item") .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) {

View file

@ -47,21 +47,19 @@ OSM.Search = function (map) {
var div = $(this).parents(".search_more"), var div = $(this).parents(".search_more"),
csrf_param = $("meta[name=csrf-param]").attr("content"), csrf_param = $("meta[name=csrf-param]").attr("content"),
csrf_token = $("meta[name=csrf-token]").attr("content"), csrf_token = $("meta[name=csrf-token]").attr("content"),
params = {}; params = new URLSearchParams();
$(this).hide(); $(this).hide();
div.find(".loader").show(); div.find(".loader").show();
params[csrf_param] = csrf_token; params.set(csrf_param, csrf_token);
$.ajax({ fetch($(this).attr("href"), {
url: $(this).attr("href"),
method: "POST", method: "POST",
data: params, body: params
success: function (data) { })
div.replaceWith(data); .then(response => response.text())
} .then(data => div.replaceWith(data));
});
} }
function showSearchResult() { function showSearchResult() {
@ -125,19 +123,20 @@ OSM.Search = function (map) {
var entry = $(this), var entry = $(this),
csrf_param = $("meta[name=csrf-param]").attr("content"), csrf_param = $("meta[name=csrf-param]").attr("content"),
csrf_token = $("meta[name=csrf-token]").attr("content"), csrf_token = $("meta[name=csrf-token]").attr("content"),
params = { params = new URLSearchParams({
zoom: map.getZoom(), zoom: map.getZoom(),
minlon: map.getBounds().getWest(), minlon: map.getBounds().getWest(),
minlat: map.getBounds().getSouth(), minlat: map.getBounds().getSouth(),
maxlon: map.getBounds().getEast(), maxlon: map.getBounds().getEast(),
maxlat: map.getBounds().getNorth() maxlat: map.getBounds().getNorth()
}; });
params[csrf_param] = csrf_token; params.set(csrf_param, csrf_token);
$.ajax({ fetch(entry.data("href"), {
url: entry.data("href"),
method: "POST", method: "POST",
data: params, body: params
success: function (html) { })
.then(response => response.text())
.then(function (html) {
entry.html(html); entry.html(html);
// go to first result of first geocoder // go to first result of first geocoder
if (index === 0) { if (index === 0) {
@ -146,8 +145,7 @@ OSM.Search = function (map) {
panToSearchResult(firstResult.data()); panToSearchResult(firstResult.data());
} }
} }
} });
});
}); });
return map.getState(); return map.getState();

View file

@ -265,9 +265,7 @@ L.OSM.Map = L.Map.extend({
this.removeObject(); this.removeObject();
if (object.type === "note" || object.type === "changeset") { if (object.type === "note" || object.type === "changeset") {
this._objectLoader = { this._objectLoader = { abort: () => {} };
abort: function () {}
};
this._object = object; this._object = object;
this._objectLayer = L.featureGroup().addTo(this); this._objectLayer = L.featureGroup().addTo(this);
@ -295,10 +293,13 @@ L.OSM.Map = L.Map.extend({
this.fire("overlayadd", { layer: this._objectLayer }); this.fire("overlayadd", { layer: this._objectLayer });
} else { // element handled by L.OSM.DataLayer } else { // element handled by L.OSM.DataLayer
var map = this; var map = this;
this._objectLoader = $.ajax({ this._objectLoader = new AbortController();
url: OSM.apiUrl(object), fetch(OSM.apiUrl(object), {
dataType: "json", headers: { accept: "application/json" },
success: function (data) { signal: this._objectLoader.signal
})
.then(response => response.json())
.then(function (data) {
map._object = object; map._object = object;
map._objectLayer = new L.OSM.DataLayer(null, { map._objectLayer = new L.OSM.DataLayer(null, {
@ -320,8 +321,8 @@ L.OSM.Map = L.Map.extend({
if (callback) callback(map._objectLayer.getBounds()); if (callback) callback(map._objectLayer.getBounds());
map.fire("overlayadd", { layer: map._objectLayer }); map.fire("overlayadd", { layer: map._objectLayer });
} })
}); .catch(() => {});
} }
}, },