1 //= require templates/browse/feature
2 //= require templates/browse/feature_list
3 //= require templates/browse/feature_history
5 function initializeBrowse(map, params) {
10 var areasHidden = false;
13 var dataLayer = map.dataLayer;
30 dataLayer.isWayArea = function () {
31 return !areasHidden && L.OSM.DataLayer.prototype.isWayArea.apply(this, arguments);
34 dataLayer.on("click", function (e) {
38 map.on('layeradd', function (e) {
39 if (e.layer === dataLayer) {
40 $.ajax({ url: "/browse/start", success: function (sidebarHtml) {
41 startBrowse(sidebarHtml);
46 map.on('layerremove', function (e) {
47 if (e.layer === dataLayer) {
52 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
53 if (params.layers.indexOf(dataLayer.options.code) >= 0) {
54 map.addLayer(dataLayer);
58 function startBrowse(sidebarHtml) {
59 locationFilter = new L.LocationFilter({
64 locationFilter.on("change", getData);
66 $("#sidebar_title").html(I18n.t('browse.start_rjs.data_frame_title'));
67 $("#sidebar_content").html(sidebarHtml);
71 if (browseObjectList) loadObjectList();
73 map.on("moveend", updateData);
76 $("#browse_filter_toggle").click(toggleFilter);
78 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
79 $("#browse_hide_areas_box").click(toggleAreas);
81 $("#sidebar").one("closed", function () {
82 map.removeLayer(dataLayer);
83 map.removeLayer(locationFilter);
84 map.off("moveend", updateData);
85 locationFilter.off("change", getData);
89 function updateData() {
90 if (!locationFilter.isEnabled()) {
91 if (map.getZoom() >= 15) {
92 var bounds = map.getBounds();
93 if (!browseBounds || !browseBounds.contains(bounds)) {
94 browseBounds = bounds;
98 setStatus(I18n.t('browse.start_rjs.zoom_or_select'));
103 function toggleFilter() {
104 if (locationFilter.isEnabled()) {
105 $("#browse_filter_toggle").html(I18n.t('browse.start_rjs.manually_select'));
106 locationFilter.disable();
108 $("#browse_filter_toggle").html(I18n.t('browse.start_rjs.view_data'));
109 locationFilter.setBounds(map.getBounds().pad(-0.2));
110 locationFilter.enable();
116 function toggleAreas(e) {
120 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
123 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.show_areas'));
130 function displayFeatureWarning(count, limit, callback) {
133 var div = document.createElement("div");
135 var p = document.createElement("p");
136 p.appendChild(document.createTextNode(I18n.t("browse.start_rjs.loaded_an_area_with_num_features", { num_features: count, max_features: limit })));
139 var input = document.createElement("input");
140 input.type = "submit";
141 input.value = I18n.t('browse.start_rjs.load_data');
142 input.onclick = callback;
143 div.appendChild(input);
145 $("#browse_content").html("");
146 $("#browse_content").append(div);
152 var bounds = locationFilter.isEnabled() ? locationFilter.getBounds() : map.getBounds();
153 var size = bounds.getSize();
155 if (size > OSM.MAX_REQUEST_AREA) {
156 setStatus(I18n.t("browse.start_rjs.unable_to_load_size", { max_bbox_size: OSM.MAX_REQUEST_AREA, bbox_size: size }));
160 setStatus(I18n.t('browse.start_rjs.loading'));
162 var url = "/api/" + OSM.API_VERSION + "/map?bbox=" + bounds.toBBoxString();
165 * Modern browsers are quite happy showing far more than 100 features in
166 * the data browser, so increase the limit to 2000 by default, but keep
167 * it restricted to 500 for IE8 and 100 for older IEs.
169 var maxFeatures = 2000;
172 if (navigator.appVersion < 8) {
174 } else if (navigator.appVersion < 9) {
179 if (dataLoader) dataLoader.abort();
181 dataLoader = $.ajax({
183 success: function (xml) {
186 $("#browse_content").empty();
187 dataLayer.clearLayers();
188 selectedLayer = null;
190 var features = dataLayer.buildFeatures(xml);
192 function addFeatures() {
193 dataLayer.addData(features);
197 dataLayer.eachLayer(function (layer) {
198 var feature = layer.feature;
199 layersById[feature.id] = layer;
201 typeName: featureTypeName(feature),
202 url: "/browse/" + feature.type + "/" + feature.id,
203 name: featureName(feature)
207 browseObjectList = $(JST["templates/browse/feature_list"]({
215 if (features.length < maxFeatures) {
218 displayFeatureWarning(features.length, maxFeatures, addFeatures);
226 function viewFeatureLink() {
227 var layer = layersById[$(this).data("feature-id")];
231 if (locationFilter.isEnabled()) {
232 map.panTo(layer.getBounds().getCenter());
238 function loadObjectList() {
239 $("#browse_content").html(browseObjectList);
240 $("#browse_content").find("a[data-feature-id]").click(viewFeatureLink);
245 function onSelect(layer) {
246 // Unselect previously selected feature
248 selectedLayer.setStyle(selectedLayer.originalStyle);
251 // Redraw in selected style
252 layer.originalStyle = layer.options;
253 layer.setStyle({color: '#0000ff', weight: 8});
255 // If the current object is the list, don't innerHTML="", since that could clear it.
256 if ($("#browse_content").firstChild == browseObjectList) {
257 $("#browse_content").removeChild(browseObjectList);
259 $("#browse_content").empty();
262 var feature = layer.feature;
264 $("#browse_content").html(JST["templates/browse/feature"]({
265 name: featureNameSelect(feature),
266 url: "/browse/" + feature.type + "/" + feature.id,
267 attributes: feature.tags
270 $("#browse_content").find("a.browse_show_list").click(loadObjectList);
271 $("#browse_content").find("a.browse_show_history").click(loadHistory);
273 // Stash the currently drawn feature
274 selectedLayer = layer;
277 function loadHistory() {
278 $(this).attr("href", "").text(I18n.t('browse.start_rjs.wait'));
280 var feature = selectedLayer.feature;
283 url: "/api/" + OSM.API_VERSION + "/" + feature.type + "/" + feature.id + "/history",
284 success: function (xml) {
285 if (selectedLayer.feature != feature || $("#browse_content").firstChild == browseObjectList) {
292 var nodes = xml.getElementsByTagName(feature.type);
293 for (var i = nodes.length - 1; i >= 0; i--) {
295 user: nodes[i].getAttribute("user") || I18n.t('browse.start_rjs.private_user'),
296 timestamp: nodes[i].getAttribute("timestamp")
300 $("#browse_content").append(JST["templates/browse/feature_history"]({
301 name: featureNameHistory(feature),
302 url: "/browse/" + feature.type + "/" + feature.id,
311 function featureTypeName(feature) {
312 return I18n.t('browse.start_rjs.object_list.type.' + feature.type);
315 function featureName(feature) {
316 return feature.tags['name:' + $('html').attr('lang')] ||
321 function featureNameSelect(feature) {
322 return feature.tags['name:' + $('html').attr('lang')] ||
324 I18n.t("browse.start_rjs.object_list.selected.type." + feature.type, { id: feature.id });
327 function featureNameHistory(feature) {
328 return feature.tags['name:' + $('html').attr('lang')] ||
330 I18n.t("browse.start_rjs.object_list.history.type." + feature.type, { id: feature.id });
333 function setStatus(status) {
334 $("#browse_status").html(status);
335 $("#browse_status").show();
338 function clearStatus() {
339 $("#browse_status").html("");
340 $("#browse_status").hide();