]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/history.js
Merge remote-tracking branch 'upstream/pull/5796'
[rails.git] / app / assets / javascripts / index / history.js
1 //= require jquery-simulate/jquery.simulate
2
3 OSM.History = function (map) {
4   const page = {};
5
6   $("#sidebar_content")
7     .on("click", ".changeset_more a", loadMore)
8     .on("mouseover", "[data-changeset]", function () {
9       highlightChangeset($(this).data("changeset").id);
10     })
11     .on("mouseout", "[data-changeset]", function () {
12       unHighlightChangeset($(this).data("changeset").id);
13     });
14
15   const group = L.featureGroup()
16     .on("mouseover", function (e) {
17       highlightChangeset(e.layer.id);
18     })
19     .on("mouseout", function (e) {
20       unHighlightChangeset(e.layer.id);
21     })
22     .on("click", function (e) {
23       clickChangeset(e.layer.id, e.originalEvent);
24     });
25
26   group.getLayerId = function (layer) {
27     return layer.id;
28   };
29
30   function highlightChangeset(id) {
31     const layer = group.getLayer(id);
32     if (layer) layer.setStyle({ fillOpacity: 0.3, color: "#FF6600", weight: 3 });
33     $("#changeset_" + id).addClass("selected");
34   }
35
36   function unHighlightChangeset(id) {
37     const layer = group.getLayer(id);
38     if (layer) layer.setStyle({ fillOpacity: 0, color: "#FF9500", weight: 2 });
39     $("#changeset_" + id).removeClass("selected");
40   }
41
42   function clickChangeset(id, e) {
43     $("#changeset_" + id).find("a.changeset_id").simulate("click", e);
44   }
45
46   function displayFirstChangesets(html) {
47     $("#sidebar_content .changesets").html(html);
48   }
49
50   function displayMoreChangesets(html) {
51     $("#sidebar_content .changeset_more").replaceWith(html);
52     const oldList = $("#sidebar_content .changesets ol").first();
53     const newList = oldList.next("ol");
54     newList.children().appendTo(oldList);
55     newList.remove();
56   }
57
58   function update() {
59     const data = new URLSearchParams();
60     const params = new URLSearchParams(location.search);
61
62     if (location.pathname === "/history") {
63       data.set("bbox", map.getBounds().wrap().toBBoxString());
64       const feedLink = $("link[type=\"application/atom+xml\"]"),
65             feedHref = feedLink.attr("href").split("?")[0];
66       feedLink.attr("href", feedHref + "?" + data);
67     }
68
69     data.set("list", "1");
70
71     if (params.has("before")) {
72       data.set("before", params.get("before"));
73     }
74
75     fetch(location.pathname + "?" + data)
76       .then(response => response.text())
77       .then(function (html) {
78         displayFirstChangesets(html);
79         updateMap();
80       });
81   }
82
83   function loadMore(e) {
84     e.preventDefault();
85     e.stopPropagation();
86
87     const div = $(this).parents(".changeset_more");
88
89     $(this).hide();
90     div.find(".loader").show();
91
92     $.get($(this).attr("href"), function (html) {
93       displayMoreChangesets(html);
94       updateMap();
95     });
96   }
97
98   let changesets = [];
99
100   function updateBounds() {
101     group.clearLayers();
102
103     for (const changeset of changesets) {
104       const bottomLeft = map.project(L.latLng(changeset.bbox.minlat, changeset.bbox.minlon)),
105             topRight = map.project(L.latLng(changeset.bbox.maxlat, changeset.bbox.maxlon)),
106             width = topRight.x - bottomLeft.x,
107             height = bottomLeft.y - topRight.y,
108             minSize = 20; // Min width/height of changeset in pixels
109
110       if (width < minSize) {
111         bottomLeft.x -= ((minSize - width) / 2);
112         topRight.x += ((minSize - width) / 2);
113       }
114
115       if (height < minSize) {
116         bottomLeft.y += ((minSize - height) / 2);
117         topRight.y -= ((minSize - height) / 2);
118       }
119
120       changeset.bounds = L.latLngBounds(map.unproject(bottomLeft),
121                                         map.unproject(topRight));
122     }
123
124     changesets.sort(function (a, b) {
125       return b.bounds.getSize() - a.bounds.getSize();
126     });
127
128     for (const changeset of changesets) {
129       const rect = L.rectangle(changeset.bounds,
130                                { weight: 2, color: "#FF9500", opacity: 1, fillColor: "#FFFFAF", fillOpacity: 0 });
131       rect.id = changeset.id;
132       rect.addTo(group);
133     }
134   }
135
136   function updateMap() {
137     changesets = $("[data-changeset]").map(function (index, element) {
138       return $(element).data("changeset");
139     }).get().filter(function (changeset) {
140       return changeset.bbox;
141     });
142
143     updateBounds();
144
145     if (location.pathname !== "/history") {
146       const bounds = group.getBounds();
147       if (bounds.isValid()) map.fitBounds(bounds);
148     }
149   }
150
151   page.pushstate = page.popstate = function (path) {
152     OSM.loadSidebarContent(path, page.load);
153   };
154
155   page.load = function () {
156     map.addLayer(group);
157
158     if (location.pathname === "/history") {
159       map.on("moveend", update);
160     }
161
162     map.on("zoomend", updateBounds);
163
164     update();
165   };
166
167   page.unload = function () {
168     map.removeLayer(group);
169     map.off("moveend", update);
170     map.off("zoomend", updateBounds);
171   };
172
173   return page;
174 };