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