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