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