]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/application.js
Move piwik code to a static JS asset
[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
17 function zoomPrecision(zoom) {
18     var decimals = Math.pow(10, Math.floor(zoom/3));
19     return function(x) {
20          return Math.round(x * decimals) / decimals;
21     };
22 }
23
24 /*
25  * Called as the user scrolls/zooms around to aniplate hrefs of the
26  * view tab and various other links
27  */
28 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
29   var toPrecision = zoomPrecision(zoom);
30   var node;
31
32   lat = toPrecision(lat);
33   lon = toPrecision(lon);
34
35   if (minlon) {
36     minlon = toPrecision(minlon);
37     minlat = toPrecision(minlat);
38     maxlon = toPrecision(maxlon);
39     maxlat = toPrecision(maxlat);
40   }
41
42   $(".geolink").each(function (index, link) {
43     var args = getArgs(link.href);
44
45     if ($(link).hasClass("llz")) {
46       args.lat = lat;
47       args.lon = lon;
48       args.zoom = zoom;
49     } else if (minlon && $(link).hasClass("bbox")) {
50       args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
51     }
52
53     if (layers && $(link).hasClass("layers")) {
54       args.layers = layers;
55     }
56
57     if (objtype && $(link).hasClass("object")) {
58       args[objtype] = objid;
59     }
60
61     var classes = $(link).attr("class").split(" ");
62
63     $(classes).each(function (index, classname) {
64       if (match = classname.match(/^minzoom([0-9]+)$/)) {
65         var minzoom = match[1];
66         var name = link.id.replace(/anchor$/, "");
67
68         $(link).off("click.minzoom");
69
70         if (zoom >= minzoom) {
71           $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
72           $(link).removeClass("disabled");
73         } else {
74           $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
75           $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
76           $(link).addClass("disabled");
77         }
78       }
79     });
80
81     link.href = setArgs(link.href, args);
82   });
83
84   $("#shortlinkanchor").each(function () {
85     var args = getArgs(this.href);
86     var code = makeShortCode(lat, lon, zoom);
87     var prefix = shortlinkPrefix();
88
89     // Add ?{node,way,relation}=id to the arguments
90     if (objtype && objid) {
91       args[objtype] = objid;
92     }
93
94     // This is a hack to omit the default mapnik layer from the shortlink.
95     if (layers && layers != "M") {
96       args.layers = layers;
97     }
98     else {
99       delete args.layers;
100     }
101
102     // Here we're assuming that all parameters but ?layers= and
103     // ?{node,way,relation}= can be safely omitted from the shortlink
104     // which encodes lat/lon/zoom. If new URL parameters are added to
105     // the main slippy map this needs to be changed.
106     if (args.layers || args[objtype]) {
107       this.href = setArgs(prefix + "/go/" + code, args);
108     } else {
109       this.href = prefix + "/go/" + code;
110     }
111   });
112 }
113
114 /*
115  * Get the URL prefix to use for a short link
116  */
117 function shortlinkPrefix() {
118   if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
119     return "http://osm.org";
120   } else {
121     return "";
122   }
123 }
124
125 /*
126  * Called to get the arguments from a URL as a hash.
127  */
128 function getArgs(url) {
129   var args = {};
130   var querystart = url.indexOf("?");
131
132   if (querystart >= 0) {
133      var querystring = url.substring(querystart + 1);
134      var queryitems = querystring.split("&");
135
136      for (var i = 0; i < queryitems.length; i++) {
137         if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
138            args[unescape(match[1])] = unescape(match[2]);
139         } else {
140            args[unescape(queryitems[i])] = null;
141         }
142      }
143   }
144
145   return args;
146 }
147
148 /*
149  * Called to set the arguments on a URL from the given hash.
150  */
151 function setArgs(url, args) {
152    var queryitems = [];
153
154    for (arg in args) {
155       if (args[arg] == null) {
156          queryitems.push(escape(arg));
157       } else {
158          queryitems.push(escape(arg) + "=" + escape(args[arg]));
159       }
160    }
161
162    return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
163 }
164
165 /*
166  * Called to interlace the bits in x and y, making a Morton code.
167  */
168 function interlace(x, y) {
169     x = (x | (x << 8)) & 0x00ff00ff;
170     x = (x | (x << 4)) & 0x0f0f0f0f;
171     x = (x | (x << 2)) & 0x33333333;
172     x = (x | (x << 1)) & 0x55555555;
173
174     y = (y | (y << 8)) & 0x00ff00ff;
175     y = (y | (y << 4)) & 0x0f0f0f0f;
176     y = (y | (y << 2)) & 0x33333333;
177     y = (y | (y << 1)) & 0x55555555;
178
179     return (x << 1) | y;
180 }
181
182 /*
183  * Called to create a short code for the short link.
184  */
185 function makeShortCode(lat, lon, zoom) {
186     char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
187     var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
188     var y = Math.round((lat +  90.0) * ((1 << 30) / 45.0));
189     // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
190     // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
191     // and drops the last 4 bits of the full 64 bit Morton code.
192     var str = "";
193     var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
194     for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
195         digit = (c1 >> (24 - 6 * i)) & 0x3f;
196         str += char_array.charAt(digit);
197     }
198     for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
199         digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
200         str += char_array.charAt(digit);
201     }
202     for (var i = 0; i < ((zoom + 8) % 3); ++i) {
203         str += "-";
204     }
205     return str;
206 }
207
208 /*
209  * Click handler to switch a rich text control to preview mode
210  */
211 function previewRichtext(event) {
212   var editor = $(this).parents(".richtext_container").find("textarea");
213   var preview = $(this).parents(".richtext_container").find(".richtext_preview");
214   var width = editor.outerWidth() - preview.outerWidth() + preview.innerWidth();
215   var minHeight = editor.outerHeight() - preview.outerHeight() + preview.innerHeight();
216
217   if (preview.contents().length == 0) {
218     preview.oneTime(500, "loading", function () {
219       preview.addClass("loading");
220     });
221
222     preview.load(editor.attr("data-preview-url"), { text: editor.val() }, function () {
223       preview.stopTime("loading");
224       preview.removeClass("loading");
225     });
226   }
227
228   editor.hide();
229   preview.width(width);
230   preview.css("min-height", minHeight + "px");
231   preview.show();
232
233   $(this).siblings(".richtext_doedit").prop("disabled", false);
234   $(this).prop("disabled", true);
235
236   event.preventDefault();
237 }
238
239 /*
240  * Click handler to switch a rich text control to edit mode
241  */
242 function editRichtext(event) {
243   var editor = $(this).parents(".richtext_container").find("textarea");
244   var preview = $(this).parents(".richtext_container").find(".richtext_preview");
245
246   preview.hide();
247   editor.show();
248
249   $(this).siblings(".richtext_dopreview").prop("disabled", false);
250   $(this).prop("disabled", true);
251
252   event.preventDefault();
253 }
254
255 /*
256  * Setup any rich text controls
257  */
258 $(document).ready(function () {
259   $(".richtext_preview").hide();
260   $(".richtext_content textarea").change(function () { 
261     $(this).parents(".richtext_container").find(".richtext_preview").empty();
262   });
263   $(".richtext_doedit").prop("disabled", true);
264   $(".richtext_dopreview").prop("disabled", false);
265   $(".richtext_doedit").click(editRichtext);
266   $(".richtext_dopreview").click(previewRichtext);
267 });
268
269 /*
270  * Forms which have been cached by rails may have he wrong
271  * authenticity token, so patch up any forms with the correct
272  * token taken from the page header.
273  */
274 $(document).ready(function () {
275   var auth_token = $("meta[name=csrf-token]").attr("content");
276   $("form input[name=authenticity_token]").val(auth_token);
277 });
278
279 /*
280  * Enable auto expansion for all text areas
281  */
282 $(document).ready(function () {
283   $("textarea").autoGrow();
284 });