1 page.replace_html :sidebar_title, t('browse.start_rjs.data_frame_title')
2 page.replace_html :sidebar_content, :partial => 'start'
6 var browseMode = "auto";
9 var browseActiveFeature;
11 var browseSelectControl;
14 OpenLayers.Feature.Vector.style['default'].strokeWidth = 3;
15 OpenLayers.Feature.Vector.style['default'].cursor = "pointer";
17 function startBrowse() {
20 openSidebar({ onclose: stopBrowse });
22 var vectors = new OpenLayers.Layer.Vector();
24 browseBoxControl = new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.RegularPolygon, {
30 callbacks: { done: endDrag }
33 map.addControl(browseBoxControl);
35 map.events.register("moveend", map, showData);
36 map.events.triggerEvent("moveend");
40 if (browseMode == "auto") {
41 if (map.getZoom() >= 15) {
44 setStatus("#{I18n.t('browse.start_rjs.zoom_or_select')}");
49 function stopBrowse() {
53 if (browseSelectControl) {
54 browseSelectControl.destroy();
55 browseSelectControl = null;
58 if (browseBoxControl) {
59 browseBoxControl.destroy();
60 browseBoxControl = null;
63 if (browseActiveFeature) {
64 browseActiveFeature.destroy();
65 browseActiveFeature = null;
68 if (browseDataLayer) {
69 browseDataLayer.destroy();
70 browseDataLayer = null;
73 map.dataLayer.setVisibility(false);
74 map.events.unregister("moveend", map, showData);
78 function startDrag() {
79 $("browse_select_box").innerHTML='#{I18n.t('browse.start_rjs.drag_a_box')}';
81 browseBoxControl.activate();
86 $("browse_select_box").onclick = startDrag;
89 var bounds = map.getExtent();
90 var projected = bounds.clone().transform(map.getProjectionObject(), epsg4326);
92 if (!browseBounds || !browseBounds.containsBounds(projected)) {
93 var center = bounds.getCenterLonLat();
94 var tileWidth = bounds.getWidth() * 1.2;
95 var tileHeight = bounds.getHeight() * 1.2;
96 var tileBounds = new OpenLayers.Bounds(center.lon - (tileWidth / 2),
97 center.lat - (tileHeight / 2),
98 center.lon + (tileWidth / 2),
99 center.lat + (tileHeight / 2));
101 browseBounds = tileBounds;
106 $("browse_select_view").style.display = "none";
112 $("browse_select_view").onclick = useMap;
114 function endDrag(bbox) {
115 var bounds = bbox.getBounds();
116 var projected = bounds.clone().transform(map.getProjectionObject(), epsg4326);
118 browseBoxControl.deactivate();
119 browseBounds = projected;
122 browseMode = "manual";
124 $("browse_select_box").innerHTML = "#{I18n.t('browse.start_rjs.manually_select')}";
125 $("browse_select_view").style.display = "inline";
128 function displayFeatureWarning() {
131 var div = document.createElement("div");
133 var p = document.createElement("p");
134 p.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.loaded_an_area')} " + browseFeatureList.length + " #{I18n.t('browse.start_rjs.browsers')}"));
137 var input = document.createElement("input");
138 input.type = "submit";
139 input.value = "#{I18n.t('browse.start_rjs.load_data')}";
140 input.onclick = loadFeatureList;
141 div.appendChild(input);
143 $("browse_content").innerHTML = "";
144 $("browse_content").appendChild(div);
147 function loadFeatureList() {
148 browseDataLayer.addFeatures(browseFeatureList);
149 browseDataLayer.events.triggerEvent("loadend");
151 browseFeatureList = [];
156 function customDataLoader(request) {
158 var doc = request.responseXML;
160 if (!doc || !doc.documentElement) {
161 doc = request.responseText;
166 OpenLayers.Util.extend(options, this.formatOptions);
168 if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
169 options.externalProjection = this.projection;
170 options.internalProjection = this.map.getProjectionObject();
173 var gml = this.format ? new this.format(options) : new OpenLayers.Format.GML(options);
175 browseFeatureList = gml.read(doc);
177 if (!this.maxFeatures || browseFeatureList.length <= this.maxFeatures) {
180 displayFeatureWarning();
185 function getData(bounds) {
186 var projected = bounds.clone().transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
187 var size = projected.getWidth() * projected.getHeight();
190 setStatus("#{I18n.t('browse.start_rjs.unable_to_load')} " + size + " #{I18n.t('browse.start_rjs.must_be_smaller')}");
192 loadGML("/api/#{API_VERSION}/map?bbox=" + projected.toBBOX());
196 function loadGML(url) {
197 setStatus("#{I18n.t('browse.start_rjs.loading')}");
198 $("browse_content").innerHTML = "";
200 if (!browseDataLayer) {
201 var style = new OpenLayers.Style();
203 style.addRules([new OpenLayers.Rule({
205 Polygon: { fillColor: '#ff0000', strokeColor: '#ff0000' },
206 Line: { fillColor: '#ffff00', strokeColor: '#000000', strokeOpacity: '0.4' },
207 Point: { fillColor: '#00ff00', strokeColor: '#00ff00' }
211 browseDataLayer = new OpenLayers.Layer.GML("Data", url, {
212 format: OpenLayers.Format.OSM,
215 interestingTagsExclude: ['source','source_ref','source:ref','history','attribution','created_by','tiger:county','tiger:tlid','tiger:upload_uuid']
218 requestSuccess: customDataLoader,
219 displayInLayerSwitcher: false,
220 styleMap: new OpenLayers.StyleMap({
222 'select': { strokeColor: '#0000ff', strokeWidth: 8 }
225 browseDataLayer.events.register("loadend", browseDataLayer, dataLoaded );
226 map.addLayer(browseDataLayer);
228 browseSelectControl = new OpenLayers.Control.SelectFeature(browseDataLayer, { onSelect: onFeatureSelect });
229 browseSelectControl.handlers.feature.stopDown = false;
230 browseSelectControl.handlers.feature.stopUp = false;
231 map.addControl(browseSelectControl);
232 browseSelectControl.activate();
234 browseDataLayer.setUrl(url);
237 browseActiveFeature = null;
240 function dataLoaded() {
244 browseObjectList = document.createElement("div")
246 var heading = document.createElement("p");
247 heading.className = "browse_heading";
248 heading.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.object_list.heading')}"));
249 browseObjectList.appendChild(heading);
251 var list = document.createElement("ul");
252 // Pull in 118n names for node/way/relation
253 var typeTranslations = new Array();
254 typeTranslations['node'] = "#{I18n.t('browse.start_rjs.object_list.type.node')}"
255 typeTranslations['way'] = "#{I18n.t('browse.start_rjs.object_list.type.way')}"
256 typeTranslations['relation'] = "#{I18n.t('browse.start_rjs.object_list.type.relation')}"
258 for (var i = 0; i < this.features.length; i++) {
259 var feature = this.features[i];
262 var type = featureType(feature);
263 var li = document.createElement("li");
264 li.appendChild(document.createTextNode(typeTranslations[type] + " "));
266 // Link, for viewing in the tab
267 var link = document.createElement("a");
268 link.href = "/browse/" + type + "/" + feature.osm_id;
269 var name = feature.attributes.name || feature.osm_id;
270 link.appendChild(document.createTextNode(name));
271 link.feature = feature;
272 link.onclick = OpenLayers.Function.bind(viewFeatureLink, link);
273 li.appendChild(link);
275 list.appendChild(li);
278 browseObjectList.appendChild(list)
280 var link = document.createElement("a");
281 link.href = this.url;
282 link.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.object_list.api')}"));
283 browseObjectList.appendChild(link);
285 $("browse_content").innerHTML = "";
286 $("browse_content").appendChild(browseObjectList);
290 function viewFeatureLink() {
291 var layer = this.feature.layer;
293 for (var i = 0; i < layer.selectedFeatures.length; i++) {
294 var f = layer.selectedFeatures[i];
295 layer.drawFeature(f, layer.styleMap.createSymbolizer(f, "default"));
298 onFeatureSelect(this.feature);
300 if (browseMode != "auto") {
301 map.setCenter(this.feature.geometry.getBounds().getCenterLonLat());
307 function loadObjectList() {
308 $("browse_content").innerHTML="";
309 $("browse_content").appendChild(browseObjectList);
314 function onFeatureSelect(feature) {
315 // Unselect previously selected feature
316 if (browseActiveFeature) {
317 browseActiveFeature.layer.drawFeature(
319 browseActiveFeature.layer.styleMap.createSymbolizer(browseActiveFeature, "default")
323 // Redraw in selected style
324 feature.layer.drawFeature(
325 feature, feature.layer.styleMap.createSymbolizer(feature, "select")
328 // If the current object is the list, don't innerHTML="", since that could clear it.
329 if ($("browse_content").firstChild == browseObjectList) {
330 $("browse_content").removeChild(browseObjectList);
332 $("browse_content").innerHTML = "";
335 // Create a link back to the object list
336 var div = document.createElement("div");
337 div.style.textAlign = "center";
338 div.style.marginBottom = "20px";
339 $("browse_content").appendChild(div);
340 var link = document.createElement("a");
342 link.onclick = loadObjectList;
343 link.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.object_list.back')}"));
344 div.appendChild(link);
346 var table = document.createElement("table");
347 table.width = "100%";
348 table.className = "browse_heading";
349 $("browse_content").appendChild(table);
351 var tr = document.createElement("tr");
352 table.appendChild(tr);
354 var heading = document.createElement("td");
355 heading.appendChild(document.createTextNode(featureNameSelect(feature)));
356 tr.appendChild(heading);
358 var td = document.createElement("td");
362 var type = featureType(feature);
363 var link = document.createElement("a");
364 link.href = "/browse/" + type + "/" + feature.osm_id;
365 link.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.object_list.details')}"));
366 td.appendChild(link);
368 var div = document.createElement("div");
369 div.className = "browse_details";
371 $("browse_content").appendChild(div);
373 // Now the list of attributes
374 var ul = document.createElement("ul");
375 for (var key in feature.attributes) {
376 var li = document.createElement("li");
377 var b = document.createElement("b");
378 b.appendChild(document.createTextNode(key));
380 li.appendChild(document.createTextNode(": " + feature.attributes[key]));
386 var link = document.createElement("a");
387 link.href = "/browse/" + type + "/" + feature.osm_id + "/history";
388 link.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.show_history')}"));
389 link.onclick = OpenLayers.Function.bind(loadHistory, {
390 type: type, feature: feature, link: link
393 div.appendChild(link);
395 // Stash the currently drawn feature
396 browseActiveFeature = feature;
399 function loadHistory() {
401 this.link.innerHTML = "#{I18n.t('browse.start_rjs.wait')}";
403 new Ajax.Request("/api/#{API_VERSION}/" + this.type + "/" + this.feature.osm_id + "/history", {
404 onComplete: OpenLayers.Function.bind(displayHistory, this)
410 function displayHistory(request) {
411 if (browseActiveFeature.osm_id != this.feature.osm_id || $("browse_content").firstChild == browseObjectList) {
415 this.link.parentNode.removeChild(this.link);
417 var doc = request.responseXML;
419 var table = document.createElement("table");
420 table.width = "100%";
421 table.className = "browse_heading";
422 $("browse_content").appendChild(table);
424 var tr = document.createElement("tr");
425 table.appendChild(tr);
427 var heading = document.createElement("td");
428 heading.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.history_for')} " + featureNameHistory(this.feature)));
429 tr.appendChild(heading);
431 var td = document.createElement("td");
435 var link = document.createElement("a");
436 link.href = "/browse/" + this.type + "/" + this.feature.osm_id + "/history";
437 link.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.details')}"));
438 td.appendChild(link);
440 var div = document.createElement("div");
441 div.className = "browse_details";
443 var nodes = doc.getElementsByTagName(this.type);
444 var history = document.createElement("ul");
445 for (var i = nodes.length - 1; i >= 0; i--) {
446 var user = nodes[i].getAttribute("user") || "#{I18n.t('browse.start_rjs.private_user')}";
447 var timestamp = nodes[i].getAttribute("timestamp");
448 var item = document.createElement("li");
449 item.appendChild(document.createTextNode("#{I18n.t('browse.start_rjs.edited_by')} " + user + " #{I18n.t('browse.start_rjs.at_timestamp')} " + timestamp));
450 history.appendChild(item);
452 div.appendChild(history);
454 $("browse_content").appendChild(div);
457 function featureType(feature) {
458 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
465 function featureNameSelect(feature) {
466 if (feature.attributes.name) {
467 return feature.attributes.name;
469 // Pull in 118n names for node/way
470 var featureNameSelectTypeTranslations = new Array();
471 featureNameSelectTypeTranslations['node'] = "#{I18n.t('browse.start_rjs.object_list.selected.type.node')}"
472 featureNameSelectTypeTranslations['way'] = "#{I18n.t('browse.start_rjs.object_list.selected.type.way')}"
473 return featureNameSelectTypeTranslations[featureType(feature)] + " " + feature.osm_id;
477 function featureNameHistory(feature) {
478 if (feature.attributes.name) {
479 return feature.attributes.name;
481 // Pull in 118n names for node/way
482 var featureNameHistoryTypeTranslations = new Array();
483 featureNameHistoryTypeTranslations['node'] = "#{I18n.t('browse.start_rjs.object_list.history.type.node')}"
484 featureNameHistoryTypeTranslations['way'] = "#{I18n.t('browse.start_rjs.object_list.history.type.way')}"
485 return featureNameHistoryTypeTranslations[featureType(feature)] + " " + feature.osm_id;
489 function setStatus(status) {
490 $("browse_status").innerHTML = status;
491 $("browse_status").style.display = "block";
494 function clearStatus() {
495 $("browse_status").innerHTML = "";
496 $("browse_status").style.display = "none";
499 function ucFirst(str) {
500 return str.substr(0,1).toUpperCase() + str.substr(1,str.length);