1 //= require jquery-simulate/jquery.simulate
3 OSM.History = function (map) {
7 .on("click", ".changeset_more a", loadMore)
8 .on("mouseover", "[data-changeset]", function () {
9 highlightChangeset($(this).data("changeset").id);
11 .on("mouseout", "[data-changeset]", function () {
12 unHighlightChangeset($(this).data("changeset").id);
15 var group = L.featureGroup()
16 .on("mouseover", function (e) {
17 highlightChangeset(e.layer.id);
19 .on("mouseout", function (e) {
20 unHighlightChangeset(e.layer.id);
22 .on("click", function (e) {
23 clickChangeset(e.layer.id, e.originalEvent);
26 group.getLayerId = function (layer) {
30 function highlightChangeset(id) {
31 var layer = group.getLayer(id);
32 if (layer) layer.setStyle({ fillOpacity: 0.3, color: "#FF6600", weight: 3 });
33 $("#changeset_" + id).addClass("selected");
36 function unHighlightChangeset(id) {
37 var layer = group.getLayer(id);
38 if (layer) layer.setStyle({ fillOpacity: 0, color: "#FF9500", weight: 2 });
39 $("#changeset_" + id).removeClass("selected");
42 function clickChangeset(id, e) {
43 $("#changeset_" + id).find("a.changeset_id").simulate("click", e);
46 function displayFirstChangesets(html) {
47 $("#sidebar_content .changesets").html(html);
50 function displayMoreChangesets(html) {
51 $("#sidebar_content .changeset_more").replaceWith(html);
52 var oldList = $("#sidebar_content .changesets ol").first();
53 var newList = oldList.next("ol");
54 newList.children().appendTo(oldList);
59 const data = new URLSearchParams();
61 if (window.location.pathname === "/history") {
62 data.set("bbox", map.getBounds().wrap().toBBoxString());
63 var feedLink = $("link[type=\"application/atom+xml\"]"),
64 feedHref = feedLink.attr("href").split("?")[0];
65 feedLink.attr("href", feedHref + "?" + data);
68 data.set("list", "1");
70 fetch(window.location.pathname + "?" + data)
71 .then(response => response.text())
72 .then(function (html) {
73 displayFirstChangesets(html);
78 function loadMore(e) {
82 var div = $(this).parents(".changeset_more");
85 div.find(".loader").show();
87 $.get($(this).attr("href"), function (html) {
88 displayMoreChangesets(html);
95 function updateBounds() {
98 for (const changeset of changesets) {
99 var bottomLeft = map.project(L.latLng(changeset.bbox.minlat, changeset.bbox.minlon)),
100 topRight = map.project(L.latLng(changeset.bbox.maxlat, changeset.bbox.maxlon)),
101 width = topRight.x - bottomLeft.x,
102 height = bottomLeft.y - topRight.y,
103 minSize = 20; // Min width/height of changeset in pixels
105 if (width < minSize) {
106 bottomLeft.x -= ((minSize - width) / 2);
107 topRight.x += ((minSize - width) / 2);
110 if (height < minSize) {
111 bottomLeft.y += ((minSize - height) / 2);
112 topRight.y -= ((minSize - height) / 2);
115 changeset.bounds = L.latLngBounds(map.unproject(bottomLeft),
116 map.unproject(topRight));
119 changesets.sort(function (a, b) {
120 return b.bounds.getSize() - a.bounds.getSize();
123 for (const changeset of changesets) {
124 const rect = L.rectangle(changeset.bounds,
125 { weight: 2, color: "#FF9500", opacity: 1, fillColor: "#FFFFAF", fillOpacity: 0 });
126 rect.id = changeset.id;
131 function updateMap() {
132 changesets = $("[data-changeset]").map(function (index, element) {
133 return $(element).data("changeset");
134 }).get().filter(function (changeset) {
135 return changeset.bbox;
140 if (window.location.pathname !== "/history") {
141 var bounds = group.getBounds();
142 if (bounds.isValid()) map.fitBounds(bounds);
146 page.pushstate = page.popstate = function (path) {
147 OSM.loadSidebarContent(path, page.load);
150 page.load = function () {
153 if (window.location.pathname === "/history") {
154 map.on("moveend", update);
157 map.on("zoomend", updateBounds);
162 page.unload = function () {
163 map.removeLayer(group);
164 map.off("moveend", update);
165 map.off("zoomend", updateBounds);