2 var browseMode = "auto";
5 var browseActiveFeature;
7 var browseSelectControl;
9 var areasHidden = false;
11 OpenLayers.Feature.Vector.style['default'].strokeWidth = 3;
12 OpenLayers.Feature.Vector.style['default'].cursor = "pointer";
14 function startBrowse() {
15 map.dataLayer.active = true;
17 $("#sidebar_title").html("<%=j t 'browse.start_rjs.data_frame_title' %>");
18 $("#sidebar_content").html("<%=j render :partial => "sidebar" %>");
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_select_box").click(startDrag);
40 $("#browse_hide_areas_box").html("<%=j t 'browse.start_rjs.hide_areas' %>");
41 $("#browse_hide_areas_box").show();
42 $("#browse_hide_areas_box").click(hideAreas);
46 if (browseMode == "auto") {
47 if (map.getZoom() >= 15) {
50 setStatus("<%=j t 'browse.start_rjs.zoom_or_select' %>");
55 function stopBrowse() {
56 if (map.dataLayer.active) {
57 map.dataLayer.active = false;
59 if (browseSelectControl) {
60 browseSelectControl.destroy();
61 browseSelectControl = null;
64 if (browseBoxControl) {
65 browseBoxControl.destroy();
66 browseBoxControl = null;
69 if (browseActiveFeature) {
70 browseActiveFeature.destroy();
71 browseActiveFeature = null;
74 if (browseDataLayer) {
75 browseDataLayer.destroy();
76 browseDataLayer = null;
79 map.dataLayer.setVisibility(false);
80 map.events.unregister("moveend", map, showData);
84 function startDrag() {
85 $("#browse_select_box").html("<%=j t 'browse.start_rjs.drag_a_box' %>");
87 browseBoxControl.activate();
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").hide();
116 function hideAreas() {
117 $("#browse_hide_areas_box").html("<%=j t 'browse.start_rjs.show_areas' %>");
118 $("#browse_hide_areas_box").show();
119 $("#browse_hide_areas_box").click(showAreas);
126 function showAreas() {
127 $("#browse_hide_areas_box").html("<%=j t 'browse.start_rjs.hide_areas' %>");
128 $("#browse_hide_areas_box").show();
129 $("#browse_hide_areas_box").click(hideAreas);
136 $("#browse_select_view").click(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").html("<%=j t 'browse.start_rjs.manually_select' %>");
149 $("#browse_select_view").show();
152 function displayFeatureWarning() {
155 var div = document.createElement("div");
157 var p = document.createElement("p");
158 p.appendChild(document.createTextNode(i18n("<%=j 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 = "<%=j t 'browse.start_rjs.load_data' %>";
164 input.onclick = loadFeatureList;
165 div.appendChild(input);
167 $("#browse_content").html("");
168 $("#browse_content").append(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("<%=j 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("<%=j t 'browse.start_rjs.loading' %>");
223 $("#browse_content").empty();
225 var formatOptions = {
227 interestingTagsExclude: ['source','source_ref','source:ref','history','attribution','created_by','tiger:county','tiger:tlid','tiger:upload_uuid']
230 if (areasHidden) formatOptions.areaTags = [];
232 if (!browseDataLayer || reload) {
233 var style = new OpenLayers.Style();
235 style.addRules([new OpenLayers.Rule({
237 Polygon: { fillColor: '#ff0000', strokeColor: '#ff0000' },
238 Line: { fillColor: '#ffff00', strokeColor: '#000000', strokeOpacity: '0.4' },
239 Point: { fillColor: '#00ff00', strokeColor: '#00ff00' }
243 if (browseDataLayer) browseDataLayer.destroyFeatures();
245 browseDataLayer = new OpenLayers.Layer.GML("Data", url, {
246 format: OpenLayers.Format.OSM,
247 formatOptions: formatOptions,
249 requestSuccess: customDataLoader,
250 displayInLayerSwitcher: false,
251 styleMap: new OpenLayers.StyleMap({
253 'select': { strokeColor: '#0000ff', strokeWidth: 8 }
256 browseDataLayer.events.register("loadend", browseDataLayer, dataLoaded );
257 map.addLayer(browseDataLayer);
259 browseSelectControl = new OpenLayers.Control.SelectFeature(browseDataLayer, { onSelect: onFeatureSelect });
260 browseSelectControl.handlers.feature.stopDown = false;
261 browseSelectControl.handlers.feature.stopUp = false;
262 map.addControl(browseSelectControl);
263 browseSelectControl.activate();
265 browseDataLayer.destroyFeatures();
266 browseDataLayer.format(formatOptions);
267 browseDataLayer.setUrl(url);
270 browseActiveFeature = null;
273 function dataLoaded() {
274 if (this.map.dataLayer.active) {
277 browseObjectList = document.createElement("div");
279 var heading = document.createElement("p");
280 heading.className = "browse_heading";
281 heading.appendChild(document.createTextNode("<%=j t 'browse.start_rjs.object_list.heading' %>"));
282 browseObjectList.appendChild(heading);
284 var list = document.createElement("ul");
286 for (var i = 0; i < this.features.length; i++) {
287 var feature = this.features[i];
290 var type = featureType(feature);
291 var typeName = featureTypeName(feature);
292 var li = document.createElement("li");
293 li.appendChild(document.createTextNode(typeName + " "));
295 // Link, for viewing in the tab
296 var link = document.createElement("a");
297 link.href = "/browse/" + type + "/" + feature.osm_id;
298 var name = featureName(feature);
299 link.appendChild(document.createTextNode(name));
300 link.feature = feature;
301 link.onclick = OpenLayers.Function.bind(viewFeatureLink, link);
302 li.appendChild(link);
304 list.appendChild(li);
307 browseObjectList.appendChild(list);
309 var link = document.createElement("a");
310 link.href = this.url;
311 link.appendChild(document.createTextNode("<%=j t 'browse.start_rjs.object_list.api' %>"));
312 browseObjectList.appendChild(link);
314 $("#browse_content").html(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").empty();
337 $("#browse_content").append(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").empty();
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").append(div);
368 var link = document.createElement("a");
370 link.onclick = loadObjectList;
371 link.appendChild(document.createTextNode("<%=j 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").append(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("<%=j 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").append(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("<%=j 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 = "<%=j t 'browse.start_rjs.wait' %>";
431 $.ajax("/api/<%= API_VERSION %>/" + this.type + "/" + this.feature.osm_id + "/history", {
432 complete: 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").append(table);
452 var tr = document.createElement("tr");
453 table.appendChild(tr);
455 var heading = document.createElement("td");
456 heading.appendChild(document.createTextNode(i18n("<%=j 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("<%=j 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") || "<%=j 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("<%=j t 'browse.start_rjs.edited_by_user_at_timestamp' %>", { user: user, timestamp: timestamp })));
478 history.appendChild(item);
480 div.appendChild(history);
482 $("#browse_content").append(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 "<%=j t 'browse.start_rjs.object_list.type.node' %>";
496 } else if (featureType(feature) == "way") {
497 return "<%=j 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("<%=j t 'browse.start_rjs.object_list.selected.type.node' %>", { id: feature.osm_id });
518 } else if (featureType(feature) == "way") {
519 return i18n("<%=j 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("<%=j t 'browse.start_rjs.object_list.history.type.node' %>", { id: feature.osm_id });
530 } else if (featureType(feature) == "way") {
531 return i18n("<%=j t 'browse.start_rjs.object_list.history.type.way' %>", { id: feature.osm_id });
535 function setStatus(status) {
536 $("#browse_status").html(status);
537 $("#browse_status").show();
540 function clearStatus() {
541 $("#browse_status").html("");
542 $("#browse_status").hide();