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.setBounds(map.getBounds().pad(-0.2));
87 locationFilter.enable();
91 function disableFilter() {
92 $("#browse_filter_toggle").html(I18n.t('browse.start_rjs.manually_select'));
93 locationFilter.disable();
97 function hideAreas() {
98 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.show_areas'));
103 function showAreas() {
104 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
109 function displayFeatureWarning(count, limit, callback) {
112 var div = document.createElement("div");
114 var p = document.createElement("p");
115 p.appendChild(document.createTextNode(I18n.t("browse.start_rjs.loaded_an_area_with_num_features", { num_features: count, max_features: limit })));
118 var input = document.createElement("input");
119 input.type = "submit";
120 input.value = I18n.t('browse.start_rjs.load_data');
121 input.onclick = callback;
122 div.appendChild(input);
124 $("#browse_content").html("");
125 $("#browse_content").append(div);
129 var bounds = locationFilter.isEnabled() ? locationFilter.getBounds() : map.getBounds();
130 var size = bounds.getSize();
132 if (size > OSM.MAX_REQUEST_AREA) {
133 setStatus(I18n.t("browse.start_rjs.unable_to_load_size", { max_bbox_size: OSM.MAX_REQUEST_AREA, bbox_size: size }));
137 setStatus(I18n.t('browse.start_rjs.loading'));
139 var url = "/api/" + OSM.API_VERSION + "/map?bbox=" + bounds.toBBOX();
142 * Modern browsers are quite happy showing far more than 100 features in
143 * the data browser, so increase the limit to 2000 by default, but keep
144 * it restricted to 500 for IE8 and 100 for older IEs.
146 var maxFeatures = 2000;
149 if (navigator.appVersion < 8) {
151 } else if (navigator.appVersion < 9) {
158 success: function (xml) {
161 $("#browse_content").empty();
162 dataLayer.clearLayers();
163 selectedLayer = null;
165 var features = dataLayer.buildFeatures(xml);
167 function addFeatures() {
168 dataLayer.addData(features);
172 dataLayer.eachLayer(function (layer) {
173 var feature = layer.feature;
174 layersById[feature.id] = layer;
176 typeName: featureTypeName(feature),
177 url: "/browse/" + feature.type + "/" + feature.id,
178 name: featureName(feature)
182 browseObjectList = $(JST["templates/browse/feature_list"]({
190 if (features.length < maxFeatures) {
193 displayFeatureWarning(features.length, maxFeatures, addFeatures);
199 function viewFeatureLink() {
200 var layer = layersById[$(this).data("feature-id")];
204 if (locationFilter.isEnabled()) {
205 map.panTo(layer.getBounds().getCenter());
211 function loadObjectList() {
212 $("#browse_content").html(browseObjectList);
213 $("#browse_content").find("a[data-feature-id]").click(viewFeatureLink);
218 function onSelect(layer) {
219 // Unselect previously selected feature
221 selectedLayer.setStyle(selectedLayer.originalStyle);
224 // Redraw in selected style
225 layer.originalStyle = layer.options;
226 layer.setStyle({color: '#0000ff', weight: 8});
228 // If the current object is the list, don't innerHTML="", since that could clear it.
229 if ($("#browse_content").firstChild == browseObjectList) {
230 $("#browse_content").removeChild(browseObjectList);
232 $("#browse_content").empty();
235 var feature = layer.feature;
237 $("#browse_content").html(JST["templates/browse/feature"]({
238 name: featureNameSelect(feature),
239 url: "/browse/" + feature.type + "/" + feature.id,
240 attributes: feature.tags
243 $("#browse_content").find("a.browse_show_list").click(loadObjectList);
244 $("#browse_content").find("a.browse_show_history").click(loadHistory);
246 // Stash the currently drawn feature
247 selectedLayer = layer;
250 dataLayer.on("click", function (e) {
254 function loadHistory() {
255 $(this).attr("href", "").text(I18n.t('browse.start_rjs.wait'));
257 var feature = selectedLayer.feature;
260 url: "/api/" + OSM.API_VERSION + "/" + feature.type + "/" + feature.id + "/history",
261 success: function (xml) {
262 if (selectedLayer.feature != feature || $("#browse_content").firstChild == browseObjectList) {
269 var nodes = xml.getElementsByTagName(feature.type);
270 for (var i = nodes.length - 1; i >= 0; i--) {
272 user: nodes[i].getAttribute("user") || I18n.t('browse.start_rjs.private_user'),
273 timestamp: nodes[i].getAttribute("timestamp")
277 $("#browse_content").append(JST["templates/browse/feature_history"]({
278 name: featureNameHistory(feature),
279 url: "/browse/" + feature.type + "/" + feature.id,
288 function featureTypeName(feature) {
289 return I18n.t('browse.start_rjs.object_list.type.' + feature.type);
292 function featureName(feature) {
293 return feature.tags['name:' + $('html').attr('lang')] ||
298 function featureNameSelect(feature) {
299 return feature.tags['name:' + $('html').attr('lang')] ||
301 I18n.t("browse.start_rjs.object_list.selected.type." + feature.type, { id: feature.id });
304 function featureNameHistory(feature) {
305 return feature.tags['name:' + $('html').attr('lang')] ||
307 I18n.t("browse.start_rjs.object_list.history.type." + feature.type, { id: feature.id });
310 function setStatus(status) {
311 $("#browse_status").html(status);
312 $("#browse_status").show();
315 function clearStatus() {
316 $("#browse_status").html("");
317 $("#browse_status").hide();