3 //= require jquery.autogrowtextarea
4 //= require jquery.timers
5 //= require jquery.cookie
8 //= require i18n/translations
21 function zoomPrecision(zoom) {
22 var decimals = Math.pow(10, Math.floor(zoom/3));
24 return Math.round(x * decimals) / decimals;
29 * Called as the user scrolls/zooms around to aniplate hrefs of the
30 * view tab and various other links
32 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
33 var toPrecision = zoomPrecision(zoom);
36 lat = toPrecision(lat);
37 lon = toPrecision(lon);
40 minlon = toPrecision(minlon);
41 minlat = toPrecision(minlat);
42 maxlon = toPrecision(maxlon);
43 maxlat = toPrecision(maxlat);
46 $(".geolink").each(function (index, link) {
47 var args = getArgs(link.href);
49 if ($(link).hasClass("llz")) {
53 } else if (minlon && $(link).hasClass("bbox")) {
54 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
57 if (layers && $(link).hasClass("layers")) {
61 if (objtype && $(link).hasClass("object")) {
62 args[objtype] = objid;
65 var minzoom = $(link).data("minzoom");
67 var name = link.id.replace(/anchor$/, "");
69 $(link).off("click.minzoom");
71 if (zoom >= minzoom) {
72 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
73 $(link).removeClass("disabled");
75 $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
76 $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
77 $(link).addClass("disabled");
81 link.href = setArgs(link.href, args);
84 $("#shortlinkanchor").each(function () {
85 var args = getArgs(this.href);
86 var code = makeShortCode(lat, lon, zoom);
87 var prefix = shortlinkPrefix();
89 // Add ?{node,way,relation}=id to the arguments
90 if (objtype && objid) {
91 args[objtype] = objid;
94 // This is a hack to omit the default mapnik layer from the shortlink.
95 if (layers && layers != "M") {
102 // Here we're assuming that all parameters but ?layers= and
103 // ?{node,way,relation}= can be safely omitted from the shortlink
104 // which encodes lat/lon/zoom. If new URL parameters are added to
105 // the main slippy map this needs to be changed.
106 if (args.layers || args[objtype]) {
107 this.href = setArgs(prefix + "/go/" + code, args);
109 this.href = prefix + "/go/" + code;
115 * Get the URL prefix to use for a short link
117 function shortlinkPrefix() {
118 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
119 return "http://osm.org";
126 * Called to get the arguments from a URL as a hash.
128 function getArgs(url) {
130 var querystart = url.indexOf("?");
132 if (querystart >= 0) {
133 var querystring = url.substring(querystart + 1);
134 var queryitems = querystring.split("&");
136 for (var i = 0; i < queryitems.length; i++) {
137 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
138 args[unescape(match[1])] = unescape(match[2]);
140 args[unescape(queryitems[i])] = null;
149 * Called to set the arguments on a URL from the given hash.
151 function setArgs(url, args) {
155 if (args[arg] == null) {
156 queryitems.push(escape(arg));
158 queryitems.push(escape(arg) + "=" + escape(args[arg]));
162 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
166 * Called to interlace the bits in x and y, making a Morton code.
168 function interlace(x, y) {
169 x = (x | (x << 8)) & 0x00ff00ff;
170 x = (x | (x << 4)) & 0x0f0f0f0f;
171 x = (x | (x << 2)) & 0x33333333;
172 x = (x | (x << 1)) & 0x55555555;
174 y = (y | (y << 8)) & 0x00ff00ff;
175 y = (y | (y << 4)) & 0x0f0f0f0f;
176 y = (y | (y << 2)) & 0x33333333;
177 y = (y | (y << 1)) & 0x55555555;
183 * Called to create a short code for the short link.
185 function makeShortCode(lat, lon, zoom) {
186 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
187 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
188 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
189 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
190 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
191 // and drops the last 4 bits of the full 64 bit Morton code.
193 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
194 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
195 digit = (c1 >> (24 - 6 * i)) & 0x3f;
196 str += char_array.charAt(digit);
198 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
199 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
200 str += char_array.charAt(digit);
202 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
209 * Forms which have been cached by rails may have he wrong
210 * authenticity token, so patch up any forms with the correct
211 * token taken from the page header.
213 $(document).ready(function () {
214 var auth_token = $("meta[name=csrf-token]").attr("content");
215 $("form input[name=authenticity_token]").val(auth_token);
219 * Enable auto expansion for all text areas
221 $(document).ready(function () {
222 $("textarea").autoGrow();