3 //= require jquery.autogrowtextarea
4 //= require jquery.timers
7 //= require i18n/translations
15 * Called as the user scrolls/zooms around to aniplate hrefs of the
16 * view tab and various other links
18 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
19 var decimals = Math.pow(10, Math.floor(zoom/3));
22 lat = Math.round(lat * decimals) / decimals;
23 lon = Math.round(lon * decimals) / decimals;
26 minlon = Math.round(minlon * decimals) / decimals;
27 minlat = Math.round(minlat * decimals) / decimals;
28 maxlon = Math.round(maxlon * decimals) / decimals;
29 maxlat = Math.round(maxlat * decimals) / decimals;
32 $(".geolink").each(function (index, link) {
33 var args = getArgs(link.href);
35 if ($(link).hasClass("llz")) {
39 } else if (minlon && $(link).hasClass("bbox")) {
40 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
43 if (layers && $(link).hasClass("layers")) {
47 if (objtype && $(link).hasClass("object")) {
48 args[objtype] = objid;
51 var classes = $(link).attr("class").split(" ");
53 $(classes).each(function (index, classname) {
54 if (match = classname.match(/^minzoom([0-9]+)$/)) {
55 var minzoom = match[1];
56 var name = link.id.replace(/anchor$/, "");
58 $(link).off("click.minzoom");
60 if (zoom >= minzoom) {
61 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
62 $(link).removeClass("disabled");
64 $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
65 $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
66 $(link).addClass("disabled");
71 link.href = setArgs(link.href, args);
74 $("#shortlinkanchor").each(function () {
75 var args = getArgs(this.href);
76 var code = makeShortCode(lat, lon, zoom);
77 var prefix = shortlinkPrefix();
79 // Add ?{node,way,relation}=id to the arguments
80 if (objtype && objid) {
81 args[objtype] = objid;
84 // This is a hack to omit the default mapnik layer from the shortlink.
85 if (layers && layers != "M") {
92 // Here we're assuming that all parameters but ?layers= and
93 // ?{node,way,relation}= can be safely omitted from the shortlink
94 // which encodes lat/lon/zoom. If new URL parameters are added to
95 // the main slippy map this needs to be changed.
96 if (args.layers || args[objtype]) {
97 this.href = setArgs(prefix + "/go/" + code, args);
99 this.href = prefix + "/go/" + code;
105 * Get the URL prefix to use for a short link
107 function shortlinkPrefix() {
108 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
109 return "http://osm.org";
116 * Called to get the arguments from a URL as a hash.
118 function getArgs(url) {
120 var querystart = url.indexOf("?");
122 if (querystart >= 0) {
123 var querystring = url.substring(querystart + 1);
124 var queryitems = querystring.split("&");
126 for (var i = 0; i < queryitems.length; i++) {
127 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
128 args[unescape(match[1])] = unescape(match[2]);
130 args[unescape(queryitems[i])] = null;
139 * Called to set the arguments on a URL from the given hash.
141 function setArgs(url, args) {
145 if (args[arg] == null) {
146 queryitems.push(escape(arg));
148 queryitems.push(escape(arg) + "=" + escape(args[arg]));
152 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
156 * Called to interlace the bits in x and y, making a Morton code.
158 function interlace(x, y) {
159 x = (x | (x << 8)) & 0x00ff00ff;
160 x = (x | (x << 4)) & 0x0f0f0f0f;
161 x = (x | (x << 2)) & 0x33333333;
162 x = (x | (x << 1)) & 0x55555555;
164 y = (y | (y << 8)) & 0x00ff00ff;
165 y = (y | (y << 4)) & 0x0f0f0f0f;
166 y = (y | (y << 2)) & 0x33333333;
167 y = (y | (y << 1)) & 0x55555555;
173 * Called to create a short code for the short link.
175 function makeShortCode(lat, lon, zoom) {
176 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
177 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
178 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
179 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
180 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
181 // and drops the last 4 bits of the full 64 bit Morton code.
183 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
184 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
185 digit = (c1 >> (24 - 6 * i)) & 0x3f;
186 str += char_array.charAt(digit);
188 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
189 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
190 str += char_array.charAt(digit);
192 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
199 * Click handler to switch a rich text control to preview mode
201 function previewRichtext(event) {
202 var editor = $(this).parents(".richtext_container").find("textarea");
203 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
204 var width = editor.outerWidth() - preview.outerWidth() + preview.innerWidth();
205 var minHeight = editor.outerHeight() - preview.outerHeight() + preview.innerHeight();
207 if (preview.contents().length == 0) {
208 preview.oneTime(500, "loading", function () {
209 preview.addClass("loading");
212 preview.load(editor.attr("data-preview-url"), { text: editor.val() }, function () {
213 preview.stopTime("loading");
214 preview.removeClass("loading");
219 preview.width(width);
220 preview.css("min-height", minHeight + "px");
223 $(this).siblings(".richtext_doedit").prop("disabled", false);
224 $(this).prop("disabled", true);
226 event.preventDefault();
230 * Click handler to switch a rich text control to edit mode
232 function editRichtext(event) {
233 var editor = $(this).parents(".richtext_container").find("textarea");
234 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
239 $(this).siblings(".richtext_dopreview").prop("disabled", false);
240 $(this).prop("disabled", true);
242 event.preventDefault();
246 * Setup any rich text controls
248 $(document).ready(function () {
249 $(".richtext_preview").hide();
250 $(".richtext_content textarea").change(function () {
251 $(this).parents(".richtext_container").find(".richtext_preview").empty();
253 $(".richtext_doedit").prop("disabled", true);
254 $(".richtext_dopreview").prop("disabled", false);
255 $(".richtext_doedit").click(editRichtext);
256 $(".richtext_dopreview").click(previewRichtext);
260 * Forms which have been cached by rails may have he wrong
261 * authenticity token, so patch up any forms with the correct
262 * token taken from the page header.
264 $(document).ready(function () {
265 var auth_token = $("meta[name=csrf-token]").attr("content");
266 $("form input[name=authenticity_token]").val(auth_token);
270 * Enable auto expansion for all text areas
272 $(document).ready(function () {
273 $("textarea").autoGrow();