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