]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Update to iD v1.3.2
[rails.git] / app / assets / javascripts / application.js
1 //= require jquery
2 //= require jquery_ujs
3 //= require jquery.timers
4 //= require jquery.cookie
5 //= require jquery.throttle-debounce
6 //= require bootstrap.tooltip
7 //= require bootstrap.dropdown
8 //= require augment
9 //= require osm
10 //= require leaflet
11 //= require leaflet.osm
12 //= require leaflet.hash
13 //= require leaflet.zoom
14 //= require leaflet.extend
15 //= require leaflet.locationfilter
16 //= require i18n/translations
17 //= require oauth
18 //= require piwik
19 //= require map
20 //= require sidebar
21 //= require richtext
22 //= require geocoder
23 //= require querystring
24
25 var querystring = require('querystring-component');
26
27 function zoomPrecision(zoom) {
28     return Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));
29 }
30
31 function normalBounds(bounds) {
32     if (bounds instanceof L.LatLngBounds) return bounds;
33     return new L.LatLngBounds(
34         new L.LatLng(bounds[0][0], bounds[0][1]),
35         new L.LatLng(bounds[1][0], bounds[1][1]));
36 }
37
38 function remoteEditHandler(bbox, select) {
39   var loaded = false,
40       query = {
41           left: bbox.getWest() - 0.0001,
42           top: bbox.getNorth() + 0.0001,
43           right: bbox.getEast() + 0.0001,
44           bottom: bbox.getSouth() - 0.0001
45       };
46
47   if (select) query.select = select;
48
49   var iframe = $('<iframe>')
50     .hide()
51     .appendTo('body')
52     .attr("src", "http://127.0.0.1:8111/load_and_zoom?" + querystring.stringify(query))
53     .on('load', function() {
54       $(this).remove();
55       loaded = true;
56     });
57
58   setTimeout(function () {
59     if (!loaded) {
60       alert(I18n.t('site.index.remote_failed'));
61       iframe.remove();
62     }
63   }, 1000);
64
65   return false;
66 }
67
68 /*
69  * Called as the user scrolls/zooms around to maniplate hrefs of the
70  * view tab and various other links
71  */
72 function updatelinks(loc, zoom, layers, bounds, object) {
73   $(".geolink").each(function(index, link) {
74     var href = link.href.split(/[?#]/)[0],
75         args = querystring.parse(link.search.substring(1));
76
77     if (bounds && $(link).hasClass("bbox")) args.bbox = normalBounds(bounds).toBBoxString();
78     if (object && $(link).hasClass("object")) args[object.type] = object.id;
79
80     var query = querystring.stringify(args);
81     if (query) href += '?' + query;
82
83     if ($(link).hasClass("llz")) {
84       args = {
85         lat: loc.lat,
86         lon: loc.lon || loc.lng,
87         zoom: zoom
88       };
89
90       if (layers && $(link).hasClass("layers")) {
91         args.layers = layers;
92       }
93
94       href += OSM.formatHash(args);
95     }
96
97     link.href = href;
98   });
99
100   var editDisabled = zoom < 13;
101   $('#edit_tab')
102     .tooltip({placement: 'bottom'})
103     .off('click.minzoom')
104     .on('click.minzoom', function() { return !editDisabled; })
105     .toggleClass('disabled', editDisabled)
106     .attr('data-original-title', editDisabled ?
107       I18n.t('javascripts.site.edit_disabled_tooltip') : '');
108
109   var historyDisabled = zoom < 11;
110   $('#history_tab')
111     .tooltip({placement: 'bottom'})
112     .off('click.minzoom')
113     .on('click.minzoom', function() { return !historyDisabled; })
114     .toggleClass('disabled', historyDisabled)
115     .attr('data-original-title', historyDisabled ?
116       I18n.t('javascripts.site.history_disabled_tooltip') : '');
117 }
118
119 // generate a cookie-safe string of map state
120 function cookieContent(map) {
121   var center = map.getCenter().wrap();
122   return [center.lng, center.lat, map.getZoom(), map.getLayersCode()].join('|');
123 }
124
125 function escapeHTML(string) {
126   var htmlEscapes = {
127     '&': '&amp;',
128     '<': '&lt;',
129     '>': '&gt;',
130     '"': '&quot;',
131     "'": '&#x27;'
132   };
133   return string == null ? '' : (string + '').replace(/[&<>"']/g, function(match) {
134       return htmlEscapes[match];
135   });
136 }
137
138 /*
139  * Forms which have been cached by rails may have the wrong
140  * authenticity token, so patch up any forms with the correct
141  * token taken from the page header.
142  */
143 $(document).ready(function () {
144   var auth_token = $("meta[name=csrf-token]").attr("content");
145   $("form input[name=authenticity_token]").val(auth_token);
146 });