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;
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");
28 if (mode == "manual") { return; }
29 if (map.getZoom() >= 15) {
32 $("status").innerHTML = "Zoom in or Select an area of the map to view.";
36 function stopBrowse() {
46 currentFeature.destroy();
47 currentFeature = null;
49 map.events.unregister("moveend", map, showData);
52 function startDrag() {
53 $("drag_box").innerHTML='Drag a box on the map to select an area';
57 $("drag_box").onclick = startDrag;
60 var bounds = map.getExtent();
61 var projected = bounds.clone().transform(map.getProjectionObject(), epsg4326);
62 if (!currentBounds || !currentBounds.containsBounds(projected)) {
63 var center = bounds.getCenterLonLat();
64 var tileWidth = bounds.getWidth() * 1.2;
65 var tileHeight = bounds.getHeight() * 1.2;
67 new OpenLayers.Bounds(center.lon - (tileWidth / 2),
68 center.lat - (tileHeight / 2),
69 center.lon + (tileWidth / 2),
70 center.lat + (tileHeight / 2));
72 currentBounds = tileBounds;
75 $("drag_box").style.display="inline";
76 $("use_map").style.display="none";
80 $("use_map").onclick = useMap;
82 function endDrag(bbox) {
83 var bounds = bbox.getBounds();
86 $("drag_box").innerHTML = "Manually select a different area";
88 $("use_map").style.display="inline";
89 $("drag_box").style.display="none";
92 function displayFeatureWarning() {
93 $("status").innerHTML = "";
94 var div = document.createElement("div");
95 var p = document.createElement("p");
96 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."));
98 var input = document.createElement("input");
99 input.type = "submit";
100 input.value = "Load Data";
101 input.onclick = loadFeatureList;
102 div.appendChild(input);
103 $("object").innerHTML="";
104 $("object").appendChild(div);
107 function loadFeatureList() {
108 gml.addFeatures(featureList);
109 gml.events.triggerEvent("loadend");
113 function customDataLoader(request) {
114 var doc = request.responseXML;
116 if (!doc || !doc.documentElement) {
117 doc = request.responseText;
122 OpenLayers.Util.extend(options, this.formatOptions);
123 if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
124 options.externalProjection = this.projection;
125 options.internalProjection = this.map.getProjectionObject();
128 var gml = this.format ? new this.format(options) : new OpenLayers.Format.GML(options);
129 var features = gml.read(doc);
130 if (!this.maxFeatures || features.length <= this.maxFeatures) {
131 this.addFeatures(features);
132 this.events.triggerEvent("loadend");
135 featureList = features;
136 displayFeatureWarning();
140 function getData(bounds) {
142 bounds.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
143 var size = bounds.getWidth() * bounds.getHeight();
145 $("status").innerHTML = "Unable to load: Bounding box size of " + size + " is too large. (Must be smaller than 0.25)<br />";
149 var url = "/api/0.5/map?bbox="+bounds.toBBOX();
153 function loadGML(url) {
154 $("status").innerHTML = "Loading...";
155 $("object").innerHTML = "";
157 var style = new OpenLayers.Style();
158 style.addRules([new OpenLayers.Rule(
160 {"Polygon": {'fillColor': '#ff0000', 'strokeColor': '#ff0000'},
161 "Line": {'fillColor': '#ffff00', 'strokeColor': '#000000', strokeOpacity: '0.4'},
162 "Point": {'fillColor': '#00ff00', 'strokeColor': '#00ff00'}}
165 gml = new OpenLayers.Layer.GML("Data",url,
166 {format: OpenLayers.Format.OSM, formatOptions: {checkTags: true},
167 maxFeatures: 100, requestSuccess: customDataLoader,
168 styleMap: new OpenLayers.StyleMap({'default': style, 'select': {'strokeColor': '#0000ff', strokeWidth: 8}})
171 gml.events.register("loadend", gml, dataLoaded );
174 sf = new OpenLayers.Control.SelectFeature(gml, {'onSelect': onFeatureSelect});
175 sf.handler.stopDown = false;
176 sf.handler.stopUp = false;
184 currentFeature = null;
186 function dataLoaded() {
187 $("status").innerHTML = "Loaded " + this.features.length + " features. (<a href='"+ this.url+"'>API</a>)";
189 objList = document.createElement("ul");
190 for (var i = 0; i < this.features.length; i++) {
191 var feature = this.features[i];
195 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
198 var nice_name = type.substr(0,1).toUpperCase() + type.substr(1,type.length);
199 var li = document.createElement("li");
200 li.appendChild(document.createTextNode(nice_name + " "));
202 // Link, for viewing in the tab
203 var link = document.createElement("a");
204 link.href = "/browse/" + type + "/" + feature.osm_id;
205 var name = feature.attributes.name || feature.osm_id;
206 link.appendChild(document.createTextNode(name));
207 link.feature = feature;
208 link.onclick = OpenLayers.Function.bind(viewFeatureLink, link);
209 li.appendChild(link);
211 objList.appendChild(li);
213 $("object").innerHTML = "";
214 $("object").appendChild(objList);
217 function viewFeatureLink() {
218 var layer = this.feature.layer;
219 for (var i = 0; i < layer.selectedFeatures.length; i++) {
220 var f = layer.selectedFeatures[i];
221 layer.drawFeature(f, layer.styleMap.createSymbolizer(f, "default"));
223 onFeatureSelect(this.feature);
224 if (mode != "auto") {
225 map.setCenter(this.feature.geometry.getBounds().getCenterLonLat());
230 function loadObjList() {
231 $("object").innerHTML="";
232 $("object").appendChild(objList);
236 function onFeatureSelect(feature) {
237 // Unselect previously selected feature
238 if (currentFeature) {
239 currentFeature.layer.drawFeature(
240 currentFeature, currentFeature.layer.styleMap.createSymbolizer(currentFeature, "default")
244 // Redraw in selected style
245 feature.layer.drawFeature(
246 feature, feature.layer.styleMap.createSymbolizer(feature, "select")
249 // If the current object is the list, don't innerHTML="", since that could clar it.
250 if ($("object").firstChild == objList) {
251 $("object").removeChild(objList);
253 $("object").innerHTML = "";
256 // Create a link back to the object list
257 var div = document.createElement("div");
258 var link = document.createElement("a");
260 link.onclick = loadObjList;
261 link.appendChild(document.createTextNode("Back to Object List"));
262 div.appendChild(link)
263 $("object").appendChild(div);
265 // Now the list of attributes
266 var ul = document.createElement("ul");
268 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
271 var li = document.createElement("li");
272 var link = document.createElement("a");
273 link.href = "/browse/"+type+"/"+feature.osm_id;
274 link.appendChild(document.createTextNode(feature.osm_id));
275 li.appendChild(link);
277 for (var key in feature.attributes) {
278 var li = document.createElement("li");
279 var b = document.createElement("b");
280 b.appendChild(document.createTextNode(key));
282 li.appendChild(document.createTextNode(": " + feature.attributes[key]));
286 var li = document.createElement("li");
287 var link = document.createElement("a");
288 link.href = "/browse/"+type+"/"+feature.osm_id+"/history";
289 link.appendChild(document.createTextNode("History"));
290 li.appendChild(link);
292 link.onclick = OpenLayers.Function.bind(loadHistory, {type: type, feature: feature, link: link});
293 $("object").appendChild(ul);
296 // Stash the currently drawn feature
297 currentFeature = feature;
299 function loadHistory() {
301 this.link.innerHTML = "Wait...";
302 new Ajax.Request("/api/0.5/"+this.type+"/"+this.feature.osm_id+"/history", {onComplete: OpenLayers.Function.bind(displayHistory, this)});
305 function displayHistory(request) {
306 if (currentFeature.osm_id != this.feature.osm_id || $("object").firstChild == objList) {
309 this.link.parentNode.parentNode.removeChild(this.link.parentNode);
310 var doc = request.responseXML;
311 var div = document.createElement("div");
312 var h3 = document.createElement("h3");
313 h3.appendChild(document.createTextNode("History"));
315 var nodes = doc.getElementsByTagName(this.type);
316 var history = document.createElement("ul");
317 for (var i = 0; i < nodes.length; i++) {
318 var user = nodes[i].getAttribute("user") || "private user";
319 var timestamp = nodes[i].getAttribute("timestamp");
320 var item = document.createElement("li");
321 item.appendChild(document.createTextNode("Edited by " + user + " at " + timestamp));
322 history.appendChild(item);
324 div.appendChild(history);
325 var link = document.createElement("a");
326 link.appendChild(document.createTextNode("History"));
327 link.href = "/browse/"+this.type+"/"+this.feature.osm_id+"/history";
328 div.appendChild(link);
329 $("object").appendChild(div);