]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
c8577f7c3f7e6529683ac3bd296da1affa071f1e
[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 leaflet
8 //= require leaflet.osm
9 //= require leaflet.hash
10 //= require leaflet.zoom
11 //= require leaflet.extend
12 //= require leaflet.locationfilter
13 //= require i18n/translations
14 //= require oauth
15 //= require osm
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 base = 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 (layers && $(link).hasClass("layers")) args.layers = layers;
69     if (object && $(link).hasClass("object")) args[object.type] = object.id;
70
71     var href = base + '?' + querystring.stringify(args);
72
73     if ($(link).hasClass("llz")) {
74       href += OSM.formatHash({lat: loc.lat, lon: loc.lon || loc.lng, zoom: zoom});
75     }
76
77     link.href = href;
78
79     var minzoom = $(link).data("minzoom");
80     if (minzoom) {
81       var name = link.id.replace(/anchor$/, "");
82       $(link).off("click.minzoom");
83       if (zoom >= minzoom) {
84         $(link)
85           .attr("title", I18n.t("javascripts.site." + name + "_tooltip"))
86           .removeClass("disabled");
87       } else {
88         $(link)
89           .attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"))
90           .addClass("disabled")
91           .on("click.minzoom", function () {
92             alert(I18n.t("javascripts.site." + name + "_zoom_alert"));
93             return false;
94           });
95       }
96     }
97   });
98 }
99
100 // generate a cookie-safe string of map state
101 function cookieContent(map) {
102   var center = map.getCenter().wrap();
103   return [center.lng, center.lat, map.getZoom(), map.getLayersCode()].join('|');
104 }
105
106 /*
107  * Forms which have been cached by rails may have the wrong
108  * authenticity token, so patch up any forms with the correct
109  * token taken from the page header.
110  */
111 $(document).ready(function () {
112   var auth_token = $("meta[name=csrf-token]").attr("content");
113   $("form input[name=authenticity_token]").val(auth_token);
114 });