1 //= require jquery-simulate/jquery.simulate
2 //= require ./history-changesets-layer
4 OSM.History = function (map) {
8 .on("click", ".changeset_more a", loadMoreChangesets)
9 .on("mouseover", "[data-changeset]", function () {
10 toggleChangesetHighlight($(this).data("changeset").id, true);
12 .on("mouseout", "[data-changeset]", function () {
13 toggleChangesetHighlight($(this).data("changeset").id, false);
16 const changesetsLayer = new OSM.HistoryChangesetsLayer()
17 .on("mouseover", function (e) {
18 toggleChangesetHighlight(e.layer.id, true);
20 .on("mouseout", function (e) {
21 toggleChangesetHighlight(e.layer.id, false);
23 .on("click", function (e) {
24 clickChangeset(e.layer.id, e.originalEvent);
27 let changesetIntersectionObserver;
29 function disableChangesetIntersectionObserver() {
30 if (changesetIntersectionObserver) {
31 changesetIntersectionObserver.disconnect();
32 changesetIntersectionObserver = null;
36 function enableChangesetIntersectionObserver() {
37 disableChangesetIntersectionObserver();
38 if (!window.IntersectionObserver) return;
40 let ignoreIntersectionEvents = true;
42 changesetIntersectionObserver = new IntersectionObserver((entries) => {
43 if (ignoreIntersectionEvents) {
44 ignoreIntersectionEvents = false;
48 let closestTargetToTop,
49 closestDistanceToTop = Infinity,
50 closestTargetToBottom,
51 closestDistanceToBottom = Infinity;
53 for (const entry of entries) {
54 if (entry.isIntersecting) continue;
56 const distanceToTop = entry.rootBounds.top - entry.boundingClientRect.bottom;
57 const distanceToBottom = entry.boundingClientRect.top - entry.rootBounds.bottom;
58 if (distanceToTop >= 0 && distanceToTop < closestDistanceToTop) {
59 closestDistanceToTop = distanceToTop;
60 closestTargetToTop = entry.target;
62 if (distanceToBottom >= 0 && distanceToBottom <= closestDistanceToBottom) {
63 closestDistanceToBottom = distanceToBottom;
64 closestTargetToBottom = entry.target;
68 if (closestTargetToTop && closestDistanceToTop < closestDistanceToBottom) {
69 const id = $(closestTargetToTop).data("changeset")?.id;
71 OSM.router.replace(location.pathname + "?" + new URLSearchParams({ before: id }) + location.hash);
73 } else if (closestTargetToBottom) {
74 const id = $(closestTargetToBottom).data("changeset")?.id;
76 OSM.router.replace(location.pathname + "?" + new URLSearchParams({ after: id }) + location.hash);
79 }, { root: $("#sidebar")[0] });
81 $("#sidebar_content .changesets ol").children().each(function () {
82 changesetIntersectionObserver.observe(this);
86 function toggleChangesetHighlight(id, state) {
87 changesetsLayer.toggleChangesetHighlight(id, state);
88 $("#changeset_" + id).toggleClass("selected", state);
91 function clickChangeset(id, e) {
92 $("#changeset_" + id).find("a.changeset_id").simulate("click", e);
95 function displayFirstChangesets(html) {
96 $("#sidebar_content .changesets").html(html);
98 if (location.pathname === "/history") {
99 setPaginationMapHashes();
103 function displayMoreChangesets(div, html) {
104 const sidebar = $("#sidebar")[0];
105 const previousScrollHeightMinusTop = sidebar.scrollHeight - sidebar.scrollTop;
107 const oldList = $("#sidebar_content .changesets ol");
109 div.replaceWith(html);
111 const prevNewList = oldList.prevAll("ol");
112 if (prevNewList.length) {
113 prevNewList.next(".changeset_more").remove();
114 prevNewList.children().prependTo(oldList);
115 prevNewList.remove();
117 // restore scroll position only if prepending
118 sidebar.scrollTop = sidebar.scrollHeight - previousScrollHeightMinusTop;
121 const nextNewList = oldList.nextAll("ol");
122 if (nextNewList.length) {
123 nextNewList.prev(".changeset_more").remove();
124 nextNewList.children().appendTo(oldList);
125 nextNewList.remove();
128 if (location.pathname === "/history") {
129 setPaginationMapHashes();
133 function setPaginationMapHashes() {
134 $("#sidebar .pagination a").each(function () {
135 $(this).prop("hash", OSM.formatHash({
136 center: map.getCenter(),
142 function loadFirstChangesets() {
143 const data = new URLSearchParams();
145 disableChangesetIntersectionObserver();
147 if (location.pathname === "/history") {
148 setBboxFetchData(data);
149 const feedLink = $("link[type=\"application/atom+xml\"]"),
150 feedHref = feedLink.attr("href").split("?")[0];
151 feedLink.attr("href", feedHref + "?" + data);
154 setListFetchData(data, location);
156 fetch(location.pathname + "?" + data)
157 .then(response => response.text())
158 .then(function (html) {
159 displayFirstChangesets(html);
160 enableChangesetIntersectionObserver();
162 if (data.has("before")) {
163 const [firstItem] = $("#sidebar_content .changesets ol").children().first();
164 firstItem?.scrollIntoView();
165 } else if (data.has("after")) {
166 const [lastItem] = $("#sidebar_content .changesets ol").children().last();
167 lastItem?.scrollIntoView(false);
169 const [sidebar] = $("#sidebar");
170 sidebar.scrollTop = 0;
177 function loadMoreChangesets(e) {
181 const div = $(this).parents(".changeset_more");
183 div.find(".pagination").addClass("invisible");
184 div.find("[hidden]").prop("hidden", false);
186 const data = new URLSearchParams();
188 if (location.pathname === "/history") {
189 setBboxFetchData(data);
192 const url = new URL($(this).attr("href"), location);
193 setListFetchData(data, url);
195 fetch(url.pathname + "?" + data)
196 .then(response => response.text())
197 .then(function (html) {
198 displayMoreChangesets(div, html);
199 enableChangesetIntersectionObserver();
205 function setBboxFetchData(data) {
206 const crs = map.options.crs;
207 const sw = map.getBounds().getSouthWest();
208 const ne = map.getBounds().getNorthEast();
209 const swClamped = crs.unproject(crs.project(sw));
210 const neClamped = crs.unproject(crs.project(ne));
212 if (sw.lat >= swClamped.lat || ne.lat <= neClamped.lat || ne.lng - sw.lng < 360) {
213 data.set("bbox", map.getBounds().toBBoxString());
217 function setListFetchData(data, url) {
218 const params = new URLSearchParams(url.search);
220 data.set("list", "1");
222 if (params.has("before")) {
223 data.set("before", params.get("before"));
225 if (params.has("after")) {
226 data.set("after", params.get("after"));
230 function moveEndListener() {
231 if (location.pathname === "/history") {
232 OSM.router.replace("/history" + window.location.hash);
233 loadFirstChangesets();
235 changesetsLayer.updateChangesetLocations(map);
239 function zoomEndListener() {
240 changesetsLayer.updateChangesetShapes(map);
243 function updateMap() {
244 const changesets = $("[data-changeset]").map(function (index, element) {
245 return $(element).data("changeset");
246 }).get().filter(function (changeset) {
247 return changeset.bbox;
250 changesetsLayer.updateChangesets(map, changesets);
252 if (location.pathname !== "/history") {
253 const bounds = changesetsLayer.getBounds();
254 if (bounds.isValid()) map.fitBounds(bounds);
258 page.pushstate = page.popstate = function (path) {
259 OSM.loadSidebarContent(path, page.load);
262 page.load = function () {
263 map.addLayer(changesetsLayer);
264 map.on("moveend", moveEndListener);
265 map.on("zoomend", zoomEndListener);
266 loadFirstChangesets();
269 page.unload = function () {
270 map.removeLayer(changesetsLayer);
271 map.off("moveend", moveEndListener);
272 map.off("zoomend", zoomEndListener);
273 disableChangesetIntersectionObserver();