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 = $("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 = $("viewanchor");
30 var args = getArgs(node.href);
35 args["layers"] = layers;
37 node.href = setArgs(node.href, args);
40 node = $("exportanchor");
42 var args = getArgs(node.href);
47 args["layers"] = layers;
49 node.href = setArgs(node.href, args);
52 node = $("editanchor");
55 var args = new Object();
59 if (objtype && objid) {
60 args[objtype] = objid;
62 node.href = setArgs("/edit", args);
63 node.title = i18n("javascripts.site.edit_tooltip");
64 node.removeClassName("disabled");
66 node.href = 'javascript:alert(i18n("javascripts.site.edit_zoom_alert"));';
67 node.title = i18n("javascripts.site.edit_disabled_tooltip");
68 node.addClassName("disabled");
72 node = $("historyanchor");
75 var args = new Object();
76 //set bbox param from 'extents' object
77 if (typeof minlon == "number" &&
78 typeof minlat == "number" &&
79 typeof maxlon == "number" &&
80 typeof maxlat == "number") {
82 minlon = Math.round(minlon * decimals) / decimals;
83 minlat = Math.round(minlat * decimals) / decimals;
84 maxlon = Math.round(maxlon * decimals) / decimals;
85 maxlat = Math.round(maxlat * decimals) / decimals;
86 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
89 node.href = setArgs("/history", args);
90 node.title = i18n("javascripts.site.history_tooltip");
91 node.removeClassName("disabled");
93 node.href = 'javascript:alert(i18n("javascripts.site.history_zoom_alert"));';
94 node.title = i18n("javascripts.site.history_disabled_tooltip");
95 node.addClassName("disabled");
99 node = $("shortlinkanchor");
101 var args = getArgs(node.href);
102 var code = makeShortCode(lat, lon, zoom);
103 var prefix = shortlinkPrefix();
105 // Add ?{node,way,relation}=id to the arguments
106 if (objtype && objid) {
107 args[objtype] = objid;
110 // This is a hack to omit the default mapnik layer (B000FTF) from
111 // the shortlink. B000FTFT is then the "Object" layer which we get
112 // on /?{node,way,relation}=id
113 if (layers && (layers != "B000FTF") && (layers != "B000FTFT")) {
114 args["layers"] = layers;
117 delete args["layers"];
120 // Here we're assuming that all parameters but ?layers= and
121 // ?{node,way,relation}= can be safely omitted from the shortlink
122 // which encodes lat/lon/zoom. If new URL parameters are added to
123 // the main slippy map this needs to be changed.
124 if (args["layers"] || args[objtype]) {
125 node.href = setArgs(prefix + "/go/" + code, args);
127 node.href = prefix + "/go/" + code;
133 * Get the URL prefix to use for a short link
135 function shortlinkPrefix() {
136 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
137 return "http://osm.org";
144 * Called to get the arguments from a URL as a hash.
146 function getArgs(url) {
147 var args = new Object();
148 var querystart = url.indexOf("?");
150 if (querystart >= 0) {
151 var querystring = url.substring(querystart + 1);
152 var queryitems = querystring.split("&");
154 for (var i = 0; i < queryitems.length; i++) {
155 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
156 args[unescape(match[1])] = unescape(match[2]);
158 args[unescape(queryitems[i])] = null
167 * Called to set the arguments on a URL from the given hash.
169 function setArgs(url, args) {
170 var queryitems = new Array();
174 if (args[arg] == null) {
175 queryitems.push(escape(arg));
177 queryitems.push(escape(arg) + "=" + escape(args[arg]));
181 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
185 * Called to get a CSS property for an element.
187 function getStyle(el, property) {
190 if (el.currentStyle) {
191 style = el.currentStyle[property];
192 } else if( window.getComputedStyle ) {
193 style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
195 style = el.style[property];
202 * Called to interpolate JavaScript variables in strings using a
203 * similar syntax to rails I18n string interpolation - the only
204 * difference is that [[foo]] is the placeholder syntax instead
205 * of {{foo}} which allows the same string to be processed by both
206 * rails and then later by javascript.
208 function i18n(string, keys) {
209 string = i18n_strings[string] || string
211 for (var key in keys) {
212 var re_key = '\\[\\[' + key + '\\]\\]';
213 var re = new RegExp(re_key, "g");
215 string = string.replace(re, keys[key]);
221 function makeShortCode(lat, lon, zoom) {
222 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@";
223 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
224 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
225 // hack around the fact that JS apparently only allows 53-bit integers?!?
226 // note that, although this reduces the accuracy of the process, it's fine for
227 // z18 so we don't need to care for now.
229 for (var i = 31; i > 16; --i) {
230 c1 = (c1 << 1) | ((x >> i) & 1);
231 c1 = (c1 << 1) | ((y >> i) & 1);
233 for (var i = 16; i > 1; --i) {
234 c2 = (c2 << 1) | ((x >> i) & 1);
235 c2 = (c2 << 1) | ((y >> i) & 1);
238 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
239 digit = (c1 >> (24 - 6 * i)) & 0x3f;
240 str += char_array.charAt(digit);
242 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
243 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
244 str += char_array.charAt(digit);
246 for (var i = 0; i < ((zoom + 8) % 3); ++i) {