OSM.initializeDataLayer = function (map) {
- var loadedBounds;
- var dataLayer = map.dataLayer;
+ let dataLoader, loadedBounds;
+ const dataLayer = map.dataLayer;
dataLayer.setStyle({
way: {
});
dataLayer.on("remove", function () {
+ if (dataLoader) dataLoader.abort();
+ dataLoader = null;
map.off("moveend", updateData);
$("#browse_status").empty();
map.fire("overlayremove", { layer: this });
});
function updateData() {
- var bounds = map.getBounds();
+ const bounds = map.getBounds();
if (!loadedBounds || !loadedBounds.contains(bounds)) {
getData();
}
}
- function displayFeatureWarning(count, limit, add, cancel) {
+ function displayFeatureWarning(num_features, add, cancel) {
$("#browse_status").html(
$("<div class='p-3'>").append(
$("<div class='d-flex'>").append(
.attr("aria-label", I18n.t("javascripts.close"))
.click(cancel))),
$("<p class='alert alert-warning'>")
- .text(I18n.t("browse.start_rjs.feature_warning", { num_features: count, max_features: limit })),
+ .text(I18n.t("browse.start_rjs.feature_warning", { num_features })),
$("<input type='submit' class='btn btn-primary d-block mx-auto'>")
.val(I18n.t("browse.start_rjs.load_data"))
.click(add)));
$("<button type='button' class='btn-close'>")
.attr("aria-label", I18n.t("javascripts.close"))
.click(close))),
- $("<div>").append(
- $("<div class='d-flex'>").append(
- $("<p class='alert alert-warning'>")
- .text(I18n.t("browse.start_rjs.feature_error", { message: message }))))));
+ $("<p class='alert alert-warning'>")
+ .text(I18n.t("browse.start_rjs.feature_error", { message: message }))));
}
- var dataLoader;
-
function getData() {
- var bounds = map.getBounds();
- var url = "/api/" + OSM.API_VERSION + "/map?bbox=" + bounds.toBBoxString();
+ const bounds = map.getBounds();
+ const url = "/api/" + OSM.API_VERSION + "/map.json?bbox=" + bounds.toBBoxString();
/*
* Modern browsers are quite happy showing far more than 100 features in
- * the data browser, so increase the limit to 2000 by default, but keep
- * it restricted to 500 for IE8 and 100 for older IEs.
+ * the data browser, so increase the limit to 4000.
*/
- var maxFeatures = 2000;
-
- /*@cc_on
- if (navigator.appVersion < 8) {
- maxFeatures = 100;
- } else if (navigator.appVersion < 9) {
- maxFeatures = 500;
- }
- @*/
+ const maxFeatures = 4000;
if (dataLoader) dataLoader.abort();
- dataLoader = $.ajax({
- url: url,
- success: function (xml) {
+ dataLoader = new AbortController();
+ fetch(url, { signal: dataLoader.signal })
+ .then(response => {
+ 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();
- var features = dataLayer.buildFeatures(xml);
+ const features = dataLayer.buildFeatures(data);
function addFeatures() {
$("#browse_status").empty();
if (features.length < maxFeatures) {
addFeatures();
} else {
- displayFeatureWarning(features.length, maxFeatures, addFeatures, cancelAddFeatures);
+ displayFeatureWarning(features.length, addFeatures, cancelAddFeatures);
}
if (map._objectLayer) {
map._objectLayer.bringToFront();
}
+ })
+ .catch(function (error) {
+ if (error.name === "AbortError") return;
- dataLoader = null;
- },
- error: function (XMLHttpRequest, textStatus) {
- dataLoader = null;
- if (textStatus === "abort") { return; }
-
- function closeError() {
+ displayLoadError(error?.message, () => {
$("#browse_status").empty();
- }
-
- if (XMLHttpRequest.status === 400 && XMLHttpRequest.responseText) {
- displayLoadError(XMLHttpRequest.responseText, closeError);
- } else if (XMLHttpRequest.statusText) {
- displayLoadError(XMLHttpRequest.statusText, closeError);
- } else {
- displayLoadError(String(XMLHttpRequest.status), closeError);
- }
- }
- });
+ });
+ })
+ .finally(() => dataLoader = null);
}
function onSelect(layer) {