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