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;
12 var areasHidden = false;
14 OpenLayers.Feature.Vector.style['default'].strokeWidth = 3;
15 OpenLayers.Feature.Vector.style['default'].cursor = "pointer";
17 function startBrowse() {
18 map.dataLayer.active = true;
20 openSidebar({ onclose: stopBrowse });
22 var vectors = new OpenLayers.Layer.Vector();
24 browseBoxControl = new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.RegularPolygon, {
32 browseBoxControl.handler.callbacks.done = endDrag;
33 map.addControl(browseBoxControl);
35 map.events.register("moveend", map, showData);
36 map.events.triggerEvent("moveend");
38 $("browse_hide_areas_box").innerHTML = "#{I18n.t('browse.start_rjs.hide_areas')}";
39 $("browse_hide_areas_box").style.display = "inline";
40 $("browse_hide_areas_box").onclick = hideAreas;
44 if (browseMode == "auto") {
45 if (map.getZoom() >= 15) {
48 setStatus("#{I18n.t('browse.start_rjs.zoom_or_select')}");
53 function stopBrowse() {
54 if (map.dataLayer.active) {
55 map.dataLayer.active = false;
57 if (browseSelectControl) {
58 browseSelectControl.destroy();
59 browseSelectControl = null;
62 if (browseBoxControl) {
63 browseBoxControl.destroy();
64 browseBoxControl = null;
67 if (browseActiveFeature) {
68 browseActiveFeature.destroy();
69 browseActiveFeature = null;
72 if (browseDataLayer) {
73 browseDataLayer.destroy();
74 browseDataLayer = null;
77 map.dataLayer.setVisibility(false);
78 map.events.unregister("moveend", map, showData);
82 function startDrag() {
83 $("browse_select_box").innerHTML="#{I18n.t('browse.start_rjs.drag_a_box')}";
85 browseBoxControl.activate();
90 $("browse_select_box").onclick = startDrag;
92 function useMap(reload) {
93 var bounds = map.getExtent();
94 var projected = bounds.clone().transform(map.getProjectionObject(), epsg4326);
96 if (!browseBounds || !browseBounds.containsBounds(projected)) {
97 var center = bounds.getCenterLonLat();
98 var tileWidth = bounds.getWidth() * 1.2;
99 var tileHeight = bounds.getHeight() * 1.2;
100 var tileBounds = new OpenLayers.Bounds(center.lon - (tileWidth / 2),
101 center.lat - (tileHeight / 2),
102 center.lon + (tileWidth / 2),
103 center.lat + (tileHeight / 2));
105 browseBounds = tileBounds;
106 getData(tileBounds, reload);
110 $("browse_select_view").style.display = "none";
116 function hideAreas() {
117 $("browse_hide_areas_box").innerHTML = "#{I18n.t('browse.start_rjs.show_areas')}";
118 $("browse_hide_areas_box").style.display = "inline";
119 $("browse_hide_areas_box").onclick = showAreas;
126 function showAreas() {
127 $("browse_hide_areas_box").innerHTML = "#{I18n.t('browse.start_rjs.hide_areas')}";
128 $("browse_hide_areas_box").style.display = "inline";
129 $("browse_hide_areas_box").onclick = hideAreas;
136 $("browse_select_view").onclick = useMap;
138 function endDrag(bbox) {
139 var bounds = bbox.getBounds();
140 var projected = bounds.clone().transform(map.getProjectionObject(), epsg4326);
142 browseBoxControl.deactivate();
143 browseBounds = projected;
146 browseMode = "manual";
148 $("browse_select_box").innerHTML = "#{I18n.t('browse.start_rjs.manually_select')}";
149 $("browse_select_view").style.display = "inline";
152 function displayFeatureWarning() {
155 var div = document.createElement("div");
157 var p = document.createElement("p");
158 p.appendChild(document.createTextNode(i18n("#{I18n.t('browse.start_rjs.loaded_an_area_with_num_features')}", { num_features: browseFeatureList.length })));
161 var input = document.createElement("input");
162 input.type = "submit";
163 input.value = "#{I18n.t('browse.start_rjs.load_data')}";
164 input.onclick = loadFeatureList;
165 div.appendChild(input);
167 $("browse_content").innerHTML = "";
168 $("browse_content").appendChild(div);
171 function loadFeatureList() {
172 browseDataLayer.addFeatures(browseFeatureList);
173 browseDataLayer.events.triggerEvent("loadend");
175 browseFeatureList = [];
180 function customDataLoader(request) {
181 if (this.map.dataLayer.active) {
182 var doc = request.responseXML;
184 if (!doc || !doc.documentElement) {
185 doc = request.responseText;
190 OpenLayers.Util.extend(options, this.formatOptions);
192 if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
193 options.externalProjection = this.projection;
194 options.internalProjection = this.map.getProjectionObject();
197 var gml = this.format ? new this.format(options) : new OpenLayers.Format.GML(options);
199 browseFeatureList = gml.read(doc);
201 if (!this.maxFeatures || browseFeatureList.length <= this.maxFeatures) {
204 displayFeatureWarning();
209 function getData(bounds, reload) {
210 var projected = bounds.clone().transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
211 var size = projected.getWidth() * projected.getHeight();
213 if (size > #{MAX_REQUEST_AREA}) {
214 setStatus(i18n("#{I18n.t('browse.start_rjs.unable_to_load_size', :max_bbox_size => MAX_REQUEST_AREA)}", { bbox_size: size }));
216 loadGML("/api/#{API_VERSION}/map?bbox=" + projected.toBBOX(), reload);
220 function loadGML(url, reload) {
221 setStatus("#{I18n.t('browse.start_rjs.loading')}");
222 $("browse_content").innerHTML = "";
224 var formatOptions = {
226 interestingTagsExclude: ['source','source_ref','source:ref','history','attribution','created_by','tiger:county','tiger:tlid','tiger:upload_uuid']
229 if (areasHidden) formatOptions.areaTags = [];
231 if (!browseDataLayer || reload) {
232 var style = new OpenLayers.Style();
234 style.addRules([new OpenLayers.Rule({
236 Polygon: { fillColor: '#ff0000', strokeColor: '#ff0000' },
237 Line: { fillColor: '#ffff00', strokeColor: '#000000', strokeOpacity: '0.4' },
238 Point: { fillColor: '#00ff00', strokeColor: '#00ff00' }
242 if (browseDataLayer) browseDataLayer.destroyFeatures();
244 browseDataLayer = new OpenLayers.Layer.GML("Data", url, {
245 format: OpenLayers.Format.OSM,
246 formatOptions: formatOptions,
248 requestSuccess: customDataLoader,
249 displayInLayerSwitcher: false,
250 styleMap: new OpenLayers.StyleMap({
252 'select': { strokeColor: '#0000ff', strokeWidth: 8 }
255 browseDataLayer.events.register("loadend", browseDataLayer, dataLoaded );
256 map.addLayer(browseDataLayer);
258 browseSelectControl = new OpenLayers.Control.SelectFeature(browseDataLayer, { onSelect: onFeatureSelect });
259 browseSelectControl.handlers.feature.stopDown = false;
260 browseSelectControl.handlers.feature.stopUp = false;
261 map.addControl(browseSelectControl);
262 browseSelectControl.activate();
264 browseDataLayer.destroyFeatures();
265 browseDataLayer.format(formatOptions);
266 browseDataLayer.setUrl(url);
269 browseActiveFeature = null;
272 function dataLoaded() {
273 if (this.map.dataLayer.active) {
276 browseObjectList = document.createElement("div")
278 var heading = document.createElement("p");
279 heading.className = "browse_heading";
280 heading.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.object_list.heading')}"));
281 browseObjectList.appendChild(heading);
283 var list = document.createElement("ul");
285 for (var i = 0; i < this.features.length; i++) {
286 var feature = this.features[i];
289 var type = featureType(feature);
290 var typeName = featureTypeName(feature);
291 var li = document.createElement("li");
292 li.appendChild(document.createTextNode(typeName + " "));
294 // Link, for viewing in the tab
295 var link = document.createElement("a");
296 link.href = "/browse/" + type + "/" + feature.osm_id;
297 var name = featureName(feature);
298 link.appendChild(document.createTextNode(name));
299 link.feature = feature;
300 link.onclick = OpenLayers.Function.bind(viewFeatureLink, link);
301 li.appendChild(link);
303 list.appendChild(li);
306 browseObjectList.appendChild(list)
308 var link = document.createElement("a");
309 link.href = this.url;
310 link.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.object_list.api')}"));
311 browseObjectList.appendChild(link);
313 $("browse_content").innerHTML = "";
314 $("browse_content").appendChild(browseObjectList);
318 function viewFeatureLink() {
319 var layer = this.feature.layer;
321 for (var i = 0; i < layer.selectedFeatures.length; i++) {
322 var f = layer.selectedFeatures[i];
323 layer.drawFeature(f, layer.styleMap.createSymbolizer(f, "default"));
326 onFeatureSelect(this.feature);
328 if (browseMode != "auto") {
329 map.setCenter(this.feature.geometry.getBounds().getCenterLonLat());
335 function loadObjectList() {
336 $("browse_content").innerHTML="";
337 $("browse_content").appendChild(browseObjectList);
342 function onFeatureSelect(feature) {
343 // Unselect previously selected feature
344 if (browseActiveFeature) {
345 browseActiveFeature.layer.drawFeature(
347 browseActiveFeature.layer.styleMap.createSymbolizer(browseActiveFeature, "default")
351 // Redraw in selected style
352 feature.layer.drawFeature(
353 feature, feature.layer.styleMap.createSymbolizer(feature, "select")
356 // If the current object is the list, don't innerHTML="", since that could clear it.
357 if ($("browse_content").firstChild == browseObjectList) {
358 $("browse_content").removeChild(browseObjectList);
360 $("browse_content").innerHTML = "";
363 // Create a link back to the object list
364 var div = document.createElement("div");
365 div.style.textAlign = "center";
366 div.style.marginBottom = "20px";
367 $("browse_content").appendChild(div);
368 var link = document.createElement("a");
370 link.onclick = loadObjectList;
371 link.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.object_list.back')}"));
372 div.appendChild(link);
374 var table = document.createElement("table");
375 table.width = "100%";
376 table.className = "browse_heading";
377 $("browse_content").appendChild(table);
379 var tr = document.createElement("tr");
380 table.appendChild(tr);
382 var heading = document.createElement("td");
383 heading.appendChild(document.createTextNode(featureNameSelect(feature)));
384 tr.appendChild(heading);
386 var td = document.createElement("td");
390 var type = featureType(feature);
391 var link = document.createElement("a");
392 link.href = "/browse/" + type + "/" + feature.osm_id;
393 link.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.object_list.details')}"));
394 td.appendChild(link);
396 var div = document.createElement("div");
397 div.className = "browse_details";
399 $("browse_content").appendChild(div);
401 // Now the list of attributes
402 var ul = document.createElement("ul");
403 for (var key in feature.attributes) {
404 var li = document.createElement("li");
405 var b = document.createElement("b");
406 b.appendChild(document.createTextNode(key));
408 li.appendChild(document.createTextNode(": " + feature.attributes[key]));
414 var link = document.createElement("a");
415 link.href = "/browse/" + type + "/" + feature.osm_id + "/history";
416 link.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.show_history')}"));
417 link.onclick = OpenLayers.Function.bind(loadHistory, {
418 type: type, feature: feature, link: link
421 div.appendChild(link);
423 // Stash the currently drawn feature
424 browseActiveFeature = feature;
427 function loadHistory() {
429 this.link.innerHTML = "#{I18n.t('browse.start_rjs.wait')}";
431 new Ajax.Request("/api/#{API_VERSION}/" + this.type + "/" + this.feature.osm_id + "/history", {
432 onComplete: OpenLayers.Function.bind(displayHistory, this)
438 function displayHistory(request) {
439 if (browseActiveFeature.osm_id != this.feature.osm_id || $("browse_content").firstChild == browseObjectList) {
443 this.link.parentNode.removeChild(this.link);
445 var doc = request.responseXML;
447 var table = document.createElement("table");
448 table.width = "100%";
449 table.className = "browse_heading";
450 $("browse_content").appendChild(table);
452 var tr = document.createElement("tr");
453 table.appendChild(tr);
455 var heading = document.createElement("td");
456 heading.appendChild(document.createTextNode(i18n("#{I18n.t('browse.start_rjs.history_for_feature')}", { feature: featureNameHistory(this.feature) })));
457 tr.appendChild(heading);
459 var td = document.createElement("td");
463 var link = document.createElement("a");
464 link.href = "/browse/" + this.type + "/" + this.feature.osm_id + "/history";
465 link.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.details')}"));
466 td.appendChild(link);
468 var div = document.createElement("div");
469 div.className = "browse_details";
471 var nodes = doc.getElementsByTagName(this.type);
472 var history = document.createElement("ul");
473 for (var i = nodes.length - 1; i >= 0; i--) {
474 var user = nodes[i].getAttribute("user") || "#{I18n.t('browse.start_rjs.private_user')}";
475 var timestamp = nodes[i].getAttribute("timestamp");
476 var item = document.createElement("li");
477 item.appendChild(document.createTextNode(i18n("#{I18n.t('browse.start_rjs.edited_by_user_at_timestamp')}", { user: user, timestamp: timestamp })));
478 history.appendChild(item);
480 div.appendChild(history);
482 $("browse_content").appendChild(div);
485 function featureType(feature) {
486 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
493 function featureTypeName(feature) {
494 if (featureType(feature) == "node") {
495 return "#{I18n.t('browse.start_rjs.object_list.type.node')}";
496 } else if (featureType(feature) == "way") {
497 return "#{I18n.t('browse.start_rjs.object_list.type.way')}";
501 function featureName(feature) {
502 if (feature.attributes['name:#{I18n.locale}']) {
503 return feature.attributes['name:#{I18n.locale}'];
504 } else if (feature.attributes.name) {
505 return feature.attributes.name;
507 return feature.osm_id;
511 function featureNameSelect(feature) {
512 if (feature.attributes['name:#{I18n.locale}']) {
513 return feature.attributes['name:#{I18n.locale}'];
514 } else if (feature.attributes.name) {
515 return feature.attributes.name;
516 } else if (featureType(feature) == "node") {
517 return i18n("#{I18n.t('browse.start_rjs.object_list.selected.type.node')}", { id: feature.osm_id });
518 } else if (featureType(feature) == "way") {
519 return i18n("#{I18n.t('browse.start_rjs.object_list.selected.type.way')}", { id: feature.osm_id });
523 function featureNameHistory(feature) {
524 if (feature.attributes['name:#{I18n.locale}']) {
525 return feature.attributes['name:#{I18n.locale}'];
526 } else if (feature.attributes.name) {
527 return feature.attributes.name;
528 } else if (featureType(feature) == "node") {
529 return i18n("#{I18n.t('browse.start_rjs.object_list.history.type.node')}", { id: feature.osm_id });
530 } else if (featureType(feature) == "way") {
531 return i18n("#{I18n.t('browse.start_rjs.object_list.history.type.way')}", { id: feature.osm_id });
535 function setStatus(status) {
536 $("browse_status").innerHTML = status;
537 $("browse_status").style.display = "block";
540 function clearStatus() {
541 $("browse_status").innerHTML = "";
542 $("browse_status").style.display = "none";