3 //= require jquery.autogrowtextarea
4 //= require jquery.timers
7 //= require i18n/translations
16 * Called as the user scrolls/zooms around to aniplate hrefs of the
17 * view tab and various other links
19 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
20 var decimals = Math.pow(10, Math.floor(zoom/3));
23 lat = Math.round(lat * decimals) / decimals;
24 lon = Math.round(lon * decimals) / decimals;
27 minlon = Math.round(minlon * decimals) / decimals;
28 minlat = Math.round(minlat * decimals) / decimals;
29 maxlon = Math.round(maxlon * decimals) / decimals;
30 maxlat = Math.round(maxlat * decimals) / decimals;
33 $(".geolink").each(function (index, link) {
34 var args = getArgs(link.href);
36 if ($(link).hasClass("llz")) {
40 } else if (minlon && $(link).hasClass("bbox")) {
41 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
44 if (layers && $(link).hasClass("layers")) {
48 if (objtype && $(link).hasClass("object")) {
49 args[objtype] = objid;
52 var classes = $(link).attr("class").split(" ");
54 $(classes).each(function (index, classname) {
55 if (match = classname.match(/^minzoom([0-9]+)$/)) {
56 var minzoom = match[1];
57 var name = link.id.replace(/anchor$/, "");
59 $(link).off("click.minzoom");
61 if (zoom >= minzoom) {
62 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
63 $(link).removeClass("disabled");
65 $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
66 $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
67 $(link).addClass("disabled");
72 link.href = setArgs(link.href, args);
75 $("#shortlinkanchor").each(function () {
76 var args = getArgs(this.href);
77 var code = makeShortCode(lat, lon, zoom);
78 var prefix = shortlinkPrefix();
80 // Add ?{node,way,relation}=id to the arguments
81 if (objtype && objid) {
82 args[objtype] = objid;
85 // This is a hack to omit the default mapnik layer from the shortlink.
86 if (layers && layers != "M") {
93 // Here we're assuming that all parameters but ?layers= and
94 // ?{node,way,relation}= can be safely omitted from the shortlink
95 // which encodes lat/lon/zoom. If new URL parameters are added to
96 // the main slippy map this needs to be changed.
97 if (args.layers || args[objtype]) {
98 this.href = setArgs(prefix + "/go/" + code, args);
100 this.href = prefix + "/go/" + code;
106 * Get the URL prefix to use for a short link
108 function shortlinkPrefix() {
109 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
110 return "http://osm.org";
117 * Called to get the arguments from a URL as a hash.
119 function getArgs(url) {
121 var querystart = url.indexOf("?");
123 if (querystart >= 0) {
124 var querystring = url.substring(querystart + 1);
125 var queryitems = querystring.split("&");
127 for (var i = 0; i < queryitems.length; i++) {
128 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
129 args[unescape(match[1])] = unescape(match[2]);
131 args[unescape(queryitems[i])] = null;
140 * Called to set the arguments on a URL from the given hash.
142 function setArgs(url, args) {
146 if (args[arg] == null) {
147 queryitems.push(escape(arg));
149 queryitems.push(escape(arg) + "=" + escape(args[arg]));
153 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
157 * Called to interlace the bits in x and y, making a Morton code.
159 function interlace(x, y) {
160 x = (x | (x << 8)) & 0x00ff00ff;
161 x = (x | (x << 4)) & 0x0f0f0f0f;
162 x = (x | (x << 2)) & 0x33333333;
163 x = (x | (x << 1)) & 0x55555555;
165 y = (y | (y << 8)) & 0x00ff00ff;
166 y = (y | (y << 4)) & 0x0f0f0f0f;
167 y = (y | (y << 2)) & 0x33333333;
168 y = (y | (y << 1)) & 0x55555555;
174 * Called to create a short code for the short link.
176 function makeShortCode(lat, lon, zoom) {
177 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
178 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
179 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
180 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
181 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
182 // and drops the last 4 bits of the full 64 bit Morton code.
184 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
185 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
186 digit = (c1 >> (24 - 6 * i)) & 0x3f;
187 str += char_array.charAt(digit);
189 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
190 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
191 str += char_array.charAt(digit);
193 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
200 * Click handler to switch a rich text control to preview mode
202 function previewRichtext(event) {
203 var editor = $(this).parents(".richtext_container").find("textarea");
204 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
205 var width = editor.outerWidth() - preview.outerWidth() + preview.innerWidth();
206 var minHeight = editor.outerHeight() - preview.outerHeight() + preview.innerHeight();
208 if (preview.contents().length == 0) {
209 preview.oneTime(500, "loading", function () {
210 preview.addClass("loading");
213 preview.load(editor.attr("data-preview-url"), { text: editor.val() }, function () {
214 preview.stopTime("loading");
215 preview.removeClass("loading");
220 preview.width(width);
221 preview.css("min-height", minHeight + "px");
224 $(this).siblings(".richtext_doedit").prop("disabled", false);
225 $(this).prop("disabled", true);
227 event.preventDefault();
231 * Click handler to switch a rich text control to edit mode
233 function editRichtext(event) {
234 var editor = $(this).parents(".richtext_container").find("textarea");
235 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
240 $(this).siblings(".richtext_dopreview").prop("disabled", false);
241 $(this).prop("disabled", true);
243 event.preventDefault();
247 * Setup any rich text controls
249 $(document).ready(function () {
250 $(".richtext_preview").hide();
251 $(".richtext_content textarea").change(function () {
252 $(this).parents(".richtext_container").find(".richtext_preview").empty();
254 $(".richtext_doedit").prop("disabled", true);
255 $(".richtext_dopreview").prop("disabled", false);
256 $(".richtext_doedit").click(editRichtext);
257 $(".richtext_dopreview").click(previewRichtext);
261 * Forms which have been cached by rails may have he wrong
262 * authenticity token, so patch up any forms with the correct
263 * token taken from the page header.
265 $(document).ready(function () {
266 var auth_token = $("meta[name=csrf-token]").attr("content");
267 $("form input[name=authenticity_token]").val(auth_token);
271 * Enable auto expansion for all text areas
273 $(document).ready(function () {
274 $("textarea").autoGrow();