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