]> git.openstreetmap.org Git - rails.git/blob - public/javascripts/site.js
Only Potlatch 1 needs a token to be created
[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 = $("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 = $("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 = $("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 = $("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.removeClassName("disabled");
65     } else {
66       node.href = 'javascript:alert(i18n("javascripts.site.edit_zoom_alert"));';
67       node.title = i18n("javascripts.site.edit_disabled_tooltip");
68       node.addClassName("disabled");
69     }
70   }
71
72   node = $("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.removeClassName("disabled");
92     } else {
93       node.href = 'javascript:alert(i18n("javascripts.site.history_zoom_alert"));';
94       node.title = i18n("javascripts.site.history_disabled_tooltip");
95       node.addClassName("disabled");
96     }
97   }
98
99   node = $("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 from the shortlink.
111     if (layers && layers != "M") {
112       args["layers"] = layers;
113     }
114     else {
115       delete args["layers"];
116     }
117
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);
124     } else {
125       node.href = prefix + "/go/" + code;
126     }
127   }
128 }
129
130 /*
131  * Get the URL prefix to use for a short link
132  */
133 function shortlinkPrefix() {
134   if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
135     return "http://osm.org";
136   } else {
137     return "";
138   }
139 }
140
141 /*
142  * Called to get the arguments from a URL as a hash.
143  */
144 function getArgs(url) {
145   var args = new Object();
146   var querystart = url.indexOf("?");
147
148   if (querystart >= 0) {
149      var querystring = url.substring(querystart + 1);
150      var queryitems = querystring.split("&");
151
152      for (var i = 0; i < queryitems.length; i++) {
153         if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
154            args[unescape(match[1])] = unescape(match[2]);
155         } else {
156            args[unescape(queryitems[i])] = null
157         }
158      }
159   }
160
161   return args;
162 }
163
164 /*
165  * Called to set the arguments on a URL from the given hash.
166  */
167 function setArgs(url, args) {
168    var queryitems = new Array();
169
170    for (arg in args)
171    {
172       if (args[arg] == null) {
173          queryitems.push(escape(arg));
174       } else {
175          queryitems.push(escape(arg) + "=" + escape(args[arg]));
176       }
177    }
178
179    return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
180 }
181
182 /*
183  * Called to get a CSS property for an element.
184  */
185 function getStyle(el, property) {
186   var style;
187
188   if (el.currentStyle) {
189     style = el.currentStyle[property];
190   } else if( window.getComputedStyle ) {
191     style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
192   } else {
193     style = el.style[property];
194   }
195
196   return style;
197 }
198
199 /*
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.
205  */
206 function i18n(string, keys) {
207   string = i18n_strings[string] || string
208
209   for (var key in keys) {
210     var re_key = '\\[\\[' + key + '\\]\\]';
211     var re = new RegExp(re_key, "g");
212
213     string = string.replace(re, keys[key]);
214   }
215
216   return string;
217 }
218
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.
226     var c1 = 0, c2 = 0;
227     for (var i = 31; i > 16; --i) {
228         c1 = (c1 << 1) | ((x >> i) & 1);
229         c1 = (c1 << 1) | ((y >> i) & 1);
230     }
231     for (var i = 16; i > 1; --i) {
232         c2 = (c2 << 1) | ((x >> i) & 1);
233         c2 = (c2 << 1) | ((y >> i) & 1);
234     }
235     var str = "";
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);
239     }
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);
243     }
244     for (var i = 0; i < ((zoom + 8) % 3); ++i) {
245         str += "-";
246     }
247     return str;
248 }