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 $("use_map").style.display="none";
81 $("use_map").onclick = useMap;
83 function endDrag(bbox) {
84 var bounds = bbox.getBounds();
87 $("drag_box").innerHTML = "Manually select a different area";
89 $("use_map").style.display="inline";
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 if (!browsing) { return; }
115 var doc = request.responseXML;
117 if (!doc || !doc.documentElement) {
118 doc = request.responseText;
123 OpenLayers.Util.extend(options, this.formatOptions);
124 if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
125 options.externalProjection = this.projection;
126 options.internalProjection = this.map.getProjectionObject();
129 var gml = this.format ? new this.format(options) : new OpenLayers.Format.GML(options);
130 var features = gml.read(doc);
131 if (!this.maxFeatures || features.length <= this.maxFeatures) {
132 this.addFeatures(features);
133 this.events.triggerEvent("loadend");
136 featureList = features;
137 displayFeatureWarning();
141 function getData(bounds) {
143 bounds.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
144 var size = bounds.getWidth() * bounds.getHeight();
146 $("status").innerHTML = "Unable to load: Bounding box size of " + size + " is too large. (Must be smaller than 0.25)<br />";
150 var url = "/api/0.5/map?bbox="+bounds.toBBOX();
154 function loadGML(url) {
155 $("status").innerHTML = "Loading...";
156 $("object").innerHTML = "";
158 var style = new OpenLayers.Style();
159 style.addRules([new OpenLayers.Rule(
161 {"Polygon": {'fillColor': '#ff0000', 'strokeColor': '#ff0000'},
162 "Line": {'fillColor': '#ffff00', 'strokeColor': '#000000', strokeOpacity: '0.4'},
163 "Point": {'fillColor': '#00ff00', 'strokeColor': '#00ff00'}}
166 gml = new OpenLayers.Layer.GML("Data",url,
167 {format: OpenLayers.Format.OSM, formatOptions: {checkTags: true},
168 maxFeatures: 100, requestSuccess: customDataLoader,
169 styleMap: new OpenLayers.StyleMap({'default': style, 'select': {'strokeColor': '#0000ff', strokeWidth: 8}})
172 gml.events.register("loadend", gml, dataLoaded );
175 sf = new OpenLayers.Control.SelectFeature(gml, {'onSelect': onFeatureSelect});
176 sf.handler.stopDown = false;
177 sf.handler.stopUp = false;
185 currentFeature = null;
187 function dataLoaded() {
188 if (!browsing) { return; }
189 $("status").innerHTML = "Loaded " + this.features.length + " features. (<a href='"+ this.url+"'>API</a>)";
191 objList = document.createElement("ul");
192 for (var i = 0; i < this.features.length; i++) {
193 var feature = this.features[i];
197 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
200 var nice_name = type.substr(0,1).toUpperCase() + type.substr(1,type.length);
201 var li = document.createElement("li");
202 li.appendChild(document.createTextNode(nice_name + " "));
204 // Link, for viewing in the tab
205 var link = document.createElement("a");
206 link.href = "/browse/" + type + "/" + feature.osm_id;
207 var name = feature.attributes.name || feature.osm_id;
208 link.appendChild(document.createTextNode(name));
209 link.feature = feature;
210 link.onclick = OpenLayers.Function.bind(viewFeatureLink, link);
211 li.appendChild(link);
213 objList.appendChild(li);
215 $("object").innerHTML = "";
216 $("object").appendChild(objList);
219 function viewFeatureLink() {
220 var layer = this.feature.layer;
221 for (var i = 0; i < layer.selectedFeatures.length; i++) {
222 var f = layer.selectedFeatures[i];
223 layer.drawFeature(f, layer.styleMap.createSymbolizer(f, "default"));
225 onFeatureSelect(this.feature);
226 if (mode != "auto") {
227 map.setCenter(this.feature.geometry.getBounds().getCenterLonLat());
232 function loadObjList() {
233 $("object").innerHTML="";
234 $("object").appendChild(objList);
238 function onFeatureSelect(feature) {
239 // Unselect previously selected feature
240 if (currentFeature) {
241 currentFeature.layer.drawFeature(
242 currentFeature, currentFeature.layer.styleMap.createSymbolizer(currentFeature, "default")
246 // Redraw in selected style
247 feature.layer.drawFeature(
248 feature, feature.layer.styleMap.createSymbolizer(feature, "select")
251 // If the current object is the list, don't innerHTML="", since that could clar it.
252 if ($("object").firstChild == objList) {
253 $("object").removeChild(objList);
255 $("object").innerHTML = "";
258 // Create a link back to the object list
259 var div = document.createElement("div");
260 var link = document.createElement("a");
262 link.onclick = loadObjList;
263 link.appendChild(document.createTextNode("Back to Object List"));
264 div.appendChild(link)
265 $("object").appendChild(div);
267 // Now the list of attributes
268 var ul = document.createElement("ul");
270 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
273 var li = document.createElement("li");
274 var link = document.createElement("a");
275 link.href = "/browse/"+type+"/"+feature.osm_id;
276 link.appendChild(document.createTextNode(feature.osm_id));
277 li.appendChild(link);
279 for (var key in feature.attributes) {
280 var li = document.createElement("li");
281 var b = document.createElement("b");
282 b.appendChild(document.createTextNode(key));
284 li.appendChild(document.createTextNode(": " + feature.attributes[key]));
288 var li = document.createElement("li");
289 var link = document.createElement("a");
290 link.href = "/browse/"+type+"/"+feature.osm_id+"/history";
291 link.appendChild(document.createTextNode("History"));
292 li.appendChild(link);
294 link.onclick = OpenLayers.Function.bind(loadHistory, {type: type, feature: feature, link: link});
295 $("object").appendChild(ul);
298 // Stash the currently drawn feature
299 currentFeature = feature;
301 function loadHistory() {
303 this.link.innerHTML = "Wait...";
304 new Ajax.Request("/api/0.5/"+this.type+"/"+this.feature.osm_id+"/history", {onComplete: OpenLayers.Function.bind(displayHistory, this)});
307 function displayHistory(request) {
308 if (currentFeature.osm_id != this.feature.osm_id || $("object").firstChild == objList) {
311 this.link.parentNode.parentNode.removeChild(this.link.parentNode);
312 var doc = request.responseXML;
313 var div = document.createElement("div");
314 var h3 = document.createElement("h3");
315 h3.appendChild(document.createTextNode("History"));
317 var nodes = doc.getElementsByTagName(this.type);
318 var history = document.createElement("ul");
319 for (var i = nodes.length - 1; i >= 0; i--) {
320 var user = nodes[i].getAttribute("user") || "private user";
321 var timestamp = nodes[i].getAttribute("timestamp");
322 var item = document.createElement("li");
323 item.appendChild(document.createTextNode("Edited by " + user + " at " + timestamp));
324 history.appendChild(item);
326 div.appendChild(history);
327 var link = document.createElement("a");
328 link.appendChild(document.createTextNode("History"));
329 link.href = "/browse/"+this.type+"/"+this.feature.osm_id+"/history";
330 div.appendChild(link);
331 $("object").appendChild(div);