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