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