]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
ee9b16ff1d5391f23cca4062a446d2b52bfa53f7
[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.locationfilter
9 //= require i18n/translations
10 //= require oauth
11 //= require osm
12 //= require piwik
13 //= require map
14 //= require menu
15 //= require sidebar
16 //= require richtext
17 //= require resize
18 //= require geocoder
19
20 function zoomPrecision(zoom) {
21     var decimals = Math.pow(10, Math.floor(zoom/3));
22     return function(x) {
23          return Math.round(x * decimals) / decimals;
24     };
25 }
26
27 function normalBounds(bounds) {
28     if (bounds instanceof L.LatLngBounds) return bounds;
29     return new L.LatLngBounds(
30         new L.LatLng(bounds[0][0], bounds[0][1]),
31         new L.LatLng(bounds[1][0], bounds[1][1]));
32 }
33
34 /*
35  * Called as the user scrolls/zooms around to maniplate hrefs of the
36  * view tab and various other links
37  */
38 function updatelinks(loc, zoom, layers, bounds, object) {
39   var toPrecision = zoomPrecision(zoom);
40   bounds = normalBounds(bounds);
41   var node;
42
43   var lat = toPrecision(loc.lat),
44       lon = toPrecision(loc.lon || loc.lng);
45
46   if (bounds) {
47     var minlon = toPrecision(bounds.getWest()),
48         minlat = toPrecision(bounds.getSouth()),
49         maxlon = toPrecision(bounds.getEast()),
50         maxlat = toPrecision(bounds.getNorth());
51   }
52
53   $(".geolink").each(setGeolink);
54   $("#shortlinkanchor").each(setShortlink);
55
56   function setGeolink(index, link) {
57     var args = getArgs(link.href);
58
59     if ($(link).hasClass("llz")) {
60       $.extend(args, {
61           lat: lat,
62           lon: lon,
63           zoom: zoom
64       });
65     } else if (minlon && $(link).hasClass("bbox")) {
66       $.extend(args, {
67           bbox: minlon + "," + minlat + "," + maxlon + "," + maxlat
68       });
69     }
70
71     if (layers && $(link).hasClass("layers")) {
72       args.layers = layers;
73     }
74
75     if (object && $(link).hasClass("object")) {
76       args[object.type] = object.id;
77     }
78
79     var minzoom = $(link).data("minzoom");
80     if (minzoom) {
81         var name = link.id.replace(/anchor$/, "");
82
83         $(link).off("click.minzoom");
84
85         if (zoom >= minzoom) {
86           $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"))
87               .removeClass("disabled");
88         } else {
89           $(link).on("click.minzoom", minZoomAlert)
90               .attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"))
91               .addClass("disabled");
92         }
93     }
94
95     link.href = setArgs(link.href, args);
96   }
97
98
99   function setShortlink() {
100     var args = getArgs(this.href);
101     var code = makeShortCode(lat, lon, zoom);
102     var prefix = shortlinkPrefix();
103
104     // Add ?{node,way,relation}=id to the arguments
105     if (object) {
106       args[object.type] = object.id;
107     }
108
109     // This is a hack to omit the default mapnik layer from the shortlink.
110     if (layers && layers != "M") {
111       args.layers = layers;
112     } else {
113       delete args.layers;
114     }
115
116     // Here we're assuming that all parameters but ?layers= and
117     // ?{node,way,relation}= can be safely omitted from the shortlink
118     // which encodes lat/lon/zoom. If new URL parameters are added to
119     // the main slippy map this needs to be changed.
120     if (args.layers || object) {
121       this.href = setArgs(prefix + "/go/" + code, args);
122     } else {
123       this.href = prefix + "/go/" + code;
124     }
125   }
126 }
127
128 function minZoomAlert() {
129     alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false;
130 }
131
132 /*
133  * Get the URL prefix to use for a short link
134  */
135 function shortlinkPrefix() {
136   if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
137     return "http://osm.org";
138   } else {
139     return "";
140   }
141 }
142
143 /*
144  * Called to get the arguments from a URL as a hash.
145  */
146 function getArgs(url) {
147   var args = {};
148   var querystart = url.indexOf("?");
149
150   if (querystart >= 0) {
151      var querystring = url.substring(querystart + 1);
152      var queryitems = querystring.split("&");
153
154      for (var i = 0; i < queryitems.length; i++) {
155         if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
156            args[unescape(match[1])] = unescape(match[2]);
157         } else {
158            args[unescape(queryitems[i])] = null;
159         }
160      }
161   }
162
163   return args;
164 }
165
166 /*
167  * Called to set the arguments on a URL from the given hash.
168  */
169 function setArgs(url, args) {
170    var queryitems = [];
171
172    for (arg in args) {
173       if (args[arg] == null) {
174          queryitems.push(escape(arg));
175       } else {
176          queryitems.push(escape(arg) + "=" + escape(args[arg]));
177       }
178    }
179
180    return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
181 }
182
183 /*
184  * Called to interlace the bits in x and y, making a Morton code.
185  */
186 function interlace(x, y) {
187     x = (x | (x << 8)) & 0x00ff00ff;
188     x = (x | (x << 4)) & 0x0f0f0f0f;
189     x = (x | (x << 2)) & 0x33333333;
190     x = (x | (x << 1)) & 0x55555555;
191
192     y = (y | (y << 8)) & 0x00ff00ff;
193     y = (y | (y << 4)) & 0x0f0f0f0f;
194     y = (y | (y << 2)) & 0x33333333;
195     y = (y | (y << 1)) & 0x55555555;
196
197     return (x << 1) | y;
198 }
199
200 /*
201  * Called to create a short code for the short link.
202  */
203 function makeShortCode(lat, lon, zoom) {
204     char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
205     var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
206     var y = Math.round((lat +  90.0) * ((1 << 30) / 45.0));
207     // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
208     // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
209     // and drops the last 4 bits of the full 64 bit Morton code.
210     var str = "";
211     var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
212     for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
213         digit = (c1 >> (24 - 6 * i)) & 0x3f;
214         str += char_array.charAt(digit);
215     }
216     for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
217         digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
218         str += char_array.charAt(digit);
219     }
220     for (var i = 0; i < ((zoom + 8) % 3); ++i) {
221         str += "-";
222     }
223     return str;
224 }
225
226 /*
227  * Forms which have been cached by rails may have he wrong
228  * authenticity token, so patch up any forms with the correct
229  * token taken from the page header.
230  */
231 $(document).ready(function () {
232   var auth_token = $("meta[name=csrf-token]").attr("content");
233   $("form input[name=authenticity_token]").val(auth_token);
234 });