]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Stop spurious +/- signs appearing in the zoom slider
[rails.git] / app / assets / javascripts / application.js
1 //= require jquery
2 //= require jquery_ujs
3 //= require jquery.autogrowtextarea
4 //= require jquery.timers
5 //= require jquery.cookie
6 //= require augment
7 //= require leaflet
8 //= require leaflet.osm
9 //= require leaflet.locationfilter
10 //= require leaflet.pan
11 //= require leaflet.zoom
12 //= require i18n/translations
13 //= require osm
14 //= require piwik
15 //= require map
16 //= require menu
17 //= require sidebar
18 //= require richtext
19 //= require resize
20
21 function zoomPrecision(zoom) {
22     var decimals = Math.pow(10, Math.floor(zoom/3));
23     return function(x) {
24          return Math.round(x * decimals) / decimals;
25     };
26 }
27
28 /*
29  * Called as the user scrolls/zooms around to aniplate hrefs of the
30  * view tab and various other links
31  */
32 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,object) {
33   var toPrecision = zoomPrecision(zoom);
34   var node;
35
36   lat = toPrecision(lat);
37   lon = toPrecision(lon);
38
39   if (minlon) {
40     minlon = toPrecision(minlon);
41     minlat = toPrecision(minlat);
42     maxlon = toPrecision(maxlon);
43     maxlat = toPrecision(maxlat);
44   }
45
46   $(".geolink").each(function (index, link) {
47     var args = getArgs(link.href);
48
49     if ($(link).hasClass("llz")) {
50       args.lat = lat;
51       args.lon = lon;
52       args.zoom = zoom;
53     } else if (minlon && $(link).hasClass("bbox")) {
54       args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
55     }
56
57     if (layers && $(link).hasClass("layers")) {
58       args.layers = layers;
59     }
60
61     if (object && $(link).hasClass("object")) {
62       args[object.type] = object.id;
63     }
64
65     var minzoom = $(link).data("minzoom");
66     if (minzoom) {
67         var name = link.id.replace(/anchor$/, "");
68
69         $(link).off("click.minzoom");
70
71         if (zoom >= minzoom) {
72           $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
73           $(link).removeClass("disabled");
74         } else {
75           $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
76           $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
77           $(link).addClass("disabled");
78         }
79     }
80
81     link.href = setArgs(link.href, args);
82   });
83
84   $("#shortlinkanchor").each(function () {
85     var args = getArgs(this.href);
86     var code = makeShortCode(lat, lon, zoom);
87     var prefix = shortlinkPrefix();
88
89     // Add ?{node,way,relation}=id to the arguments
90     if (object) {
91       args[object.type] = object.id;
92     }
93
94     // This is a hack to omit the default mapnik layer from the shortlink.
95     if (layers && layers != "M") {
96       args.layers = layers;
97     }
98     else {
99       delete args.layers;
100     }
101
102     // Here we're assuming that all parameters but ?layers= and
103     // ?{node,way,relation}= can be safely omitted from the shortlink
104     // which encodes lat/lon/zoom. If new URL parameters are added to
105     // the main slippy map this needs to be changed.
106     if (args.layers || object) {
107       this.href = setArgs(prefix + "/go/" + code, args);
108     } else {
109       this.href = prefix + "/go/" + code;
110     }
111   });
112 }
113
114 /*
115  * Get the URL prefix to use for a short link
116  */
117 function shortlinkPrefix() {
118   if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
119     return "http://osm.org";
120   } else {
121     return "";
122   }
123 }
124
125 /*
126  * Called to get the arguments from a URL as a hash.
127  */
128 function getArgs(url) {
129   var args = {};
130   var querystart = url.indexOf("?");
131
132   if (querystart >= 0) {
133      var querystring = url.substring(querystart + 1);
134      var queryitems = querystring.split("&");
135
136      for (var i = 0; i < queryitems.length; i++) {
137         if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
138            args[unescape(match[1])] = unescape(match[2]);
139         } else {
140            args[unescape(queryitems[i])] = null;
141         }
142      }
143   }
144
145   return args;
146 }
147
148 /*
149  * Called to set the arguments on a URL from the given hash.
150  */
151 function setArgs(url, args) {
152    var queryitems = [];
153
154    for (arg in args) {
155       if (args[arg] == null) {
156          queryitems.push(escape(arg));
157       } else {
158          queryitems.push(escape(arg) + "=" + escape(args[arg]));
159       }
160    }
161
162    return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
163 }
164
165 /*
166  * Called to interlace the bits in x and y, making a Morton code.
167  */
168 function interlace(x, y) {
169     x = (x | (x << 8)) & 0x00ff00ff;
170     x = (x | (x << 4)) & 0x0f0f0f0f;
171     x = (x | (x << 2)) & 0x33333333;
172     x = (x | (x << 1)) & 0x55555555;
173
174     y = (y | (y << 8)) & 0x00ff00ff;
175     y = (y | (y << 4)) & 0x0f0f0f0f;
176     y = (y | (y << 2)) & 0x33333333;
177     y = (y | (y << 1)) & 0x55555555;
178
179     return (x << 1) | y;
180 }
181
182 /*
183  * Called to create a short code for the short link.
184  */
185 function makeShortCode(lat, lon, zoom) {
186     char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
187     var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
188     var y = Math.round((lat +  90.0) * ((1 << 30) / 45.0));
189     // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
190     // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
191     // and drops the last 4 bits of the full 64 bit Morton code.
192     var str = "";
193     var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
194     for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
195         digit = (c1 >> (24 - 6 * i)) & 0x3f;
196         str += char_array.charAt(digit);
197     }
198     for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
199         digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
200         str += char_array.charAt(digit);
201     }
202     for (var i = 0; i < ((zoom + 8) % 3); ++i) {
203         str += "-";
204     }
205     return str;
206 }
207
208 /*
209  * Forms which have been cached by rails may have he wrong
210  * authenticity token, so patch up any forms with the correct
211  * token taken from the page header.
212  */
213 $(document).ready(function () {
214   var auth_token = $("meta[name=csrf-token]").attr("content");
215   $("form input[name=authenticity_token]").val(auth_token);
216 });
217
218 /*
219  * Enable auto expansion for all text areas
220  */
221 $(document).ready(function () {
222   $("textarea").autoGrow();
223 });