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);
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);
47 var data = { list: "1" };
49 if (window.location.pathname === "/history") {
50 data.bbox = map.getBounds().wrap().toBBoxString();
54 url: window.location.pathname,
57 success: function (html) {
58 $("#sidebar_content .changesets").html(html);
63 var feedLink = $("link[type=\"application/atom+xml\"]"),
64 feedHref = feedLink.attr("href").split("?")[0];
66 feedLink.attr("href", feedHref + "?bbox=" + data.bbox);
69 function loadMore(e) {
73 var div = $(this).parents(".changeset_more");
76 div.find(".loader").show();
78 $.get($(this).attr("href"), function (data) {
79 div.replaceWith(data);
86 function updateBounds() {
89 changesets.forEach(function (changeset) {
90 var bottomLeft = map.project(L.latLng(changeset.bbox.minlat, changeset.bbox.minlon)),
91 topRight = map.project(L.latLng(changeset.bbox.maxlat, changeset.bbox.maxlon)),
92 width = topRight.x - bottomLeft.x,
93 height = bottomLeft.y - topRight.y,
94 minSize = 20; // Min width/height of changeset in pixels
96 if (width < minSize) {
97 bottomLeft.x -= ((minSize - width) / 2);
98 topRight.x += ((minSize - width) / 2);
101 if (height < minSize) {
102 bottomLeft.y += ((minSize - height) / 2);
103 topRight.y -= ((minSize - height) / 2);
106 changeset.bounds = L.latLngBounds(map.unproject(bottomLeft),
107 map.unproject(topRight));
110 changesets.sort(function (a, b) {
111 return b.bounds.getSize() - a.bounds.getSize();
114 for (var i = 0; i < changesets.length; ++i) {
115 var changeset = changesets[i],
116 rect = L.rectangle(changeset.bounds,
117 { weight: 2, color: "#FF9500", opacity: 1, fillColor: "#FFFFAF", fillOpacity: 0 });
118 rect.id = changeset.id;
123 function updateMap() {
124 changesets = $("[data-changeset]").map(function (index, element) {
125 return $(element).data("changeset");
126 }).get().filter(function (changeset) {
127 return changeset.bbox;
132 if (window.location.pathname !== "/history") {
133 var bounds = group.getBounds();
134 if (bounds.isValid()) map.fitBounds(bounds);
138 page.pushstate = page.popstate = function (path) {
139 $("#history_tab").addClass("current");
140 OSM.loadSidebarContent(path, page.load);
143 page.load = function () {
146 if (window.location.pathname === "/history") {
147 map.on("moveend", update);
150 map.on("zoomend", updateBounds);
155 page.unload = function () {
156 map.removeLayer(group);
157 map.off("moveend", update);
159 $("#history_tab").removeClass("current");