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