]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/history.js
Rewrite bbox only in place history atom links
[rails.git] / app / assets / javascripts / index / history.js
1 //= require jquery-simulate/jquery.simulate
2
3 OSM.History = function (map) {
4   var page = {};
5
6   $("#sidebar_content")
7     .on("click", ".changeset_more a", loadMore)
8     .on("mouseover", "[data-changeset]", function () {
9       highlightChangeset($(this).data("changeset").id);
10     })
11     .on("mouseout", "[data-changeset]", function () {
12       unHighlightChangeset($(this).data("changeset").id);
13     });
14
15   var group = L.featureGroup()
16     .on("mouseover", function (e) {
17       highlightChangeset(e.layer.id);
18     })
19     .on("mouseout", function (e) {
20       unHighlightChangeset(e.layer.id);
21     })
22     .on("click", function (e) {
23       clickChangeset(e.layer.id, e);
24     });
25
26   group.getLayerId = function (layer) {
27     return layer.id;
28   };
29
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");
34   }
35
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");
40   }
41
42   function clickChangeset(id, e) {
43     $("#changeset_" + id).find("a.changeset_id").simulate("click", e);
44   }
45
46   function update() {
47     var data = { list: "1" };
48
49     if (window.location.pathname === "/history") {
50       data.bbox = map.getBounds().wrap().toBBoxString();
51       var feedLink = $("link[type=\"application/atom+xml\"]"),
52           feedHref = feedLink.attr("href").split("?")[0];
53       feedLink.attr("href", feedHref + "?bbox=" + data.bbox);
54     }
55
56     $.ajax({
57       url: window.location.pathname,
58       method: "GET",
59       data: data,
60       success: function (html) {
61         $("#sidebar_content .changesets").html(html);
62         updateMap();
63       }
64     });
65   }
66
67   function loadMore(e) {
68     e.preventDefault();
69     e.stopPropagation();
70
71     var div = $(this).parents(".changeset_more");
72
73     $(this).hide();
74     div.find(".loader").show();
75
76     $.get($(this).attr("href"), function (data) {
77       div.replaceWith(data);
78       updateMap();
79     });
80   }
81
82   var changesets = [];
83
84   function updateBounds() {
85     group.clearLayers();
86
87     changesets.forEach(function (changeset) {
88       var bottomLeft = map.project(L.latLng(changeset.bbox.minlat, changeset.bbox.minlon)),
89           topRight = map.project(L.latLng(changeset.bbox.maxlat, changeset.bbox.maxlon)),
90           width = topRight.x - bottomLeft.x,
91           height = bottomLeft.y - topRight.y,
92           minSize = 20; // Min width/height of changeset in pixels
93
94       if (width < minSize) {
95         bottomLeft.x -= ((minSize - width) / 2);
96         topRight.x += ((minSize - width) / 2);
97       }
98
99       if (height < minSize) {
100         bottomLeft.y += ((minSize - height) / 2);
101         topRight.y -= ((minSize - height) / 2);
102       }
103
104       changeset.bounds = L.latLngBounds(map.unproject(bottomLeft),
105                                         map.unproject(topRight));
106     });
107
108     changesets.sort(function (a, b) {
109       return b.bounds.getSize() - a.bounds.getSize();
110     });
111
112     for (var i = 0; i < changesets.length; ++i) {
113       var changeset = changesets[i],
114           rect = L.rectangle(changeset.bounds,
115                              { weight: 2, color: "#FF9500", opacity: 1, fillColor: "#FFFFAF", fillOpacity: 0 });
116       rect.id = changeset.id;
117       rect.addTo(group);
118     }
119   }
120
121   function updateMap() {
122     changesets = $("[data-changeset]").map(function (index, element) {
123       return $(element).data("changeset");
124     }).get().filter(function (changeset) {
125       return changeset.bbox;
126     });
127
128     updateBounds();
129
130     if (window.location.pathname !== "/history") {
131       var bounds = group.getBounds();
132       if (bounds.isValid()) map.fitBounds(bounds);
133     }
134   }
135
136   page.pushstate = page.popstate = function (path) {
137     $("#history_tab").addClass("current");
138     OSM.loadSidebarContent(path, page.load);
139   };
140
141   page.load = function () {
142     map.addLayer(group);
143
144     if (window.location.pathname === "/history") {
145       map.on("moveend", update);
146     }
147
148     map.on("zoomend", updateBounds);
149
150     update();
151   };
152
153   page.unload = function () {
154     map.removeLayer(group);
155     map.off("moveend", update);
156
157     $("#history_tab").removeClass("current");
158   };
159
160   return page;
161 };