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 from the shortlink.
111 if (layers && layers != "M") {
112 args["layers"] = layers;
115 delete args["layers"];
118 // Here we're assuming that all parameters but ?layers= and
119 // ?{node,way,relation}= can be safely omitted from the shortlink
120 // which encodes lat/lon/zoom. If new URL parameters are added to
121 // the main slippy map this needs to be changed.
122 if (args["layers"] || args[objtype]) {
123 node.href = setArgs(prefix + "/go/" + code, args);
125 node.href = prefix + "/go/" + code;
131 * Get the URL prefix to use for a short link
133 function shortlinkPrefix() {
134 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
135 return "http://osm.org";
142 * Called to get the arguments from a URL as a hash.
144 function getArgs(url) {
145 var args = new Object();
146 var querystart = url.indexOf("?");
148 if (querystart >= 0) {
149 var querystring = url.substring(querystart + 1);
150 var queryitems = querystring.split("&");
152 for (var i = 0; i < queryitems.length; i++) {
153 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
154 args[unescape(match[1])] = unescape(match[2]);
156 args[unescape(queryitems[i])] = null
165 * Called to set the arguments on a URL from the given hash.
167 function setArgs(url, args) {
168 var queryitems = new Array();
172 if (args[arg] == null) {
173 queryitems.push(escape(arg));
175 queryitems.push(escape(arg) + "=" + escape(args[arg]));
179 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
183 * Called to get a CSS property for an element.
185 function getStyle(el, property) {
188 if (el.currentStyle) {
189 style = el.currentStyle[property];
190 } else if( window.getComputedStyle ) {
191 style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
193 style = el.style[property];
200 * Called to interpolate JavaScript variables in strings using a
201 * similar syntax to rails I18n string interpolation - the only
202 * difference is that [[foo]] is the placeholder syntax instead
203 * of {{foo}} which allows the same string to be processed by both
204 * rails and then later by javascript.
206 function i18n(string, keys) {
207 string = i18n_strings[string] || string
209 for (var key in keys) {
210 var re_key = '\\[\\[' + key + '\\]\\]';
211 var re = new RegExp(re_key, "g");
213 string = string.replace(re, keys[key]);
219 function makeShortCode(lat, lon, zoom) {
220 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@";
221 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
222 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
223 // hack around the fact that JS apparently only allows 53-bit integers?!?
224 // note that, although this reduces the accuracy of the process, it's fine for
225 // z18 so we don't need to care for now.
227 for (var i = 31; i > 16; --i) {
228 c1 = (c1 << 1) | ((x >> i) & 1);
229 c1 = (c1 << 1) | ((y >> i) & 1);
231 for (var i = 16; i > 1; --i) {
232 c2 = (c2 << 1) | ((x >> i) & 1);
233 c2 = (c2 << 1) | ((y >> i) & 1);
236 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
237 digit = (c1 >> (24 - 6 * i)) & 0x3f;
238 str += char_array.charAt(digit);
240 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
241 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
242 str += char_array.charAt(digit);
244 for (var i = 0; i < ((zoom + 8) % 3); ++i) {