3 //= require jquery.autogrowtextarea
6 * Called as the user scrolls/zooms around to aniplate hrefs of the
7 * view tab and various other links
9 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
10 var decimals = Math.pow(10, Math.floor(zoom/3));
13 lat = Math.round(lat * decimals) / decimals;
14 lon = Math.round(lon * decimals) / decimals;
17 minlon = Math.round(minlon * decimals) / decimals;
18 minlat = Math.round(minlat * decimals) / decimals;
19 maxlon = Math.round(maxlon * decimals) / decimals;
20 maxlat = Math.round(maxlat * decimals) / decimals;
23 $(".geolink").each(function (index, link) {
24 var args = getArgs(link.href);
26 if ($(link).hasClass("llz")) {
30 } else if (minlon && $(link).hasClass("bbox")) {
31 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
34 if (layers && $(link).hasClass("layers")) {
38 if (objtype && $(link).hasClass("object")) {
39 args[objtype] = objid;
42 var classes = $(link).attr("class").split(" ");
44 $(classes).each(function (index, classname) {
45 if (match = classname.match(/^minzoom([0-9]+)$/)) {
46 var minzoom = match[1];
47 var name = link.id.replace(/anchor$/, "");
49 $(link).off("click.minzoom");
51 if (zoom >= minzoom) {
52 $(link).attr("title", i18n("javascripts.site." + name + "_tooltip"));
53 $(link).removeClass("disabled");
55 $(link).on("click.minzoom", function () { alert(i18n("javascripts.site." + name + "_zoom_alert")); return false; });
56 $(link).attr("title", i18n("javascripts.site." + name + "_disabled_tooltip"));
57 $(link).addClass("disabled");
62 link.href = setArgs(link.href, args);
65 $("#shortlinkanchor").each(function () {
66 var args = getArgs(this.href);
67 var code = makeShortCode(lat, lon, zoom);
68 var prefix = shortlinkPrefix();
70 // Add ?{node,way,relation}=id to the arguments
71 if (objtype && objid) {
72 args[objtype] = objid;
75 // This is a hack to omit the default mapnik layer from the shortlink.
76 if (layers && layers != "M") {
83 // Here we're assuming that all parameters but ?layers= and
84 // ?{node,way,relation}= can be safely omitted from the shortlink
85 // which encodes lat/lon/zoom. If new URL parameters are added to
86 // the main slippy map this needs to be changed.
87 if (args.layers || args[objtype]) {
88 this.href = setArgs(prefix + "/go/" + code, args);
90 this.href = prefix + "/go/" + code;
96 * Get the URL prefix to use for a short link
98 function shortlinkPrefix() {
99 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
100 return "http://osm.org";
107 * Called to get the arguments from a URL as a hash.
109 function getArgs(url) {
111 var querystart = url.indexOf("?");
113 if (querystart >= 0) {
114 var querystring = url.substring(querystart + 1);
115 var queryitems = querystring.split("&");
117 for (var i = 0; i < queryitems.length; i++) {
118 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
119 args[unescape(match[1])] = unescape(match[2]);
121 args[unescape(queryitems[i])] = null;
130 * Called to set the arguments on a URL from the given hash.
132 function setArgs(url, args) {
136 if (args[arg] == null) {
137 queryitems.push(escape(arg));
139 queryitems.push(escape(arg) + "=" + escape(args[arg]));
143 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
147 * Called to interpolate JavaScript variables in strings using a
148 * similar syntax to rails I18n string interpolation - the only
149 * difference is that [[foo]] is the placeholder syntax instead
150 * of {{foo}} which allows the same string to be processed by both
151 * rails and then later by javascript.
153 function i18n(string, keys) {
154 string = i18n_strings[string] || string;
156 for (var key in keys) {
157 var re_key = '\\[\\[' + key + '\\]\\]';
158 var re = new RegExp(re_key, "g");
160 string = string.replace(re, keys[key]);
167 * Called to interlace the bits in x and y, making a Morton code.
169 function interlace(x, y) {
170 x = (x | (x << 8)) & 0x00ff00ff;
171 x = (x | (x << 4)) & 0x0f0f0f0f;
172 x = (x | (x << 2)) & 0x33333333;
173 x = (x | (x << 1)) & 0x55555555;
175 y = (y | (y << 8)) & 0x00ff00ff;
176 y = (y | (y << 4)) & 0x0f0f0f0f;
177 y = (y | (y << 2)) & 0x33333333;
178 y = (y | (y << 1)) & 0x55555555;
184 * Called to create a short code for the short link.
186 function makeShortCode(lat, lon, zoom) {
187 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
188 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
189 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
190 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
191 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
192 // and drops the last 4 bits of the full 64 bit Morton code.
194 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
195 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
196 digit = (c1 >> (24 - 6 * i)) & 0x3f;
197 str += char_array.charAt(digit);
199 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
200 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
201 str += char_array.charAt(digit);
203 for (var i = 0; i < ((zoom + 8) % 3); ++i) {