]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
f5022683b8050fec55c3c346664e835996c566a0
[rails.git] / app / assets / javascripts / application.js
1 //= require jquery
2 //= require jquery_ujs
3 //= require jquery.timers
4 //= require jquery.cookie
5 //= require augment
6 //= require leaflet
7 //= require leaflet.osm
8 //= require leaflet.extend
9 //= require leaflet.locationfilter
10 //= require i18n/translations
11 //= require oauth
12 //= require osm
13 //= require piwik
14 //= require map
15 //= require menu
16 //= require sidebar
17 //= require richtext
18 //= require resize
19 //= require geocoder
20 //= require querystring
21
22 var querystring = require('querystring-component');
23
24 function zoomPrecision(zoom) {
25     var decimals = Math.pow(10, Math.floor(zoom/3));
26     return function(x) {
27          return Math.round(x * decimals) / decimals;
28     };
29 }
30
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]));
36 }
37
38 function remoteEditHandler(bbox, select) {
39   var loaded = false,
40       query = {
41           left: bbox.getWest() - 0.0001,
42           top: bbox.getNorth() + 0.0001,
43           right: bbox.getEast() + 0.0001,
44           bottom: bbox.getSouth() - 0.0001
45       };
46
47   if (select) query.select = select;
48   $("#linkloader")
49     .attr("src", "http://127.0.0.1:8111/load_and_zoom?" + querystring.stringify(query))
50     .load(function() { loaded = true; });
51
52   setTimeout(function () {
53     if (!loaded) alert(I18n.t('site.index.remote_failed'));
54   }, 1000);
55
56   return false;
57 }
58
59 /*
60  * Called as the user scrolls/zooms around to maniplate hrefs of the
61  * view tab and various other links
62  */
63 function updatelinks(loc, zoom, layers, bounds, object) {
64   var toPrecision = zoomPrecision(zoom);
65   bounds = normalBounds(bounds);
66   var node;
67
68   var lat = toPrecision(loc.lat),
69       lon = toPrecision(loc.lon || loc.lng);
70
71   if (bounds) {
72     var minlon = toPrecision(bounds.getWest()),
73         minlat = toPrecision(bounds.getSouth()),
74         maxlon = toPrecision(bounds.getEast()),
75         maxlat = toPrecision(bounds.getNorth());
76   }
77
78   $(".geolink").each(setGeolink);
79
80   function setGeolink(index, link) {
81     var base = link.href.split('?')[0],
82         qs = link.href.split('?')[1],
83         args = querystring.parse(qs);
84
85     if ($(link).hasClass("llz")) {
86       $.extend(args, {
87           lat: lat,
88           lon: lon,
89           zoom: zoom
90       });
91     } else if (minlon && $(link).hasClass("bbox")) {
92       $.extend(args, {
93           bbox: minlon + "," + minlat + "," + maxlon + "," + maxlat
94       });
95     }
96
97     if (layers && $(link).hasClass("layers")) args.layers = layers;
98     if (object && $(link).hasClass("object")) args[object.type] = object.id;
99
100     var minzoom = $(link).data("minzoom");
101     if (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");
107         } else {
108           $(link).on("click.minzoom", minZoomAlert)
109               .attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"))
110               .addClass("disabled");
111         }
112     }
113     link.href = base + '?' + querystring.stringify(args);
114   }
115 }
116
117 function minZoomAlert() {
118     alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false;
119 }
120
121 function getShortUrl(map) {
122   return (window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
123           'http://osm.org/go/' : '/go/') +
124           makeShortCode(map);
125 }
126
127 function getUrl(map) {
128   var center = map.getCenter(),
129       zoom = map.getZoom(),
130       toZoom = zoomPrecision(zoom);
131
132   return (window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
133           'http://openstreetmap.org/?' : '/?') +
134         querystring.stringify({
135             lat: toZoom(center.lat),
136             lon: toZoom(center.lng),
137             zoom: zoom,
138             layers: map.getLayersCode()
139         });
140 }
141
142 // Called to create a short code for the short link.
143 function makeShortCode(map) {
144     var zoom = map.getZoom(),
145         str = '',
146         char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
147         x = Math.round((map.getCenter().lng + 180.0) * ((1 << 30) / 90.0)),
148         y = Math.round((map.getCenter().lat +  90.0) * ((1 << 30) / 45.0)),
149         // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
150         // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
151         // and drops the last 4 bits of the full 64 bit Morton code.
152         c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
153
154     for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
155         digit = (c1 >> (24 - 6 * i)) & 0x3f;
156         str += char_array.charAt(digit);
157     }
158     for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
159         digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
160         str += char_array.charAt(digit);
161     }
162     for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
163
164     // Called to interlace the bits in x and y, making a Morton code.
165     function interlace(x, y) {
166         x = (x | (x << 8)) & 0x00ff00ff;
167         x = (x | (x << 4)) & 0x0f0f0f0f;
168         x = (x | (x << 2)) & 0x33333333;
169         x = (x | (x << 1)) & 0x55555555;
170         y = (y | (y << 8)) & 0x00ff00ff;
171         y = (y | (y << 4)) & 0x0f0f0f0f;
172         y = (y | (y << 2)) & 0x33333333;
173         y = (y | (y << 1)) & 0x55555555;
174         return (x << 1) | y;
175     }
176
177     return str;
178 }
179
180 // generate a cookie-safe string of map state
181 function cookieContent(map) {
182   var center = map.getCenter().wrap();
183   return [center.lng, center.lat, map.getZoom(), map.getLayersCode()].join('|');
184 }
185
186 /*
187  * Forms which have been cached by rails may have the wrong
188  * authenticity token, so patch up any forms with the correct
189  * token taken from the page header.
190  */
191 $(document).ready(function () {
192   var auth_token = $("meta[name=csrf-token]").attr("content");
193   $("form input[name=authenticity_token]").val(auth_token);
194 });