3 //= require jquery.timers
4 //= require jquery.cookie
7 //= require leaflet.osm
8 //= require leaflet.extend
9 //= require leaflet.locationfilter
10 //= require i18n/translations
20 //= require querystring
22 var querystring = require('querystring');
24 function zoomPrecision(zoom) {
25 var decimals = Math.pow(10, Math.floor(zoom/3));
27 return Math.round(x * decimals) / decimals;
31 function normalBounds(bounds) {
32 if (bounds instanceof L.LatLngBounds) return bounds;
33 return new L.LatLngBounds(
34 new L.LatLng(bounds[0][0], bounds[0][1]),
35 new L.LatLng(bounds[1][0], bounds[1][1]));
39 * Called as the user scrolls/zooms around to maniplate hrefs of the
40 * view tab and various other links
42 function updatelinks(loc, zoom, layers, bounds, object) {
43 var toPrecision = zoomPrecision(zoom);
44 bounds = normalBounds(bounds);
47 var lat = toPrecision(loc.lat),
48 lon = toPrecision(loc.lon || loc.lng);
51 var minlon = toPrecision(bounds.getWest()),
52 minlat = toPrecision(bounds.getSouth()),
53 maxlon = toPrecision(bounds.getEast()),
54 maxlat = toPrecision(bounds.getNorth());
57 $(".geolink").each(setGeolink);
59 function setGeolink(index, link) {
60 var base = link.href.split('?')[0],
61 qs = link.href.split('?')[1],
62 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$/, "");
82 $(link).off("click.minzoom");
83 if (zoom >= minzoom) {
84 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"))
85 .removeClass("disabled");
87 $(link).on("click.minzoom", minZoomAlert)
88 .attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"))
89 .addClass("disabled");
92 link.href = base + '?' + querystring.stringify(args);
96 function getShortUrl(map) {
97 return (window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
98 'http://osm.org/go/' : '/go/') +
102 function minZoomAlert() {
103 alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false;
107 * Called to create a short code for the short link.
109 function makeShortCode(map) {
110 var lon = map.getCenter().lng,
111 lat = map.getCenter().lat,
112 zoom = map.getZoom(),
114 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
115 x = Math.round((lon + 180.0) * ((1 << 30) / 90.0)),
116 y = Math.round((lat + 90.0) * ((1 << 30) / 45.0)),
117 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
118 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
119 // and drops the last 4 bits of the full 64 bit Morton code.
120 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
122 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
123 digit = (c1 >> (24 - 6 * i)) & 0x3f;
124 str += char_array.charAt(digit);
126 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
127 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
128 str += char_array.charAt(digit);
130 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
133 * Called to interlace the bits in x and y, making a Morton code.
135 function interlace(x, y) {
136 x = (x | (x << 8)) & 0x00ff00ff;
137 x = (x | (x << 4)) & 0x0f0f0f0f;
138 x = (x | (x << 2)) & 0x33333333;
139 x = (x | (x << 1)) & 0x55555555;
140 y = (y | (y << 8)) & 0x00ff00ff;
141 y = (y | (y << 4)) & 0x0f0f0f0f;
142 y = (y | (y << 2)) & 0x33333333;
143 y = (y | (y << 1)) & 0x55555555;
151 * Forms which have been cached by rails may have the wrong
152 * authenticity token, so patch up any forms with the correct
153 * token taken from the page header.
155 $(document).ready(function () {
156 var auth_token = $("meta[name=csrf-token]").attr("content");
157 $("form input[name=authenticity_token]").val(auth_token);