]> git.openstreetmap.org Git - rails.git/blob - public/javascripts/site.js
59999b8c022bf5dff709ff04ebfd4243f9f19d3a
[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,objtype,objid) {
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     if (objtype && objid) {
22       args[objtype] = objid;
23     }
24     node.href = setArgs(node.href, args);
25   }
26
27   node = document.getElementById("viewanchor");
28   if (node) {
29     var args = getArgs(node.href);
30     args["lat"] = lat;
31     args["lon"] = lon;
32     args["zoom"] = zoom;
33     if (layers) {
34       args["layers"] = layers;
35     }
36     node.href = setArgs(node.href, args);
37   }
38
39   node = document.getElementById("exportanchor");
40   if (node) {
41     var args = getArgs(node.href);
42     args["lat"] = lat;
43     args["lon"] = lon;
44     args["zoom"] = zoom;
45     if (layers) {
46       args["layers"] = layers;
47     }
48     node.href = setArgs(node.href, args);
49   }
50
51   node = document.getElementById("editanchor");
52   if (node) {
53     if (zoom >= 13) {
54       var args = new Object();
55       args.lat = lat;
56       args.lon = lon;
57       args.zoom = zoom;
58       node.href = setArgs("/edit", args);
59       node.style.fontStyle = 'normal';
60     } else {
61       node.href = 'javascript:alert("zoom in to edit map");';
62       node.style.fontStyle = 'italic';
63     }
64   }
65   
66   node = document.getElementById("historyanchor");
67   if (node) {
68     if (zoom >= 11) {
69       var args = new Object();
70       //set bbox param from 'extents' object
71       if (typeof minlon == "number" &&
72           typeof minlat == "number" &&
73           typeof maxlon == "number" &&
74           typeof maxlat == "number") {
75       
76         minlon = Math.round(minlon * decimals) / decimals;
77         minlat = Math.round(minlat * decimals) / decimals;
78         maxlon = Math.round(maxlon * decimals) / decimals;
79         maxlat = Math.round(maxlat * decimals) / decimals;
80         args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
81       }
82       
83       node.href = setArgs("/history", args);
84       node.style.fontStyle = 'normal';
85     } else {
86       node.href = 'javascript:alert("zoom in to see editing history");';
87       node.style.fontStyle = 'italic';
88     }
89   }
90
91   node = document.getElementById("shortlinkanchor");
92   if (node) {
93     var args = getArgs(node.href);
94     var code = makeShortCode(lat, lon, zoom);
95     var prefix = shortlinkPrefix();
96
97     // Add ?{node,way,relation}=id to the arguments
98     if (objtype && objid) {
99       args[objtype] = objid;
100     }
101
102     // This is a hack to omit the default mapnik layer (B000FTF) from
103     // the shortlink. B000FTFT is then the "Object" layer which we get
104     // on /?{node,way,relation}=id
105     if (layers && (layers != "B000FTF") && (layers != "B000FTFT")) {
106       args["layers"] = layers;
107     }
108
109     // Here we're assuming that all parameters but ?layers= and
110     // ?{node,way,relation}= can be safely omitted from the shortlink
111     // which encodes lat/lon/zoom. If new URL parameters are added to
112     // the main slippy map this needs to be changed.
113     if (args["layers"] || args[objtype]) {
114       node.href = setArgs(prefix + "/go/" + code, args);
115     } else {
116       node.href = prefix + "/go/" + code;
117     }
118   }
119 }
120
121 /*
122  * Get the URL prefix to use for a short link
123  */
124 function shortlinkPrefix() {
125   if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
126     return "http://osm.org";
127   } else {
128     return "";     
129   }
130 }
131
132 /*
133  * Called to get the arguments from a URL as a hash.
134  */
135 function getArgs(url) {
136   var args = new Object();
137   var querystart = url.indexOf("?");
138
139   if (querystart >= 0) {
140      var querystring = url.substring(querystart + 1);
141      var queryitems = querystring.split("&");
142
143      for (var i = 0; i < queryitems.length; i++) {
144         if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
145            args[unescape(match[1])] = unescape(match[2]);
146         } else {
147            args[unescape(queryitems[i])] = null
148         }
149      }
150   }
151
152   return args;
153 }
154
155 /*
156  * Called to set the arguments on a URL from the given hash.
157  */
158 function setArgs(url, args) {
159    var queryitems = new Array();
160
161    for (arg in args)
162    {
163       if (args[arg] == null) {
164          queryitems.push(escape(arg));
165       } else {
166          queryitems.push(escape(arg) + "=" + escape(args[arg]));
167       }
168    }
169
170    return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
171 }
172
173 /*
174  * Called to get a CSS property for an element.
175  */
176 function getStyle(el, property) {
177   var style;
178
179   if (el.currentStyle) {
180     style = el.currentStyle[property];
181   } else if( window.getComputedStyle ) {
182     style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
183   } else {
184     style = el.style[property];
185   }
186
187   return style;
188 }
189
190 /*
191  * Called to interpolate JavaScript variables in strings using a
192  * similar syntax to rails I18n string interpolation - the only
193  * difference is that [[foo]] is the placeholder syntax instead
194  * of {{foo}} which allows the same string to be processed by both
195  * rails and then later by javascript.
196  */
197 function i18n(string, keys) {
198   for (var key in keys) {
199     var re_key = '\\[\\[' + key + '\\]\\]';
200     var re = new RegExp(re_key, "g");
201       
202     string = string.replace(re, keys[key]);
203   }
204    
205   return string;
206
207
208 function makeShortCode(lat, lon, zoom) {
209     char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@";
210     var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
211     var y = Math.round((lat +  90.0) * ((1 << 30) / 45.0));
212     // hack around the fact that JS apparently only allows 53-bit integers?!?
213     // note that, although this reduces the accuracy of the process, it's fine for
214     // z18 so we don't need to care for now.
215     var c1 = 0, c2 = 0;
216     for (var i = 31; i > 16; --i) {
217         c1 = (c1 << 1) | ((x >> i) & 1);
218         c1 = (c1 << 1) | ((y >> i) & 1);
219     }
220     for (var i = 16; i > 1; --i) {
221         c2 = (c2 << 1) | ((x >> i) & 1);
222         c2 = (c2 << 1) | ((y >> i) & 1);
223     }
224     var str = "";
225     for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
226         digit = (c1 >> (24 - 6 * i)) & 0x3f;
227         str += char_array.charAt(digit);
228     }
229     for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
230         digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
231         str += char_array.charAt(digit);
232     }
233     for (var i = 0; i < ((zoom + 8) % 3); ++i) {
234         str += "-";
235     }
236     return str;
237 }