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