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) {
14 var browseMode = "auto";
19 var areasHidden = false;
21 var dataLayer = new L.OSM(null, {
40 dataLayer.isWayArea = function () {
41 return !areasHidden && L.OSM.prototype.isWayArea.apply(this, arguments);
44 var drawHandler = new L.Rectangle.Draw(map, {title: I18n.t('browse.start_rjs.drag_a_box')});
45 map.on('draw:rectangle-created', endDrag);
47 $("#sidebar_title").html(I18n.t('browse.start_rjs.data_frame_title'));
48 $("#sidebar_content").html(sidebarHtml);
52 map.on("moveend", updateData);
55 $("#browse_select_view").click(useMap);
57 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
58 $("#browse_hide_areas_box").toggle(hideAreas, showAreas);
60 function updateData() {
61 if (browseMode == "auto") {
62 if (map.getZoom() >= 15) {
65 setStatus(I18n.t('browse.start_rjs.zoom_or_select'));
70 $("#sidebar").one("closed", function () {
71 map.removeLayer(dataLayer);
72 map.off("moveend", updateData);
73 map.off('draw:rectangle-created', endDrag);
74 drawHandler.disable();
77 $("#browse_select_box").click(function () {
78 $("#browse_select_box").html(I18n.t('browse.start_rjs.drag_a_box'));
86 var bounds = map.getBounds();
88 if (!browseBounds || !browseBounds.contains(bounds)) {
89 browseBounds = bounds;
94 $("#browse_select_view").hide();
100 function hideAreas() {
101 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.show_areas'));
108 function showAreas() {
109 $("#browse_hide_areas_box").html(I18n.t('browse.start_rjs.hide_areas'));
116 function endDrag(e) {
117 browseBounds = e.rect.getBounds();
118 browseMode = "manual";
122 $("#browse_select_box").html(I18n.t('browse.start_rjs.manually_select'));
123 $("#browse_select_view").show();
125 drawHandler.disable();
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);
148 var size = browseBounds.getSize();
150 if (size > OSM.MAX_REQUEST_AREA) {
151 setStatus(I18n.t("browse.start_rjs.unable_to_load_size", { max_bbox_size: OSM.MAX_REQUEST_AREA, bbox_size: size }));
155 setStatus(I18n.t('browse.start_rjs.loading'));
157 $("#browse_content").empty();
158 dataLayer.clearLayers();
159 selectedLayer = null;
161 var url = "/api/" + OSM.API_VERSION + "/map?bbox=" + browseBounds.toBBOX();
164 * Modern browsers are quite happy showing far more than 100 features in
165 * the data browser, so increase the limit to 2000 by default, but keep
166 * it restricted to 500 for IE8 and 100 for older IEs.
168 var maxFeatures = 2000;
171 if (navigator.appVersion < 8) {
173 } else if (navigator.appVersion < 9) {
180 success: function (xml) {
183 dataLayer.addData(xml);
188 dataLayer.eachLayer(function (layer) {
189 var feature = layer.feature;
190 layersById[feature.id] = layer;
192 typeName: featureTypeName(feature),
193 url: "/browse/" + feature.type + "/" + feature.id,
194 name: featureName(feature),
199 browseObjectList = $(JST["templates/browse/feature_list"]({
209 function viewFeatureLink() {
210 var layer = layersById[$(this).data("feature-id")];
214 if (browseMode != "auto") {
215 map.panTo(layer.getBounds().getCenter());
221 function loadObjectList() {
222 $("#browse_content").html(browseObjectList);
223 $("#browse_content").find("a[data-feature-id]").click(viewFeatureLink);
228 function onSelect(layer) {
229 // Unselect previously selected feature
231 selectedLayer.setStyle(selectedLayer.originalStyle);
234 // Redraw in selected style
235 layer.originalStyle = layer.options;
236 layer.setStyle({color: '#0000ff', weight: 8});
238 // If the current object is the list, don't innerHTML="", since that could clear it.
239 if ($("#browse_content").firstChild == browseObjectList) {
240 $("#browse_content").removeChild(browseObjectList);
242 $("#browse_content").empty();
245 var feature = layer.feature;
247 $("#browse_content").html(JST["templates/browse/feature"]({
248 name: featureNameSelect(feature),
249 url: "/browse/" + feature.type + "/" + feature.id,
250 attributes: feature.tags
253 $("#browse_content").find("a.browse_show_list").click(loadObjectList);
254 $("#browse_content").find("a.browse_show_history").click(loadHistory);
256 // Stash the currently drawn feature
257 selectedLayer = layer;
260 dataLayer.on("click", function (e) {
264 function loadHistory() {
265 $(this).attr("href", "").text(I18n.t('browse.start_rjs.wait'));
267 var feature = selectedLayer.feature;
270 url: "/api/" + OSM.API_VERSION + "/" + feature.type + "/" + feature.id + "/history",
271 success: function (xml) {
272 if (selectedLayer.feature != feature || $("#browse_content").firstChild == browseObjectList) {
279 var nodes = xml.getElementsByTagName(feature.type);
280 for (var i = nodes.length - 1; i >= 0; i--) {
282 user: nodes[i].getAttribute("user") || I18n.t('browse.start_rjs.private_user'),
283 timestamp: nodes[i].getAttribute("timestamp")
287 $("#browse_content").append(JST["templates/browse/feature_history"]({
288 name: featureNameHistory(feature),
289 url: "/browse/" + feature.type + "/" + feature.id,
298 function featureTypeName(feature) {
299 return I18n.t('browse.start_rjs.object_list.type.' + feature.type);
302 function featureName(feature) {
303 return feature.tags['name:' + $('html').attr('lang')] ||
308 function featureNameSelect(feature) {
309 return feature.tags['name:' + $('html').attr('lang')] ||
311 I18n.t("browse.start_rjs.object_list.selected.type." + feature.type, { id: feature.id });
314 function featureNameHistory(feature) {
315 return feature.tags['name:' + $('html').attr('lang')] ||
317 I18n.t("browse.start_rjs.object_list.history.type." + feature.type, { id: feature.id });
320 function setStatus(status) {
321 $("#browse_status").html(status);
322 $("#browse_status").show();
325 function clearStatus() {
326 $("#browse_status").html("");
327 $("#browse_status").hide();