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