]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Use note.closed? in a consistent way
[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 oauth
14 //= require osm
15 //= require piwik
16 //= require map
17 //= require menu
18 //= require sidebar
19 //= require richtext
20 //= require resize
21 //= require geocoder
22
23 function zoomPrecision(zoom) {
24     var decimals = Math.pow(10, Math.floor(zoom/3));
25     return function(x) {
26          return Math.round(x * decimals) / decimals;
27     };
28 }
29
30 /*
31  * Called as the user scrolls/zooms around to aniplate hrefs of the
32  * view tab and various other links
33  */
34 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,object) {
35   var toPrecision = zoomPrecision(zoom);
36   var node;
37
38   lat = toPrecision(lat);
39   lon = toPrecision(lon);
40
41   if (minlon) {
42     minlon = toPrecision(minlon);
43     minlat = toPrecision(minlat);
44     maxlon = toPrecision(maxlon);
45     maxlat = toPrecision(maxlat);
46   }
47
48   $(".geolink").each(function (index, link) {
49     var args = getArgs(link.href);
50
51     if ($(link).hasClass("llz")) {
52       args.lat = lat;
53       args.lon = lon;
54       args.zoom = zoom;
55     } else if (minlon && $(link).hasClass("bbox")) {
56       args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
57     }
58
59     if (layers && $(link).hasClass("layers")) {
60       args.layers = layers;
61     }
62
63     if (object && $(link).hasClass("object")) {
64       args[object.type] = object.id;
65     }
66
67     var minzoom = $(link).data("minzoom");
68     if (minzoom) {
69         var name = link.id.replace(/anchor$/, "");
70
71         $(link).off("click.minzoom");
72
73         if (zoom >= minzoom) {
74           $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
75           $(link).removeClass("disabled");
76         } else {
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");
80         }
81     }
82
83     link.href = setArgs(link.href, args);
84   });
85
86   $("#shortlinkanchor").each(function () {
87     var args = getArgs(this.href);
88     var code = makeShortCode(lat, lon, zoom);
89     var prefix = shortlinkPrefix();
90
91     // Add ?{node,way,relation}=id to the arguments
92     if (object) {
93       args[object.type] = object.id;
94     }
95
96     // This is a hack to omit the default mapnik layer from the shortlink.
97     if (layers && layers != "M") {
98       args.layers = layers;
99     }
100     else {
101       delete args.layers;
102     }
103
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);
110     } else {
111       this.href = prefix + "/go/" + code;
112     }
113   });
114 }
115
116 /*
117  * Get the URL prefix to use for a short link
118  */
119 function shortlinkPrefix() {
120   if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
121     return "http://osm.org";
122   } else {
123     return "";
124   }
125 }
126
127 /*
128  * Called to get the arguments from a URL as a hash.
129  */
130 function getArgs(url) {
131   var args = {};
132   var querystart = url.indexOf("?");
133
134   if (querystart >= 0) {
135      var querystring = url.substring(querystart + 1);
136      var queryitems = querystring.split("&");
137
138      for (var i = 0; i < queryitems.length; i++) {
139         if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
140            args[unescape(match[1])] = unescape(match[2]);
141         } else {
142            args[unescape(queryitems[i])] = null;
143         }
144      }
145   }
146
147   return args;
148 }
149
150 /*
151  * Called to set the arguments on a URL from the given hash.
152  */
153 function setArgs(url, args) {
154    var queryitems = [];
155
156    for (arg in args) {
157       if (args[arg] == null) {
158          queryitems.push(escape(arg));
159       } else {
160          queryitems.push(escape(arg) + "=" + escape(args[arg]));
161       }
162    }
163
164    return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
165 }
166
167 /*
168  * Called to interlace the bits in x and y, making a Morton code.
169  */
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;
175
176     y = (y | (y << 8)) & 0x00ff00ff;
177     y = (y | (y << 4)) & 0x0f0f0f0f;
178     y = (y | (y << 2)) & 0x33333333;
179     y = (y | (y << 1)) & 0x55555555;
180
181     return (x << 1) | y;
182 }
183
184 /*
185  * Called to create a short code for the short link.
186  */
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.
194     var str = "";
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);
199     }
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);
203     }
204     for (var i = 0; i < ((zoom + 8) % 3); ++i) {
205         str += "-";
206     }
207     return str;
208 }
209
210 /*
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.
214  */
215 $(document).ready(function () {
216   var auth_token = $("meta[name=csrf-token]").attr("content");
217   $("form input[name=authenticity_token]").val(auth_token);
218 });
219
220 /*
221  * Enable auto expansion for all text areas
222  */
223 $(document).ready(function () {
224   $("textarea").autoGrow();
225 });