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
23 function zoomPrecision(zoom) {
24 var decimals = Math.pow(10, Math.floor(zoom/3));
26 return Math.round(x * decimals) / decimals;
31 * Called as the user scrolls/zooms around to aniplate hrefs of the
32 * view tab and various other links
34 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,object) {
35 var toPrecision = zoomPrecision(zoom);
38 lat = toPrecision(lat);
39 lon = toPrecision(lon);
42 minlon = toPrecision(minlon);
43 minlat = toPrecision(minlat);
44 maxlon = toPrecision(maxlon);
45 maxlat = toPrecision(maxlat);
48 $(".geolink").each(function (index, link) {
49 var args = getArgs(link.href);
51 if ($(link).hasClass("llz")) {
55 } else if (minlon && $(link).hasClass("bbox")) {
56 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
59 if (layers && $(link).hasClass("layers")) {
63 if (object && $(link).hasClass("object")) {
64 args[object.type] = object.id;
67 var minzoom = $(link).data("minzoom");
69 var name = link.id.replace(/anchor$/, "");
71 $(link).off("click.minzoom");
73 if (zoom >= minzoom) {
74 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
75 $(link).removeClass("disabled");
77 $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
78 $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
79 $(link).addClass("disabled");
83 link.href = setArgs(link.href, args);
86 $("#shortlinkanchor").each(function () {
87 var args = getArgs(this.href);
88 var code = makeShortCode(lat, lon, zoom);
89 var prefix = shortlinkPrefix();
91 // Add ?{node,way,relation}=id to the arguments
93 args[object.type] = object.id;
96 // This is a hack to omit the default mapnik layer from the shortlink.
97 if (layers && layers != "M") {
104 // Here we're assuming that all parameters but ?layers= and
105 // ?{node,way,relation}= can be safely omitted from the shortlink
106 // which encodes lat/lon/zoom. If new URL parameters are added to
107 // the main slippy map this needs to be changed.
108 if (args.layers || object) {
109 this.href = setArgs(prefix + "/go/" + code, args);
111 this.href = prefix + "/go/" + code;
117 * Get the URL prefix to use for a short link
119 function shortlinkPrefix() {
120 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
121 return "http://osm.org";
128 * Called to get the arguments from a URL as a hash.
130 function getArgs(url) {
132 var querystart = url.indexOf("?");
134 if (querystart >= 0) {
135 var querystring = url.substring(querystart + 1);
136 var queryitems = querystring.split("&");
138 for (var i = 0; i < queryitems.length; i++) {
139 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
140 args[unescape(match[1])] = unescape(match[2]);
142 args[unescape(queryitems[i])] = null;
151 * Called to set the arguments on a URL from the given hash.
153 function setArgs(url, args) {
157 if (args[arg] == null) {
158 queryitems.push(escape(arg));
160 queryitems.push(escape(arg) + "=" + escape(args[arg]));
164 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
168 * Called to interlace the bits in x and y, making a Morton code.
170 function interlace(x, y) {
171 x = (x | (x << 8)) & 0x00ff00ff;
172 x = (x | (x << 4)) & 0x0f0f0f0f;
173 x = (x | (x << 2)) & 0x33333333;
174 x = (x | (x << 1)) & 0x55555555;
176 y = (y | (y << 8)) & 0x00ff00ff;
177 y = (y | (y << 4)) & 0x0f0f0f0f;
178 y = (y | (y << 2)) & 0x33333333;
179 y = (y | (y << 1)) & 0x55555555;
185 * Called to create a short code for the short link.
187 function makeShortCode(lat, lon, zoom) {
188 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
189 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
190 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
191 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
192 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
193 // and drops the last 4 bits of the full 64 bit Morton code.
195 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
196 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
197 digit = (c1 >> (24 - 6 * i)) & 0x3f;
198 str += char_array.charAt(digit);
200 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
201 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
202 str += char_array.charAt(digit);
204 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
211 * Forms which have been cached by rails may have he wrong
212 * authenticity token, so patch up any forms with the correct
213 * token taken from the page header.
215 $(document).ready(function () {
216 var auth_token = $("meta[name=csrf-token]").attr("content");
217 $("form input[name=authenticity_token]").val(auth_token);
221 * Enable auto expansion for all text areas
223 $(document).ready(function () {
224 $("textarea").autoGrow();