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