3 //= require jquery.autogrowtextarea
4 //= require jquery.timers
5 //= require jquery.cookie
8 //= require leaflet.osm
9 //= require leaflet.locationfilter
10 //= require leaflet.pan
11 //= require leaflet.zoom
12 //= require i18n/translations
22 function zoomPrecision(zoom) {
23 var decimals = Math.pow(10, Math.floor(zoom/3));
25 return Math.round(x * decimals) / decimals;
30 * Called as the user scrolls/zooms around to aniplate hrefs of the
31 * view tab and various other links
33 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,object) {
34 var toPrecision = zoomPrecision(zoom);
37 lat = toPrecision(lat);
38 lon = toPrecision(lon);
41 minlon = toPrecision(minlon);
42 minlat = toPrecision(minlat);
43 maxlon = toPrecision(maxlon);
44 maxlat = toPrecision(maxlat);
47 $(".geolink").each(function (index, link) {
48 var args = getArgs(link.href);
50 if ($(link).hasClass("llz")) {
54 } else if (minlon && $(link).hasClass("bbox")) {
55 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
58 if (layers && $(link).hasClass("layers")) {
62 if (object && $(link).hasClass("object")) {
63 args[object.type] = object.id;
66 var minzoom = $(link).data("minzoom");
68 var name = link.id.replace(/anchor$/, "");
70 $(link).off("click.minzoom");
72 if (zoom >= minzoom) {
73 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
74 $(link).removeClass("disabled");
76 $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
77 $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
78 $(link).addClass("disabled");
82 link.href = setArgs(link.href, args);
85 $("#shortlinkanchor").each(function () {
86 var args = getArgs(this.href);
87 var code = makeShortCode(lat, lon, zoom);
88 var prefix = shortlinkPrefix();
90 // Add ?{node,way,relation}=id to the arguments
92 args[object.type] = object.id;
95 // This is a hack to omit the default mapnik layer from the shortlink.
96 if (layers && layers != "M") {
103 // Here we're assuming that all parameters but ?layers= and
104 // ?{node,way,relation}= can be safely omitted from the shortlink
105 // which encodes lat/lon/zoom. If new URL parameters are added to
106 // the main slippy map this needs to be changed.
107 if (args.layers || object) {
108 this.href = setArgs(prefix + "/go/" + code, args);
110 this.href = prefix + "/go/" + code;
116 * Get the URL prefix to use for a short link
118 function shortlinkPrefix() {
119 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
120 return "http://osm.org";
127 * Called to get the arguments from a URL as a hash.
129 function getArgs(url) {
131 var querystart = url.indexOf("?");
133 if (querystart >= 0) {
134 var querystring = url.substring(querystart + 1);
135 var queryitems = querystring.split("&");
137 for (var i = 0; i < queryitems.length; i++) {
138 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
139 args[unescape(match[1])] = unescape(match[2]);
141 args[unescape(queryitems[i])] = null;
150 * Called to set the arguments on a URL from the given hash.
152 function setArgs(url, args) {
156 if (args[arg] == null) {
157 queryitems.push(escape(arg));
159 queryitems.push(escape(arg) + "=" + escape(args[arg]));
163 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
167 * Called to interlace the bits in x and y, making a Morton code.
169 function interlace(x, y) {
170 x = (x | (x << 8)) & 0x00ff00ff;
171 x = (x | (x << 4)) & 0x0f0f0f0f;
172 x = (x | (x << 2)) & 0x33333333;
173 x = (x | (x << 1)) & 0x55555555;
175 y = (y | (y << 8)) & 0x00ff00ff;
176 y = (y | (y << 4)) & 0x0f0f0f0f;
177 y = (y | (y << 2)) & 0x33333333;
178 y = (y | (y << 1)) & 0x55555555;
184 * Called to create a short code for the short link.
186 function makeShortCode(lat, lon, zoom) {
187 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
188 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
189 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
190 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
191 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
192 // and drops the last 4 bits of the full 64 bit Morton code.
194 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
195 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
196 digit = (c1 >> (24 - 6 * i)) & 0x3f;
197 str += char_array.charAt(digit);
199 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
200 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
201 str += char_array.charAt(digit);
203 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
210 * Forms which have been cached by rails may have he wrong
211 * authenticity token, so patch up any forms with the correct
212 * token taken from the page header.
214 $(document).ready(function () {
215 var auth_token = $("meta[name=csrf-token]").attr("content");
216 $("form input[name=authenticity_token]").val(auth_token);
220 * Enable auto expansion for all text areas
222 $(document).ready(function () {
223 $("textarea").autoGrow();