]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/site.js
Handle openlayers.js as a template
[rails.git] / app / assets / javascripts / site.js
1 //= require prototype
2 //= require prototype_ujs
3 //= require effects
4 //= require dragdrop
5 //= require controls
6
7 /*
8  * Called as the user scrolls/zooms around to aniplate hrefs of the
9  * view tab and various other links
10  */
11 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
12   var decimals = Math.pow(10, Math.floor(zoom/3));
13   var node;
14
15   lat = Math.round(lat * decimals) / decimals;
16   lon = Math.round(lon * decimals) / decimals;
17
18   if (minlon) {
19     minlon = Math.round(minlon * decimals) / decimals;
20     minlat = Math.round(minlat * decimals) / decimals;
21     maxlon = Math.round(maxlon * decimals) / decimals;
22     maxlat = Math.round(maxlat * decimals) / decimals;
23   }
24
25   $$(".geolink").each(function (link) {
26     var args = getArgs(link.href);
27
28     if (link.hasClassName("llz")) {
29       args.lat = lat;
30       args.lon = lon;
31       args.zoom = zoom;
32     } else if (minlon && link.hasClassName("bbox")) {
33       args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
34     }
35
36     if (layers && link.hasClassName("layers")) {
37       args.layers = layers;
38     }
39
40     if (objtype && link.hasClassName("object")) {
41       args[objtype] = objid;
42     }
43
44     if (link.hasClassName("minzoom[0-9]+")) {
45       $w(link.className).each(function (classname) {
46         if (match = classname.match(/^minzoom([0-9]+)$/)) {
47           var minzoom = match[1];
48           var name = link.id.replace(/anchor$/, "");
49
50           if (zoom >= minzoom) {
51             link.onclick = null;
52             link.title = i18n("javascripts.site." + name + "_tooltip");
53             link.removeClassName("disabled");
54           } else {
55             link.onclick = function () { alert(i18n("javascripts.site." + name + "_zoom_alert")); return false; };
56             link.title = i18n("javascripts.site." + name + "_disabled_tooltip");
57             link.addClassName("disabled");
58           }
59         }
60       });
61     }
62
63     link.href = setArgs(link.href, args);
64   });
65
66   node = $("shortlinkanchor");
67   if (node) {
68     var args = getArgs(node.href);
69     var code = makeShortCode(lat, lon, zoom);
70     var prefix = shortlinkPrefix();
71
72     // Add ?{node,way,relation}=id to the arguments
73     if (objtype && objid) {
74       args[objtype] = objid;
75     }
76
77     // This is a hack to omit the default mapnik layer from the shortlink.
78     if (layers && layers != "M") {
79       args["layers"] = layers;
80     }
81     else {
82       delete args["layers"];
83     }
84
85     // Here we're assuming that all parameters but ?layers= and
86     // ?{node,way,relation}= can be safely omitted from the shortlink
87     // which encodes lat/lon/zoom. If new URL parameters are added to
88     // the main slippy map this needs to be changed.
89     if (args["layers"] || args[objtype]) {
90       node.href = setArgs(prefix + "/go/" + code, args);
91     } else {
92       node.href = prefix + "/go/" + code;
93     }
94   }
95 }
96
97 /*
98  * Get the URL prefix to use for a short link
99  */
100 function shortlinkPrefix() {
101   if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
102     return "http://osm.org";
103   } else {
104     return "";
105   }
106 }
107
108 /*
109  * Called to get the arguments from a URL as a hash.
110  */
111 function getArgs(url) {
112   var args = new Object();
113   var querystart = url.indexOf("?");
114
115   if (querystart >= 0) {
116      var querystring = url.substring(querystart + 1);
117      var queryitems = querystring.split("&");
118
119      for (var i = 0; i < queryitems.length; i++) {
120         if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
121            args[unescape(match[1])] = unescape(match[2]);
122         } else {
123            args[unescape(queryitems[i])] = null
124         }
125      }
126   }
127
128   return args;
129 }
130
131 /*
132  * Called to set the arguments on a URL from the given hash.
133  */
134 function setArgs(url, args) {
135    var queryitems = new Array();
136
137    for (arg in args)
138    {
139       if (args[arg] == null) {
140          queryitems.push(escape(arg));
141       } else {
142          queryitems.push(escape(arg) + "=" + escape(args[arg]));
143       }
144    }
145
146    return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
147 }
148
149 /*
150  * Called to get a CSS property for an element.
151  */
152 function getStyle(el, property) {
153   var style;
154
155   if (el.currentStyle) {
156     style = el.currentStyle[property];
157   } else if( window.getComputedStyle ) {
158     style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
159   } else {
160     style = el.style[property];
161   }
162
163   return style;
164 }
165
166 /*
167  * Called to interpolate JavaScript variables in strings using a
168  * similar syntax to rails I18n string interpolation - the only
169  * difference is that [[foo]] is the placeholder syntax instead
170  * of {{foo}} which allows the same string to be processed by both
171  * rails and then later by javascript.
172  */
173 function i18n(string, keys) {
174   string = i18n_strings[string] || string
175
176   for (var key in keys) {
177     var re_key = '\\[\\[' + key + '\\]\\]';
178     var re = new RegExp(re_key, "g");
179
180     string = string.replace(re, keys[key]);
181   }
182
183   return string;
184 }
185
186 /*
187  * Called to interlace the bits in x and y, making a Morton code.
188  */
189 function interlace(x, y) {
190     x = (x | (x << 8)) & 0x00ff00ff;
191     x = (x | (x << 4)) & 0x0f0f0f0f;
192     x = (x | (x << 2)) & 0x33333333;
193     x = (x | (x << 1)) & 0x55555555;
194
195     y = (y | (y << 8)) & 0x00ff00ff;
196     y = (y | (y << 4)) & 0x0f0f0f0f;
197     y = (y | (y << 2)) & 0x33333333;
198     y = (y | (y << 1)) & 0x55555555;
199
200     return (x << 1) | y;
201 }
202
203 /*
204  * Called to create a short code for the short link.
205  */
206 function makeShortCode(lat, lon, zoom) {
207     char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
208     var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
209     var y = Math.round((lat +  90.0) * ((1 << 30) / 45.0));
210     // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
211     // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
212     // and drops the last 4 bits of the full 64 bit Morton code.
213     var str = "";
214     var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
215     for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
216         digit = (c1 >> (24 - 6 * i)) & 0x3f;
217         str += char_array.charAt(digit);
218     }
219     for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
220         digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
221         str += char_array.charAt(digit);
222     }
223     for (var i = 0; i < ((zoom + 8) % 3); ++i) {
224         str += "-";
225     }
226     return str;
227 }