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_content").html(sidebarHtml);
62 if (browseObjectList) loadObjectList();
64 map.on("moveend", updateData);
67 $("#browse_filter_toggle").click(toggleFilter);
69 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
70 $("#browse_hide_areas_box").click(toggleAreas);
72 $("#sidebar").one("closed", function () {
73 map.removeLayer(dataLayer);
74 map.removeLayer(locationFilter);
75 map.off("moveend", updateData);
76 locationFilter.off("change", getData);
80 function updateData() {
81 if (!locationFilter.isEnabled()) {
82 if (map.getZoom() >= 15) {
83 var bounds = map.getBounds();
84 if (!browseBounds || !browseBounds.contains(bounds)) {
85 browseBounds = bounds;
89 setStatus(I18n.t('browse.start_rjs.zoom_or_select'));
94 function toggleFilter() {
95 if (locationFilter.isEnabled()) {
96 $("#browse_filter_toggle").html(I18n.t('browse.start_rjs.manually_select'));
97 locationFilter.disable();
99 $("#browse_filter_toggle").html(I18n.t('browse.start_rjs.view_data'));
100 locationFilter.setBounds(map.getBounds().pad(-0.2));
101 locationFilter.enable();
107 function toggleAreas(e) {
111 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
114 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.show_areas'));
121 function displayFeatureWarning(count, limit, callback) {
124 var div = document.createElement("div");
126 var p = document.createElement("p");
127 p.appendChild(document.createTextNode(I18n.t("browse.start_rjs.loaded_an_area_with_num_features", { num_features: count, max_features: limit })));
130 var input = document.createElement("input");
131 input.type = "submit";
132 input.value = I18n.t('browse.start_rjs.load_data');
133 input.onclick = callback;
134 div.appendChild(input);
136 $("#browse_content").html("");
137 $("#browse_content").append(div);
143 var bounds = locationFilter.isEnabled() ? locationFilter.getBounds() : map.getBounds();
144 var size = bounds.getSize();
146 if (size > OSM.MAX_REQUEST_AREA) {
147 setStatus(I18n.t("browse.start_rjs.unable_to_load_size", { max_bbox_size: OSM.MAX_REQUEST_AREA, bbox_size: size }));
151 setStatus(I18n.t('browse.start_rjs.loading'));
153 var url = "/api/" + OSM.API_VERSION + "/map?bbox=" + bounds.toBBoxString();
156 * Modern browsers are quite happy showing far more than 100 features in
157 * the data browser, so increase the limit to 2000 by default, but keep
158 * it restricted to 500 for IE8 and 100 for older IEs.
160 var maxFeatures = 2000;
163 if (navigator.appVersion < 8) {
165 } else if (navigator.appVersion < 9) {
170 if (dataLoader) dataLoader.abort();
172 dataLoader = $.ajax({
174 success: function (xml) {
177 $("#browse_content").empty();
178 dataLayer.clearLayers();
179 selectedLayer = null;
181 var features = dataLayer.buildFeatures(xml);
183 function addFeatures() {
184 dataLayer.addData(features);
188 dataLayer.eachLayer(function (layer) {
189 var feature = layer.feature;
190 layersById[feature.id] = layer;
192 typeName: featureTypeName(feature),
193 url: "/browse/" + feature.type + "/" + feature.id,
194 name: featureName(feature)
198 browseObjectList = $(JST["templates/browse/feature_list"]({
206 if (features.length < maxFeatures) {
209 displayFeatureWarning(features.length, maxFeatures, addFeatures);
217 function viewFeatureLink() {
218 var layer = layersById[$(this).data("feature-id")];
222 if (locationFilter.isEnabled()) {
223 map.panTo(layer.getBounds().getCenter());
229 function loadObjectList() {
230 $("#browse_content").html(browseObjectList);
231 $("#browse_content").find("a[data-feature-id]").click(viewFeatureLink);
236 function onSelect(layer) {
237 // Unselect previously selected feature
239 selectedLayer.setStyle(selectedLayer.originalStyle);
242 // Redraw in selected style
243 layer.originalStyle = layer.options;
244 layer.setStyle({color: '#0000ff', weight: 8});
246 // If the current object is the list, don't innerHTML="", since that could clear it.
247 if ($("#browse_content").firstChild == browseObjectList) {
248 $("#browse_content").removeChild(browseObjectList);
250 $("#browse_content").empty();
253 var feature = layer.feature;
255 $("#browse_content").html(JST["templates/browse/feature"]({
256 name: featureNameSelect(feature),
257 url: "/browse/" + feature.type + "/" + feature.id,
258 attributes: feature.tags
261 $("#browse_content").find("a.browse_show_list").click(loadObjectList);
262 $("#browse_content").find("a.browse_show_history").click(loadHistory);
264 // Stash the currently drawn feature
265 selectedLayer = layer;
268 function loadHistory() {
269 $(this).attr("href", "").text(I18n.t('browse.start_rjs.wait'));
271 var feature = selectedLayer.feature;
274 url: "/api/" + OSM.API_VERSION + "/" + feature.type + "/" + feature.id + "/history",
275 success: function (xml) {
276 if (selectedLayer.feature != feature || $("#browse_content").firstChild == browseObjectList) {
283 var nodes = xml.getElementsByTagName(feature.type);
284 for (var i = nodes.length - 1; i >= 0; i--) {
286 user: nodes[i].getAttribute("user") || I18n.t('browse.start_rjs.private_user'),
287 timestamp: nodes[i].getAttribute("timestamp")
291 $("#browse_content").append(JST["templates/browse/feature_history"]({
292 name: featureNameHistory(feature),
293 url: "/browse/" + feature.type + "/" + feature.id,
302 function featureTypeName(feature) {
303 return I18n.t('browse.start_rjs.object_list.type.' + feature.type);
306 function featureName(feature) {
307 return feature.tags['name:' + $('html').attr('lang')] ||
312 function featureNameSelect(feature) {
313 return feature.tags['name:' + $('html').attr('lang')] ||
315 I18n.t("browse.start_rjs.object_list.selected.type." + feature.type, { id: feature.id });
318 function featureNameHistory(feature) {
319 return feature.tags['name:' + $('html').attr('lang')] ||
321 I18n.t("browse.start_rjs.object_list.history.type." + feature.type, { id: feature.id });
324 function setStatus(status) {
325 $("#browse_status").html(status);
326 $("#browse_status").show();
329 function clearStatus() {
330 $("#browse_status").html("");
331 $("#browse_status").hide();