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 node.href = setArgs("/edit", args);
59 node.style.fontStyle = 'normal';
61 node.href = 'javascript:alert("zoom in to edit map");';
62 node.style.fontStyle = 'italic';
66 node = document.getElementById("historyanchor");
69 var args = new Object();
70 //set bbox param from 'extents' object
71 if (typeof minlon == "number" &&
72 typeof minlat == "number" &&
73 typeof maxlon == "number" &&
74 typeof maxlat == "number") {
76 minlon = Math.round(minlon * decimals) / decimals;
77 minlat = Math.round(minlat * decimals) / decimals;
78 maxlon = Math.round(maxlon * decimals) / decimals;
79 maxlat = Math.round(maxlat * decimals) / decimals;
80 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
83 node.href = setArgs("/history", args);
84 node.style.fontStyle = 'normal';
86 node.href = 'javascript:alert("zoom in to see editing history");';
87 node.style.fontStyle = 'italic';
91 node = document.getElementById("shortlinkanchor");
93 var args = getArgs(node.href);
94 var code = makeShortCode(lat, lon, zoom);
95 var prefix = shortlinkPrefix();
97 // Add ?{node,way,relation}=id to the arguments
98 if (objtype && objid) {
99 args[objtype] = objid;
102 // little hack. may the gods of hardcoding please forgive me, or
103 // show me the Right way to do it.
104 if (layers && (layers != "B000FTF")) {
105 args["layers"] = layers;
106 node.href = setArgs(prefix + "/go/" + code, args);
108 node.href = prefix + "/go/" + code;
114 * Get the URL prefix to use for a short link
116 function shortlinkPrefix() {
117 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
118 return "http://osm.org";
125 * Called to get the arguments from a URL as a hash.
127 function getArgs(url) {
128 var args = new Object();
129 var querystart = url.indexOf("?");
131 if (querystart >= 0) {
132 var querystring = url.substring(querystart + 1);
133 var queryitems = querystring.split("&");
135 for (var i = 0; i < queryitems.length; i++) {
136 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
137 args[unescape(match[1])] = unescape(match[2]);
139 args[unescape(queryitems[i])] = null
148 * Called to set the arguments on a URL from the given hash.
150 function setArgs(url, args) {
151 var queryitems = new Array();
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 get a CSS property for an element.
168 function getStyle(el, property) {
171 if (el.currentStyle) {
172 style = el.currentStyle[property];
173 } else if( window.getComputedStyle ) {
174 style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
176 style = el.style[property];
183 * Called to interpolate JavaScript variables in strings using a
184 * similar syntax to rails I18n string interpolation - the only
185 * difference is that [[foo]] is the placeholder syntax instead
186 * of {{foo}} which allows the same string to be processed by both
187 * rails and then later by javascript.
189 function i18n(string, keys) {
190 for (var key in keys) {
191 var re_key = '\\[\\[' + key + '\\]\\]';
192 var re = new RegExp(re_key, "g");
194 string = string.replace(re, keys[key]);
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 // hack around the fact that JS apparently only allows 53-bit integers?!?
205 // note that, although this reduces the accuracy of the process, it's fine for
206 // z18 so we don't need to care for now.
208 for (var i = 31; i > 16; --i) {
209 c1 = (c1 << 1) | ((x >> i) & 1);
210 c1 = (c1 << 1) | ((y >> i) & 1);
212 for (var i = 16; i > 1; --i) {
213 c2 = (c2 << 1) | ((x >> i) & 1);
214 c2 = (c2 << 1) | ((y >> i) & 1);
217 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
218 digit = (c1 >> (24 - 6 * i)) & 0x3f;
219 str += char_array.charAt(digit);
221 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
222 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
223 str += char_array.charAt(digit);
225 for (var i = 0; i < ((zoom + 8) % 3); ++i) {