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