]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Move search form to header on small screens
[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 querystring
22
23 var querystring = require('querystring-component');
24
25 function zoomPrecision(zoom) {
26     return Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));
27 }
28
29 function remoteEditHandler(bbox, select) {
30   var loaded = false,
31       query = {
32           left: bbox.getWest() - 0.0001,
33           top: bbox.getNorth() + 0.0001,
34           right: bbox.getEast() + 0.0001,
35           bottom: bbox.getSouth() - 0.0001
36       };
37
38   if (select) query.select = select;
39
40   var iframe = $('<iframe>')
41     .hide()
42     .appendTo('body')
43     .attr("src", "http://127.0.0.1:8111/load_and_zoom?" + querystring.stringify(query))
44     .on('load', function() {
45       $(this).remove();
46       loaded = true;
47     });
48
49   setTimeout(function () {
50     if (!loaded) {
51       alert(I18n.t('site.index.remote_failed'));
52       iframe.remove();
53     }
54   }, 1000);
55
56   return false;
57 }
58
59 /*
60  * Called as the user scrolls/zooms around to maniplate hrefs of the
61  * view tab and various other links
62  */
63 function updatelinks(loc, zoom, layers, object) {
64   $(".geolink").each(function(index, link) {
65     var href = link.href.split(/[?#]/)[0],
66       args = querystring.parse(link.search.substring(1)),
67       editlink = $(link).hasClass("editlink");
68
69     if (object && editlink) 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 && !editlink) {
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
135   $("#menu-icon").on("click", function(e) {
136     e.preventDefault();
137     $("header").toggleClass("closed");
138   });
139
140   $("nav.primary li a").on("click", function() {
141     $("header").toggleClass("closed");
142   });
143 });