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);
140 var bounds = locationFilter.isEnabled() ? locationFilter.getBounds() : map.getBounds();
141 var size = bounds.getSize();
143 if (size > OSM.MAX_REQUEST_AREA) {
144 setStatus(I18n.t("browse.start_rjs.unable_to_load_size", { max_bbox_size: OSM.MAX_REQUEST_AREA, bbox_size: size }));
148 setStatus(I18n.t('browse.start_rjs.loading'));
150 var url = "/api/" + OSM.API_VERSION + "/map?bbox=" + bounds.toBBoxString();
153 * Modern browsers are quite happy showing far more than 100 features in
154 * the data browser, so increase the limit to 2000 by default, but keep
155 * it restricted to 500 for IE8 and 100 for older IEs.
157 var maxFeatures = 2000;
160 if (navigator.appVersion < 8) {
162 } else if (navigator.appVersion < 9) {
169 success: function (xml) {
172 $("#browse_content").empty();
173 dataLayer.clearLayers();
174 selectedLayer = null;
176 var features = dataLayer.buildFeatures(xml);
178 function addFeatures() {
179 dataLayer.addData(features);
183 dataLayer.eachLayer(function (layer) {
184 var feature = layer.feature;
185 layersById[feature.id] = layer;
187 typeName: featureTypeName(feature),
188 url: "/browse/" + feature.type + "/" + feature.id,
189 name: featureName(feature)
193 browseObjectList = $(JST["templates/browse/feature_list"]({
201 if (features.length < maxFeatures) {
204 displayFeatureWarning(features.length, maxFeatures, addFeatures);
210 function viewFeatureLink() {
211 var layer = layersById[$(this).data("feature-id")];
215 if (locationFilter.isEnabled()) {
216 map.panTo(layer.getBounds().getCenter());
222 function loadObjectList() {
223 $("#browse_content").html(browseObjectList);
224 $("#browse_content").find("a[data-feature-id]").click(viewFeatureLink);
229 function onSelect(layer) {
230 // Unselect previously selected feature
232 selectedLayer.setStyle(selectedLayer.originalStyle);
235 // Redraw in selected style
236 layer.originalStyle = layer.options;
237 layer.setStyle({color: '#0000ff', weight: 8});
239 // If the current object is the list, don't innerHTML="", since that could clear it.
240 if ($("#browse_content").firstChild == browseObjectList) {
241 $("#browse_content").removeChild(browseObjectList);
243 $("#browse_content").empty();
246 var feature = layer.feature;
248 $("#browse_content").html(JST["templates/browse/feature"]({
249 name: featureNameSelect(feature),
250 url: "/browse/" + feature.type + "/" + feature.id,
251 attributes: feature.tags
254 $("#browse_content").find("a.browse_show_list").click(loadObjectList);
255 $("#browse_content").find("a.browse_show_history").click(loadHistory);
257 // Stash the currently drawn feature
258 selectedLayer = layer;
261 function loadHistory() {
262 $(this).attr("href", "").text(I18n.t('browse.start_rjs.wait'));
264 var feature = selectedLayer.feature;
267 url: "/api/" + OSM.API_VERSION + "/" + feature.type + "/" + feature.id + "/history",
268 success: function (xml) {
269 if (selectedLayer.feature != feature || $("#browse_content").firstChild == browseObjectList) {
276 var nodes = xml.getElementsByTagName(feature.type);
277 for (var i = nodes.length - 1; i >= 0; i--) {
279 user: nodes[i].getAttribute("user") || I18n.t('browse.start_rjs.private_user'),
280 timestamp: nodes[i].getAttribute("timestamp")
284 $("#browse_content").append(JST["templates/browse/feature_history"]({
285 name: featureNameHistory(feature),
286 url: "/browse/" + feature.type + "/" + feature.id,
295 function featureTypeName(feature) {
296 return I18n.t('browse.start_rjs.object_list.type.' + feature.type);
299 function featureName(feature) {
300 return feature.tags['name:' + $('html').attr('lang')] ||
305 function featureNameSelect(feature) {
306 return feature.tags['name:' + $('html').attr('lang')] ||
308 I18n.t("browse.start_rjs.object_list.selected.type." + feature.type, { id: feature.id });
311 function featureNameHistory(feature) {
312 return feature.tags['name:' + $('html').attr('lang')] ||
314 I18n.t("browse.start_rjs.object_list.history.type." + feature.type, { id: feature.id });
317 function setStatus(status) {
318 $("#browse_status").html(status);
319 $("#browse_status").show();
322 function clearStatus() {
323 $("#browse_status").html("");
324 $("#browse_status").hide();