3 //= require jquery.autogrowtextarea
4 //= require jquery.timers
5 //= require jquery.cookie
8 //= require leaflet.osm
9 //= require leaflet.draw
10 //= require i18n/translations
19 function zoomPrecision(zoom) {
20 var decimals = Math.pow(10, Math.floor(zoom/3));
22 return Math.round(x * decimals) / decimals;
27 * Called as the user scrolls/zooms around to aniplate hrefs of the
28 * view tab and various other links
30 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,object) {
31 var toPrecision = zoomPrecision(zoom);
34 lat = toPrecision(lat);
35 lon = toPrecision(lon);
38 minlon = toPrecision(minlon);
39 minlat = toPrecision(minlat);
40 maxlon = toPrecision(maxlon);
41 maxlat = toPrecision(maxlat);
44 $(".geolink").each(function (index, link) {
45 var args = getArgs(link.href);
47 if ($(link).hasClass("llz")) {
51 } else if (minlon && $(link).hasClass("bbox")) {
52 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
55 if (layers && $(link).hasClass("layers")) {
59 if (object && $(link).hasClass("object")) {
60 args[object.type] = object.id;
63 var minzoom = $(link).data("minzoom");
65 var name = link.id.replace(/anchor$/, "");
67 $(link).off("click.minzoom");
69 if (zoom >= minzoom) {
70 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
71 $(link).removeClass("disabled");
73 $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
74 $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
75 $(link).addClass("disabled");
79 link.href = setArgs(link.href, args);
82 $("#shortlinkanchor").each(function () {
83 var args = getArgs(this.href);
84 var code = makeShortCode(lat, lon, zoom);
85 var prefix = shortlinkPrefix();
87 // Add ?{node,way,relation}=id to the arguments
89 args[object.type] = object.id;
92 // This is a hack to omit the default mapnik layer from the shortlink.
93 if (layers && layers != "M") {
100 // Here we're assuming that all parameters but ?layers= and
101 // ?{node,way,relation}= can be safely omitted from the shortlink
102 // which encodes lat/lon/zoom. If new URL parameters are added to
103 // the main slippy map this needs to be changed.
104 if (args.layers || object) {
105 this.href = setArgs(prefix + "/go/" + code, args);
107 this.href = prefix + "/go/" + code;
113 * Get the URL prefix to use for a short link
115 function shortlinkPrefix() {
116 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
117 return "http://osm.org";
124 * Called to get the arguments from a URL as a hash.
126 function getArgs(url) {
128 var querystart = url.indexOf("?");
130 if (querystart >= 0) {
131 var querystring = url.substring(querystart + 1);
132 var queryitems = querystring.split("&");
134 for (var i = 0; i < queryitems.length; i++) {
135 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
136 args[unescape(match[1])] = unescape(match[2]);
138 args[unescape(queryitems[i])] = null;
147 * Called to set the arguments on a URL from the given hash.
149 function setArgs(url, args) {
153 if (args[arg] == null) {
154 queryitems.push(escape(arg));
156 queryitems.push(escape(arg) + "=" + escape(args[arg]));
160 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
164 * Called to interlace the bits in x and y, making a Morton code.
166 function interlace(x, y) {
167 x = (x | (x << 8)) & 0x00ff00ff;
168 x = (x | (x << 4)) & 0x0f0f0f0f;
169 x = (x | (x << 2)) & 0x33333333;
170 x = (x | (x << 1)) & 0x55555555;
172 y = (y | (y << 8)) & 0x00ff00ff;
173 y = (y | (y << 4)) & 0x0f0f0f0f;
174 y = (y | (y << 2)) & 0x33333333;
175 y = (y | (y << 1)) & 0x55555555;
181 * Called to create a short code for the short link.
183 function makeShortCode(lat, lon, zoom) {
184 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
185 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
186 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
187 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
188 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
189 // and drops the last 4 bits of the full 64 bit Morton code.
191 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
192 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
193 digit = (c1 >> (24 - 6 * i)) & 0x3f;
194 str += char_array.charAt(digit);
196 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
197 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
198 str += char_array.charAt(digit);
200 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
207 * Forms which have been cached by rails may have he wrong
208 * authenticity token, so patch up any forms with the correct
209 * token taken from the page header.
211 $(document).ready(function () {
212 var auth_token = $("meta[name=csrf-token]").attr("content");
213 $("form input[name=authenticity_token]").val(auth_token);
217 * Enable auto expansion for all text areas
219 $(document).ready(function () {
220 $("textarea").autoGrow();