1 page.replace_html :sidebar_title, 'Data'
2 page.replace_html :sidebar_content, :partial => 'start'
5 var gml, sf, objList, currentFeature, featureList, mode = "auto", currentBounds, browsing;
6 OpenLayers.Feature.Vector.style['default'].strokeWidth = 3;
7 OpenLayers.Feature.Vector.style['default'].cursor = "pointer";
10 openSidebar({ onclose: stopBrowse });
11 var vectors = new OpenLayers.Layer.Vector();
13 box = new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.RegularPolygon, {
19 callbacks: { done: endDrag }
23 map.events.register("moveend", map, showData);
24 map.events.triggerEvent("moveend");
29 if (mode == "manual") { return; }
30 if (map.getZoom() >= 15) {
33 $("status").innerHTML = "Zoom in or Select an area of the map to view.";
37 function stopBrowse() {
47 currentFeature.destroy();
48 currentFeature = null;
50 map.events.unregister("moveend", map, showData);
54 function startDrag() {
55 $("drag_box").innerHTML='Drag a box on the map to select an area';
59 $("drag_box").onclick = startDrag;
62 var bounds = map.getExtent();
63 var projected = bounds.clone().transform(map.getProjectionObject(), epsg4326);
64 if (!currentBounds || !currentBounds.containsBounds(projected)) {
65 var center = bounds.getCenterLonLat();
66 var tileWidth = bounds.getWidth() * 1.2;
67 var tileHeight = bounds.getHeight() * 1.2;
69 new OpenLayers.Bounds(center.lon - (tileWidth / 2),
70 center.lat - (tileHeight / 2),
71 center.lon + (tileWidth / 2),
72 center.lat + (tileHeight / 2));
74 currentBounds = tileBounds;
77 $("drag_box").style.display="inline";
78 $("use_map").style.display="none";
82 $("use_map").onclick = useMap;
84 function endDrag(bbox) {
85 var bounds = bbox.getBounds();
88 $("drag_box").innerHTML = "Manually select a different area";
90 $("use_map").style.display="inline";
91 $("drag_box").style.display="none";
94 function displayFeatureWarning() {
95 $("status").innerHTML = "";
96 var div = document.createElement("div");
97 var p = document.createElement("p");
98 p.appendChild(document.createTextNode("You have loaded an area which contains " + featureList.length + " features. In general, some browsers may not cope well with displaying this quantity of data. Generally, browsers work best at displaying less than 100 features at a time: doing anything else may make your browser slow/unresponsive. If you are sure you want to display this data, you may do so by clicking the button below."));
100 var input = document.createElement("input");
101 input.type = "submit";
102 input.value = "Load Data";
103 input.onclick = loadFeatureList;
104 div.appendChild(input);
105 $("object").innerHTML="";
106 $("object").appendChild(div);
109 function loadFeatureList() {
110 gml.addFeatures(featureList);
111 gml.events.triggerEvent("loadend");
115 function customDataLoader(request) {
116 if (!browsing) { return; }
117 var doc = request.responseXML;
119 if (!doc || !doc.documentElement) {
120 doc = request.responseText;
125 OpenLayers.Util.extend(options, this.formatOptions);
126 if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
127 options.externalProjection = this.projection;
128 options.internalProjection = this.map.getProjectionObject();
131 var gml = this.format ? new this.format(options) : new OpenLayers.Format.GML(options);
132 var features = gml.read(doc);
133 if (!this.maxFeatures || features.length <= this.maxFeatures) {
134 this.addFeatures(features);
135 this.events.triggerEvent("loadend");
138 featureList = features;
139 displayFeatureWarning();
143 function getData(bounds) {
145 bounds.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
146 var size = bounds.getWidth() * bounds.getHeight();
148 $("status").innerHTML = "Unable to load: Bounding box size of " + size + " is too large. (Must be smaller than 0.25)<br />";
152 var url = "/api/0.5/map?bbox="+bounds.toBBOX();
156 function loadGML(url) {
157 $("status").innerHTML = "Loading...";
158 $("object").innerHTML = "";
160 var style = new OpenLayers.Style();
161 style.addRules([new OpenLayers.Rule(
163 {"Polygon": {'fillColor': '#ff0000', 'strokeColor': '#ff0000'},
164 "Line": {'fillColor': '#ffff00', 'strokeColor': '#000000', strokeOpacity: '0.4'},
165 "Point": {'fillColor': '#00ff00', 'strokeColor': '#00ff00'}}
168 gml = new OpenLayers.Layer.GML("Data",url,
169 {format: OpenLayers.Format.OSM, formatOptions: {checkTags: true},
170 maxFeatures: 100, requestSuccess: customDataLoader,
171 styleMap: new OpenLayers.StyleMap({'default': style, 'select': {'strokeColor': '#0000ff', strokeWidth: 8}})
174 gml.events.register("loadend", gml, dataLoaded );
177 sf = new OpenLayers.Control.SelectFeature(gml, {'onSelect': onFeatureSelect});
178 sf.handler.stopDown = false;
179 sf.handler.stopUp = false;
187 currentFeature = null;
189 function dataLoaded() {
190 if (!browsing) { return; }
191 $("status").innerHTML = "Loaded " + this.features.length + " features. (<a href='"+ this.url+"'>API</a>)";
193 objList = document.createElement("ul");
194 for (var i = 0; i < this.features.length; i++) {
195 var feature = this.features[i];
199 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
202 var nice_name = type.substr(0,1).toUpperCase() + type.substr(1,type.length);
203 var li = document.createElement("li");
204 li.appendChild(document.createTextNode(nice_name + " "));
206 // Link, for viewing in the tab
207 var link = document.createElement("a");
208 link.href = "/browse/" + type + "/" + feature.osm_id;
209 var name = feature.attributes.name || feature.osm_id;
210 link.appendChild(document.createTextNode(name));
211 link.feature = feature;
212 link.onclick = OpenLayers.Function.bind(viewFeatureLink, link);
213 li.appendChild(link);
215 objList.appendChild(li);
217 $("object").innerHTML = "";
218 $("object").appendChild(objList);
221 function viewFeatureLink() {
222 var layer = this.feature.layer;
223 for (var i = 0; i < layer.selectedFeatures.length; i++) {
224 var f = layer.selectedFeatures[i];
225 layer.drawFeature(f, layer.styleMap.createSymbolizer(f, "default"));
227 onFeatureSelect(this.feature);
228 if (mode != "auto") {
229 map.setCenter(this.feature.geometry.getBounds().getCenterLonLat());
234 function loadObjList() {
235 $("object").innerHTML="";
236 $("object").appendChild(objList);
240 function onFeatureSelect(feature) {
241 // Unselect previously selected feature
242 if (currentFeature) {
243 currentFeature.layer.drawFeature(
244 currentFeature, currentFeature.layer.styleMap.createSymbolizer(currentFeature, "default")
248 // Redraw in selected style
249 feature.layer.drawFeature(
250 feature, feature.layer.styleMap.createSymbolizer(feature, "select")
253 // If the current object is the list, don't innerHTML="", since that could clar it.
254 if ($("object").firstChild == objList) {
255 $("object").removeChild(objList);
257 $("object").innerHTML = "";
260 // Create a link back to the object list
261 var div = document.createElement("div");
262 var link = document.createElement("a");
264 link.onclick = loadObjList;
265 link.appendChild(document.createTextNode("Back to Object List"));
266 div.appendChild(link)
267 $("object").appendChild(div);
269 // Now the list of attributes
270 var ul = document.createElement("ul");
272 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
275 var li = document.createElement("li");
276 var link = document.createElement("a");
277 link.href = "/browse/"+type+"/"+feature.osm_id;
278 link.appendChild(document.createTextNode(feature.osm_id));
279 li.appendChild(link);
281 for (var key in feature.attributes) {
282 var li = document.createElement("li");
283 var b = document.createElement("b");
284 b.appendChild(document.createTextNode(key));
286 li.appendChild(document.createTextNode(": " + feature.attributes[key]));
290 var li = document.createElement("li");
291 var link = document.createElement("a");
292 link.href = "/browse/"+type+"/"+feature.osm_id+"/history";
293 link.appendChild(document.createTextNode("History"));
294 li.appendChild(link);
296 link.onclick = OpenLayers.Function.bind(loadHistory, {type: type, feature: feature, link: link});
297 $("object").appendChild(ul);
300 // Stash the currently drawn feature
301 currentFeature = feature;
303 function loadHistory() {
305 this.link.innerHTML = "Wait...";
306 new Ajax.Request("/api/0.5/"+this.type+"/"+this.feature.osm_id+"/history", {onComplete: OpenLayers.Function.bind(displayHistory, this)});
309 function displayHistory(request) {
310 if (currentFeature.osm_id != this.feature.osm_id || $("object").firstChild == objList) {
313 this.link.parentNode.parentNode.removeChild(this.link.parentNode);
314 var doc = request.responseXML;
315 var div = document.createElement("div");
316 var h3 = document.createElement("h3");
317 h3.appendChild(document.createTextNode("History"));
319 var nodes = doc.getElementsByTagName(this.type);
320 var history = document.createElement("ul");
321 for (var i = nodes.length - 1; i >= 0; i--) {
322 var user = nodes[i].getAttribute("user") || "private user";
323 var timestamp = nodes[i].getAttribute("timestamp");
324 var item = document.createElement("li");
325 item.appendChild(document.createTextNode("Edited by " + user + " at " + timestamp));
326 history.appendChild(item);
328 div.appendChild(history);
329 var link = document.createElement("a");
330 link.appendChild(document.createTextNode("History"));
331 link.href = "/browse/"+this.type+"/"+this.feature.osm_id+"/history";
332 div.appendChild(link);
333 $("object").appendChild(div);