]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/layers/data.js
Abort loading when data layer is removed
[rails.git] / app / assets / javascripts / index / layers / data.js
1 OSM.initializeDataLayer = function (map) {
2   let dataLoader, loadedBounds;
3   const dataLayer = map.dataLayer;
4
5   dataLayer.setStyle({
6     way: {
7       weight: 3,
8       color: "#000000",
9       opacity: 0.4
10     },
11     area: {
12       weight: 3,
13       color: "#ff0000"
14     },
15     node: {
16       color: "#00ff00"
17     }
18   });
19
20   dataLayer.isWayArea = function () {
21     return false;
22   };
23
24   dataLayer.on("click", function (e) {
25     onSelect(e.layer);
26   });
27
28   dataLayer.on("add", function () {
29     map.fire("overlayadd", { layer: this });
30     map.on("moveend", updateData);
31     updateData();
32   });
33
34   dataLayer.on("remove", function () {
35     if (dataLoader) dataLoader.abort();
36     dataLoader = null;
37     map.off("moveend", updateData);
38     $("#browse_status").empty();
39     map.fire("overlayremove", { layer: this });
40   });
41
42   function updateData() {
43     var bounds = map.getBounds();
44     if (!loadedBounds || !loadedBounds.contains(bounds)) {
45       getData();
46     }
47   }
48
49   function displayFeatureWarning(num_features, add, cancel) {
50     $("#browse_status").html(
51       $("<div class='p-3'>").append(
52         $("<div class='d-flex'>").append(
53           $("<h2 class='flex-grow-1 text-break'>")
54             .text(I18n.t("browse.start_rjs.load_data")),
55           $("<div>").append(
56             $("<button type='button' class='btn-close'>")
57               .attr("aria-label", I18n.t("javascripts.close"))
58               .click(cancel))),
59         $("<p class='alert alert-warning'>")
60           .text(I18n.t("browse.start_rjs.feature_warning", { num_features })),
61         $("<input type='submit' class='btn btn-primary d-block mx-auto'>")
62           .val(I18n.t("browse.start_rjs.load_data"))
63           .click(add)));
64   }
65
66   function displayLoadError(message, close) {
67     $("#browse_status").html(
68       $("<div class='p-3'>").append(
69         $("<div class='d-flex'>").append(
70           $("<h2 class='flex-grow-1 text-break'>")
71             .text(I18n.t("browse.start_rjs.load_data")),
72           $("<div>").append(
73             $("<button type='button' class='btn-close'>")
74               .attr("aria-label", I18n.t("javascripts.close"))
75               .click(close))),
76         $("<div>").append(
77           $("<div class='d-flex'>").append(
78             $("<p class='alert alert-warning'>")
79               .text(I18n.t("browse.start_rjs.feature_error", { message: message }))))));
80   }
81
82   function getData() {
83     var bounds = map.getBounds();
84     var url = "/api/" + OSM.API_VERSION + "/map?bbox=" + bounds.toBBoxString();
85
86     /*
87      * Modern browsers are quite happy showing far more than 100 features in
88      * the data browser, so increase the limit to 4000 by default.
89      */
90     const maxFeatures = 4000;
91
92     if (dataLoader) dataLoader.abort();
93
94     dataLoader = $.ajax({
95       url: url,
96       dataType: "json",
97       success: function (data) {
98         dataLayer.clearLayers();
99
100         var features = dataLayer.buildFeatures(data);
101
102         function addFeatures() {
103           $("#browse_status").empty();
104           dataLayer.addData(features);
105           loadedBounds = bounds;
106         }
107
108         function cancelAddFeatures() {
109           $("#browse_status").empty();
110         }
111
112         if (features.length < maxFeatures) {
113           addFeatures();
114         } else {
115           displayFeatureWarning(features.length, addFeatures, cancelAddFeatures);
116         }
117
118         if (map._objectLayer) {
119           map._objectLayer.bringToFront();
120         }
121
122         dataLoader = null;
123       },
124       error: function (XMLHttpRequest, textStatus) {
125         dataLoader = null;
126         if (textStatus === "abort") { return; }
127
128         function closeError() {
129           $("#browse_status").empty();
130         }
131
132         if (XMLHttpRequest.status === 400 && XMLHttpRequest.responseText) {
133           displayLoadError(XMLHttpRequest.responseText, closeError);
134         } else if (XMLHttpRequest.statusText) {
135           displayLoadError(XMLHttpRequest.statusText, closeError);
136         } else {
137           displayLoadError(String(XMLHttpRequest.status), closeError);
138         }
139       }
140     });
141   }
142
143   function onSelect(layer) {
144     OSM.router.route("/" + layer.feature.type + "/" + layer.feature.id);
145   }
146 };