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