3 //= require jquery.autogrowtextarea
4 //= require jquery.timers
5 //= require jquery.cookie
8 //= require i18n/translations
17 function zoomPrecision(zoom) {
18 var decimals = Math.pow(10, Math.floor(zoom/3));
20 return Math.round(x * decimals) / decimals;
25 * Called as the user scrolls/zooms around to aniplate hrefs of the
26 * view tab and various other links
28 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,object) {
29 var toPrecision = zoomPrecision(zoom);
32 lat = toPrecision(lat);
33 lon = toPrecision(lon);
36 minlon = toPrecision(minlon);
37 minlat = toPrecision(minlat);
38 maxlon = toPrecision(maxlon);
39 maxlat = toPrecision(maxlat);
42 $(".geolink").each(function (index, link) {
43 var args = getArgs(link.href);
45 if ($(link).hasClass("llz")) {
49 } else if (minlon && $(link).hasClass("bbox")) {
50 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
53 if (layers && $(link).hasClass("layers")) {
57 if (object && $(link).hasClass("object")) {
58 args[object.type] = object.id;
61 var minzoom = $(link).data("minzoom");
63 var name = link.id.replace(/anchor$/, "");
65 $(link).off("click.minzoom");
67 if (zoom >= minzoom) {
68 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
69 $(link).removeClass("disabled");
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");
77 link.href = setArgs(link.href, args);
80 $("#shortlinkanchor").each(function () {
81 var args = getArgs(this.href);
82 var code = makeShortCode(lat, lon, zoom);
83 var prefix = shortlinkPrefix();
85 // Add ?{node,way,relation}=id to the arguments
87 args[object.type] = object.id;
90 // This is a hack to omit the default mapnik layer from the shortlink.
91 if (layers && layers != "M") {
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 || object) {
103 this.href = setArgs(prefix + "/go/" + code, args);
105 this.href = prefix + "/go/" + code;
111 * Get the URL prefix to use for a short link
113 function shortlinkPrefix() {
114 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
115 return "http://osm.org";
122 * Called to get the arguments from a URL as a hash.
124 function getArgs(url) {
126 var querystart = url.indexOf("?");
128 if (querystart >= 0) {
129 var querystring = url.substring(querystart + 1);
130 var queryitems = querystring.split("&");
132 for (var i = 0; i < queryitems.length; i++) {
133 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
134 args[unescape(match[1])] = unescape(match[2]);
136 args[unescape(queryitems[i])] = null;
145 * Called to set the arguments on a URL from the given hash.
147 function setArgs(url, args) {
151 if (args[arg] == null) {
152 queryitems.push(escape(arg));
154 queryitems.push(escape(arg) + "=" + escape(args[arg]));
158 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
162 * Called to interlace the bits in x and y, making a Morton code.
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;
170 y = (y | (y << 8)) & 0x00ff00ff;
171 y = (y | (y << 4)) & 0x0f0f0f0f;
172 y = (y | (y << 2)) & 0x33333333;
173 y = (y | (y << 1)) & 0x55555555;
179 * Called to create a short code for the short link.
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.
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);
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);
198 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
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.
209 $(document).ready(function () {
210 var auth_token = $("meta[name=csrf-token]").attr("content");
211 $("form input[name=authenticity_token]").val(auth_token);
215 * Enable auto expansion for all text areas
217 $(document).ready(function () {
218 $("textarea").autoGrow();