1 page.replace_html :sidebar_title, t('browse.start_rjs.data_frame_title')
2 page.replace_html :sidebar_content, :partial => 'start'
5 var browseMode = "auto";
8 var browseActiveFeature;
10 var browseSelectControl;
13 OpenLayers.Feature.Vector.style['default'].strokeWidth = 3;
14 OpenLayers.Feature.Vector.style['default'].cursor = "pointer";
16 function startBrowse() {
17 map.dataLayer.active = true;
19 openSidebar({ onclose: stopBrowse });
21 var vectors = new OpenLayers.Layer.Vector();
23 browseBoxControl = new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.RegularPolygon, {
29 callbacks: { done: endDrag }
32 map.addControl(browseBoxControl);
34 map.events.register("moveend", map, showData);
35 map.events.triggerEvent("moveend");
39 if (browseMode == "auto") {
40 if (map.getZoom() >= 15) {
43 setStatus("#{I18n.t('browse.start_rjs.zoom_or_select')}");
48 function stopBrowse() {
49 if (map.dataLayer.active) {
50 map.dataLayer.active = false;
52 if (browseSelectControl) {
53 browseSelectControl.destroy();
54 browseSelectControl = null;
57 if (browseBoxControl) {
58 browseBoxControl.destroy();
59 browseBoxControl = null;
62 if (browseActiveFeature) {
63 browseActiveFeature.destroy();
64 browseActiveFeature = null;
67 if (browseDataLayer) {
68 browseDataLayer.destroy();
69 browseDataLayer = null;
72 map.dataLayer.setVisibility(false);
73 map.events.unregister("moveend", map, showData);
77 function startDrag() {
78 $("browse_select_box").innerHTML="#{I18n.t('browse.start_rjs.drag_a_box')}";
80 browseBoxControl.activate();
85 $("browse_select_box").onclick = startDrag;
88 var bounds = map.getExtent();
89 var projected = bounds.clone().transform(map.getProjectionObject(), epsg4326);
91 if (!browseBounds || !browseBounds.containsBounds(projected)) {
92 var center = bounds.getCenterLonLat();
93 var tileWidth = bounds.getWidth() * 1.2;
94 var tileHeight = bounds.getHeight() * 1.2;
95 var tileBounds = new OpenLayers.Bounds(center.lon - (tileWidth / 2),
96 center.lat - (tileHeight / 2),
97 center.lon + (tileWidth / 2),
98 center.lat + (tileHeight / 2));
100 browseBounds = tileBounds;
105 $("browse_select_view").style.display = "none";
111 $("browse_select_view").onclick = useMap;
113 function endDrag(bbox) {
114 var bounds = bbox.getBounds();
115 var projected = bounds.clone().transform(map.getProjectionObject(), epsg4326);
117 browseBoxControl.deactivate();
118 browseBounds = projected;
121 browseMode = "manual";
123 $("browse_select_box").innerHTML = "#{I18n.t('browse.start_rjs.manually_select')}";
124 $("browse_select_view").style.display = "inline";
127 function displayFeatureWarning() {
130 var div = document.createElement("div");
132 var p = document.createElement("p");
133 p.appendChild(document.createTextNode(i18n("#{I18n.t('browse.start_rjs.loaded_an_area_with_num_features')}", { num_features: browseFeatureList.length })));
136 var input = document.createElement("input");
137 input.type = "submit";
138 input.value = "#{I18n.t('browse.start_rjs.load_data')}";
139 input.onclick = loadFeatureList;
140 div.appendChild(input);
142 $("browse_content").innerHTML = "";
143 $("browse_content").appendChild(div);
146 function loadFeatureList() {
147 browseDataLayer.addFeatures(browseFeatureList);
148 browseDataLayer.events.triggerEvent("loadend");
150 browseFeatureList = [];
155 function customDataLoader(request) {
156 if (this.map.dataLayer.active) {
157 var doc = request.responseXML;
159 if (!doc || !doc.documentElement) {
160 doc = request.responseText;
165 OpenLayers.Util.extend(options, this.formatOptions);
167 if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
168 options.externalProjection = this.projection;
169 options.internalProjection = this.map.getProjectionObject();
172 var gml = this.format ? new this.format(options) : new OpenLayers.Format.GML(options);
174 browseFeatureList = gml.read(doc);
176 if (!this.maxFeatures || browseFeatureList.length <= this.maxFeatures) {
179 displayFeatureWarning();
184 function getData(bounds) {
185 var projected = bounds.clone().transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
186 var size = projected.getWidth() * projected.getHeight();
188 if (size > #{APP_CONFIG['max_request_area']}) {
189 setStatus(i18n("#{I18n.t('browse.start_rjs.unable_to_load_size', :max_bbox_size => APP_CONFIG['max_request_area'])}", { bbox_size: size }));
191 loadGML("/api/#{API_VERSION}/map?bbox=" + projected.toBBOX());
195 function loadGML(url) {
196 setStatus("#{I18n.t('browse.start_rjs.loading')}");
197 $("browse_content").innerHTML = "";
199 if (!browseDataLayer) {
200 var style = new OpenLayers.Style();
202 style.addRules([new OpenLayers.Rule({
204 Polygon: { fillColor: '#ff0000', strokeColor: '#ff0000' },
205 Line: { fillColor: '#ffff00', strokeColor: '#000000', strokeOpacity: '0.4' },
206 Point: { fillColor: '#00ff00', strokeColor: '#00ff00' }
210 browseDataLayer = new OpenLayers.Layer.GML("Data", url, {
211 format: OpenLayers.Format.OSM,
214 interestingTagsExclude: ['source','source_ref','source:ref','history','attribution','created_by','tiger:county','tiger:tlid','tiger:upload_uuid']
217 requestSuccess: customDataLoader,
218 displayInLayerSwitcher: false,
219 styleMap: new OpenLayers.StyleMap({
221 'select': { strokeColor: '#0000ff', strokeWidth: 8 }
224 browseDataLayer.events.register("loadend", browseDataLayer, dataLoaded );
225 map.addLayer(browseDataLayer);
227 browseSelectControl = new OpenLayers.Control.SelectFeature(browseDataLayer, { onSelect: onFeatureSelect });
228 browseSelectControl.handlers.feature.stopDown = false;
229 browseSelectControl.handlers.feature.stopUp = false;
230 map.addControl(browseSelectControl);
231 browseSelectControl.activate();
233 browseDataLayer.setUrl(url);
236 browseActiveFeature = null;
239 function dataLoaded() {
240 if (this.map.dataLayer.active) {
243 browseObjectList = document.createElement("div")
245 var heading = document.createElement("p");
246 heading.className = "browse_heading";
247 heading.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.object_list.heading')}"));
248 browseObjectList.appendChild(heading);
250 var list = document.createElement("ul");
252 for (var i = 0; i < this.features.length; i++) {
253 var feature = this.features[i];
256 var type = featureType(feature);
257 var typeName = featureTypeName(feature);
258 var li = document.createElement("li");
259 li.appendChild(document.createTextNode(typeName + " "));
261 // Link, for viewing in the tab
262 var link = document.createElement("a");
263 link.href = "/browse/" + type + "/" + feature.osm_id;
264 var name = featureName(feature);
265 link.appendChild(document.createTextNode(name));
266 link.feature = feature;
267 link.onclick = OpenLayers.Function.bind(viewFeatureLink, link);
268 li.appendChild(link);
270 list.appendChild(li);
273 browseObjectList.appendChild(list)
275 var link = document.createElement("a");
276 link.href = this.url;
277 link.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.object_list.api')}"));
278 browseObjectList.appendChild(link);
280 $("browse_content").innerHTML = "";
281 $("browse_content").appendChild(browseObjectList);
285 function viewFeatureLink() {
286 var layer = this.feature.layer;
288 for (var i = 0; i < layer.selectedFeatures.length; i++) {
289 var f = layer.selectedFeatures[i];
290 layer.drawFeature(f, layer.styleMap.createSymbolizer(f, "default"));
293 onFeatureSelect(this.feature);
295 if (browseMode != "auto") {
296 map.setCenter(this.feature.geometry.getBounds().getCenterLonLat());
302 function loadObjectList() {
303 $("browse_content").innerHTML="";
304 $("browse_content").appendChild(browseObjectList);
309 function onFeatureSelect(feature) {
310 // Unselect previously selected feature
311 if (browseActiveFeature) {
312 browseActiveFeature.layer.drawFeature(
314 browseActiveFeature.layer.styleMap.createSymbolizer(browseActiveFeature, "default")
318 // Redraw in selected style
319 feature.layer.drawFeature(
320 feature, feature.layer.styleMap.createSymbolizer(feature, "select")
323 // If the current object is the list, don't innerHTML="", since that could clear it.
324 if ($("browse_content").firstChild == browseObjectList) {
325 $("browse_content").removeChild(browseObjectList);
327 $("browse_content").innerHTML = "";
330 // Create a link back to the object list
331 var div = document.createElement("div");
332 div.style.textAlign = "center";
333 div.style.marginBottom = "20px";
334 $("browse_content").appendChild(div);
335 var link = document.createElement("a");
337 link.onclick = loadObjectList;
338 link.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.object_list.back')}"));
339 div.appendChild(link);
341 var table = document.createElement("table");
342 table.width = "100%";
343 table.className = "browse_heading";
344 $("browse_content").appendChild(table);
346 var tr = document.createElement("tr");
347 table.appendChild(tr);
349 var heading = document.createElement("td");
350 heading.appendChild(document.createTextNode(featureNameSelect(feature)));
351 tr.appendChild(heading);
353 var td = document.createElement("td");
357 var type = featureType(feature);
358 var link = document.createElement("a");
359 link.href = "/browse/" + type + "/" + feature.osm_id;
360 link.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.object_list.details')}"));
361 td.appendChild(link);
363 var div = document.createElement("div");
364 div.className = "browse_details";
366 $("browse_content").appendChild(div);
368 // Now the list of attributes
369 var ul = document.createElement("ul");
370 for (var key in feature.attributes) {
371 var li = document.createElement("li");
372 var b = document.createElement("b");
373 b.appendChild(document.createTextNode(key));
375 li.appendChild(document.createTextNode(": " + feature.attributes[key]));
381 var link = document.createElement("a");
382 link.href = "/browse/" + type + "/" + feature.osm_id + "/history";
383 link.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.show_history')}"));
384 link.onclick = OpenLayers.Function.bind(loadHistory, {
385 type: type, feature: feature, link: link
388 div.appendChild(link);
390 // Stash the currently drawn feature
391 browseActiveFeature = feature;
394 function loadHistory() {
396 this.link.innerHTML = "#{I18n.t('browse.start_rjs.wait')}";
398 new Ajax.Request("/api/#{API_VERSION}/" + this.type + "/" + this.feature.osm_id + "/history", {
399 onComplete: OpenLayers.Function.bind(displayHistory, this)
405 function displayHistory(request) {
406 if (browseActiveFeature.osm_id != this.feature.osm_id || $("browse_content").firstChild == browseObjectList) {
410 this.link.parentNode.removeChild(this.link);
412 var doc = request.responseXML;
414 var table = document.createElement("table");
415 table.width = "100%";
416 table.className = "browse_heading";
417 $("browse_content").appendChild(table);
419 var tr = document.createElement("tr");
420 table.appendChild(tr);
422 var heading = document.createElement("td");
423 heading.appendChild(document.createTextNode(i18n("#{I18n.t('browse.start_rjs.history_for_feature')}", { feature: featureNameHistory(this.feature) })));
424 tr.appendChild(heading);
426 var td = document.createElement("td");
430 var link = document.createElement("a");
431 link.href = "/browse/" + this.type + "/" + this.feature.osm_id + "/history";
432 link.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.details')}"));
433 td.appendChild(link);
435 var div = document.createElement("div");
436 div.className = "browse_details";
438 var nodes = doc.getElementsByTagName(this.type);
439 var history = document.createElement("ul");
440 for (var i = nodes.length - 1; i >= 0; i--) {
441 var user = nodes[i].getAttribute("user") || "#{I18n.t('browse.start_rjs.private_user')}";
442 var timestamp = nodes[i].getAttribute("timestamp");
443 var item = document.createElement("li");
444 item.appendChild(document.createTextNode(i18n("#{I18n.t('browse.start_rjs.edited_by_user_at_timestamp')}", { user: user, timestamp: timestamp })));
445 history.appendChild(item);
447 div.appendChild(history);
449 $("browse_content").appendChild(div);
452 function featureType(feature) {
453 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
460 function featureTypeName(feature) {
461 if (featureType(feature) == "node") {
462 return "#{I18n.t('browse.start_rjs.object_list.type.node')}";
463 } else if (featureType(feature) == "way") {
464 return "#{I18n.t('browse.start_rjs.object_list.type.way')}";
468 function featureName(feature) {
469 if (feature.attributes['name:#{I18n.locale}']) {
470 return feature.attributes['name:#{I18n.locale}'];
471 } else if (feature.attributes.name) {
472 return feature.attributes.name;
474 return feature.osm_id;
478 function featureNameSelect(feature) {
479 if (feature.attributes['name:#{I18n.locale}']) {
480 return feature.attributes['name:#{I18n.locale}'];
481 } else if (feature.attributes.name) {
482 return feature.attributes.name;
483 } else if (featureType(feature) == "node") {
484 return i18n("#{I18n.t('browse.start_rjs.object_list.selected.type.node')}", { id: feature.osm_id });
485 } else if (featureType(feature) == "way") {
486 return i18n("#{I18n.t('browse.start_rjs.object_list.selected.type.way')}", { id: feature.osm_id });
490 function featureNameHistory(feature) {
491 if (feature.attributes['name:#{I18n.locale}']) {
492 return feature.attributes['name:#{I18n.locale}'];
493 } else if (feature.attributes.name) {
494 return feature.attributes.name;
495 } else if (featureType(feature) == "node") {
496 return i18n("#{I18n.t('browse.start_rjs.object_list.history.type.node')}", { id: feature.osm_id });
497 } else if (featureType(feature) == "way") {
498 return i18n("#{I18n.t('browse.start_rjs.object_list.history.type.way')}", { id: feature.osm_id });
502 function setStatus(status) {
503 $("browse_status").innerHTML = status;
504 $("browse_status").style.display = "block";
507 function clearStatus() {
508 $("browse_status").innerHTML = "";
509 $("browse_status").style.display = "none";