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