]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
a0ddcf9c2b0b0b14e643f693fcfc4a6298afe8d8
[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 remoteEditHandler(bbox, select) {
32   var loaded = false,
33       query = {
34           left: bbox.getWest() - 0.0001,
35           top: bbox.getNorth() + 0.0001,
36           right: bbox.getEast() + 0.0001,
37           bottom: bbox.getSouth() - 0.0001
38       };
39
40   if (select) query.select = select;
41
42   var iframe = $('<iframe>')
43     .hide()
44     .appendTo('body')
45     .attr("src", "http://127.0.0.1:8111/load_and_zoom?" + querystring.stringify(query))
46     .on('load', function() {
47       $(this).remove();
48       loaded = true;
49     });
50
51   setTimeout(function () {
52     if (!loaded) {
53       alert(I18n.t('site.index.remote_failed'));
54       iframe.remove();
55     }
56   }, 1000);
57
58   return false;
59 }
60
61 /*
62  * Called as the user scrolls/zooms around to maniplate hrefs of the
63  * view tab and various other links
64  */
65 function updatelinks(loc, zoom, layers, object) {
66   $(".geolink").each(function(index, link) {
67     var href = link.href.split(/[?#]/)[0],
68         args = querystring.parse(link.search.substring(1));
69
70     if (object && $(link).hasClass("object")) args[object.type] = object.id;
71
72     var query = querystring.stringify(args);
73     if (query) href += '?' + query;
74
75     args = {
76       lat: loc.lat,
77       lon: loc.lon || loc.lng,
78       zoom: zoom
79     };
80
81     if (layers && $(link).hasClass("layers")) {
82       args.layers = layers;
83     }
84
85     href += OSM.formatHash(args);
86
87     link.href = href;
88   });
89
90   var editDisabled = zoom < 13;
91   $('#edit_tab')
92     .tooltip({placement: 'bottom'})
93     .off('click.minzoom')
94     .on('click.minzoom', function() { return !editDisabled; })
95     .toggleClass('disabled', editDisabled)
96     .attr('data-original-title', editDisabled ?
97       I18n.t('javascripts.site.edit_disabled_tooltip') : '');
98 }
99
100 // generate a cookie-safe string of map state
101 function cookieContent(map) {
102   var center = map.getCenter().wrap();
103   return [center.lng, center.lat, map.getZoom(), map.getLayersCode()].join('|');
104 }
105
106 function escapeHTML(string) {
107   var htmlEscapes = {
108     '&': '&amp;',
109     '<': '&lt;',
110     '>': '&gt;',
111     '"': '&quot;',
112     "'": '&#x27;'
113   };
114   return string == null ? '' : (string + '').replace(/[&<>"']/g, function(match) {
115       return htmlEscapes[match];
116   });
117 }
118
119 /*
120  * Forms which have been cached by rails may have the wrong
121  * authenticity token, so patch up any forms with the correct
122  * token taken from the page header.
123  */
124 $(document).ready(function () {
125   var auth_token = $("meta[name=csrf-token]").attr("content");
126   $("form input[name=authenticity_token]").val(auth_token);
127 });