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() {
118 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
121 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.show_areas'));
128 function displayFeatureWarning(count, limit, callback) {
131 var div = document.createElement("div");
133 var p = document.createElement("p");
134 p.appendChild(document.createTextNode(I18n.t("browse.start_rjs.loaded_an_area_with_num_features", { num_features: count, max_features: limit })));
137 var input = document.createElement("input");
138 input.type = "submit";
139 input.value = I18n.t('browse.start_rjs.load_data');
140 input.onclick = callback;
141 div.appendChild(input);
143 $("#browse_content").html("");
144 $("#browse_content").append(div);
150 var bounds = locationFilter.isEnabled() ? locationFilter.getBounds() : map.getBounds();
151 var size = bounds.getSize();
153 if (size > OSM.MAX_REQUEST_AREA) {
154 setStatus(I18n.t("browse.start_rjs.unable_to_load_size", { max_bbox_size: OSM.MAX_REQUEST_AREA, bbox_size: size }));
158 setStatus(I18n.t('browse.start_rjs.loading'));
160 var url = "/api/" + OSM.API_VERSION + "/map?bbox=" + bounds.toBBoxString();
163 * Modern browsers are quite happy showing far more than 100 features in
164 * the data browser, so increase the limit to 2000 by default, but keep
165 * it restricted to 500 for IE8 and 100 for older IEs.
167 var maxFeatures = 2000;
170 if (navigator.appVersion < 8) {
172 } else if (navigator.appVersion < 9) {
177 if (dataLoader) dataLoader.abort();
179 dataLoader = $.ajax({
181 success: function (xml) {
184 $("#browse_content").empty();
185 dataLayer.clearLayers();
186 selectedLayer = null;
188 var features = dataLayer.buildFeatures(xml);
190 function addFeatures() {
191 dataLayer.addData(features);
195 dataLayer.eachLayer(function (layer) {
196 var feature = layer.feature;
197 layersById[feature.id] = layer;
199 typeName: featureTypeName(feature),
200 url: "/browse/" + feature.type + "/" + feature.id,
201 name: featureName(feature)
205 browseObjectList = $(JST["templates/browse/feature_list"]({
213 if (features.length < maxFeatures) {
216 displayFeatureWarning(features.length, maxFeatures, addFeatures);
224 function viewFeatureLink() {
225 var layer = layersById[$(this).data("feature-id")];
229 if (locationFilter.isEnabled()) {
230 map.panTo(layer.getBounds().getCenter());
236 function loadObjectList() {
237 $("#browse_content").html(browseObjectList);
238 $("#browse_content").find("a[data-feature-id]").click(viewFeatureLink);
243 function onSelect(layer) {
244 // Unselect previously selected feature
246 selectedLayer.setStyle(selectedLayer.originalStyle);
249 // Redraw in selected style
250 layer.originalStyle = layer.options;
251 layer.setStyle({color: '#0000ff', weight: 8});
253 // If the current object is the list, don't innerHTML="", since that could clear it.
254 if ($("#browse_content").firstChild == browseObjectList) {
255 $("#browse_content").removeChild(browseObjectList);
257 $("#browse_content").empty();
260 var feature = layer.feature;
262 $("#browse_content").html(JST["templates/browse/feature"]({
263 name: featureNameSelect(feature),
264 url: "/browse/" + feature.type + "/" + feature.id,
265 attributes: feature.tags
268 $("#browse_content").find("a.browse_show_list").click(loadObjectList);
269 $("#browse_content").find("a.browse_show_history").click(loadHistory);
271 // Stash the currently drawn feature
272 selectedLayer = layer;
275 function loadHistory() {
276 $(this).attr("href", "").text(I18n.t('browse.start_rjs.wait'));
278 var feature = selectedLayer.feature;
281 url: "/api/" + OSM.API_VERSION + "/" + feature.type + "/" + feature.id + "/history",
282 success: function (xml) {
283 if (selectedLayer.feature != feature || $("#browse_content").firstChild == browseObjectList) {
290 var nodes = xml.getElementsByTagName(feature.type);
291 for (var i = nodes.length - 1; i >= 0; i--) {
293 user: nodes[i].getAttribute("user") || I18n.t('browse.start_rjs.private_user'),
294 timestamp: nodes[i].getAttribute("timestamp")
298 $("#browse_content").append(JST["templates/browse/feature_history"]({
299 name: featureNameHistory(feature),
300 url: "/browse/" + feature.type + "/" + feature.id,
309 function featureTypeName(feature) {
310 return I18n.t('browse.start_rjs.object_list.type.' + feature.type);
313 function featureName(feature) {
314 return feature.tags['name:' + $('html').attr('lang')] ||
319 function featureNameSelect(feature) {
320 return feature.tags['name:' + $('html').attr('lang')] ||
322 I18n.t("browse.start_rjs.object_list.selected.type." + feature.type, { id: feature.id });
325 function featureNameHistory(feature) {
326 return feature.tags['name:' + $('html').attr('lang')] ||
328 I18n.t("browse.start_rjs.object_list.history.type." + feature.type, { id: feature.id });
331 function setStatus(status) {
332 $("#browse_status").html(status);
333 $("#browse_status").show();
336 function clearStatus() {
337 $("#browse_status").html("");
338 $("#browse_status").hide();