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-component');
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]));
38 function remoteEditHandler(bbox, select) {
41 left: bbox.getWest() - 0.0001,
42 top: bbox.getNorth() + 0.0001,
43 right: bbox.getEast() + 0.0001,
44 bottom: bbox.getSouth() - 0.0001
47 if (select) query.select = select;
49 .attr("src", "http://127.0.0.1:8111/load_and_zoom?" + querystring.stringify(query))
50 .load(function() { loaded = true; });
52 setTimeout(function () {
53 if (!loaded) alert(I18n.t('site.index.remote_failed'));
60 * Called as the user scrolls/zooms around to maniplate hrefs of the
61 * view tab and various other links
63 function updatelinks(loc, zoom, layers, bounds, object) {
64 var toPrecision = zoomPrecision(zoom);
65 bounds = normalBounds(bounds);
68 var lat = toPrecision(loc.lat),
69 lon = toPrecision(loc.lon || loc.lng);
72 var minlon = toPrecision(bounds.getWest()),
73 minlat = toPrecision(bounds.getSouth()),
74 maxlon = toPrecision(bounds.getEast()),
75 maxlat = toPrecision(bounds.getNorth());
78 $(".geolink").each(setGeolink);
80 function setGeolink(index, link) {
81 var base = link.href.split('?')[0],
82 qs = link.href.split('?')[1],
83 args = querystring.parse(qs);
85 if ($(link).hasClass("llz")) {
91 } else if (minlon && $(link).hasClass("bbox")) {
93 bbox: minlon + "," + minlat + "," + maxlon + "," + maxlat
97 if (layers && $(link).hasClass("layers")) args.layers = layers;
98 if (object && $(link).hasClass("object")) args[object.type] = object.id;
100 var minzoom = $(link).data("minzoom");
102 var name = link.id.replace(/anchor$/, "");
103 $(link).off("click.minzoom");
104 if (zoom >= minzoom) {
105 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"))
106 .removeClass("disabled");
108 $(link).on("click.minzoom", minZoomAlert)
109 .attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"))
110 .addClass("disabled");
113 link.href = base + '?' + querystring.stringify(args);
117 function getShortUrl(map) {
118 return (window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
119 'http://osm.org/go/' : '/go/') +
123 function minZoomAlert() {
124 alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false;
127 // Called to create a short code for the short link.
128 function makeShortCode(map) {
129 var zoom = map.getZoom(),
131 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
132 x = Math.round((map.getCenter().lng + 180.0) * ((1 << 30) / 90.0)),
133 y = Math.round((map.getCenter().lat + 90.0) * ((1 << 30) / 45.0)),
134 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
135 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
136 // and drops the last 4 bits of the full 64 bit Morton code.
137 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
139 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
140 digit = (c1 >> (24 - 6 * i)) & 0x3f;
141 str += char_array.charAt(digit);
143 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
144 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
145 str += char_array.charAt(digit);
147 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
149 // Called to interlace the bits in x and y, making a Morton code.
150 function interlace(x, y) {
151 x = (x | (x << 8)) & 0x00ff00ff;
152 x = (x | (x << 4)) & 0x0f0f0f0f;
153 x = (x | (x << 2)) & 0x33333333;
154 x = (x | (x << 1)) & 0x55555555;
155 y = (y | (y << 8)) & 0x00ff00ff;
156 y = (y | (y << 4)) & 0x0f0f0f0f;
157 y = (y | (y << 2)) & 0x33333333;
158 y = (y | (y << 1)) & 0x55555555;
166 * Forms which have been cached by rails may have the wrong
167 * authenticity token, so patch up any forms with the correct
168 * token taken from the page header.
170 $(document).ready(function () {
171 var auth_token = $("meta[name=csrf-token]").attr("content");
172 $("form input[name=authenticity_token]").val(auth_token);