2 * Called as the user scrolls/zooms around to aniplate hrefs of the
3 * view tab and various other links
5 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
6 var decimals = Math.pow(10, Math.floor(zoom/3));
9 lat = Math.round(lat * decimals) / decimals;
10 lon = Math.round(lon * decimals) / decimals;
13 minlon = Math.round(minlon * decimals) / decimals;
14 minlat = Math.round(minlat * decimals) / decimals;
15 maxlon = Math.round(maxlon * decimals) / decimals;
16 maxlat = Math.round(maxlat * decimals) / decimals;
19 $$(".geolink").each(function (link) {
20 var args = getArgs(link.href);
22 if (link.hasClassName("llz")) {
26 } else if (minlon && link.hasClassName("bbox")) {
27 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
30 if (layers && link.hasClassName("layers")) {
34 if (objtype && link.hasClassName("object")) {
35 args[objtype] = objid;
38 if (link.hasClassName("minzoom[0-9]+")) {
39 $w(link.className).each(function (classname) {
40 if (match = classname.match(/^minzoom([0-9]+)$/)) {
41 var minzoom = match[1];
42 var name = link.id.replace(/anchor$/, "");
44 if (zoom >= minzoom) {
46 link.title = i18n("javascripts.site." + name + "_tooltip");
47 link.removeClassName("disabled");
49 link.onclick = function () { alert(i18n("javascripts.site." + name + "_zoom_alert")); return false; };
50 link.title = i18n("javascripts.site." + name + "_disabled_tooltip");
51 link.addClassName("disabled");
57 link.href = setArgs(link.href, args);
60 node = $("shortlinkanchor");
62 var args = getArgs(node.href);
63 var code = makeShortCode(lat, lon, zoom);
64 var prefix = shortlinkPrefix();
66 // Add ?{node,way,relation}=id to the arguments
67 if (objtype && objid) {
68 args[objtype] = objid;
71 // This is a hack to omit the default mapnik layer from the shortlink.
72 if (layers && layers != "M") {
73 args["layers"] = layers;
76 delete args["layers"];
79 // Here we're assuming that all parameters but ?layers= and
80 // ?{node,way,relation}= can be safely omitted from the shortlink
81 // which encodes lat/lon/zoom. If new URL parameters are added to
82 // the main slippy map this needs to be changed.
83 if (args["layers"] || args[objtype]) {
84 node.href = setArgs(prefix + "/go/" + code, args);
86 node.href = prefix + "/go/" + code;
92 * Get the URL prefix to use for a short link
94 function shortlinkPrefix() {
95 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
96 return "http://osm.org";
103 * Called to get the arguments from a URL as a hash.
105 function getArgs(url) {
106 var args = new Object();
107 var querystart = url.indexOf("?");
109 if (querystart >= 0) {
110 var querystring = url.substring(querystart + 1);
111 var queryitems = querystring.split("&");
113 for (var i = 0; i < queryitems.length; i++) {
114 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
115 args[unescape(match[1])] = unescape(match[2]);
117 args[unescape(queryitems[i])] = null
126 * Called to set the arguments on a URL from the given hash.
128 function setArgs(url, args) {
129 var queryitems = new Array();
133 if (args[arg] == null) {
134 queryitems.push(escape(arg));
136 queryitems.push(escape(arg) + "=" + escape(args[arg]));
140 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
144 * Called to get a CSS property for an element.
146 function getStyle(el, property) {
149 if (el.currentStyle) {
150 style = el.currentStyle[property];
151 } else if( window.getComputedStyle ) {
152 style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
154 style = el.style[property];
161 * Called to interpolate JavaScript variables in strings using a
162 * similar syntax to rails I18n string interpolation - the only
163 * difference is that [[foo]] is the placeholder syntax instead
164 * of {{foo}} which allows the same string to be processed by both
165 * rails and then later by javascript.
167 function i18n(string, keys) {
168 string = i18n_strings[string] || string
170 for (var key in keys) {
171 var re_key = '\\[\\[' + key + '\\]\\]';
172 var re = new RegExp(re_key, "g");
174 string = string.replace(re, keys[key]);
181 * Called to interlace the bits in x and y, making a Morton code.
183 function interlace(x, y) {
184 x = (x | (x << 8)) & 0x00ff00ff;
185 x = (x | (x << 4)) & 0x0f0f0f0f;
186 x = (x | (x << 2)) & 0x33333333;
187 x = (x | (x << 1)) & 0x55555555;
189 y = (y | (y << 8)) & 0x00ff00ff;
190 y = (y | (y << 4)) & 0x0f0f0f0f;
191 y = (y | (y << 2)) & 0x33333333;
192 y = (y | (y << 1)) & 0x55555555;
198 * Called to create a short code for the short link.
200 function makeShortCode(lat, lon, zoom) {
201 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@";
202 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
203 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
204 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
205 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
206 // and drops the last 4 bits of the full 64 bit Morton code.
208 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
209 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
210 digit = (c1 >> (24 - 6 * i)) & 0x3f;
211 str += char_array.charAt(digit);
213 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
214 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
215 str += char_array.charAt(digit);
217 for (var i = 0; i < ((zoom + 8) % 3); ++i) {