]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Refactor URL generating methods
[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     var decimals = Math.pow(10, Math.floor(zoom/3));
27     return function(x) {
28          return Math.round(x * decimals) / decimals;
29     };
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   $("#linkloader")
50     .attr("src", "http://127.0.0.1:8111/load_and_zoom?" + querystring.stringify(query))
51     .load(function() { loaded = true; });
52
53   setTimeout(function () {
54     if (!loaded) alert(I18n.t('site.index.remote_failed'));
55   }, 1000);
56
57   return false;
58 }
59
60 /*
61  * Called as the user scrolls/zooms around to maniplate hrefs of the
62  * view tab and various other links
63  */
64 function updatelinks(loc, zoom, layers, bounds, object) {
65   var toPrecision = zoomPrecision(zoom);
66   bounds = normalBounds(bounds);
67   var node;
68
69   var lat = toPrecision(loc.lat),
70       lon = toPrecision(loc.lon || loc.lng);
71
72   if (bounds) {
73     var minlon = toPrecision(bounds.getWest()),
74         minlat = toPrecision(bounds.getSouth()),
75         maxlon = toPrecision(bounds.getEast()),
76         maxlat = toPrecision(bounds.getNorth());
77   }
78
79   $(".geolink").each(setGeolink);
80
81   function setGeolink(index, link) {
82     var base = link.href.split('?')[0],
83         qs = link.href.split('?')[1],
84         args = querystring.parse(qs);
85
86     if ($(link).hasClass("llz")) {
87       $.extend(args, {
88           lat: lat,
89           lon: lon,
90           zoom: zoom
91       });
92     } else if (minlon && $(link).hasClass("bbox")) {
93       $.extend(args, {
94           bbox: minlon + "," + minlat + "," + maxlon + "," + maxlat
95       });
96     }
97
98     if (layers && $(link).hasClass("layers")) args.layers = layers;
99     if (object && $(link).hasClass("object")) args[object.type] = object.id;
100
101     var minzoom = $(link).data("minzoom");
102     if (minzoom) {
103       var name = link.id.replace(/anchor$/, "");
104       $(link).off("click.minzoom");
105       if (zoom >= minzoom) {
106         $(link)
107           .attr("title", I18n.t("javascripts.site." + name + "_tooltip"))
108           .removeClass("disabled");
109       } else {
110         $(link)
111           .attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"))
112           .addClass("disabled")
113           .on("click.minzoom", function () {
114             alert(I18n.t("javascripts.site." + name + "_zoom_alert"));
115             return false;
116           });
117       }
118     }
119     link.href = base + '?' + querystring.stringify(args);
120   }
121 }
122
123 // generate a cookie-safe string of map state
124 function cookieContent(map) {
125   var center = map.getCenter().wrap();
126   return [center.lng, center.lat, map.getZoom(), map.getLayersCode()].join('|');
127 }
128
129 /*
130  * Forms which have been cached by rails may have the wrong
131  * authenticity token, so patch up any forms with the correct
132  * token taken from the page header.
133  */
134 $(document).ready(function () {
135   var auth_token = $("meta[name=csrf-token]").attr("content");
136   $("form input[name=authenticity_token]").val(auth_token);
137 });