]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/history.js
4264e5dce34c8501518d1c8937588b3d6ddd52ea
[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(div, html) {
51     const oldList = $("#sidebar_content .changesets ol");
52
53     div.replaceWith(html);
54
55     const prevNewList = oldList.prevAll("ol");
56     if (prevNewList.length) {
57       prevNewList.next(".changeset_more").remove();
58       prevNewList.children().prependTo(oldList);
59       prevNewList.remove();
60     }
61
62     const nextNewList = oldList.nextAll("ol");
63     if (nextNewList.length) {
64       nextNewList.prev(".changeset_more").remove();
65       nextNewList.children().appendTo(oldList);
66       nextNewList.remove();
67     }
68   }
69
70   function update() {
71     const data = new URLSearchParams();
72     const params = new URLSearchParams(location.search);
73
74     if (location.pathname === "/history") {
75       data.set("bbox", map.getBounds().wrap().toBBoxString());
76       const feedLink = $("link[type=\"application/atom+xml\"]"),
77             feedHref = feedLink.attr("href").split("?")[0];
78       feedLink.attr("href", feedHref + "?" + data);
79     }
80
81     data.set("list", "1");
82
83     if (params.has("before")) {
84       data.set("before", params.get("before"));
85     }
86     if (params.has("after")) {
87       data.set("after", params.get("after"));
88     }
89
90     fetch(location.pathname + "?" + data)
91       .then(response => response.text())
92       .then(function (html) {
93         displayFirstChangesets(html);
94         updateMap();
95       });
96   }
97
98   function loadMore(e) {
99     e.preventDefault();
100     e.stopPropagation();
101
102     const div = $(this).parents(".changeset_more");
103
104     $(this).hide();
105     div.find(".loader").show();
106
107     $.get($(this).attr("href"), function (html) {
108       displayMoreChangesets(div, html);
109       updateMap();
110     });
111   }
112
113   let changesets = [];
114
115   function updateBounds() {
116     group.clearLayers();
117
118     for (const changeset of changesets) {
119       const bottomLeft = map.project(L.latLng(changeset.bbox.minlat, changeset.bbox.minlon)),
120             topRight = map.project(L.latLng(changeset.bbox.maxlat, changeset.bbox.maxlon)),
121             width = topRight.x - bottomLeft.x,
122             height = bottomLeft.y - topRight.y,
123             minSize = 20; // Min width/height of changeset in pixels
124
125       if (width < minSize) {
126         bottomLeft.x -= ((minSize - width) / 2);
127         topRight.x += ((minSize - width) / 2);
128       }
129
130       if (height < minSize) {
131         bottomLeft.y += ((minSize - height) / 2);
132         topRight.y -= ((minSize - height) / 2);
133       }
134
135       changeset.bounds = L.latLngBounds(map.unproject(bottomLeft),
136                                         map.unproject(topRight));
137     }
138
139     changesets.sort(function (a, b) {
140       return b.bounds.getSize() - a.bounds.getSize();
141     });
142
143     for (const changeset of changesets) {
144       const rect = L.rectangle(changeset.bounds,
145                                { weight: 2, color: "#FF9500", opacity: 1, fillColor: "#FFFFAF", fillOpacity: 0 });
146       rect.id = changeset.id;
147       rect.addTo(group);
148     }
149   }
150
151   function updateMap() {
152     changesets = $("[data-changeset]").map(function (index, element) {
153       return $(element).data("changeset");
154     }).get().filter(function (changeset) {
155       return changeset.bbox;
156     });
157
158     updateBounds();
159
160     if (location.pathname !== "/history") {
161       const bounds = group.getBounds();
162       if (bounds.isValid()) map.fitBounds(bounds);
163     }
164   }
165
166   page.pushstate = page.popstate = function (path) {
167     OSM.loadSidebarContent(path, page.load);
168   };
169
170   page.load = function () {
171     map.addLayer(group);
172
173     if (location.pathname === "/history") {
174       map.on("moveend", update);
175     }
176
177     map.on("zoomend", updateBounds);
178
179     update();
180   };
181
182   page.unload = function () {
183     map.removeLayer(group);
184     map.off("moveend", update);
185     map.off("zoomend", updateBounds);
186   };
187
188   return page;
189 };