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);
58 function setGeolink(index, link) {
59 var base = link.href.split('?')[0],
60 qs = link.href.split('?')[1],
61 args = querystring.parse(qs);
63 if ($(link).hasClass("llz")) {
69 } else if (minlon && $(link).hasClass("bbox")) {
71 bbox: minlon + "," + minlat + "," + maxlon + "," + maxlat
75 if (layers && $(link).hasClass("layers")) args.layers = layers;
76 if (object && $(link).hasClass("object")) args[object.type] = object.id;
78 var minzoom = $(link).data("minzoom");
80 var name = link.id.replace(/anchor$/, "");
82 $(link).off("click.minzoom");
84 if (zoom >= minzoom) {
85 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"))
86 .removeClass("disabled");
88 $(link).on("click.minzoom", minZoomAlert)
89 .attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"))
90 .addClass("disabled");
93 link.href = base + '?' + querystring.stringify(args);
97 function getShortUrl(map) {
98 return (window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
99 'http://osm.org/go/' : '/go/') +
103 function minZoomAlert() {
104 alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false;
108 * Called to create a short code for the short link.
110 function makeShortCode(map) {
111 var lon = map.getCenter().lng,
112 lat = map.getCenter().lat,
113 zoom = map.getZoom(),
115 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
116 x = Math.round((lon + 180.0) * ((1 << 30) / 90.0)),
117 y = Math.round((lat + 90.0) * ((1 << 30) / 45.0)),
118 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
119 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
120 // and drops the last 4 bits of the full 64 bit Morton code.
121 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
123 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
124 digit = (c1 >> (24 - 6 * i)) & 0x3f;
125 str += char_array.charAt(digit);
127 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
128 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
129 str += char_array.charAt(digit);
131 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
134 * Called to interlace the bits in x and y, making a Morton code.
136 function interlace(x, y) {
137 x = (x | (x << 8)) & 0x00ff00ff;
138 x = (x | (x << 4)) & 0x0f0f0f0f;
139 x = (x | (x << 2)) & 0x33333333;
140 x = (x | (x << 1)) & 0x55555555;
141 y = (y | (y << 8)) & 0x00ff00ff;
142 y = (y | (y << 4)) & 0x0f0f0f0f;
143 y = (y | (y << 2)) & 0x33333333;
144 y = (y | (y << 1)) & 0x55555555;
152 * Forms which have been cached by rails may have the wrong
153 * authenticity token, so patch up any forms with the correct
154 * token taken from the page header.
156 $(document).ready(function () {
157 var auth_token = $("meta[name=csrf-token]").attr("content");
158 $("form input[name=authenticity_token]").val(auth_token);