3 //= require jquery.timers
4 //= require jquery.cookie
7 //= require leaflet.osm
8 //= require leaflet.locationfilter
9 //= require i18n/translations
20 function zoomPrecision(zoom) {
21 var decimals = Math.pow(10, Math.floor(zoom/3));
23 return Math.round(x * decimals) / decimals;
28 * Called as the user scrolls/zooms around to aniplate hrefs of the
29 * view tab and various other links
31 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,object) {
32 var toPrecision = zoomPrecision(zoom);
35 lat = toPrecision(lat);
36 lon = toPrecision(lon);
39 minlon = toPrecision(minlon);
40 minlat = toPrecision(minlat);
41 maxlon = toPrecision(maxlon);
42 maxlat = toPrecision(maxlat);
45 $(".geolink").each(function (index, link) {
46 var args = getArgs(link.href);
48 if ($(link).hasClass("llz")) {
52 } else if (minlon && $(link).hasClass("bbox")) {
53 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
56 if (layers && $(link).hasClass("layers")) {
60 if (object && $(link).hasClass("object")) {
61 args[object.type] = object.id;
64 var minzoom = $(link).data("minzoom");
66 var name = link.id.replace(/anchor$/, "");
68 $(link).off("click.minzoom");
70 if (zoom >= minzoom) {
71 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
72 $(link).removeClass("disabled");
74 $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
75 $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
76 $(link).addClass("disabled");
80 link.href = setArgs(link.href, args);
83 $("#shortlinkanchor").each(function () {
84 var args = getArgs(this.href);
85 var code = makeShortCode(lat, lon, zoom);
86 var prefix = shortlinkPrefix();
88 // Add ?{node,way,relation}=id to the arguments
90 args[object.type] = object.id;
93 // This is a hack to omit the default mapnik layer from the shortlink.
94 if (layers && layers != "M") {
101 // Here we're assuming that all parameters but ?layers= and
102 // ?{node,way,relation}= can be safely omitted from the shortlink
103 // which encodes lat/lon/zoom. If new URL parameters are added to
104 // the main slippy map this needs to be changed.
105 if (args.layers || object) {
106 this.href = setArgs(prefix + "/go/" + code, args);
108 this.href = prefix + "/go/" + code;
114 * Get the URL prefix to use for a short link
116 function shortlinkPrefix() {
117 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
118 return "http://osm.org";
125 * Called to get the arguments from a URL as a hash.
127 function getArgs(url) {
129 var querystart = url.indexOf("?");
131 if (querystart >= 0) {
132 var querystring = url.substring(querystart + 1);
133 var queryitems = querystring.split("&");
135 for (var i = 0; i < queryitems.length; i++) {
136 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
137 args[unescape(match[1])] = unescape(match[2]);
139 args[unescape(queryitems[i])] = null;
148 * Called to set the arguments on a URL from the given hash.
150 function setArgs(url, args) {
154 if (args[arg] == null) {
155 queryitems.push(escape(arg));
157 queryitems.push(escape(arg) + "=" + escape(args[arg]));
161 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
165 * Called to interlace the bits in x and y, making a Morton code.
167 function interlace(x, y) {
168 x = (x | (x << 8)) & 0x00ff00ff;
169 x = (x | (x << 4)) & 0x0f0f0f0f;
170 x = (x | (x << 2)) & 0x33333333;
171 x = (x | (x << 1)) & 0x55555555;
173 y = (y | (y << 8)) & 0x00ff00ff;
174 y = (y | (y << 4)) & 0x0f0f0f0f;
175 y = (y | (y << 2)) & 0x33333333;
176 y = (y | (y << 1)) & 0x55555555;
182 * Called to create a short code for the short link.
184 function makeShortCode(lat, lon, zoom) {
185 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
186 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
187 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
188 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
189 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
190 // and drops the last 4 bits of the full 64 bit Morton code.
192 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
193 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
194 digit = (c1 >> (24 - 6 * i)) & 0x3f;
195 str += char_array.charAt(digit);
197 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
198 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
199 str += char_array.charAt(digit);
201 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
208 * Forms which have been cached by rails may have he wrong
209 * authenticity token, so patch up any forms with the correct
210 * token taken from the page header.
212 $(document).ready(function () {
213 var auth_token = $("meta[name=csrf-token]").attr("content");
214 $("form input[name=authenticity_token]").val(auth_token);