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