1 function startBrowse(sidebarHtml) {
3 var browseMode = "auto";
6 var browseActiveFeature;
8 var browseSelectControl;
10 var areasHidden = false;
12 OpenLayers.Feature.Vector.style['default'].strokeWidth = 3;
13 OpenLayers.Feature.Vector.style['default'].cursor = "pointer";
15 map.dataLayer.active = true;
17 $("#sidebar_title").html(I18n.t('browse.start_rjs.data_frame_title'));
18 $("#sidebar_content").html(sidebarHtml);
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, updateData);
36 map.events.triggerEvent("moveend");
38 $("#browse_select_view").click(useMap);
40 $("#browse_select_box").click(startDrag);
42 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
43 $("#browse_hide_areas_box").show();
44 $("#browse_hide_areas_box").click(hideAreas);
46 function updateData() {
47 if (browseMode == "auto") {
48 if (map.getZoom() >= 15) {
51 setStatus(I18n.t('browse.start_rjs.zoom_or_select'));
56 function stopBrowse() {
57 if (map.dataLayer.active) {
58 map.dataLayer.active = false;
60 if (browseSelectControl) {
61 browseSelectControl.destroy();
62 browseSelectControl = null;
65 if (browseBoxControl) {
66 browseBoxControl.destroy();
67 browseBoxControl = null;
70 if (browseActiveFeature) {
71 browseActiveFeature.destroy();
72 browseActiveFeature = null;
75 if (browseDataLayer) {
76 browseDataLayer.destroy();
77 browseDataLayer = null;
80 map.dataLayer.setVisibility(false);
81 map.events.unregister("moveend", map, updateData);
85 function startDrag() {
86 $("#browse_select_box").html(I18n.t('browse.start_rjs.drag_a_box'));
88 browseBoxControl.activate();
93 function useMap(reload) {
94 var bounds = map.getExtent();
95 var projected = bounds.clone().transform(map.getProjectionObject(), epsg4326);
97 if (!browseBounds || !browseBounds.containsBounds(projected)) {
98 var center = bounds.getCenterLonLat();
99 var tileWidth = bounds.getWidth() * 1.2;
100 var tileHeight = bounds.getHeight() * 1.2;
101 var tileBounds = new OpenLayers.Bounds(center.lon - (tileWidth / 2),
102 center.lat - (tileHeight / 2),
103 center.lon + (tileWidth / 2),
104 center.lat + (tileHeight / 2));
106 browseBounds = tileBounds;
107 getData(tileBounds, reload);
111 $("#browse_select_view").hide();
117 function hideAreas() {
118 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.show_areas'));
119 $("#browse_hide_areas_box").show();
120 $("#browse_hide_areas_box").click(showAreas);
127 function showAreas() {
128 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
129 $("#browse_hide_areas_box").show();
130 $("#browse_hide_areas_box").click(hideAreas);
137 function endDrag(bbox) {
138 var bounds = bbox.getBounds();
139 var projected = bounds.clone().transform(map.getProjectionObject(), epsg4326);
141 browseBoxControl.deactivate();
142 browseBounds = projected;
145 browseMode = "manual";
147 $("#browse_select_box").html(I18n.t('browse.start_rjs.manually_select'));
148 $("#browse_select_view").show();
151 function displayFeatureWarning(count, limit, callback) {
154 var div = document.createElement("div");
156 var p = document.createElement("p");
157 p.appendChild(document.createTextNode(I18n.t("browse.start_rjs.loaded_an_area_with_num_features", { num_features: count, max_features: limit })));
160 var input = document.createElement("input");
161 input.type = "submit";
162 input.value = I18n.t('browse.start_rjs.load_data');
163 input.onclick = callback;
164 div.appendChild(input);
166 $("#browse_content").html("");
167 $("#browse_content").append(div);
170 function customDataLoader(resp, options) {
171 if (map.dataLayer.active) {
172 var request = resp.priv;
173 var doc = request.responseXML;
175 if (!doc || !doc.documentElement) {
176 doc = request.responseText;
179 resp.features = this.format.read(doc);
181 if (!this.maxFeatures || resp.features.length <= this.maxFeatures) {
182 options.callback.call(options.scope, resp);
184 displayFeatureWarning(resp.features.length, this.maxFeatures, function () {
185 options.callback.call(options.scope, resp);
191 function getData(bounds, reload) {
192 var projected = bounds.clone().transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
193 var size = projected.getWidth() * projected.getHeight();
195 if (size > OSM.MAX_REQUEST_AREA) {
196 setStatus(I18n.t("browse.start_rjs.unable_to_load_size", { max_bbox_size: OSM.MAX_REQUEST_AREA, bbox_size: size }));
198 loadData("/api/" + OSM.API_VERSION + "/map?bbox=" + projected.toBBOX(), reload);
202 function loadData(url, reload) {
203 setStatus(I18n.t('browse.start_rjs.loading'));
205 $("#browse_content").empty();
207 var formatOptions = {
209 interestingTagsExclude: ['source','source_ref','source:ref','history','attribution','created_by','tiger:county','tiger:tlid','tiger:upload_uuid']
212 if (areasHidden) formatOptions.areaTags = [];
214 if (!browseDataLayer || reload) {
215 var style = new OpenLayers.Style();
217 style.addRules([new OpenLayers.Rule({
219 Polygon: { fillColor: '#ff0000', strokeColor: '#ff0000' },
220 Line: { fillColor: '#ffff00', strokeColor: '#000000', strokeOpacity: '0.4' },
221 Point: { fillColor: '#00ff00', strokeColor: '#00ff00' }
225 if (browseDataLayer) browseDataLayer.destroyFeatures();
228 * Modern browsers are quite happy showing far more than 100 features in
229 * the data browser, so increase the limit to 2000 by default, but keep
230 * it restricted to 500 for IE8 and 100 for older IEs.
232 var maxFeatures = 2000;
235 if (navigator.appVersion < 8) {
237 } else if (navigator.appVersion < 9) {
242 browseDataLayer = new OpenLayers.Layer.Vector("Data", {
244 new OpenLayers.Strategy.Fixed()
246 protocol: new OpenLayers.Protocol.HTTP({
248 format: new OpenLayers.Format.OSM(formatOptions),
249 maxFeatures: maxFeatures,
250 handleRead: customDataLoader
252 projection: new OpenLayers.Projection("EPSG:4326"),
253 displayInLayerSwitcher: false,
254 styleMap: new OpenLayers.StyleMap({
256 'select': { strokeColor: '#0000ff', strokeWidth: 8 }
259 browseDataLayer.events.register("loadend", browseDataLayer, dataLoaded );
260 map.addLayer(browseDataLayer);
262 browseSelectControl = new OpenLayers.Control.SelectFeature(browseDataLayer, { onSelect: onFeatureSelect });
263 browseSelectControl.handlers.feature.stopDown = false;
264 browseSelectControl.handlers.feature.stopUp = false;
265 map.addControl(browseSelectControl);
266 browseSelectControl.activate();
268 browseDataLayer.destroyFeatures();
269 browseDataLayer.refresh({ url: url });
272 browseActiveFeature = null;
275 function dataLoaded() {
276 if (this.map.dataLayer.active) {
279 browseObjectList = document.createElement("div");
281 var heading = document.createElement("p");
282 heading.className = "browse_heading";
283 heading.appendChild(document.createTextNode(I18n.t('browse.start_rjs.object_list.heading')));
284 browseObjectList.appendChild(heading);
286 var list = document.createElement("ul");
288 for (var i = 0; i < this.features.length; i++) {
289 var feature = this.features[i];
292 var type = featureType(feature);
293 var typeName = featureTypeName(feature);
294 var li = document.createElement("li");
295 li.appendChild(document.createTextNode(typeName + " "));
297 // Link, for viewing in the tab
298 var link = document.createElement("a");
299 link.href = "/browse/" + type + "/" + feature.osm_id;
300 var name = featureName(feature);
301 link.appendChild(document.createTextNode(name));
302 link.feature = feature;
303 link.onclick = OpenLayers.Function.bind(viewFeatureLink, link);
304 li.appendChild(link);
306 list.appendChild(li);
309 browseObjectList.appendChild(list);
311 var link = document.createElement("a");
312 link.href = this.protocol.url;
313 link.appendChild(document.createTextNode(I18n.t('browse.start_rjs.object_list.api')));
314 browseObjectList.appendChild(link);
316 $("#browse_content").html(browseObjectList);
320 function viewFeatureLink() {
321 var layer = this.feature.layer;
323 for (var i = 0; i < layer.selectedFeatures.length; i++) {
324 var f = layer.selectedFeatures[i];
325 layer.drawFeature(f, layer.styleMap.createSymbolizer(f, "default"));
328 onFeatureSelect(this.feature);
330 if (browseMode != "auto") {
331 map.setCenter(this.feature.geometry.getBounds().getCenterLonLat());
337 function loadObjectList() {
338 $("#browse_content").empty();
339 $("#browse_content").append(browseObjectList);
344 function onFeatureSelect(feature) {
345 // Unselect previously selected feature
346 if (browseActiveFeature) {
347 browseActiveFeature.layer.drawFeature(
349 browseActiveFeature.layer.styleMap.createSymbolizer(browseActiveFeature, "default")
353 // Redraw in selected style
354 feature.layer.drawFeature(
355 feature, feature.layer.styleMap.createSymbolizer(feature, "select")
358 // If the current object is the list, don't innerHTML="", since that could clear it.
359 if ($("#browse_content").firstChild == browseObjectList) {
360 $("#browse_content").removeChild(browseObjectList);
362 $("#browse_content").empty();
365 // Create a link back to the object list
366 var div = document.createElement("div");
367 div.style.textAlign = "center";
368 div.style.marginBottom = "20px";
369 $("#browse_content").append(div);
370 var link = document.createElement("a");
372 link.onclick = loadObjectList;
373 link.appendChild(document.createTextNode(I18n.t('browse.start_rjs.object_list.back')));
374 div.appendChild(link);
376 var table = document.createElement("table");
377 table.width = "100%";
378 table.className = "browse_heading";
379 $("#browse_content").append(table);
381 var tr = document.createElement("tr");
382 table.appendChild(tr);
384 var heading = document.createElement("td");
385 heading.appendChild(document.createTextNode(featureNameSelect(feature)));
386 tr.appendChild(heading);
388 var td = document.createElement("td");
392 var type = featureType(feature);
393 var link = document.createElement("a");
394 link.href = "/browse/" + type + "/" + feature.osm_id;
395 link.appendChild(document.createTextNode(I18n.t('browse.start_rjs.object_list.details')));
396 td.appendChild(link);
398 var div = document.createElement("div");
399 div.className = "browse_details";
401 $("#browse_content").append(div);
403 // Now the list of attributes
404 var ul = document.createElement("ul");
405 for (var key in feature.attributes) {
406 var li = document.createElement("li");
407 var b = document.createElement("b");
408 b.appendChild(document.createTextNode(key));
410 li.appendChild(document.createTextNode(": " + feature.attributes[key]));
416 var link = document.createElement("a");
417 link.href = "/browse/" + type + "/" + feature.osm_id + "/history";
418 link.appendChild(document.createTextNode(I18n.t('browse.start_rjs.show_history')));
419 link.onclick = OpenLayers.Function.bind(loadHistory, {
420 type: type, feature: feature, link: link
423 div.appendChild(link);
425 // Stash the currently drawn feature
426 browseActiveFeature = feature;
429 function loadHistory() {
431 this.link.innerHTML = I18n.t('browse.start_rjs.wait');
433 $.ajax("/api/" + OSM.API_VERSION + "/" + this.type + "/" + this.feature.osm_id + "/history", {
434 complete: OpenLayers.Function.bind(displayHistory, this)
440 function displayHistory(request) {
441 if (browseActiveFeature.osm_id != this.feature.osm_id || $("#browse_content").firstChild == browseObjectList) {
445 this.link.parentNode.removeChild(this.link);
447 var doc = request.responseXML;
449 var table = document.createElement("table");
450 table.width = "100%";
451 table.className = "browse_heading";
452 $("#browse_content").append(table);
454 var tr = document.createElement("tr");
455 table.appendChild(tr);
457 var heading = document.createElement("td");
458 heading.appendChild(document.createTextNode(I18n.t("browse.start_rjs.history_for_feature", { feature: featureNameHistory(this.feature) })));
459 tr.appendChild(heading);
461 var td = document.createElement("td");
465 var link = document.createElement("a");
466 link.href = "/browse/" + this.type + "/" + this.feature.osm_id + "/history";
467 link.appendChild(document.createTextNode(I18n.t('browse.start_rjs.details')));
468 td.appendChild(link);
470 var div = document.createElement("div");
471 div.className = "browse_details";
473 var nodes = doc.getElementsByTagName(this.type);
474 var history = document.createElement("ul");
475 for (var i = nodes.length - 1; i >= 0; i--) {
476 var user = nodes[i].getAttribute("user") || I18n.t('browse.start_rjs.private_user');
477 var timestamp = nodes[i].getAttribute("timestamp");
478 var item = document.createElement("li");
479 item.appendChild(document.createTextNode(I18n.t("browse.start_rjs.edited_by_user_at_timestamp", { user: user, timestamp: timestamp })));
480 history.appendChild(item);
482 div.appendChild(history);
484 $("#browse_content").append(div);
487 function featureType(feature) {
488 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
495 function featureTypeName(feature) {
496 if (featureType(feature) == "node") {
497 return I18n.t('browse.start_rjs.object_list.type.node');
498 } else if (featureType(feature) == "way") {
499 return I18n.t('browse.start_rjs.object_list.type.way');
503 function featureName(feature) {
504 var lang = $('html').attr('lang');
505 if (feature.attributes['name:' + lang]) {
506 return feature.attributes['name:' + lang];
507 } else if (feature.attributes.name) {
508 return feature.attributes.name;
510 return feature.osm_id;
514 function featureNameSelect(feature) {
515 var lang = $('html').attr('lang');
516 if (feature.attributes['name:' + lang]) {
517 return feature.attributes['name:' + lang];
518 } else if (feature.attributes.name) {
519 return feature.attributes.name;
520 } else if (featureType(feature) == "node") {
521 return I18n.t("browse.start_rjs.object_list.selected.type.node", { id: feature.osm_id });
522 } else if (featureType(feature) == "way") {
523 return I18n.t("browse.start_rjs.object_list.selected.type.way", { id: feature.osm_id });
527 function featureNameHistory(feature) {
528 var lang = $('html').attr('lang');
529 if (feature.attributes['name:' + lang]) {
530 return feature.attributes['name:' + lang];
531 } else if (feature.attributes.name) {
532 return feature.attributes.name;
533 } else if (featureType(feature) == "node") {
534 return I18n.t("browse.start_rjs.object_list.history.type.node", { id: feature.osm_id });
535 } else if (featureType(feature) == "way") {
536 return I18n.t("browse.start_rjs.object_list.history.type.way", { id: feature.osm_id });
540 function setStatus(status) {
541 $("#browse_status").html(status);
542 $("#browse_status").show();
545 function clearStatus() {
546 $("#browse_status").html("");
547 $("#browse_status").hide();