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