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(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,object) {
35 var toPrecision = zoomPrecision(zoom);
38 lat = toPrecision(lat);
39 lon = toPrecision(lon);
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 $(link).removeClass("disabled");
80 $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
81 $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
82 $(link).addClass("disabled");
86 link.href = setArgs(link.href, args);
89 function setShortlink() {
90 var args = getArgs(this.href);
91 var code = makeShortCode(lat, lon, zoom);
92 var prefix = shortlinkPrefix();
94 // Add ?{node,way,relation}=id to the arguments
96 args[object.type] = object.id;
99 // This is a hack to omit the default mapnik layer from the shortlink.
100 if (layers && layers != "M") {
101 args.layers = layers;
107 // Here we're assuming that all parameters but ?layers= and
108 // ?{node,way,relation}= can be safely omitted from the shortlink
109 // which encodes lat/lon/zoom. If new URL parameters are added to
110 // the main slippy map this needs to be changed.
111 if (args.layers || object) {
112 this.href = setArgs(prefix + "/go/" + code, args);
114 this.href = prefix + "/go/" + code;
120 * Get the URL prefix to use for a short link
122 function shortlinkPrefix() {
123 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
124 return "http://osm.org";
131 * Called to get the arguments from a URL as a hash.
133 function getArgs(url) {
135 var querystart = url.indexOf("?");
137 if (querystart >= 0) {
138 var querystring = url.substring(querystart + 1);
139 var queryitems = querystring.split("&");
141 for (var i = 0; i < queryitems.length; i++) {
142 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
143 args[unescape(match[1])] = unescape(match[2]);
145 args[unescape(queryitems[i])] = null;
154 * Called to set the arguments on a URL from the given hash.
156 function setArgs(url, args) {
160 if (args[arg] == null) {
161 queryitems.push(escape(arg));
163 queryitems.push(escape(arg) + "=" + escape(args[arg]));
167 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
171 * Called to interlace the bits in x and y, making a Morton code.
173 function interlace(x, y) {
174 x = (x | (x << 8)) & 0x00ff00ff;
175 x = (x | (x << 4)) & 0x0f0f0f0f;
176 x = (x | (x << 2)) & 0x33333333;
177 x = (x | (x << 1)) & 0x55555555;
179 y = (y | (y << 8)) & 0x00ff00ff;
180 y = (y | (y << 4)) & 0x0f0f0f0f;
181 y = (y | (y << 2)) & 0x33333333;
182 y = (y | (y << 1)) & 0x55555555;
188 * Called to create a short code for the short link.
190 function makeShortCode(lat, lon, zoom) {
191 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
192 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
193 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
194 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
195 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
196 // and drops the last 4 bits of the full 64 bit Morton code.
198 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
199 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
200 digit = (c1 >> (24 - 6 * i)) & 0x3f;
201 str += char_array.charAt(digit);
203 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
204 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
205 str += char_array.charAt(digit);
207 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
214 * Forms which have been cached by rails may have he wrong
215 * authenticity token, so patch up any forms with the correct
216 * token taken from the page header.
218 $(document).ready(function () {
219 var auth_token = $("meta[name=csrf-token]").attr("content");
220 $("form input[name=authenticity_token]").val(auth_token);