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