3 //= require jquery.timers
4 //= require jquery.cookie
7 //= require leaflet.osm
8 //= require leaflet.locationfilter
9 //= require i18n/translations
19 //= require querystring
21 var querystring = require('querystring');
23 function zoomPrecision(zoom) {
24 var decimals = Math.pow(10, Math.floor(zoom/3));
26 return Math.round(x * decimals) / decimals;
30 function normalBounds(bounds) {
31 if (bounds instanceof L.LatLngBounds) return bounds;
32 return new L.LatLngBounds(
33 new L.LatLng(bounds[0][0], bounds[0][1]),
34 new L.LatLng(bounds[1][0], bounds[1][1]));
38 * Called as the user scrolls/zooms around to maniplate hrefs of the
39 * view tab and various other links
41 function updatelinks(loc, zoom, layers, bounds, object) {
42 var toPrecision = zoomPrecision(zoom);
43 bounds = normalBounds(bounds);
46 var lat = toPrecision(loc.lat),
47 lon = toPrecision(loc.lon || loc.lng);
50 var minlon = toPrecision(bounds.getWest()),
51 minlat = toPrecision(bounds.getSouth()),
52 maxlon = toPrecision(bounds.getEast()),
53 maxlat = toPrecision(bounds.getNorth());
56 $(".geolink").each(setGeolink);
57 $("#shortlinkanchor").each(setShortlink);
59 function setGeolink(index, link) {
60 var base = link.href.split('?')[0];
61 var qs = link.href.split('?')[1];
62 var args = querystring.parse(qs);
64 if ($(link).hasClass("llz")) {
70 } else if (minlon && $(link).hasClass("bbox")) {
72 bbox: minlon + "," + minlat + "," + maxlon + "," + maxlat
76 if (layers && $(link).hasClass("layers")) args.layers = layers;
77 if (object && $(link).hasClass("object")) 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 = base + '?' + querystring.stringify(args);
99 function setShortlink() {
100 var base = link.href.split('?')[0],
101 qs = link.href.split('?')[1],
102 args = querystring.parse(qs),
103 code = makeShortCode(lat, lon, zoom),
104 prefix = shortlinkPrefix();
106 // Add ?{node,way,relation}=id to the arguments
108 args[object.type] = object.id;
111 // This is a hack to omit the default mapnik layer from the shortlink.
112 if (layers && layers != "M") {
113 args.layers = layers;
118 // Here we're assuming that all parameters but ?layers= and
119 // ?{node,way,relation}= can be safely omitted from the shortlink
120 // which encodes lat/lon/zoom. If new URL parameters are added to
121 // the main slippy map this needs to be changed.
122 if (args.layers || object) {
123 this.href = prefix + "/go/" + code + '?' + querystring.stringify(args);
125 this.href = prefix + "/go/" + code;
130 function minZoomAlert() {
131 alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false;
135 * Get the URL prefix to use for a short link
137 function shortlinkPrefix() {
138 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
139 return "http://osm.org";
146 * Called to interlace the bits in x and y, making a Morton code.
148 function interlace(x, y) {
149 x = (x | (x << 8)) & 0x00ff00ff;
150 x = (x | (x << 4)) & 0x0f0f0f0f;
151 x = (x | (x << 2)) & 0x33333333;
152 x = (x | (x << 1)) & 0x55555555;
154 y = (y | (y << 8)) & 0x00ff00ff;
155 y = (y | (y << 4)) & 0x0f0f0f0f;
156 y = (y | (y << 2)) & 0x33333333;
157 y = (y | (y << 1)) & 0x55555555;
163 * Called to create a short code for the short link.
165 function makeShortCode(lat, lon, zoom) {
166 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
167 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
168 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
169 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
170 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
171 // and drops the last 4 bits of the full 64 bit Morton code.
173 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
174 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
175 digit = (c1 >> (24 - 6 * i)) & 0x3f;
176 str += char_array.charAt(digit);
178 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
179 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
180 str += char_array.charAt(digit);
182 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
189 * Forms which have been cached by rails may have he wrong
190 * authenticity token, so patch up any forms with the correct
191 * token taken from the page header.
193 $(document).ready(function () {
194 var auth_token = $("meta[name=csrf-token]").attr("content");
195 $("form input[name=authenticity_token]").val(auth_token);