]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/history.js
Merge remote-tracking branch 'upstream/pull/4717'
[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.originalEvent);
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 displayFirstChangesets(html) {
47     $("#sidebar_content .changesets").html(html);
48   }
49
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);
55     newList.remove();
56   }
57
58   function update() {
59     var data = { list: "1" };
60
61     if (window.location.pathname === "/history") {
62       data.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 + "?bbox=" + data.bbox);
66     }
67
68     $.ajax({
69       url: window.location.pathname,
70       method: "GET",
71       data: data,
72       success: function (html) {
73         displayFirstChangesets(html);
74         updateMap();
75       }
76     });
77   }
78
79   function loadMore(e) {
80     e.preventDefault();
81     e.stopPropagation();
82
83     var div = $(this).parents(".changeset_more");
84
85     $(this).hide();
86     div.find(".loader").show();
87
88     $.get($(this).attr("href"), function (html) {
89       displayMoreChangesets(html);
90       updateMap();
91     });
92   }
93
94   var changesets = [];
95
96   function updateBounds() {
97     group.clearLayers();
98
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
105
106       if (width < minSize) {
107         bottomLeft.x -= ((minSize - width) / 2);
108         topRight.x += ((minSize - width) / 2);
109       }
110
111       if (height < minSize) {
112         bottomLeft.y += ((minSize - height) / 2);
113         topRight.y -= ((minSize - height) / 2);
114       }
115
116       changeset.bounds = L.latLngBounds(map.unproject(bottomLeft),
117                                         map.unproject(topRight));
118     });
119
120     changesets.sort(function (a, b) {
121       return b.bounds.getSize() - a.bounds.getSize();
122     });
123
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;
129       rect.addTo(group);
130     }
131   }
132
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;
138     });
139
140     updateBounds();
141
142     if (window.location.pathname !== "/history") {
143       var bounds = group.getBounds();
144       if (bounds.isValid()) map.fitBounds(bounds);
145     }
146   }
147
148   page.pushstate = page.popstate = function (path) {
149     OSM.loadSidebarContent(path, page.load);
150   };
151
152   page.load = function () {
153     map.addLayer(group);
154
155     if (window.location.pathname === "/history") {
156       map.on("moveend", update);
157     }
158
159     map.on("zoomend", updateBounds);
160
161     update();
162   };
163
164   page.unload = function () {
165     map.removeLayer(group);
166     map.off("moveend", update);
167   };
168
169   return page;
170 };