1 //= require templates/browse/feature
2 //= require templates/browse/feature_list
3 //= require templates/browse/feature_history
5 $(document).ready(function () {
6 $("#show_data").click(function (e) {
7 $.ajax({ url: $(this).attr('href'), success: function (sidebarHtml) {
8 startBrowse(sidebarHtml);
13 function startBrowse(sidebarHtml) {
18 var areasHidden = false;
20 var dataLayer = new L.OSM.DataLayer(null, {
39 dataLayer.isWayArea = function () {
40 return !areasHidden && L.OSM.DataLayer.prototype.isWayArea.apply(this, arguments);
43 var locationFilter = new L.LocationFilter({
48 locationFilter.on("change", getData);
50 $("#sidebar_title").html(I18n.t('browse.start_rjs.data_frame_title'));
51 $("#sidebar_content").html(sidebarHtml);
55 map.on("moveend", updateData);
58 $("#browse_filter_toggle").toggle(enableFilter, disableFilter);
60 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
61 $("#browse_hide_areas_box").toggle(hideAreas, showAreas);
63 function updateData() {
64 if (!locationFilter.isEnabled()) {
65 if (map.getZoom() >= 15) {
66 var bounds = map.getBounds();
67 if (!browseBounds || !browseBounds.contains(bounds)) {
68 browseBounds = bounds;
72 setStatus(I18n.t('browse.start_rjs.zoom_or_select'));
77 $("#sidebar").one("closed", function () {
78 map.removeLayer(dataLayer);
79 map.removeLayer(locationFilter);
80 map.off("moveend", updateData);
81 locationFilter.off("change", getData);
84 function enableFilter() {
85 $("#browse_filter_toggle").html(I18n.t('browse.start_rjs.view_data'));
86 locationFilter.enable();
90 function disableFilter() {
91 $("#browse_filter_toggle").html(I18n.t('browse.start_rjs.manually_select'));
92 locationFilter.disable();
96 function hideAreas() {
97 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.show_areas'));
102 function showAreas() {
103 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
108 function displayFeatureWarning(count, limit, callback) {
111 var div = document.createElement("div");
113 var p = document.createElement("p");
114 p.appendChild(document.createTextNode(I18n.t("browse.start_rjs.loaded_an_area_with_num_features", { num_features: count, max_features: limit })));
117 var input = document.createElement("input");
118 input.type = "submit";
119 input.value = I18n.t('browse.start_rjs.load_data');
120 input.onclick = callback;
121 div.appendChild(input);
123 $("#browse_content").html("");
124 $("#browse_content").append(div);
128 var bounds = locationFilter.isEnabled() ? locationFilter.getBounds() : map.getBounds();
129 var size = bounds.getSize();
131 if (size > OSM.MAX_REQUEST_AREA) {
132 setStatus(I18n.t("browse.start_rjs.unable_to_load_size", { max_bbox_size: OSM.MAX_REQUEST_AREA, bbox_size: size }));
136 setStatus(I18n.t('browse.start_rjs.loading'));
138 var url = "/api/" + OSM.API_VERSION + "/map?bbox=" + bounds.toBBOX();
141 * Modern browsers are quite happy showing far more than 100 features in
142 * the data browser, so increase the limit to 2000 by default, but keep
143 * it restricted to 500 for IE8 and 100 for older IEs.
145 var maxFeatures = 2000;
148 if (navigator.appVersion < 8) {
150 } else if (navigator.appVersion < 9) {
157 success: function (xml) {
160 $("#browse_content").empty();
161 dataLayer.clearLayers();
162 selectedLayer = null;
164 var features = dataLayer.buildFeatures(xml);
166 function addFeatures() {
167 dataLayer.addData(features);
171 dataLayer.eachLayer(function (layer) {
172 var feature = layer.feature;
173 layersById[feature.id] = layer;
175 typeName: featureTypeName(feature),
176 url: "/browse/" + feature.type + "/" + feature.id,
177 name: featureName(feature)
181 browseObjectList = $(JST["templates/browse/feature_list"]({
189 if (features.length < maxFeatures) {
192 displayFeatureWarning(features.length, maxFeatures, addFeatures);
198 function viewFeatureLink() {
199 var layer = layersById[$(this).data("feature-id")];
203 if (locationFilter.isEnabled()) {
204 map.panTo(layer.getBounds().getCenter());
210 function loadObjectList() {
211 $("#browse_content").html(browseObjectList);
212 $("#browse_content").find("a[data-feature-id]").click(viewFeatureLink);
217 function onSelect(layer) {
218 // Unselect previously selected feature
220 selectedLayer.setStyle(selectedLayer.originalStyle);
223 // Redraw in selected style
224 layer.originalStyle = layer.options;
225 layer.setStyle({color: '#0000ff', weight: 8});
227 // If the current object is the list, don't innerHTML="", since that could clear it.
228 if ($("#browse_content").firstChild == browseObjectList) {
229 $("#browse_content").removeChild(browseObjectList);
231 $("#browse_content").empty();
234 var feature = layer.feature;
236 $("#browse_content").html(JST["templates/browse/feature"]({
237 name: featureNameSelect(feature),
238 url: "/browse/" + feature.type + "/" + feature.id,
239 attributes: feature.tags
242 $("#browse_content").find("a.browse_show_list").click(loadObjectList);
243 $("#browse_content").find("a.browse_show_history").click(loadHistory);
245 // Stash the currently drawn feature
246 selectedLayer = layer;
249 dataLayer.on("click", function (e) {
253 function loadHistory() {
254 $(this).attr("href", "").text(I18n.t('browse.start_rjs.wait'));
256 var feature = selectedLayer.feature;
259 url: "/api/" + OSM.API_VERSION + "/" + feature.type + "/" + feature.id + "/history",
260 success: function (xml) {
261 if (selectedLayer.feature != feature || $("#browse_content").firstChild == browseObjectList) {
268 var nodes = xml.getElementsByTagName(feature.type);
269 for (var i = nodes.length - 1; i >= 0; i--) {
271 user: nodes[i].getAttribute("user") || I18n.t('browse.start_rjs.private_user'),
272 timestamp: nodes[i].getAttribute("timestamp")
276 $("#browse_content").append(JST["templates/browse/feature_history"]({
277 name: featureNameHistory(feature),
278 url: "/browse/" + feature.type + "/" + feature.id,
287 function featureTypeName(feature) {
288 return I18n.t('browse.start_rjs.object_list.type.' + feature.type);
291 function featureName(feature) {
292 return feature.tags['name:' + $('html').attr('lang')] ||
297 function featureNameSelect(feature) {
298 return feature.tags['name:' + $('html').attr('lang')] ||
300 I18n.t("browse.start_rjs.object_list.selected.type." + feature.type, { id: feature.id });
303 function featureNameHistory(feature) {
304 return feature.tags['name:' + $('html').attr('lang')] ||
306 I18n.t("browse.start_rjs.object_list.history.type." + feature.type, { id: feature.id });
309 function setStatus(status) {
310 $("#browse_status").html(status);
311 $("#browse_status").show();
314 function clearStatus() {
315 $("#browse_status").html("");
316 $("#browse_status").hide();