3 * Called as the user scrolls/zooms around to aniplate hrefs of the
4 * view tab and various other links
6 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
7 var decimals = Math.pow(10, Math.floor(zoom/3));
10 lat = Math.round(lat * decimals) / decimals;
11 lon = Math.round(lon * decimals) / decimals;
13 node = document.getElementById("permalinkanchor");
15 var args = getArgs(node.href);
20 args["layers"] = layers;
22 if (objtype && objid) {
23 args[objtype] = objid;
25 node.href = setArgs(node.href, args);
28 node = document.getElementById("viewanchor");
30 var args = getArgs(node.href);
35 args["layers"] = layers;
37 node.href = setArgs(node.href, args);
40 node = document.getElementById("exportanchor");
42 var args = getArgs(node.href);
47 args["layers"] = layers;
49 node.href = setArgs(node.href, args);
52 node = document.getElementById("editanchor");
55 var args = new Object();
59 if (objtype && objid) {
60 args[objtype] = objid;
62 node.href = setArgs("/edit", args);
63 node.style.fontStyle = 'normal';
65 node.href = 'javascript:alert(i18n("javascripts.site.edit_zoom_alert"));';
66 node.style.fontStyle = 'italic';
70 node = document.getElementById("historyanchor");
73 var args = new Object();
74 //set bbox param from 'extents' object
75 if (typeof minlon == "number" &&
76 typeof minlat == "number" &&
77 typeof maxlon == "number" &&
78 typeof maxlat == "number") {
80 minlon = Math.round(minlon * decimals) / decimals;
81 minlat = Math.round(minlat * decimals) / decimals;
82 maxlon = Math.round(maxlon * decimals) / decimals;
83 maxlat = Math.round(maxlat * decimals) / decimals;
84 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
87 node.href = setArgs("/history", args);
88 node.style.fontStyle = 'normal';
90 node.href = 'javascript:alert(i18n("javascripts.site.history_zoom_alert"));';
91 node.style.fontStyle = 'italic';
95 node = document.getElementById("shortlinkanchor");
97 var args = getArgs(node.href);
98 var code = makeShortCode(lat, lon, zoom);
99 var prefix = shortlinkPrefix();
101 // Add ?{node,way,relation}=id to the arguments
102 if (objtype && objid) {
103 args[objtype] = objid;
106 // This is a hack to omit the default mapnik layer (B000FTF) from
107 // the shortlink. B000FTFT is then the "Object" layer which we get
108 // on /?{node,way,relation}=id
109 if (layers && (layers != "B000FTF") && (layers != "B000FTFT")) {
110 args["layers"] = layers;
113 delete args["layers"];
116 // Here we're assuming that all parameters but ?layers= and
117 // ?{node,way,relation}= can be safely omitted from the shortlink
118 // which encodes lat/lon/zoom. If new URL parameters are added to
119 // the main slippy map this needs to be changed.
120 if (args["layers"] || args[objtype]) {
121 node.href = setArgs(prefix + "/go/" + code, args);
123 node.href = prefix + "/go/" + code;
129 * Get the URL prefix to use for a short link
131 function shortlinkPrefix() {
132 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
133 return "http://osm.org";
140 * Called to get the arguments from a URL as a hash.
142 function getArgs(url) {
143 var args = new Object();
144 var querystart = url.indexOf("?");
146 if (querystart >= 0) {
147 var querystring = url.substring(querystart + 1);
148 var queryitems = querystring.split("&");
150 for (var i = 0; i < queryitems.length; i++) {
151 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
152 args[unescape(match[1])] = unescape(match[2]);
154 args[unescape(queryitems[i])] = null
163 * Called to set the arguments on a URL from the given hash.
165 function setArgs(url, args) {
166 var queryitems = new Array();
170 if (args[arg] == null) {
171 queryitems.push(escape(arg));
173 queryitems.push(escape(arg) + "=" + escape(args[arg]));
177 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
181 * Called to get a CSS property for an element.
183 function getStyle(el, property) {
186 if (el.currentStyle) {
187 style = el.currentStyle[property];
188 } else if( window.getComputedStyle ) {
189 style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
191 style = el.style[property];
198 * Called to interpolate JavaScript variables in strings using a
199 * similar syntax to rails I18n string interpolation - the only
200 * difference is that [[foo]] is the placeholder syntax instead
201 * of {{foo}} which allows the same string to be processed by both
202 * rails and then later by javascript.
204 function i18n(string, keys) {
205 string = i18n_strings[string] || string
207 for (var key in keys) {
208 var re_key = '\\[\\[' + key + '\\]\\]';
209 var re = new RegExp(re_key, "g");
211 string = string.replace(re, keys[key]);
217 function makeShortCode(lat, lon, zoom) {
218 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@";
219 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
220 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
221 // hack around the fact that JS apparently only allows 53-bit integers?!?
222 // note that, although this reduces the accuracy of the process, it's fine for
223 // z18 so we don't need to care for now.
225 for (var i = 31; i > 16; --i) {
226 c1 = (c1 << 1) | ((x >> i) & 1);
227 c1 = (c1 << 1) | ((y >> i) & 1);
229 for (var i = 16; i > 1; --i) {
230 c2 = (c2 << 1) | ((x >> i) & 1);
231 c2 = (c2 << 1) | ((y >> i) & 1);
234 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
235 digit = (c1 >> (24 - 6 * i)) & 0x3f;
236 str += char_array.charAt(digit);
238 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
239 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
240 str += char_array.charAt(digit);
242 for (var i = 0; i < ((zoom + 8) % 3); ++i) {