3 //= require jquery.autogrowtextarea
4 //= require jquery.timers
6 //= require i18n/translations
14 if ( !Array.prototype.forEach ) {
15 Array.prototype.forEach = function(fn, scope) {
16 for(var i = 0, len = this.length; i < len; ++i) {
17 fn.call(scope || this, this[i], i, this);
23 * Called as the user scrolls/zooms around to aniplate hrefs of the
24 * view tab and various other links
26 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
27 var decimals = Math.pow(10, Math.floor(zoom/3));
30 lat = Math.round(lat * decimals) / decimals;
31 lon = Math.round(lon * decimals) / decimals;
34 minlon = Math.round(minlon * decimals) / decimals;
35 minlat = Math.round(minlat * decimals) / decimals;
36 maxlon = Math.round(maxlon * decimals) / decimals;
37 maxlat = Math.round(maxlat * decimals) / decimals;
40 $(".geolink").each(function (index, link) {
41 var args = getArgs(link.href);
43 if ($(link).hasClass("llz")) {
47 } else if (minlon && $(link).hasClass("bbox")) {
48 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
51 if (layers && $(link).hasClass("layers")) {
55 if (objtype && $(link).hasClass("object")) {
56 args[objtype] = objid;
59 var classes = $(link).attr("class").split(" ");
61 $(classes).each(function (index, classname) {
62 if (match = classname.match(/^minzoom([0-9]+)$/)) {
63 var minzoom = match[1];
64 var name = link.id.replace(/anchor$/, "");
66 $(link).off("click.minzoom");
68 if (zoom >= minzoom) {
69 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
70 $(link).removeClass("disabled");
72 $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
73 $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
74 $(link).addClass("disabled");
79 link.href = setArgs(link.href, args);
82 $("#shortlinkanchor").each(function () {
83 var args = getArgs(this.href);
84 var code = makeShortCode(lat, lon, zoom);
85 var prefix = shortlinkPrefix();
87 // Add ?{node,way,relation}=id to the arguments
88 if (objtype && objid) {
89 args[objtype] = objid;
92 // This is a hack to omit the default mapnik layer from the shortlink.
93 if (layers && layers != "M") {
100 // Here we're assuming that all parameters but ?layers= and
101 // ?{node,way,relation}= can be safely omitted from the shortlink
102 // which encodes lat/lon/zoom. If new URL parameters are added to
103 // the main slippy map this needs to be changed.
104 if (args.layers || args[objtype]) {
105 this.href = setArgs(prefix + "/go/" + code, args);
107 this.href = prefix + "/go/" + code;
113 * Get the URL prefix to use for a short link
115 function shortlinkPrefix() {
116 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
117 return "http://osm.org";
124 * Called to get the arguments from a URL as a hash.
126 function getArgs(url) {
128 var querystart = url.indexOf("?");
130 if (querystart >= 0) {
131 var querystring = url.substring(querystart + 1);
132 var queryitems = querystring.split("&");
134 for (var i = 0; i < queryitems.length; i++) {
135 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
136 args[unescape(match[1])] = unescape(match[2]);
138 args[unescape(queryitems[i])] = null;
147 * Called to set the arguments on a URL from the given hash.
149 function setArgs(url, args) {
153 if (args[arg] == null) {
154 queryitems.push(escape(arg));
156 queryitems.push(escape(arg) + "=" + escape(args[arg]));
160 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
164 * Called to interlace the bits in x and y, making a Morton code.
166 function interlace(x, y) {
167 x = (x | (x << 8)) & 0x00ff00ff;
168 x = (x | (x << 4)) & 0x0f0f0f0f;
169 x = (x | (x << 2)) & 0x33333333;
170 x = (x | (x << 1)) & 0x55555555;
172 y = (y | (y << 8)) & 0x00ff00ff;
173 y = (y | (y << 4)) & 0x0f0f0f0f;
174 y = (y | (y << 2)) & 0x33333333;
175 y = (y | (y << 1)) & 0x55555555;
181 * Called to create a short code for the short link.
183 function makeShortCode(lat, lon, zoom) {
184 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
185 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
186 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
187 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
188 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
189 // and drops the last 4 bits of the full 64 bit Morton code.
191 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
192 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
193 digit = (c1 >> (24 - 6 * i)) & 0x3f;
194 str += char_array.charAt(digit);
196 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
197 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
198 str += char_array.charAt(digit);
200 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
207 * Click handler to switch a rich text control to preview mode
209 function previewRichtext(event) {
210 var editor = $(this).parents(".richtext_container").find("textarea");
211 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
212 var width = editor.outerWidth() - preview.outerWidth() + preview.innerWidth();
213 var minHeight = editor.outerHeight() - preview.outerHeight() + preview.innerHeight();
215 if (preview.contents().length == 0) {
216 preview.oneTime(500, "loading", function () {
217 preview.addClass("loading");
220 preview.load(editor.attr("data-preview-url"), { text: editor.val() }, function () {
221 preview.stopTime("loading");
222 preview.removeClass("loading");
227 preview.width(width);
228 preview.css("min-height", minHeight + "px");
231 $(this).siblings(".richtext_doedit").prop("disabled", false);
232 $(this).prop("disabled", true);
234 event.preventDefault();
238 * Click handler to switch a rich text control to edit mode
240 function editRichtext(event) {
241 var editor = $(this).parents(".richtext_container").find("textarea");
242 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
247 $(this).siblings(".richtext_dopreview").prop("disabled", false);
248 $(this).prop("disabled", true);
250 event.preventDefault();
254 * Setup any rich text controls
256 $(document).ready(function () {
257 $(".richtext_preview").hide();
258 $(".richtext_content textarea").change(function () {
259 $(this).parents(".richtext_container").find(".richtext_preview").empty();
261 $(".richtext_doedit").prop("disabled", true);
262 $(".richtext_dopreview").prop("disabled", false);
263 $(".richtext_doedit").click(editRichtext);
264 $(".richtext_dopreview").click(previewRichtext);
268 * Forms which have been cached by rails may have he wrong
269 * authenticity token, so patch up any forms with the correct
270 * token taken from the page header.
272 $(document).ready(function () {
273 var auth_token = $("meta[name=csrf-token]").attr("content");
274 $("form input[name=authenticity_token]").val(auth_token);
278 * Enable auto expansion for all text areas
280 $(document).ready(function () {
281 $("textarea").autoGrow();