3 //= require jquery.timers
4 //= require jquery.cookie
7 //= require leaflet.osm
8 //= require leaflet.locationfilter
9 //= require leaflet.locate
10 //= require leaflet.note
11 //= require i18n/translations
18 //= require leaflet.share
23 function zoomPrecision(zoom) {
24 var decimals = Math.pow(10, Math.floor(zoom/3));
26 return Math.round(x * decimals) / decimals;
31 * Called as the user scrolls/zooms around to aniplate hrefs of the
32 * view tab and various other links
34 function updatelinks(loc, zoom, layers, minlon, minlat, maxlon, maxlat, object) {
35 var toPrecision = zoomPrecision(zoom);
38 var lat = toPrecision(loc.lat),
39 lon = toPrecision(loc.lon || loc.lng);
42 minlon = toPrecision(minlon);
43 minlat = toPrecision(minlat);
44 maxlon = toPrecision(maxlon);
45 maxlat = toPrecision(maxlat);
48 $(".geolink").each(setGeolink);
49 $("#shortlinkanchor").each(setShortlink);
51 function setGeolink(index, link) {
52 var args = getArgs(link.href);
54 if ($(link).hasClass("llz")) {
58 } else if (minlon && $(link).hasClass("bbox")) {
59 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
62 if (layers && $(link).hasClass("layers")) {
66 if (object && $(link).hasClass("object")) {
67 args[object.type] = object.id;
70 var minzoom = $(link).data("minzoom");
72 var name = link.id.replace(/anchor$/, "");
74 $(link).off("click.minzoom");
76 if (zoom >= minzoom) {
77 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"))
78 .removeClass("disabled");
80 $(link).on("click.minzoom", minZoomAlert)
81 .attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"))
82 .addClass("disabled");
86 link.href = setArgs(link.href, args);
89 function minZoomAlert() {
90 alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false;
93 function setShortlink() {
94 var args = getArgs(this.href);
95 var code = makeShortCode(lat, lon, zoom);
96 var prefix = shortlinkPrefix();
98 // Add ?{node,way,relation}=id to the arguments
100 args[object.type] = object.id;
103 // This is a hack to omit the default mapnik layer from the shortlink.
104 if (layers && layers != "M") {
105 args.layers = layers;
111 // Here we're assuming that all parameters but ?layers= and
112 // ?{node,way,relation}= can be safely omitted from the shortlink
113 // which encodes lat/lon/zoom. If new URL parameters are added to
114 // the main slippy map this needs to be changed.
115 if (args.layers || object) {
116 this.href = setArgs(prefix + "/go/" + code, args);
118 this.href = prefix + "/go/" + code;
124 * Get the URL prefix to use for a short link
126 function shortlinkPrefix() {
127 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
128 return "http://osm.org";
135 * Called to get the arguments from a URL as a hash.
137 function getArgs(url) {
139 var querystart = url.indexOf("?");
141 if (querystart >= 0) {
142 var querystring = url.substring(querystart + 1);
143 var queryitems = querystring.split("&");
145 for (var i = 0; i < queryitems.length; i++) {
146 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
147 args[unescape(match[1])] = unescape(match[2]);
149 args[unescape(queryitems[i])] = null;
158 * Called to set the arguments on a URL from the given hash.
160 function setArgs(url, args) {
164 if (args[arg] == null) {
165 queryitems.push(escape(arg));
167 queryitems.push(escape(arg) + "=" + escape(args[arg]));
171 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
175 * Called to interlace the bits in x and y, making a Morton code.
177 function interlace(x, y) {
178 x = (x | (x << 8)) & 0x00ff00ff;
179 x = (x | (x << 4)) & 0x0f0f0f0f;
180 x = (x | (x << 2)) & 0x33333333;
181 x = (x | (x << 1)) & 0x55555555;
183 y = (y | (y << 8)) & 0x00ff00ff;
184 y = (y | (y << 4)) & 0x0f0f0f0f;
185 y = (y | (y << 2)) & 0x33333333;
186 y = (y | (y << 1)) & 0x55555555;
192 * Called to create a short code for the short link.
194 function makeShortCode(lat, lon, zoom) {
195 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
196 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
197 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
198 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
199 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
200 // and drops the last 4 bits of the full 64 bit Morton code.
202 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
203 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
204 digit = (c1 >> (24 - 6 * i)) & 0x3f;
205 str += char_array.charAt(digit);
207 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
208 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
209 str += char_array.charAt(digit);
211 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
218 * Forms which have been cached by rails may have he wrong
219 * authenticity token, so patch up any forms with the correct
220 * token taken from the page header.
222 $(document).ready(function () {
223 var auth_token = $("meta[name=csrf-token]").attr("content");
224 $("form input[name=authenticity_token]").val(auth_token);