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;
12 node = document.getElementById("permalinkanchor");
14 var args = getArgs(node.href);
19 args["layers"] = layers;
21 if (objtype && objid) {
22 args[objtype] = objid;
24 node.href = setArgs(node.href, args);
27 node = document.getElementById("viewanchor");
29 var args = getArgs(node.href);
34 args["layers"] = layers;
36 node.href = setArgs(node.href, args);
39 node = document.getElementById("exportanchor");
41 var args = getArgs(node.href);
46 args["layers"] = layers;
48 node.href = setArgs(node.href, args);
51 node = document.getElementById("editanchor");
54 var args = new Object();
58 if (objtype && objid) {
59 args[objtype] = objid;
61 node.href = setArgs("/edit", args);
62 node.style.fontStyle = 'normal';
64 node.href = 'javascript:alert("zoom in to edit map");';
65 node.style.fontStyle = 'italic';
69 node = document.getElementById("historyanchor");
72 var args = new Object();
73 //set bbox param from 'extents' object
74 if (typeof minlon == "number" &&
75 typeof minlat == "number" &&
76 typeof maxlon == "number" &&
77 typeof maxlat == "number") {
79 minlon = Math.round(minlon * decimals) / decimals;
80 minlat = Math.round(minlat * decimals) / decimals;
81 maxlon = Math.round(maxlon * decimals) / decimals;
82 maxlat = Math.round(maxlat * decimals) / decimals;
83 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
86 node.href = setArgs("/history", args);
87 node.style.fontStyle = 'normal';
89 node.href = 'javascript:alert("zoom in to see editing history");';
90 node.style.fontStyle = 'italic';
94 node = document.getElementById("shortlinkanchor");
96 var args = getArgs(node.href);
97 var code = makeShortCode(lat, lon, zoom);
98 var prefix = shortlinkPrefix();
100 // Add ?{node,way,relation}=id to the arguments
101 if (objtype && objid) {
102 args[objtype] = objid;
105 // This is a hack to omit the default mapnik layer (B000FTF) from
106 // the shortlink. B000FTFT is then the "Object" layer which we get
107 // on /?{node,way,relation}=id
108 if (layers && (layers != "B000FTF") && (layers != "B000FTFT")) {
109 args["layers"] = layers;
112 delete args["layers"];
115 // Here we're assuming that all parameters but ?layers= and
116 // ?{node,way,relation}= can be safely omitted from the shortlink
117 // which encodes lat/lon/zoom. If new URL parameters are added to
118 // the main slippy map this needs to be changed.
119 if (args["layers"] || args[objtype]) {
120 node.href = setArgs(prefix + "/go/" + code, args);
122 node.href = prefix + "/go/" + code;
128 * Get the URL prefix to use for a short link
130 function shortlinkPrefix() {
131 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
132 return "http://osm.org";
139 * Called to get the arguments from a URL as a hash.
141 function getArgs(url) {
142 var args = new Object();
143 var querystart = url.indexOf("?");
145 if (querystart >= 0) {
146 var querystring = url.substring(querystart + 1);
147 var queryitems = querystring.split("&");
149 for (var i = 0; i < queryitems.length; i++) {
150 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
151 args[unescape(match[1])] = unescape(match[2]);
153 args[unescape(queryitems[i])] = null
162 * Called to set the arguments on a URL from the given hash.
164 function setArgs(url, args) {
165 var queryitems = new Array();
169 if (args[arg] == null) {
170 queryitems.push(escape(arg));
172 queryitems.push(escape(arg) + "=" + escape(args[arg]));
176 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
180 * Called to get a CSS property for an element.
182 function getStyle(el, property) {
185 if (el.currentStyle) {
186 style = el.currentStyle[property];
187 } else if( window.getComputedStyle ) {
188 style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
190 style = el.style[property];
197 * Called to interpolate JavaScript variables in strings using a
198 * similar syntax to rails I18n string interpolation - the only
199 * difference is that [[foo]] is the placeholder syntax instead
200 * of {{foo}} which allows the same string to be processed by both
201 * rails and then later by javascript.
203 function i18n(string, keys) {
204 for (var key in keys) {
205 var re_key = '\\[\\[' + key + '\\]\\]';
206 var re = new RegExp(re_key, "g");
208 string = string.replace(re, keys[key]);
214 function makeShortCode(lat, lon, zoom) {
215 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@";
216 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
217 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
218 // hack around the fact that JS apparently only allows 53-bit integers?!?
219 // note that, although this reduces the accuracy of the process, it's fine for
220 // z18 so we don't need to care for now.
222 for (var i = 31; i > 16; --i) {
223 c1 = (c1 << 1) | ((x >> i) & 1);
224 c1 = (c1 << 1) | ((y >> i) & 1);
226 for (var i = 16; i > 1; --i) {
227 c2 = (c2 << 1) | ((x >> i) & 1);
228 c2 = (c2 << 1) | ((y >> i) & 1);
231 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
232 digit = (c1 >> (24 - 6 * i)) & 0x3f;
233 str += char_array.charAt(digit);
235 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
236 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
237 str += char_array.charAt(digit);
239 for (var i = 0; i < ((zoom + 8) % 3); ++i) {