]> git.openstreetmap.org Git - rails.git/blob - public/javascripts/site.js
23ea3bc6839acf7bffe4226e7dce207454942001
[rails.git] / public / javascripts / site.js
1 /*
2  * Called as the user scrolls/zooms around to aniplate hrefs of the
3  * view tab and various other links
4  */
5 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat) {
6   var decimals = Math.pow(10, Math.floor(zoom/3));
7   var node;
8
9   lat = Math.round(lat * decimals) / decimals;
10   lon = Math.round(lon * decimals) / decimals;
11
12   node = document.getElementById("permalinkanchor");
13   if (node) {
14     var args = getArgs(node.href);
15     args["lat"] = lat;
16     args["lon"] = lon;
17     args["zoom"] = zoom;
18     if (layers) {
19       args["layers"] = layers;
20     }
21     node.href = setArgs(node.href, args);
22   }
23
24   node = document.getElementById("viewanchor");
25   if (node) {
26     var args = getArgs(node.href);
27     args["lat"] = lat;
28     args["lon"] = lon;
29     args["zoom"] = zoom;
30     if (layers) {
31       args["layers"] = layers;
32     }
33     node.href = setArgs(node.href, args);
34   }
35
36   node = document.getElementById("exportanchor");
37   if (node) {
38     var args = getArgs(node.href);
39     args["lat"] = lat;
40     args["lon"] = lon;
41     args["zoom"] = zoom;
42     if (layers) {
43       args["layers"] = layers;
44     }
45     node.href = setArgs(node.href, args);
46   }
47
48   node = document.getElementById("editanchor");
49   if (node) {
50     if (zoom >= 13) {
51       var args = new Object();
52       args.lat = lat;
53       args.lon = lon;
54       args.zoom = zoom;
55       node.href = setArgs("/edit", args);
56       node.style.fontStyle = 'normal';
57     } else {
58       node.href = 'javascript:alert("zoom in to edit map");';
59       node.style.fontStyle = 'italic';
60     }
61   }
62   
63   node = document.getElementById("historyanchor");
64   if (node) {
65     if (zoom >= 11) {
66       var args = new Object();
67       //set bbox param from 'extents' object
68       if (typeof minlon == "number" &&
69           typeof minlat == "number" &&
70           typeof maxlon == "number" &&
71           typeof maxlat == "number") {
72       
73         minlon = Math.round(minlon * decimals) / decimals;
74         minlat = Math.round(minlat * decimals) / decimals;
75         maxlon = Math.round(maxlon * decimals) / decimals;
76         maxlat = Math.round(maxlat * decimals) / decimals;
77         args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
78       }
79       
80       node.href = setArgs("/history", args);
81       node.style.fontStyle = 'normal';
82     } else {
83       node.href = 'javascript:alert("zoom in to see editing history");';
84       node.style.fontStyle = 'italic';
85     }
86   }
87
88   node = document.getElementById("shortlinkanchor");
89   if (node) {
90     var args = getArgs(node.href);
91     var code = makeShortCode(lat, lon, zoom);
92     // little hack. may the gods of hardcoding please forgive me, or 
93     // show me the Right way to do it.
94     if (layers && (layers != "B000FTF")) {
95       args["layers"] = layers;
96       node.href = setArgs("/go/" + code, args);
97     } else {
98       node.href = "/go/" + code;
99     }
100   }
101 }
102
103 function getArgs(url) {
104   var args = new Object();
105   var querystart = url.indexOf("?");
106
107   if (querystart >= 0) {
108      var querystring = url.substring(querystart + 1);
109      var queryitems = querystring.split("&");
110
111      for (var i = 0; i < queryitems.length; i++) {
112         if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
113            args[unescape(match[1])] = unescape(match[2]);
114         } else {
115            args[unescape(queryitems[i])] = null
116         }
117      }
118   }
119
120   return args;
121 }
122
123 /*
124  * Called to set the arguments on a URL from the given hash.
125  */
126 function setArgs(url, args) {
127    var queryitems = new Array();
128
129    for (arg in args)
130    {
131       if (args[arg] == null) {
132          queryitems.push(escape(arg));
133       } else {
134          queryitems.push(escape(arg) + "=" + escape(args[arg]));
135       }
136    }
137
138    return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
139 }
140
141 /*
142  * Called to get the arguments from a URL as a hash.
143  */
144 function getStyle(el, property) {
145   var style;
146
147   if (el.currentStyle) {
148     style = el.currentStyle[property];
149   } else if( window.getComputedStyle ) {
150     style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
151   } else {
152     style = el.style[property];
153   }
154
155   return style;
156 }
157
158 /*
159  * Called to interpolate JavaScript variables in strings using a
160  * similar syntax to rails I18n string interpolation - the only
161  * difference is that [[foo]] is the placeholder syntax instead
162  * of {{foo}} which allows the same string to be processed by both
163  * rails and then later by javascript.
164  */
165 function i18n(string, keys) {
166   for (var key in keys) {
167     var re_key = '\\[\\[' + key + '\\]\\]';
168     var re = new RegExp(re_key, "g");
169       
170     string = string.replace(re, keys[key]);
171   }
172    
173   return string;
174
175
176 function makeShortCode(lat, lon, zoom) {
177     char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@";
178     var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
179     var y = Math.round((lat +  90.0) * ((1 << 30) / 45.0));
180     // hack around the fact that JS apparently only allows 53-bit integers?!?
181     // note that, although this reduces the accuracy of the process, it's fine for
182     // z18 so we don't need to care for now.
183     var c1 = 0, c2 = 0;
184     for (var i = 31; i > 16; --i) {
185         c1 = (c1 << 1) | ((x >> i) & 1);
186         c1 = (c1 << 1) | ((y >> i) & 1);
187     }
188     for (var i = 16; i > 1; --i) {
189         c2 = (c2 << 1) | ((x >> i) & 1);
190         c2 = (c2 << 1) | ((y >> i) & 1);
191     }
192     var str = "";
193     for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
194         digit = (c1 >> (24 - 6 * i)) & 0x3f;
195         str += char_array.charAt(digit);
196     }
197     for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
198         digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
199         str += char_array.charAt(digit);
200     }
201     for (var i = 0; i < ((zoom + 8) % 3); ++i) {
202         str += "=";
203     }
204     return str;
205 }