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_content").html(sidebarHtml);
68 if (browseObjectList) loadObjectList();
70 map.on("moveend", updateData);
73 $("#browse_filter_toggle").click(toggleFilter);
75 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
76 $("#browse_hide_areas_box").click(toggleAreas);
78 $("#sidebar").one("closed", function () {
79 map.removeLayer(dataLayer);
80 map.removeLayer(locationFilter);
81 map.off("moveend", updateData);
82 locationFilter.off("change", getData);
86 function updateData() {
87 if (!locationFilter.isEnabled()) {
88 if (map.getZoom() >= 15) {
89 var bounds = map.getBounds();
90 if (!browseBounds || !browseBounds.contains(bounds)) {
91 browseBounds = bounds;
95 setStatus(I18n.t('browse.start_rjs.zoom_or_select'));
100 function toggleFilter() {
101 if (locationFilter.isEnabled()) {
102 $("#browse_filter_toggle").html(I18n.t('browse.start_rjs.manually_select'));
103 locationFilter.disable();
105 $("#browse_filter_toggle").html(I18n.t('browse.start_rjs.view_data'));
106 locationFilter.setBounds(map.getBounds().pad(-0.2));
107 locationFilter.enable();
113 function toggleAreas(e) {
117 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
120 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.show_areas'));
127 function displayFeatureWarning(count, limit, callback) {
130 var div = document.createElement("div");
132 var p = document.createElement("p");
133 p.appendChild(document.createTextNode(I18n.t("browse.start_rjs.loaded_an_area_with_num_features", { num_features: count, max_features: limit })));
136 var input = document.createElement("input");
137 input.type = "submit";
138 input.value = I18n.t('browse.start_rjs.load_data');
139 input.onclick = callback;
140 div.appendChild(input);
142 $("#browse_content").html("");
143 $("#browse_content").append(div);
149 var bounds = locationFilter.isEnabled() ? locationFilter.getBounds() : map.getBounds();
150 var size = bounds.getSize();
152 if (size > OSM.MAX_REQUEST_AREA) {
153 setStatus(I18n.t("browse.start_rjs.unable_to_load_size", { max_bbox_size: OSM.MAX_REQUEST_AREA, bbox_size: size }));
157 setStatus(I18n.t('browse.start_rjs.loading'));
159 var url = "/api/" + OSM.API_VERSION + "/map?bbox=" + bounds.toBBoxString();
162 * Modern browsers are quite happy showing far more than 100 features in
163 * the data browser, so increase the limit to 2000 by default, but keep
164 * it restricted to 500 for IE8 and 100 for older IEs.
166 var maxFeatures = 2000;
169 if (navigator.appVersion < 8) {
171 } else if (navigator.appVersion < 9) {
176 if (dataLoader) dataLoader.abort();
178 dataLoader = $.ajax({
180 success: function (xml) {
183 $("#browse_content").empty();
184 dataLayer.clearLayers();
185 selectedLayer = null;
187 var features = dataLayer.buildFeatures(xml);
189 function addFeatures() {
190 dataLayer.addData(features);
194 dataLayer.eachLayer(function (layer) {
195 var feature = layer.feature;
196 layersById[feature.id] = layer;
198 typeName: featureTypeName(feature),
199 url: "/browse/" + feature.type + "/" + feature.id,
200 name: featureName(feature)
204 browseObjectList = $(JST["templates/browse/feature_list"]({
212 if (features.length < maxFeatures) {
215 displayFeatureWarning(features.length, maxFeatures, addFeatures);
223 function viewFeatureLink() {
224 var layer = layersById[$(this).data("feature-id")];
228 if (locationFilter.isEnabled()) {
229 map.panTo(layer.getBounds().getCenter());
235 function loadObjectList() {
236 $("#browse_content").html(browseObjectList);
237 $("#browse_content").find("a[data-feature-id]").click(viewFeatureLink);
242 function onSelect(layer) {
243 // Unselect previously selected feature
245 selectedLayer.setStyle(selectedLayer.originalStyle);
248 // Redraw in selected style
249 layer.originalStyle = layer.options;
250 layer.setStyle({color: '#0000ff', weight: 8});
252 // If the current object is the list, don't innerHTML="", since that could clear it.
253 if ($("#browse_content").firstChild == browseObjectList) {
254 $("#browse_content").removeChild(browseObjectList);
256 $("#browse_content").empty();
259 var feature = layer.feature;
261 $("#browse_content").html(JST["templates/browse/feature"]({
262 name: featureNameSelect(feature),
263 url: "/browse/" + feature.type + "/" + feature.id,
264 attributes: feature.tags
267 $("#browse_content").find("a.browse_show_list").click(loadObjectList);
268 $("#browse_content").find("a.browse_show_history").click(loadHistory);
270 // Stash the currently drawn feature
271 selectedLayer = layer;
274 function loadHistory() {
275 $(this).attr("href", "").text(I18n.t('browse.start_rjs.wait'));
277 var feature = selectedLayer.feature;
280 url: "/api/" + OSM.API_VERSION + "/" + feature.type + "/" + feature.id + "/history",
281 success: function (xml) {
282 if (selectedLayer.feature != feature || $("#browse_content").firstChild == browseObjectList) {
289 var nodes = xml.getElementsByTagName(feature.type);
290 for (var i = nodes.length - 1; i >= 0; i--) {
292 user: nodes[i].getAttribute("user") || I18n.t('browse.start_rjs.private_user'),
293 timestamp: nodes[i].getAttribute("timestamp")
297 $("#browse_content").append(JST["templates/browse/feature_history"]({
298 name: featureNameHistory(feature),
299 url: "/browse/" + feature.type + "/" + feature.id,
308 function featureTypeName(feature) {
309 return I18n.t('browse.start_rjs.object_list.type.' + feature.type);
312 function featureName(feature) {
313 return feature.tags['name:' + $('html').attr('lang')] ||
318 function featureNameSelect(feature) {
319 return feature.tags['name:' + $('html').attr('lang')] ||
321 I18n.t("browse.start_rjs.object_list.selected.type." + feature.type, { id: feature.id });
324 function featureNameHistory(feature) {
325 return feature.tags['name:' + $('html').attr('lang')] ||
327 I18n.t("browse.start_rjs.object_list.history.type." + feature.type, { id: feature.id });
330 function setStatus(status) {
331 $("#browse_status").html(status);
332 $("#browse_status").show();
335 function clearStatus() {
336 $("#browse_status").html("");
337 $("#browse_status").hide();