1 //= require templates/browse/feature
2 //= require templates/browse/feature_list
3 //= require templates/browse/feature_history
5 $(document).ready(function () {
10 var areasHidden = false;
13 var dataLayer = new L.OSM.DataLayer(null, {
30 dataLayer.isWayArea = function () {
31 return !areasHidden && L.OSM.DataLayer.prototype.isWayArea.apply(this, arguments);
34 dataLayer.on("click", function (e) {
38 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
39 map.layersControl.addOverlay(dataLayer, I18n.t("browse.start_rjs.data_layer_name"));
42 map.on('layeradd', function (e) {
43 if (e.layer === dataLayer) {
44 $.ajax({ url: "/browse/start", success: function (sidebarHtml) {
45 startBrowse(sidebarHtml);
50 map.on('layerremove', function (e) {
51 if (e.layer === dataLayer) {
56 function startBrowse(sidebarHtml) {
57 locationFilter = new L.LocationFilter({
62 locationFilter.on("change", getData);
64 $("#sidebar_title").html(I18n.t('browse.start_rjs.data_frame_title'));
65 $("#sidebar_content").html(sidebarHtml);
69 map.on("moveend", updateData);
72 $("#browse_filter_toggle").click(toggleFilter);
74 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
75 $("#browse_hide_areas_box").click(toggleAreas);
77 $("#sidebar").one("closed", function () {
78 map.removeLayer(dataLayer);
79 map.removeLayer(locationFilter);
80 map.off("moveend", updateData);
81 locationFilter.off("change", getData);
85 function updateData() {
86 if (!locationFilter.isEnabled()) {
87 if (map.getZoom() >= 15) {
88 var bounds = map.getBounds();
89 if (!browseBounds || !browseBounds.contains(bounds)) {
90 browseBounds = bounds;
94 setStatus(I18n.t('browse.start_rjs.zoom_or_select'));
99 function toggleFilter() {
100 if (locationFilter.isEnabled()) {
101 $("#browse_filter_toggle").html(I18n.t('browse.start_rjs.manually_select'));
102 locationFilter.disable();
104 $("#browse_filter_toggle").html(I18n.t('browse.start_rjs.view_data'));
105 locationFilter.setBounds(map.getBounds().pad(-0.2));
106 locationFilter.enable();
112 function toggleAreas() {
114 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
117 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.show_areas'));
124 function displayFeatureWarning(count, limit, callback) {
127 var div = document.createElement("div");
129 var p = document.createElement("p");
130 p.appendChild(document.createTextNode(I18n.t("browse.start_rjs.loaded_an_area_with_num_features", { num_features: count, max_features: limit })));
133 var input = document.createElement("input");
134 input.type = "submit";
135 input.value = I18n.t('browse.start_rjs.load_data');
136 input.onclick = callback;
137 div.appendChild(input);
139 $("#browse_content").html("");
140 $("#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) {
173 success: function (xml) {
176 $("#browse_content").empty();
177 dataLayer.clearLayers();
178 selectedLayer = null;
180 var features = dataLayer.buildFeatures(xml);
182 function addFeatures() {
183 dataLayer.addData(features);
187 dataLayer.eachLayer(function (layer) {
188 var feature = layer.feature;
189 layersById[feature.id] = layer;
191 typeName: featureTypeName(feature),
192 url: "/browse/" + feature.type + "/" + feature.id,
193 name: featureName(feature)
197 browseObjectList = $(JST["templates/browse/feature_list"]({
205 if (features.length < maxFeatures) {
208 displayFeatureWarning(features.length, maxFeatures, addFeatures);
214 function viewFeatureLink() {
215 var layer = layersById[$(this).data("feature-id")];
219 if (locationFilter.isEnabled()) {
220 map.panTo(layer.getBounds().getCenter());
226 function loadObjectList() {
227 $("#browse_content").html(browseObjectList);
228 $("#browse_content").find("a[data-feature-id]").click(viewFeatureLink);
233 function onSelect(layer) {
234 // Unselect previously selected feature
236 selectedLayer.setStyle(selectedLayer.originalStyle);
239 // Redraw in selected style
240 layer.originalStyle = layer.options;
241 layer.setStyle({color: '#0000ff', weight: 8});
243 // If the current object is the list, don't innerHTML="", since that could clear it.
244 if ($("#browse_content").firstChild == browseObjectList) {
245 $("#browse_content").removeChild(browseObjectList);
247 $("#browse_content").empty();
250 var feature = layer.feature;
252 $("#browse_content").html(JST["templates/browse/feature"]({
253 name: featureNameSelect(feature),
254 url: "/browse/" + feature.type + "/" + feature.id,
255 attributes: feature.tags
258 $("#browse_content").find("a.browse_show_list").click(loadObjectList);
259 $("#browse_content").find("a.browse_show_history").click(loadHistory);
261 // Stash the currently drawn feature
262 selectedLayer = layer;
265 function loadHistory() {
266 $(this).attr("href", "").text(I18n.t('browse.start_rjs.wait'));
268 var feature = selectedLayer.feature;
271 url: "/api/" + OSM.API_VERSION + "/" + feature.type + "/" + feature.id + "/history",
272 success: function (xml) {
273 if (selectedLayer.feature != feature || $("#browse_content").firstChild == browseObjectList) {
280 var nodes = xml.getElementsByTagName(feature.type);
281 for (var i = nodes.length - 1; i >= 0; i--) {
283 user: nodes[i].getAttribute("user") || I18n.t('browse.start_rjs.private_user'),
284 timestamp: nodes[i].getAttribute("timestamp")
288 $("#browse_content").append(JST["templates/browse/feature_history"]({
289 name: featureNameHistory(feature),
290 url: "/browse/" + feature.type + "/" + feature.id,
299 function featureTypeName(feature) {
300 return I18n.t('browse.start_rjs.object_list.type.' + feature.type);
303 function featureName(feature) {
304 return feature.tags['name:' + $('html').attr('lang')] ||
309 function featureNameSelect(feature) {
310 return feature.tags['name:' + $('html').attr('lang')] ||
312 I18n.t("browse.start_rjs.object_list.selected.type." + feature.type, { id: feature.id });
315 function featureNameHistory(feature) {
316 return feature.tags['name:' + $('html').attr('lang')] ||
318 I18n.t("browse.start_rjs.object_list.history.type." + feature.type, { id: feature.id });
321 function setStatus(status) {
322 $("#browse_status").html(status);
323 $("#browse_status").show();
326 function clearStatus() {
327 $("#browse_status").html("");
328 $("#browse_status").hide();