]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/history.js
Change colors of bboxes when they enter/exit the viewport
[rails.git] / app / assets / javascripts / index / history.js
1 //= require jquery-simulate/jquery.simulate
2 //= require ./history-changesets-layer
3
4 OSM.History = function (map) {
5   const page = {};
6
7   $("#sidebar_content")
8     .on("click", ".changeset_more a", loadMoreChangesets)
9     .on("mouseover", "[data-changeset]", function () {
10       toggleChangesetHighlight($(this).data("changeset").id, true);
11     })
12     .on("mouseout", "[data-changeset]", function () {
13       toggleChangesetHighlight($(this).data("changeset").id, false);
14     });
15
16   const changesetsLayer = new OSM.HistoryChangesetsLayer()
17     .on("mouseover", function (e) {
18       toggleChangesetHighlight(e.layer.id, true);
19     })
20     .on("mouseout", function (e) {
21       toggleChangesetHighlight(e.layer.id, false);
22     })
23     .on("click", function (e) {
24       clickChangeset(e.layer.id, e.originalEvent);
25     });
26
27   let changesetIntersectionObserver;
28
29   function disableChangesetIntersectionObserver() {
30     if (changesetIntersectionObserver) {
31       changesetIntersectionObserver.disconnect();
32       changesetIntersectionObserver = null;
33     }
34   }
35
36   function enableChangesetIntersectionObserver() {
37     disableChangesetIntersectionObserver();
38     if (!window.IntersectionObserver) return;
39
40     let keepInitialLocation = true;
41
42     changesetIntersectionObserver = new IntersectionObserver((entries) => {
43       let closestTargetToTop,
44           closestDistanceToTop = Infinity,
45           closestTargetToBottom,
46           closestDistanceToBottom = Infinity;
47
48       for (const entry of entries) {
49         const id = $(entry.target).data("changeset")?.id;
50
51         if (entry.isIntersecting) {
52           if (id) changesetsLayer.setChangesetSidebarRelativePosition(id, 0);
53           continue;
54         }
55
56         const distanceToTop = entry.rootBounds.top - entry.boundingClientRect.bottom;
57         const distanceToBottom = entry.boundingClientRect.top - entry.rootBounds.bottom;
58
59         if (id) changesetsLayer.setChangesetSidebarRelativePosition(id, distanceToTop >= 0 ? 1 : -1);
60
61         if (distanceToTop >= 0 && distanceToTop < closestDistanceToTop) {
62           closestDistanceToTop = distanceToTop;
63           closestTargetToTop = entry.target;
64         }
65         if (distanceToBottom >= 0 && distanceToBottom <= closestDistanceToBottom) {
66           closestDistanceToBottom = distanceToBottom;
67           closestTargetToBottom = entry.target;
68         }
69       }
70
71       if (keepInitialLocation) {
72         keepInitialLocation = false;
73         return;
74       }
75
76       if (closestTargetToTop && closestDistanceToTop < closestDistanceToBottom) {
77         const id = $(closestTargetToTop).data("changeset")?.id;
78         if (id) {
79           OSM.router.replace(location.pathname + "?" + new URLSearchParams({ before: id }) + location.hash);
80         }
81       } else if (closestTargetToBottom) {
82         const id = $(closestTargetToBottom).data("changeset")?.id;
83         if (id) {
84           OSM.router.replace(location.pathname + "?" + new URLSearchParams({ after: id }) + location.hash);
85         }
86       }
87     }, { root: $("#sidebar")[0] });
88
89     $("#sidebar_content .changesets ol").children().each(function () {
90       changesetIntersectionObserver.observe(this);
91     });
92   }
93
94   function toggleChangesetHighlight(id, state) {
95     changesetsLayer.toggleChangesetHighlight(id, state);
96     $("#changeset_" + id).toggleClass("selected", state);
97   }
98
99   function clickChangeset(id, e) {
100     $("#changeset_" + id).find("a.changeset_id").simulate("click", e);
101   }
102
103   function displayFirstChangesets(html) {
104     $("#sidebar_content .changesets").html(html);
105
106     if (location.pathname === "/history") {
107       setPaginationMapHashes();
108     }
109   }
110
111   function displayMoreChangesets(div, html) {
112     const sidebar = $("#sidebar")[0];
113     const previousScrollHeightMinusTop = sidebar.scrollHeight - sidebar.scrollTop;
114
115     const oldList = $("#sidebar_content .changesets ol");
116
117     div.replaceWith(html);
118
119     const prevNewList = oldList.prevAll("ol");
120     if (prevNewList.length) {
121       prevNewList.next(".changeset_more").remove();
122       prevNewList.children().prependTo(oldList);
123       prevNewList.remove();
124
125       // restore scroll position only if prepending
126       sidebar.scrollTop = sidebar.scrollHeight - previousScrollHeightMinusTop;
127     }
128
129     const nextNewList = oldList.nextAll("ol");
130     if (nextNewList.length) {
131       nextNewList.prev(".changeset_more").remove();
132       nextNewList.children().appendTo(oldList);
133       nextNewList.remove();
134     }
135
136     if (location.pathname === "/history") {
137       setPaginationMapHashes();
138     }
139   }
140
141   function setPaginationMapHashes() {
142     $("#sidebar .pagination a").each(function () {
143       $(this).prop("hash", OSM.formatHash({
144         center: map.getCenter(),
145         zoom: map.getZoom()
146       }));
147     });
148   }
149
150   function loadFirstChangesets() {
151     const data = new URLSearchParams();
152
153     disableChangesetIntersectionObserver();
154
155     if (location.pathname === "/history") {
156       setBboxFetchData(data);
157       const feedLink = $("link[type=\"application/atom+xml\"]"),
158             feedHref = feedLink.attr("href").split("?")[0];
159       feedLink.attr("href", feedHref + "?" + data);
160     }
161
162     setListFetchData(data, location);
163
164     fetch(location.pathname + "?" + data)
165       .then(response => response.text())
166       .then(function (html) {
167         displayFirstChangesets(html);
168         enableChangesetIntersectionObserver();
169
170         if (data.has("before")) {
171           const [firstItem] = $("#sidebar_content .changesets ol").children().first();
172           firstItem?.scrollIntoView();
173         } else if (data.has("after")) {
174           const [lastItem] = $("#sidebar_content .changesets ol").children().last();
175           lastItem?.scrollIntoView(false);
176         } else {
177           const [sidebar] = $("#sidebar");
178           sidebar.scrollTop = 0;
179         }
180
181         updateMap();
182       });
183   }
184
185   function loadMoreChangesets(e) {
186     e.preventDefault();
187     e.stopPropagation();
188
189     const div = $(this).parents(".changeset_more");
190
191     div.find(".pagination").addClass("invisible");
192     div.find("[hidden]").prop("hidden", false);
193
194     const data = new URLSearchParams();
195
196     if (location.pathname === "/history") {
197       setBboxFetchData(data);
198     }
199
200     const url = new URL($(this).attr("href"), location);
201     setListFetchData(data, url);
202
203     fetch(url.pathname + "?" + data)
204       .then(response => response.text())
205       .then(function (html) {
206         displayMoreChangesets(div, html);
207         enableChangesetIntersectionObserver();
208
209         updateMap();
210       });
211   }
212
213   function setBboxFetchData(data) {
214     const crs = map.options.crs;
215     const sw = map.getBounds().getSouthWest();
216     const ne = map.getBounds().getNorthEast();
217     const swClamped = crs.unproject(crs.project(sw));
218     const neClamped = crs.unproject(crs.project(ne));
219
220     if (sw.lat >= swClamped.lat || ne.lat <= neClamped.lat || ne.lng - sw.lng < 360) {
221       data.set("bbox", map.getBounds().toBBoxString());
222     }
223   }
224
225   function setListFetchData(data, url) {
226     const params = new URLSearchParams(url.search);
227
228     data.set("list", "1");
229
230     if (params.has("before")) {
231       data.set("before", params.get("before"));
232     }
233     if (params.has("after")) {
234       data.set("after", params.get("after"));
235     }
236   }
237
238   function moveEndListener() {
239     if (location.pathname === "/history") {
240       OSM.router.replace("/history" + window.location.hash);
241       loadFirstChangesets();
242     } else {
243       changesetsLayer.updateChangesetLocations(map);
244     }
245   }
246
247   function zoomEndListener() {
248     changesetsLayer.updateChangesetShapes(map);
249   }
250
251   function updateMap() {
252     const changesets = $("[data-changeset]").map(function (index, element) {
253       return $(element).data("changeset");
254     }).get().filter(function (changeset) {
255       return changeset.bbox;
256     });
257
258     changesetsLayer.updateChangesets(map, changesets);
259
260     if (location.pathname !== "/history") {
261       const bounds = changesetsLayer.getBounds();
262       if (bounds.isValid()) map.fitBounds(bounds);
263     }
264   }
265
266   page.pushstate = page.popstate = function (path) {
267     OSM.loadSidebarContent(path, page.load);
268   };
269
270   page.load = function () {
271     map.addLayer(changesetsLayer);
272     map.on("moveend", moveEndListener);
273     map.on("zoomend", zoomEndListener);
274     loadFirstChangesets();
275   };
276
277   page.unload = function () {
278     map.removeLayer(changesetsLayer);
279     map.off("moveend", moveEndListener);
280     map.off("zoomend", zoomEndListener);
281     disableChangesetIntersectionObserver();
282   };
283
284   return page;
285 };