]> git.openstreetmap.org Git - rails.git/blob - public/javascripts/site.js
c17500f571701c9ebb5fff2ec317db5bf7eba09a
[rails.git] / public / javascripts / site.js
1
2 /*
3  * Called as the user scrolls/zooms around to aniplate hrefs of the
4  * view tab and various other links
5  */
6 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
7   var decimals = Math.pow(10, Math.floor(zoom/3));
8   var node;
9
10   lat = Math.round(lat * decimals) / decimals;
11   lon = Math.round(lon * decimals) / decimals;
12
13   node = document.getElementById("permalinkanchor");
14   if (node) {
15     var args = getArgs(node.href);
16     args["lat"] = lat;
17     args["lon"] = lon;
18     args["zoom"] = zoom;
19     if (layers) {
20       args["layers"] = layers;
21     }
22     if (objtype && objid) {
23       args[objtype] = objid;
24     }
25     node.href = setArgs(node.href, args);
26   }
27
28   node = document.getElementById("viewanchor");
29   if (node) {
30     var args = getArgs(node.href);
31     args["lat"] = lat;
32     args["lon"] = lon;
33     args["zoom"] = zoom;
34     if (layers) {
35       args["layers"] = layers;
36     }
37     node.href = setArgs(node.href, args);
38   }
39
40   node = document.getElementById("exportanchor");
41   if (node) {
42     var args = getArgs(node.href);
43     args["lat"] = lat;
44     args["lon"] = lon;
45     args["zoom"] = zoom;
46     if (layers) {
47       args["layers"] = layers;
48     }
49     node.href = setArgs(node.href, args);
50   }
51
52   node = document.getElementById("editanchor");
53   if (node) {
54     if (zoom >= 13) {
55       var args = new Object();
56       args.lat = lat;
57       args.lon = lon;
58       args.zoom = zoom;
59       if (objtype && objid) {
60         args[objtype] = objid;
61       }
62       node.href = setArgs("/edit", args);
63       node.title = i18n("javascripts.site.edit_tooltip");
64       node.style.fontStyle = 'normal';
65     } else {
66       node.href = 'javascript:alert(i18n("javascripts.site.edit_zoom_alert"));';
67       node.title = i18n("javascripts.site.edit_disabled_tooltip");
68       node.style.fontStyle = 'italic';
69     }
70   }
71
72   node = document.getElementById("historyanchor");
73   if (node) {
74     if (zoom >= 11) {
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") {
81
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;
87       }
88
89       node.href = setArgs("/history", args);
90       node.title = i18n("javascripts.site.history_tooltip");
91       node.style.fontStyle = 'normal';
92     } else {
93       node.href = 'javascript:alert(i18n("javascripts.site.history_zoom_alert"));';
94       node.title = i18n("javascripts.site.history_disabled_tooltip");
95       node.style.fontStyle = 'italic';
96     }
97   }
98
99   node = document.getElementById("shortlinkanchor");
100   if (node) {
101     var args = getArgs(node.href);
102     var code = makeShortCode(lat, lon, zoom);
103     var prefix = shortlinkPrefix();
104
105     // Add ?{node,way,relation}=id to the arguments
106     if (objtype && objid) {
107       args[objtype] = objid;
108     }
109
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;
115     }
116     else {
117       delete args["layers"];
118     }
119
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);
126     } else {
127       node.href = prefix + "/go/" + code;
128     }
129   }
130 }
131
132 /*
133  * Get the URL prefix to use for a short link
134  */
135 function shortlinkPrefix() {
136   if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
137     return "http://osm.org";
138   } else {
139     return "";
140   }
141 }
142
143 /*
144  * Called to get the arguments from a URL as a hash.
145  */
146 function getArgs(url) {
147   var args = new Object();
148   var querystart = url.indexOf("?");
149
150   if (querystart >= 0) {
151      var querystring = url.substring(querystart + 1);
152      var queryitems = querystring.split("&");
153
154      for (var i = 0; i < queryitems.length; i++) {
155         if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
156            args[unescape(match[1])] = unescape(match[2]);
157         } else {
158            args[unescape(queryitems[i])] = null
159         }
160      }
161   }
162
163   return args;
164 }
165
166 /*
167  * Called to set the arguments on a URL from the given hash.
168  */
169 function setArgs(url, args) {
170    var queryitems = new Array();
171
172    for (arg in args)
173    {
174       if (args[arg] == null) {
175          queryitems.push(escape(arg));
176       } else {
177          queryitems.push(escape(arg) + "=" + escape(args[arg]));
178       }
179    }
180
181    return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
182 }
183
184 /*
185  * Called to get a CSS property for an element.
186  */
187 function getStyle(el, property) {
188   var style;
189
190   if (el.currentStyle) {
191     style = el.currentStyle[property];
192   } else if( window.getComputedStyle ) {
193     style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
194   } else {
195     style = el.style[property];
196   }
197
198   return style;
199 }
200
201 /*
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.
207  */
208 function i18n(string, keys) {
209   string = i18n_strings[string] || string
210
211   for (var key in keys) {
212     var re_key = '\\[\\[' + key + '\\]\\]';
213     var re = new RegExp(re_key, "g");
214
215     string = string.replace(re, keys[key]);
216   }
217
218   return string;
219 }
220
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.
228     var c1 = 0, c2 = 0;
229     for (var i = 31; i > 16; --i) {
230         c1 = (c1 << 1) | ((x >> i) & 1);
231         c1 = (c1 << 1) | ((y >> i) & 1);
232     }
233     for (var i = 16; i > 1; --i) {
234         c2 = (c2 << 1) | ((x >> i) & 1);
235         c2 = (c2 << 1) | ((y >> i) & 1);
236     }
237     var str = "";
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);
241     }
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);
245     }
246     for (var i = 0; i < ((zoom + 8) % 3); ++i) {
247         str += "-";
248     }
249     return str;
250 }