3 //= require jquery.timers
4 //= require jquery.cookie
7 //= require leaflet.osm
8 //= require leaflet.locationfilter
9 //= require leaflet.locate
10 //= require i18n/translations
17 //= require leaflet.share
22 function zoomPrecision(zoom) {
23 var decimals = Math.pow(10, Math.floor(zoom/3));
25 return Math.round(x * decimals) / decimals;
30 * Called as the user scrolls/zooms around to aniplate hrefs of the
31 * view tab and various other links
33 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,object) {
34 var toPrecision = zoomPrecision(zoom);
37 lat = toPrecision(lat);
38 lon = toPrecision(lon);
41 minlon = toPrecision(minlon);
42 minlat = toPrecision(minlat);
43 maxlon = toPrecision(maxlon);
44 maxlat = toPrecision(maxlat);
47 $(".geolink").each(setGeolink);
48 $("#shortlinkanchor").each(setShortlink);
50 function setGeolink(index, link) {
51 var args = getArgs(link.href);
53 if ($(link).hasClass("llz")) {
57 } else if (minlon && $(link).hasClass("bbox")) {
58 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
61 if (layers && $(link).hasClass("layers")) {
65 if (object && $(link).hasClass("object")) {
66 args[object.type] = object.id;
69 var minzoom = $(link).data("minzoom");
71 var name = link.id.replace(/anchor$/, "");
73 $(link).off("click.minzoom");
75 if (zoom >= minzoom) {
76 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
77 $(link).removeClass("disabled");
79 $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
80 $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
81 $(link).addClass("disabled");
85 link.href = setArgs(link.href, args);
88 function setShortlink() {
89 var args = getArgs(this.href);
90 var code = makeShortCode(lat, lon, zoom);
91 var prefix = shortlinkPrefix();
93 // Add ?{node,way,relation}=id to the arguments
95 args[object.type] = object.id;
98 // This is a hack to omit the default mapnik layer from the shortlink.
99 if (layers && layers != "M") {
100 args.layers = layers;
106 // Here we're assuming that all parameters but ?layers= and
107 // ?{node,way,relation}= can be safely omitted from the shortlink
108 // which encodes lat/lon/zoom. If new URL parameters are added to
109 // the main slippy map this needs to be changed.
110 if (args.layers || object) {
111 this.href = setArgs(prefix + "/go/" + code, args);
113 this.href = prefix + "/go/" + code;
119 * Get the URL prefix to use for a short link
121 function shortlinkPrefix() {
122 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
123 return "http://osm.org";
130 * Called to get the arguments from a URL as a hash.
132 function getArgs(url) {
134 var querystart = url.indexOf("?");
136 if (querystart >= 0) {
137 var querystring = url.substring(querystart + 1);
138 var queryitems = querystring.split("&");
140 for (var i = 0; i < queryitems.length; i++) {
141 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
142 args[unescape(match[1])] = unescape(match[2]);
144 args[unescape(queryitems[i])] = null;
153 * Called to set the arguments on a URL from the given hash.
155 function setArgs(url, args) {
159 if (args[arg] == null) {
160 queryitems.push(escape(arg));
162 queryitems.push(escape(arg) + "=" + escape(args[arg]));
166 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
170 * Called to interlace the bits in x and y, making a Morton code.
172 function interlace(x, y) {
173 x = (x | (x << 8)) & 0x00ff00ff;
174 x = (x | (x << 4)) & 0x0f0f0f0f;
175 x = (x | (x << 2)) & 0x33333333;
176 x = (x | (x << 1)) & 0x55555555;
178 y = (y | (y << 8)) & 0x00ff00ff;
179 y = (y | (y << 4)) & 0x0f0f0f0f;
180 y = (y | (y << 2)) & 0x33333333;
181 y = (y | (y << 1)) & 0x55555555;
187 * Called to create a short code for the short link.
189 function makeShortCode(lat, lon, zoom) {
190 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
191 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
192 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
193 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
194 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
195 // and drops the last 4 bits of the full 64 bit Morton code.
197 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
198 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
199 digit = (c1 >> (24 - 6 * i)) & 0x3f;
200 str += char_array.charAt(digit);
202 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
203 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
204 str += char_array.charAt(digit);
206 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
213 * Forms which have been cached by rails may have he wrong
214 * authenticity token, so patch up any forms with the correct
215 * token taken from the page header.
217 $(document).ready(function () {
218 var auth_token = $("meta[name=csrf-token]").attr("content");
219 $("form input[name=authenticity_token]").val(auth_token);