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");
25 map.dataLayer.stopBrowse = stopBrowse;
30 if (mode == "manual") { return; }
31 if (map.getZoom() >= 15) {
34 $("status").innerHTML = "Zoom in or Select an area of the map to view.";
38 function stopBrowse() {
41 map.dataLayer.stopBrowse = null;
55 currentFeature.destroy();
56 currentFeature = null;
58 map.dataLayer.setVisibility(false);
59 map.events.unregister("moveend", map, showData);
63 function startDrag() {
64 $("drag_box").innerHTML='Drag a box on the map to select an area';
68 $("drag_box").onclick = startDrag;
71 var bounds = map.getExtent();
72 var projected = bounds.clone().transform(map.getProjectionObject(), epsg4326);
73 if (!currentBounds || !currentBounds.containsBounds(projected)) {
74 var center = bounds.getCenterLonLat();
75 var tileWidth = bounds.getWidth() * 1.2;
76 var tileHeight = bounds.getHeight() * 1.2;
78 new OpenLayers.Bounds(center.lon - (tileWidth / 2),
79 center.lat - (tileHeight / 2),
80 center.lon + (tileWidth / 2),
81 center.lat + (tileHeight / 2));
83 currentBounds = tileBounds;
86 $("use_map").style.display="none";
90 $("use_map").onclick = useMap;
92 function endDrag(bbox) {
93 var bounds = bbox.getBounds();
95 currentBounds = bounds;
97 $("drag_box").innerHTML = "Manually select a different area";
99 $("use_map").style.display="inline";
102 function displayFeatureWarning() {
103 $("status").innerHTML = "";
104 var div = document.createElement("div");
105 var p = document.createElement("p");
106 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."));
108 var input = document.createElement("input");
109 input.type = "submit";
110 input.value = "Load Data";
111 input.onclick = loadFeatureList;
112 div.appendChild(input);
113 $("object").innerHTML="";
114 $("object").appendChild(div);
117 function loadFeatureList() {
118 gml.addFeatures(featureList);
119 gml.events.triggerEvent("loadend");
123 function customDataLoader(request) {
124 if (!browsing) { return; }
125 var doc = request.responseXML;
127 if (!doc || !doc.documentElement) {
128 doc = request.responseText;
133 OpenLayers.Util.extend(options, this.formatOptions);
134 if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
135 options.externalProjection = this.projection;
136 options.internalProjection = this.map.getProjectionObject();
139 var gml = this.format ? new this.format(options) : new OpenLayers.Format.GML(options);
140 var features = gml.read(doc);
141 if (!this.maxFeatures || features.length <= this.maxFeatures) {
142 this.addFeatures(features);
143 this.events.triggerEvent("loadend");
146 featureList = features;
147 displayFeatureWarning();
151 function getData(bounds) {
153 bounds.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
154 var size = bounds.getWidth() * bounds.getHeight();
156 $("status").innerHTML = "Unable to load: Bounding box size of " + size + " is too large. (Must be smaller than 0.25)<br />";
160 var url = "/api/0.5/map?bbox="+bounds.toBBOX();
164 function loadGML(url) {
165 $("status").innerHTML = "Loading...";
166 $("object").innerHTML = "";
168 var style = new OpenLayers.Style();
169 style.addRules([new OpenLayers.Rule(
171 {"Polygon": {'fillColor': '#ff0000', 'strokeColor': '#ff0000'},
172 "Line": {'fillColor': '#ffff00', 'strokeColor': '#000000', strokeOpacity: '0.4'},
173 "Point": {'fillColor': '#00ff00', 'strokeColor': '#00ff00'}}
176 gml = new OpenLayers.Layer.GML("Data",url,
177 {format: OpenLayers.Format.OSM, formatOptions: {checkTags: true},
178 maxFeatures: 100, requestSuccess: customDataLoader,
179 displayInLayerSwitcher: false,
180 styleMap: new OpenLayers.StyleMap({'default': style, 'select': {'strokeColor': '#0000ff', strokeWidth: 8}})
183 gml.events.register("loadend", gml, dataLoaded );
186 sf = new OpenLayers.Control.SelectFeature(gml, {'onSelect': onFeatureSelect});
187 sf.handler.stopDown = false;
188 sf.handler.stopUp = false;
196 currentFeature = null;
198 function dataLoaded() {
199 if (!browsing) { return; }
200 $("status").innerHTML = "Loaded."
202 objList = document.createElement("div")
204 list = document.createElement("ul");
205 for (var i = 0; i < this.features.length; i++) {
206 var feature = this.features[i];
210 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
213 var nice_name = type.substr(0,1).toUpperCase() + type.substr(1,type.length);
214 var li = document.createElement("li");
215 li.appendChild(document.createTextNode(nice_name + " "));
217 // Link, for viewing in the tab
218 var link = document.createElement("a");
219 link.href = "/browse/" + type + "/" + feature.osm_id;
220 var name = feature.attributes.name || feature.osm_id;
221 link.appendChild(document.createTextNode(name));
222 link.feature = feature;
223 link.onclick = OpenLayers.Function.bind(viewFeatureLink, link);
224 li.appendChild(link);
226 list.appendChild(li);
228 objList.appendChild(list)
229 var link = document.createElement("a");
230 link.href = this.url;
231 link.appendChild(document.createTextNode("API"));
232 objList.appendChild(link)
233 $("object").innerHTML = "";
234 $("object").appendChild(objList);
238 function viewFeatureLink() {
239 var layer = this.feature.layer;
240 for (var i = 0; i < layer.selectedFeatures.length; i++) {
241 var f = layer.selectedFeatures[i];
242 layer.drawFeature(f, layer.styleMap.createSymbolizer(f, "default"));
244 onFeatureSelect(this.feature);
245 if (mode != "auto") {
246 map.setCenter(this.feature.geometry.getBounds().getCenterLonLat());
251 function loadObjList() {
252 $("object").innerHTML="";
253 $("object").appendChild(objList);
257 function onFeatureSelect(feature) {
258 // Unselect previously selected feature
259 if (currentFeature) {
260 currentFeature.layer.drawFeature(
261 currentFeature, currentFeature.layer.styleMap.createSymbolizer(currentFeature, "default")
265 // Redraw in selected style
266 feature.layer.drawFeature(
267 feature, feature.layer.styleMap.createSymbolizer(feature, "select")
270 // If the current object is the list, don't innerHTML="", since that could clar it.
271 if ($("object").firstChild == objList) {
272 $("object").removeChild(objList);
274 $("object").innerHTML = "";
277 // Create a link back to the object list
278 var div = document.createElement("div");
279 var link = document.createElement("a");
281 link.onclick = loadObjList;
282 link.appendChild(document.createTextNode("Back to Object List"));
283 div.appendChild(link)
284 $("object").appendChild(div);
287 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
291 var link = document.createElement("a");
292 link.href = "/browse/"+type+"/"+feature.osm_id;
293 link.appendChild(document.createTextNode("Database entry for " + feature.osm_id));
295 var div = document.createElement("div");
296 div.style.marginTop = "20px"
297 div.appendChild(link);
299 $("object").appendChild(div);
301 // Now the list of attributes
302 var ul = document.createElement("ul");
303 for (var key in feature.attributes) {
304 var li = document.createElement("li");
305 var b = document.createElement("b");
306 b.appendChild(document.createTextNode(key));
308 li.appendChild(document.createTextNode(": " + feature.attributes[key]));
312 $("object").appendChild(ul);
314 var link = document.createElement("a");
315 link.href = "/browse/"+type+"/"+feature.osm_id+"/history";
316 link.appendChild(document.createTextNode("History"));
318 link.onclick = OpenLayers.Function.bind(loadHistory, {type: type, feature: feature, link: link});
320 $("object").appendChild(link);
322 // Stash the currently drawn feature
323 currentFeature = feature;
325 function loadHistory() {
327 this.link.innerHTML = "Wait...";
328 new Ajax.Request("/api/0.5/"+this.type+"/"+this.feature.osm_id+"/history", {onComplete: OpenLayers.Function.bind(displayHistory, this)});
331 function displayHistory(request) {
332 if (currentFeature.osm_id != this.feature.osm_id || $("object").firstChild == objList) {
335 this.link.parentNode.removeChild(this.link);
336 var doc = request.responseXML;
337 var div = document.createElement("div");
338 var h3 = document.createElement("h3");
339 h3.appendChild(document.createTextNode("History"));
341 var nodes = doc.getElementsByTagName(this.type);
342 var history = document.createElement("ul");
343 for (var i = nodes.length - 1; i >= 0; i--) {
344 var user = nodes[i].getAttribute("user") || "private user";
345 var timestamp = nodes[i].getAttribute("timestamp");
346 var item = document.createElement("li");
347 item.appendChild(document.createTextNode("Edited by " + user + " at " + timestamp));
348 history.appendChild(item);
350 div.appendChild(history);
351 var link = document.createElement("a");
352 link.appendChild(document.createTextNode("History entry for " + this.feature.osm_id));
353 link.href = "/browse/"+this.type+"/"+this.feature.osm_id+"/history";
354 div.appendChild(link);
355 $("object").appendChild(div);