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