1 //= require 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);
14 .on("mousedown", "[data-changeset]", function () {
17 .one("click", function (e) {
18 if (!moved && !$(e.target).is("a")) {
19 clickChangeset($(this).data("changeset").id, e);
22 .one("mousemove", function () {
27 var group = L.featureGroup()
28 .on("mouseover", function (e) {
29 highlightChangeset(e.layer.id);
31 .on("mouseout", function (e) {
32 unHighlightChangeset(e.layer.id);
34 .on("click", function (e) {
35 clickChangeset(e.layer.id, e);
38 group.getLayerId = function (layer) {
42 function highlightChangeset(id) {
43 group.getLayer(id).setStyle({ fillOpacity: 0.3, color: "#FF6600", weight: 3 });
44 $("#changeset_" + id).addClass("selected");
47 function unHighlightChangeset(id) {
48 group.getLayer(id).setStyle({ fillOpacity: 0, color: "#FF9500", weight: 2 });
49 $("#changeset_" + id).removeClass("selected");
52 function clickChangeset(id, e) {
53 $("#changeset_" + id).find("a.changeset_id").simulate("click", e);
57 var data = { list: "1" };
59 if (window.location.pathname === "/history") {
60 data.bbox = map.getBounds().wrap().toBBoxString();
64 url: window.location.pathname,
67 success: function (html) {
68 $("#sidebar_content .changesets").html(html);
73 var feedLink = $("link[type=\"application/atom+xml\"]"),
74 feedHref = feedLink.attr("href").split("?")[0];
76 feedLink.attr("href", feedHref + "?bbox=" + data.bbox);
79 function loadMore(e) {
83 var div = $(this).parents(".changeset_more");
86 div.find(".loader").show();
88 $.get($(this).attr("href"), function (data) {
89 div.replaceWith(data);
96 function updateBounds() {
99 changesets.forEach(function (changeset) {
100 var bottomLeft = map.project(L.latLng(changeset.bbox.minlat, changeset.bbox.minlon)),
101 topRight = map.project(L.latLng(changeset.bbox.maxlat, changeset.bbox.maxlon)),
102 width = topRight.x - bottomLeft.x,
103 height = bottomLeft.y - topRight.y,
104 minSize = 20; // Min width/height of changeset in pixels
106 if (width < minSize) {
107 bottomLeft.x -= ((minSize - width) / 2);
108 topRight.x += ((minSize - width) / 2);
111 if (height < minSize) {
112 bottomLeft.y += ((minSize - height) / 2);
113 topRight.y -= ((minSize - height) / 2);
116 changeset.bounds = L.latLngBounds(map.unproject(bottomLeft),
117 map.unproject(topRight));
120 changesets.sort(function (a, b) {
121 return b.bounds.getSize() - a.bounds.getSize();
124 for (var i = 0; i < changesets.length; ++i) {
125 var changeset = changesets[i],
126 rect = L.rectangle(changeset.bounds,
127 { weight: 2, color: "#FF9500", opacity: 1, fillColor: "#FFFFAF", fillOpacity: 0 });
128 rect.id = changeset.id;
133 function updateMap() {
134 changesets = $("[data-changeset]").map(function (index, element) {
135 return $(element).data("changeset");
136 }).get().filter(function (changeset) {
137 return changeset.bbox;
142 if (window.location.pathname !== "/history") {
143 var bounds = group.getBounds();
144 if (bounds.isValid()) map.fitBounds(bounds);
148 page.pushstate = page.popstate = function (path) {
149 $("#history_tab").addClass("current");
150 OSM.loadSidebarContent(path, page.load);
153 page.load = function () {
156 if (window.location.pathname === "/history") {
157 map.on("moveend", update);
160 map.on("zoomend", updateBounds);
165 page.unload = function () {
166 map.removeLayer(group);
167 map.off("moveend", update);
169 $("#history_tab").removeClass("current");