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