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