]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Refactor forever
[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.locationfilter
9 //= require i18n/translations
10 //= require oauth
11 //= require osm
12 //= require piwik
13 //= require map
14 //= require menu
15 //= require sidebar
16 //= require richtext
17 //= require resize
18 //= require geocoder
19 //= require querystring
20
21 var querystring = require('querystring');
22
23 function zoomPrecision(zoom) {
24     var decimals = Math.pow(10, Math.floor(zoom/3));
25     return function(x) {
26          return Math.round(x * decimals) / decimals;
27     };
28 }
29
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]));
35 }
36
37 /*
38  * Called as the user scrolls/zooms around to maniplate hrefs of the
39  * view tab and various other links
40  */
41 function updatelinks(loc, zoom, layers, bounds, object) {
42   var toPrecision = zoomPrecision(zoom);
43   bounds = normalBounds(bounds);
44   var node;
45
46   var lat = toPrecision(loc.lat),
47       lon = toPrecision(loc.lon || loc.lng);
48
49   if (bounds) {
50     var minlon = toPrecision(bounds.getWest()),
51         minlat = toPrecision(bounds.getSouth()),
52         maxlon = toPrecision(bounds.getEast()),
53         maxlat = toPrecision(bounds.getNorth());
54   }
55
56   $(".geolink").each(setGeolink);
57
58   function setGeolink(index, link) {
59     var base = link.href.split('?')[0],
60         qs = link.href.split('?')[1],
61         args = querystring.parse(qs);
62
63     if ($(link).hasClass("llz")) {
64       $.extend(args, {
65           lat: '' + lat,
66           lon: '' + lon,
67           zoom: '' + zoom
68       });
69     } else if (minlon && $(link).hasClass("bbox")) {
70       $.extend(args, {
71           bbox: minlon + "," + minlat + "," + maxlon + "," + maxlat
72       });
73     }
74
75     if (layers && $(link).hasClass("layers")) args.layers = layers;
76     if (object && $(link).hasClass("object")) args[object.type] = object.id;
77
78     var minzoom = $(link).data("minzoom");
79     if (minzoom) {
80         var name = link.id.replace(/anchor$/, "");
81
82         $(link).off("click.minzoom");
83
84         if (zoom >= minzoom) {
85           $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"))
86               .removeClass("disabled");
87         } else {
88           $(link).on("click.minzoom", minZoomAlert)
89               .attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"))
90               .addClass("disabled");
91         }
92     }
93     link.href = base + '?' + querystring.stringify(args);
94   }
95 }
96
97 function getShortUrl(map) {
98   return (window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
99           'http://osm.org/go/' : '/go/') +
100           makeShortCode(map);
101 }
102
103 function minZoomAlert() {
104     alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false;
105 }
106
107 /*
108  * Called to create a short code for the short link.
109  */
110 function makeShortCode(map) {
111     var lon = map.getCenter().lng,
112         lat = map.getCenter().lat,
113         zoom = map.getZoom(),
114         str = '',
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);
122
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);
126     }
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);
130     }
131     for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
132
133     /*
134      * Called to interlace the bits in x and y, making a Morton code.
135      */
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;
145         return (x << 1) | y;
146     }
147
148     return str;
149 }
150
151 /*
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.
155  */
156 $(document).ready(function () {
157   var auth_token = $("meta[name=csrf-token]").attr("content");
158   $("form input[name=authenticity_token]").val(auth_token);
159 });