]> git.openstreetmap.org Git - rails.git/blob - public/javascripts/site.js
recompile
[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.style.fontStyle = 'normal';
64     } else {
65       node.href = 'javascript:alert(i18n("javascripts.site.edit_zoom_alert"));';
66       node.style.fontStyle = 'italic';
67     }
68   }
69
70   node = document.getElementById("historyanchor");
71   if (node) {
72     if (zoom >= 11) {
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") {
79
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;
85       }
86
87       node.href = setArgs("/history", args);
88       node.style.fontStyle = 'normal';
89     } else {
90       node.href = 'javascript:alert(i18n("javascripts.site.history_zoom_alert"));';
91       node.style.fontStyle = 'italic';
92     }
93   }
94
95   node = document.getElementById("shortlinkanchor");
96   if (node) {
97     var args = getArgs(node.href);
98     var code = makeShortCode(lat, lon, zoom);
99     var prefix = shortlinkPrefix();
100
101     // Add ?{node,way,relation}=id to the arguments
102     if (objtype && objid) {
103       args[objtype] = objid;
104     }
105
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;
111     }
112     else {
113       delete args["layers"];
114     }
115
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);
122     } else {
123       node.href = prefix + "/go/" + code;
124     }
125   }
126 }
127
128 /*
129  * Get the URL prefix to use for a short link
130  */
131 function shortlinkPrefix() {
132   if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
133     return "http://osm.org";
134   } else {
135     return "";
136   }
137 }
138
139 /*
140  * Called to get the arguments from a URL as a hash.
141  */
142 function getArgs(url) {
143   var args = new Object();
144   var querystart = url.indexOf("?");
145
146   if (querystart >= 0) {
147      var querystring = url.substring(querystart + 1);
148      var queryitems = querystring.split("&");
149
150      for (var i = 0; i < queryitems.length; i++) {
151         if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
152            args[unescape(match[1])] = unescape(match[2]);
153         } else {
154            args[unescape(queryitems[i])] = null
155         }
156      }
157   }
158
159   return args;
160 }
161
162 /*
163  * Called to set the arguments on a URL from the given hash.
164  */
165 function setArgs(url, args) {
166    var queryitems = new Array();
167
168    for (arg in args)
169    {
170       if (args[arg] == null) {
171          queryitems.push(escape(arg));
172       } else {
173          queryitems.push(escape(arg) + "=" + escape(args[arg]));
174       }
175    }
176
177    return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
178 }
179
180 /*
181  * Called to get a CSS property for an element.
182  */
183 function getStyle(el, property) {
184   var style;
185
186   if (el.currentStyle) {
187     style = el.currentStyle[property];
188   } else if( window.getComputedStyle ) {
189     style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
190   } else {
191     style = el.style[property];
192   }
193
194   return style;
195 }
196
197 /*
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.
203  */
204 function i18n(string, keys) {
205   string = i18n_strings[string] || string
206
207   for (var key in keys) {
208     var re_key = '\\[\\[' + key + '\\]\\]';
209     var re = new RegExp(re_key, "g");
210
211     string = string.replace(re, keys[key]);
212   }
213
214   return string;
215 }
216
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.
224     var c1 = 0, c2 = 0;
225     for (var i = 31; i > 16; --i) {
226         c1 = (c1 << 1) | ((x >> i) & 1);
227         c1 = (c1 << 1) | ((y >> i) & 1);
228     }
229     for (var i = 16; i > 1; --i) {
230         c2 = (c2 << 1) | ((x >> i) & 1);
231         c2 = (c2 << 1) | ((y >> i) & 1);
232     }
233     var str = "";
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);
237     }
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);
241     }
242     for (var i = 0; i < ((zoom + 8) % 3); ++i) {
243         str += "-";
244     }
245     return str;
246 }