3 //= require jquery.timers
4 //= require jquery.cookie
7 //= require leaflet.osm
8 //= require leaflet.hash
9 //= require leaflet.zoom
10 //= require leaflet.extend
11 //= require leaflet.locationfilter
12 //= require i18n/translations
21 //= require querystring
23 var querystring = require('querystring-component');
25 function zoomPrecision(zoom) {
26 var decimals = Math.pow(10, Math.floor(zoom/3));
28 return Math.round(x * decimals) / decimals;
32 function normalBounds(bounds) {
33 if (bounds instanceof L.LatLngBounds) return bounds;
34 return new L.LatLngBounds(
35 new L.LatLng(bounds[0][0], bounds[0][1]),
36 new L.LatLng(bounds[1][0], bounds[1][1]));
39 function remoteEditHandler(bbox, select) {
42 left: bbox.getWest() - 0.0001,
43 top: bbox.getNorth() + 0.0001,
44 right: bbox.getEast() + 0.0001,
45 bottom: bbox.getSouth() - 0.0001
48 if (select) query.select = select;
50 .attr("src", "http://127.0.0.1:8111/load_and_zoom?" + querystring.stringify(query))
51 .load(function() { loaded = true; });
53 setTimeout(function () {
54 if (!loaded) alert(I18n.t('site.index.remote_failed'));
61 * Called as the user scrolls/zooms around to maniplate hrefs of the
62 * view tab and various other links
64 function updatelinks(loc, zoom, layers, bounds, object) {
65 var toPrecision = zoomPrecision(zoom);
66 bounds = normalBounds(bounds);
69 var lat = toPrecision(loc.lat),
70 lon = toPrecision(loc.lon || loc.lng);
73 var minlon = toPrecision(bounds.getWest()),
74 minlat = toPrecision(bounds.getSouth()),
75 maxlon = toPrecision(bounds.getEast()),
76 maxlat = toPrecision(bounds.getNorth());
79 $(".geolink").each(setGeolink);
81 function setGeolink(index, link) {
82 var base = link.href.split('?')[0],
83 qs = link.href.split('?')[1],
84 args = querystring.parse(qs);
86 if ($(link).hasClass("llz")) {
92 } else if (minlon && $(link).hasClass("bbox")) {
94 bbox: minlon + "," + minlat + "," + maxlon + "," + maxlat
98 if (layers && $(link).hasClass("layers")) args.layers = layers;
99 if (object && $(link).hasClass("object")) args[object.type] = object.id;
101 var minzoom = $(link).data("minzoom");
103 var name = link.id.replace(/anchor$/, "");
104 $(link).off("click.minzoom");
105 if (zoom >= minzoom) {
107 .attr("title", I18n.t("javascripts.site." + name + "_tooltip"))
108 .removeClass("disabled");
111 .attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"))
112 .addClass("disabled")
113 .on("click.minzoom", function () {
114 alert(I18n.t("javascripts.site." + name + "_zoom_alert"));
119 link.href = base + '?' + querystring.stringify(args);
123 function getShortUrl(map) {
124 return (window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
125 'http://osm.org/go/' : 'http://' + window.location.hostname + '/go/') +
129 function getUrl(map) {
130 var center = map.getCenter(),
131 zoom = map.getZoom(),
132 toZoom = zoomPrecision(zoom);
134 return (window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
135 'http://openstreetmap.org/?' : 'http://' + window.location.hostname + '/?') +
136 querystring.stringify({
137 lat: toZoom(center.lat),
138 lon: toZoom(center.lng),
140 layers: map.getLayersCode()
144 // Called to create a short code for the short link.
145 function makeShortCode(map) {
146 var zoom = map.getZoom(),
148 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
149 x = Math.round((map.getCenter().lng + 180.0) * ((1 << 30) / 90.0)),
150 y = Math.round((map.getCenter().lat + 90.0) * ((1 << 30) / 45.0)),
151 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
152 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
153 // and drops the last 4 bits of the full 64 bit Morton code.
154 c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
156 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
157 digit = (c1 >> (24 - 6 * i)) & 0x3f;
158 str += char_array.charAt(digit);
160 for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
161 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
162 str += char_array.charAt(digit);
164 for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
166 // Called to interlace the bits in x and y, making a Morton code.
167 function interlace(x, y) {
168 x = (x | (x << 8)) & 0x00ff00ff;
169 x = (x | (x << 4)) & 0x0f0f0f0f;
170 x = (x | (x << 2)) & 0x33333333;
171 x = (x | (x << 1)) & 0x55555555;
172 y = (y | (y << 8)) & 0x00ff00ff;
173 y = (y | (y << 4)) & 0x0f0f0f0f;
174 y = (y | (y << 2)) & 0x33333333;
175 y = (y | (y << 1)) & 0x55555555;
182 // generate a cookie-safe string of map state
183 function cookieContent(map) {
184 var center = map.getCenter().wrap();
185 return [center.lng, center.lat, map.getZoom(), map.getLayersCode()].join('|');
189 * Forms which have been cached by rails may have the 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);