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