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