1 //= require templates/browse/feature
2 //= require templates/browse/feature_list
3 //= require templates/browse/feature_history
5 function initializeBrowse(map) {
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 function startBrowse(sidebarHtml) {
53 locationFilter = new L.LocationFilter({
58 locationFilter.on("change", getData);
60 $("#sidebar_title").html(I18n.t('browse.start_rjs.data_frame_title'));
61 $("#sidebar_content").html(sidebarHtml);
65 map.on("moveend", updateData);
68 $("#browse_filter_toggle").click(toggleFilter);
70 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
71 $("#browse_hide_areas_box").click(toggleAreas);
73 $("#sidebar").one("closed", function () {
74 map.removeLayer(dataLayer);
75 map.removeLayer(locationFilter);
76 map.off("moveend", updateData);
77 locationFilter.off("change", getData);
81 function updateData() {
82 if (!locationFilter.isEnabled()) {
83 if (map.getZoom() >= 15) {
84 var bounds = map.getBounds();
85 if (!browseBounds || !browseBounds.contains(bounds)) {
86 browseBounds = bounds;
90 setStatus(I18n.t('browse.start_rjs.zoom_or_select'));
95 function toggleFilter() {
96 if (locationFilter.isEnabled()) {
97 $("#browse_filter_toggle").html(I18n.t('browse.start_rjs.manually_select'));
98 locationFilter.disable();
100 $("#browse_filter_toggle").html(I18n.t('browse.start_rjs.view_data'));
101 locationFilter.setBounds(map.getBounds().pad(-0.2));
102 locationFilter.enable();
108 function toggleAreas() {
110 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
113 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.show_areas'));
120 function displayFeatureWarning(count, limit, callback) {
123 var div = document.createElement("div");
125 var p = document.createElement("p");
126 p.appendChild(document.createTextNode(I18n.t("browse.start_rjs.loaded_an_area_with_num_features", { num_features: count, max_features: limit })));
129 var input = document.createElement("input");
130 input.type = "submit";
131 input.value = I18n.t('browse.start_rjs.load_data');
132 input.onclick = callback;
133 div.appendChild(input);
135 $("#browse_content").html("");
136 $("#browse_content").append(div);
142 var bounds = locationFilter.isEnabled() ? locationFilter.getBounds() : map.getBounds();
143 var size = bounds.getSize();
145 if (size > OSM.MAX_REQUEST_AREA) {
146 setStatus(I18n.t("browse.start_rjs.unable_to_load_size", { max_bbox_size: OSM.MAX_REQUEST_AREA, bbox_size: size }));
150 setStatus(I18n.t('browse.start_rjs.loading'));
152 var url = "/api/" + OSM.API_VERSION + "/map?bbox=" + bounds.toBBoxString();
155 * Modern browsers are quite happy showing far more than 100 features in
156 * the data browser, so increase the limit to 2000 by default, but keep
157 * it restricted to 500 for IE8 and 100 for older IEs.
159 var maxFeatures = 2000;
162 if (navigator.appVersion < 8) {
164 } else if (navigator.appVersion < 9) {
169 if (dataLoader) dataLoader.abort();
171 dataLoader = $.ajax({
173 success: function (xml) {
176 $("#browse_content").empty();
177 dataLayer.clearLayers();
178 selectedLayer = null;
180 var features = dataLayer.buildFeatures(xml);
182 function addFeatures() {
183 dataLayer.addData(features);
187 dataLayer.eachLayer(function (layer) {
188 var feature = layer.feature;
189 layersById[feature.id] = layer;
191 typeName: featureTypeName(feature),
192 url: "/browse/" + feature.type + "/" + feature.id,
193 name: featureName(feature)
197 browseObjectList = $(JST["templates/browse/feature_list"]({
205 if (features.length < maxFeatures) {
208 displayFeatureWarning(features.length, maxFeatures, addFeatures);
216 function viewFeatureLink() {
217 var layer = layersById[$(this).data("feature-id")];
221 if (locationFilter.isEnabled()) {
222 map.panTo(layer.getBounds().getCenter());
228 function loadObjectList() {
229 $("#browse_content").html(browseObjectList);
230 $("#browse_content").find("a[data-feature-id]").click(viewFeatureLink);
235 function onSelect(layer) {
236 // Unselect previously selected feature
238 selectedLayer.setStyle(selectedLayer.originalStyle);
241 // Redraw in selected style
242 layer.originalStyle = layer.options;
243 layer.setStyle({color: '#0000ff', weight: 8});
245 // If the current object is the list, don't innerHTML="", since that could clear it.
246 if ($("#browse_content").firstChild == browseObjectList) {
247 $("#browse_content").removeChild(browseObjectList);
249 $("#browse_content").empty();
252 var feature = layer.feature;
254 $("#browse_content").html(JST["templates/browse/feature"]({
255 name: featureNameSelect(feature),
256 url: "/browse/" + feature.type + "/" + feature.id,
257 attributes: feature.tags
260 $("#browse_content").find("a.browse_show_list").click(loadObjectList);
261 $("#browse_content").find("a.browse_show_history").click(loadHistory);
263 // Stash the currently drawn feature
264 selectedLayer = layer;
267 function loadHistory() {
268 $(this).attr("href", "").text(I18n.t('browse.start_rjs.wait'));
270 var feature = selectedLayer.feature;
273 url: "/api/" + OSM.API_VERSION + "/" + feature.type + "/" + feature.id + "/history",
274 success: function (xml) {
275 if (selectedLayer.feature != feature || $("#browse_content").firstChild == browseObjectList) {
282 var nodes = xml.getElementsByTagName(feature.type);
283 for (var i = nodes.length - 1; i >= 0; i--) {
285 user: nodes[i].getAttribute("user") || I18n.t('browse.start_rjs.private_user'),
286 timestamp: nodes[i].getAttribute("timestamp")
290 $("#browse_content").append(JST["templates/browse/feature_history"]({
291 name: featureNameHistory(feature),
292 url: "/browse/" + feature.type + "/" + feature.id,
301 function featureTypeName(feature) {
302 return I18n.t('browse.start_rjs.object_list.type.' + feature.type);
305 function featureName(feature) {
306 return feature.tags['name:' + $('html').attr('lang')] ||
311 function featureNameSelect(feature) {
312 return feature.tags['name:' + $('html').attr('lang')] ||
314 I18n.t("browse.start_rjs.object_list.selected.type." + feature.type, { id: feature.id });
317 function featureNameHistory(feature) {
318 return feature.tags['name:' + $('html').attr('lang')] ||
320 I18n.t("browse.start_rjs.object_list.history.type." + feature.type, { id: feature.id });
323 function setStatus(status) {
324 $("#browse_status").html(status);
325 $("#browse_status").show();
328 function clearStatus() {
329 $("#browse_status").html("");
330 $("#browse_status").hide();