3 //= require jquery.autogrowtextarea
4 //= require jquery.timers
5 //= require jquery.cookie
8 //= require i18n/translations
18 function zoomPrecision(zoom) {
19 var decimals = Math.pow(10, Math.floor(zoom/3));
21 return Math.round(x * decimals) / decimals;
26 * Called as the user scrolls/zooms around to aniplate hrefs of the
27 * view tab and various other links
29 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
30 var toPrecision = zoomPrecision(zoom);
33 lat = toPrecision(lat);
34 lon = toPrecision(lon);
37 minlon = toPrecision(minlon);
38 minlat = toPrecision(minlat);
39 maxlon = toPrecision(maxlon);
40 maxlat = toPrecision(maxlat);
43 $(".geolink").each(function (index, link) {
44 var args = getArgs(link.href);
46 if ($(link).hasClass("llz")) {
50 } else if (minlon && $(link).hasClass("bbox")) {
51 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
54 if (layers && $(link).hasClass("layers")) {
58 if (objtype && $(link).hasClass("object")) {
59 args[objtype] = objid;
62 var minzoom = $(link).data("minzoom");
64 var name = link.id.replace(/anchor$/, "");
66 $(link).off("click.minzoom");
68 if (zoom >= minzoom) {
69 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
70 $(link).removeClass("disabled");
72 $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
73 $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
74 $(link).addClass("disabled");
78 link.href = setArgs(link.href, args);
81 $("#shortlinkanchor").each(function () {
82 var args = getArgs(this.href);
83 var code = makeShortCode(lat, lon, zoom);
84 var prefix = shortlinkPrefix();
86 // Add ?{node,way,relation}=id to the arguments
87 if (objtype && objid) {
88 args[objtype] = objid;
91 // This is a hack to omit the default mapnik layer from the shortlink.
92 if (layers && layers != "M") {
99 // Here we're assuming that all parameters but ?layers= and
100 // ?{node,way,relation}= can be safely omitted from the shortlink
101 // which encodes lat/lon/zoom. If new URL parameters are added to
102 // the main slippy map this needs to be changed.
103 if (args.layers || args[objtype]) {
104 this.href = setArgs(prefix + "/go/" + code, args);
106 this.href = prefix + "/go/" + code;
112 * Get the URL prefix to use for a short link
114 function shortlinkPrefix() {
115 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
116 return "http://osm.org";
123 * Called to get the arguments from a URL as a hash.
125 function getArgs(url) {
127 var querystart = url.indexOf("?");
129 if (querystart >= 0) {
130 var querystring = url.substring(querystart + 1);
131 var queryitems = querystring.split("&");
133 for (var i = 0; i < queryitems.length; i++) {
134 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
135 args[unescape(match[1])] = unescape(match[2]);
137 args[unescape(queryitems[i])] = null;
146 * Called to set the arguments on a URL from the given hash.
148 function setArgs(url, args) {
152 if (args[arg] == null) {
153 queryitems.push(escape(arg));
155 queryitems.push(escape(arg) + "=" + escape(args[arg]));
159 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
163 * Called to interlace the bits in x and y, making a Morton code.
165 function interlace(x, y) {
166 x = (x | (x << 8)) & 0x00ff00ff;
167 x = (x | (x << 4)) & 0x0f0f0f0f;
168 x = (x | (x << 2)) & 0x33333333;
169 x = (x | (x << 1)) & 0x55555555;
171 y = (y | (y << 8)) & 0x00ff00ff;
172 y = (y | (y << 4)) & 0x0f0f0f0f;
173 y = (y | (y << 2)) & 0x33333333;
174 y = (y | (y << 1)) & 0x55555555;
180 * Called to create a short code for the short link.
182 function makeShortCode(lat, lon, zoom) {
183 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
184 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
185 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
186 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
187 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
188 // and drops the last 4 bits of the full 64 bit Morton code.
190 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
191 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
192 digit = (c1 >> (24 - 6 * i)) & 0x3f;
193 str += char_array.charAt(digit);
195 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
196 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
197 str += char_array.charAt(digit);
199 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
206 * Forms which have been cached by rails may have he wrong
207 * authenticity token, so patch up any forms with the correct
208 * token taken from the page header.
210 $(document).ready(function () {
211 var auth_token = $("meta[name=csrf-token]").attr("content");
212 $("form input[name=authenticity_token]").val(auth_token);
216 * Enable auto expansion for all text areas
218 $(document).ready(function () {
219 $("textarea").autoGrow();