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