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