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;
27 function normalBounds(bounds) {
28 if (bounds instanceof L.LatLngBounds) return bounds;
29 return new L.LatLngBounds(
30 new L.LatLng(bounds[0][0], bounds[0][1]),
31 new L.LatLng(bounds[1][0], bounds[1][1]));
35 * Called as the user scrolls/zooms around to maniplate hrefs of the
36 * view tab and various other links
38 function updatelinks(loc, zoom, layers, bounds, object) {
39 var toPrecision = zoomPrecision(zoom);
40 bounds = normalBounds(bounds);
43 var lat = toPrecision(loc.lat),
44 lon = toPrecision(loc.lon || loc.lng);
47 var minlon = toPrecision(bounds.getWest()),
48 minlat = toPrecision(bounds.getSouth()),
49 maxlon = toPrecision(bounds.getEast()),
50 maxlat = toPrecision(bounds.getNorth());
53 $(".geolink").each(setGeolink);
54 $("#shortlinkanchor").each(setShortlink);
56 function setGeolink(index, link) {
57 var args = getArgs(link.href);
59 if ($(link).hasClass("llz")) {
65 } else if (minlon && $(link).hasClass("bbox")) {
67 bbox: minlon + "," + minlat + "," + maxlon + "," + maxlat
71 if (layers && $(link).hasClass("layers")) {
75 if (object && $(link).hasClass("object")) {
76 args[object.type] = object.id;
79 var minzoom = $(link).data("minzoom");
81 var name = link.id.replace(/anchor$/, "");
83 $(link).off("click.minzoom");
85 if (zoom >= minzoom) {
86 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"))
87 .removeClass("disabled");
89 $(link).on("click.minzoom", minZoomAlert)
90 .attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"))
91 .addClass("disabled");
95 link.href = setArgs(link.href, args);
99 function setShortlink() {
100 var args = getArgs(this.href);
101 var code = makeShortCode(lat, lon, zoom);
102 var prefix = shortlinkPrefix();
104 // Add ?{node,way,relation}=id to the arguments
106 args[object.type] = object.id;
109 // This is a hack to omit the default mapnik layer from the shortlink.
110 if (layers && layers != "M") {
111 args.layers = layers;
116 // Here we're assuming that all parameters but ?layers= and
117 // ?{node,way,relation}= can be safely omitted from the shortlink
118 // which encodes lat/lon/zoom. If new URL parameters are added to
119 // the main slippy map this needs to be changed.
120 if (args.layers || object) {
121 this.href = setArgs(prefix + "/go/" + code, args);
123 this.href = prefix + "/go/" + code;
128 function minZoomAlert() {
129 alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false;
133 * Get the URL prefix to use for a short link
135 function shortlinkPrefix() {
136 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
137 return "http://osm.org";
144 * Called to get the arguments from a URL as a hash.
146 function getArgs(url) {
148 var querystart = url.indexOf("?");
150 if (querystart >= 0) {
151 var querystring = url.substring(querystart + 1);
152 var queryitems = querystring.split("&");
154 for (var i = 0; i < queryitems.length; i++) {
155 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
156 args[unescape(match[1])] = unescape(match[2]);
158 args[unescape(queryitems[i])] = null;
167 * Called to set the arguments on a URL from the given hash.
169 function setArgs(url, args) {
173 if (args[arg] == null) {
174 queryitems.push(escape(arg));
176 queryitems.push(escape(arg) + "=" + escape(args[arg]));
180 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
184 * Called to interlace the bits in x and y, making a Morton code.
186 function interlace(x, y) {
187 x = (x | (x << 8)) & 0x00ff00ff;
188 x = (x | (x << 4)) & 0x0f0f0f0f;
189 x = (x | (x << 2)) & 0x33333333;
190 x = (x | (x << 1)) & 0x55555555;
192 y = (y | (y << 8)) & 0x00ff00ff;
193 y = (y | (y << 4)) & 0x0f0f0f0f;
194 y = (y | (y << 2)) & 0x33333333;
195 y = (y | (y << 1)) & 0x55555555;
201 * Called to create a short code for the short link.
203 function makeShortCode(lat, lon, zoom) {
204 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
205 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
206 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
207 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
208 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
209 // and drops the last 4 bits of the full 64 bit Morton code.
211 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
212 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
213 digit = (c1 >> (24 - 6 * i)) & 0x3f;
214 str += char_array.charAt(digit);
216 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
217 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
218 str += char_array.charAt(digit);
220 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
227 * Forms which have been cached by rails may have he wrong
228 * authenticity token, so patch up any forms with the correct
229 * token taken from the page header.
231 $(document).ready(function () {
232 var auth_token = $("meta[name=csrf-token]").attr("content");
233 $("form input[name=authenticity_token]").val(auth_token);