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 if (browseObjectList) loadObjectList();
67 map.on("moveend", updateData);
70 $("#browse_filter_toggle").click(toggleFilter);
72 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
73 $("#browse_hide_areas_box").click(toggleAreas);
75 $("#sidebar").one("closed", function () {
76 map.removeLayer(dataLayer);
77 map.removeLayer(locationFilter);
78 map.off("moveend", updateData);
79 locationFilter.off("change", getData);
83 function updateData() {
84 if (!locationFilter.isEnabled()) {
85 if (map.getZoom() >= 15) {
86 var bounds = map.getBounds();
87 if (!browseBounds || !browseBounds.contains(bounds)) {
88 browseBounds = bounds;
92 setStatus(I18n.t('browse.start_rjs.zoom_or_select'));
97 function toggleFilter() {
98 if (locationFilter.isEnabled()) {
99 $("#browse_filter_toggle").html(I18n.t('browse.start_rjs.manually_select'));
100 locationFilter.disable();
102 $("#browse_filter_toggle").html(I18n.t('browse.start_rjs.view_data'));
103 locationFilter.setBounds(map.getBounds().pad(-0.2));
104 locationFilter.enable();
110 function toggleAreas() {
112 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
115 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.show_areas'));
122 function displayFeatureWarning(count, limit, callback) {
125 var div = document.createElement("div");
127 var p = document.createElement("p");
128 p.appendChild(document.createTextNode(I18n.t("browse.start_rjs.loaded_an_area_with_num_features", { num_features: count, max_features: limit })));
131 var input = document.createElement("input");
132 input.type = "submit";
133 input.value = I18n.t('browse.start_rjs.load_data');
134 input.onclick = callback;
135 div.appendChild(input);
137 $("#browse_content").html("");
138 $("#browse_content").append(div);
144 var bounds = locationFilter.isEnabled() ? locationFilter.getBounds() : map.getBounds();
145 var size = bounds.getSize();
147 if (size > OSM.MAX_REQUEST_AREA) {
148 setStatus(I18n.t("browse.start_rjs.unable_to_load_size", { max_bbox_size: OSM.MAX_REQUEST_AREA, bbox_size: size }));
152 setStatus(I18n.t('browse.start_rjs.loading'));
154 var url = "/api/" + OSM.API_VERSION + "/map?bbox=" + bounds.toBBoxString();
157 * Modern browsers are quite happy showing far more than 100 features in
158 * the data browser, so increase the limit to 2000 by default, but keep
159 * it restricted to 500 for IE8 and 100 for older IEs.
161 var maxFeatures = 2000;
164 if (navigator.appVersion < 8) {
166 } else if (navigator.appVersion < 9) {
171 if (dataLoader) dataLoader.abort();
173 dataLoader = $.ajax({
175 success: function (xml) {
178 $("#browse_content").empty();
179 dataLayer.clearLayers();
180 selectedLayer = null;
182 var features = dataLayer.buildFeatures(xml);
184 function addFeatures() {
185 dataLayer.addData(features);
189 dataLayer.eachLayer(function (layer) {
190 var feature = layer.feature;
191 layersById[feature.id] = layer;
193 typeName: featureTypeName(feature),
194 url: "/browse/" + feature.type + "/" + feature.id,
195 name: featureName(feature)
199 browseObjectList = $(JST["templates/browse/feature_list"]({
207 if (features.length < maxFeatures) {
210 displayFeatureWarning(features.length, maxFeatures, addFeatures);
218 function viewFeatureLink() {
219 var layer = layersById[$(this).data("feature-id")];
223 if (locationFilter.isEnabled()) {
224 map.panTo(layer.getBounds().getCenter());
230 function loadObjectList() {
231 $("#browse_content").html(browseObjectList);
232 $("#browse_content").find("a[data-feature-id]").click(viewFeatureLink);
237 function onSelect(layer) {
238 // Unselect previously selected feature
240 selectedLayer.setStyle(selectedLayer.originalStyle);
243 // Redraw in selected style
244 layer.originalStyle = layer.options;
245 layer.setStyle({color: '#0000ff', weight: 8});
247 // If the current object is the list, don't innerHTML="", since that could clear it.
248 if ($("#browse_content").firstChild == browseObjectList) {
249 $("#browse_content").removeChild(browseObjectList);
251 $("#browse_content").empty();
254 var feature = layer.feature;
256 $("#browse_content").html(JST["templates/browse/feature"]({
257 name: featureNameSelect(feature),
258 url: "/browse/" + feature.type + "/" + feature.id,
259 attributes: feature.tags
262 $("#browse_content").find("a.browse_show_list").click(loadObjectList);
263 $("#browse_content").find("a.browse_show_history").click(loadHistory);
265 // Stash the currently drawn feature
266 selectedLayer = layer;
269 function loadHistory() {
270 $(this).attr("href", "").text(I18n.t('browse.start_rjs.wait'));
272 var feature = selectedLayer.feature;
275 url: "/api/" + OSM.API_VERSION + "/" + feature.type + "/" + feature.id + "/history",
276 success: function (xml) {
277 if (selectedLayer.feature != feature || $("#browse_content").firstChild == browseObjectList) {
284 var nodes = xml.getElementsByTagName(feature.type);
285 for (var i = nodes.length - 1; i >= 0; i--) {
287 user: nodes[i].getAttribute("user") || I18n.t('browse.start_rjs.private_user'),
288 timestamp: nodes[i].getAttribute("timestamp")
292 $("#browse_content").append(JST["templates/browse/feature_history"]({
293 name: featureNameHistory(feature),
294 url: "/browse/" + feature.type + "/" + feature.id,
303 function featureTypeName(feature) {
304 return I18n.t('browse.start_rjs.object_list.type.' + feature.type);
307 function featureName(feature) {
308 return feature.tags['name:' + $('html').attr('lang')] ||
313 function featureNameSelect(feature) {
314 return feature.tags['name:' + $('html').attr('lang')] ||
316 I18n.t("browse.start_rjs.object_list.selected.type." + feature.type, { id: feature.id });
319 function featureNameHistory(feature) {
320 return feature.tags['name:' + $('html').attr('lang')] ||
322 I18n.t("browse.start_rjs.object_list.history.type." + feature.type, { id: feature.id });
325 function setStatus(status) {
326 $("#browse_status").html(status);
327 $("#browse_status").show();
330 function clearStatus() {
331 $("#browse_status").html("");
332 $("#browse_status").hide();