3 //= require jquery.autogrowtextarea
4 //= require jquery.timers
7 //= require i18n/translations
16 function zoomPrecision(zoom) {
17 var decimals = Math.pow(10, Math.floor(zoom/3));
19 return Math.round(x * decimals) / decimals;
24 * Called as the user scrolls/zooms around to aniplate hrefs of the
25 * view tab and various other links
27 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
28 var toPrecision = zoomPrecision(zoom);
31 lat = toPrecision(lat);
32 lon = toPrecision(lon);
35 minlon = toPrecision(minlon);
36 minlat = toPrecision(minlat);
37 maxlon = toPrecision(maxlon);
38 maxlat = toPrecision(maxlat);
41 $(".geolink").each(function (index, link) {
42 var args = getArgs(link.href);
44 if ($(link).hasClass("llz")) {
48 } else if (minlon && $(link).hasClass("bbox")) {
49 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
52 if (layers && $(link).hasClass("layers")) {
56 if (objtype && $(link).hasClass("object")) {
57 args[objtype] = objid;
60 var classes = $(link).attr("class").split(" ");
62 $(classes).each(function (index, classname) {
63 if (match = classname.match(/^minzoom([0-9]+)$/)) {
64 var minzoom = match[1];
65 var name = link.id.replace(/anchor$/, "");
67 $(link).off("click.minzoom");
69 if (zoom >= minzoom) {
70 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
71 $(link).removeClass("disabled");
73 $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
74 $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
75 $(link).addClass("disabled");
80 link.href = setArgs(link.href, args);
83 $("#shortlinkanchor").each(function () {
84 var args = getArgs(this.href);
85 var code = makeShortCode(lat, lon, zoom);
86 var prefix = shortlinkPrefix();
88 // Add ?{node,way,relation}=id to the arguments
89 if (objtype && objid) {
90 args[objtype] = objid;
93 // This is a hack to omit the default mapnik layer from the shortlink.
94 if (layers && layers != "M") {
101 // Here we're assuming that all parameters but ?layers= and
102 // ?{node,way,relation}= can be safely omitted from the shortlink
103 // which encodes lat/lon/zoom. If new URL parameters are added to
104 // the main slippy map this needs to be changed.
105 if (args.layers || args[objtype]) {
106 this.href = setArgs(prefix + "/go/" + code, args);
108 this.href = prefix + "/go/" + code;
114 * Get the URL prefix to use for a short link
116 function shortlinkPrefix() {
117 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
118 return "http://osm.org";
125 * Called to get the arguments from a URL as a hash.
127 function getArgs(url) {
129 var querystart = url.indexOf("?");
131 if (querystart >= 0) {
132 var querystring = url.substring(querystart + 1);
133 var queryitems = querystring.split("&");
135 for (var i = 0; i < queryitems.length; i++) {
136 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
137 args[unescape(match[1])] = unescape(match[2]);
139 args[unescape(queryitems[i])] = null;
148 * Called to set the arguments on a URL from the given hash.
150 function setArgs(url, args) {
154 if (args[arg] == null) {
155 queryitems.push(escape(arg));
157 queryitems.push(escape(arg) + "=" + escape(args[arg]));
161 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
165 * Called to interlace the bits in x and y, making a Morton code.
167 function interlace(x, y) {
168 x = (x | (x << 8)) & 0x00ff00ff;
169 x = (x | (x << 4)) & 0x0f0f0f0f;
170 x = (x | (x << 2)) & 0x33333333;
171 x = (x | (x << 1)) & 0x55555555;
173 y = (y | (y << 8)) & 0x00ff00ff;
174 y = (y | (y << 4)) & 0x0f0f0f0f;
175 y = (y | (y << 2)) & 0x33333333;
176 y = (y | (y << 1)) & 0x55555555;
182 * Called to create a short code for the short link.
184 function makeShortCode(lat, lon, zoom) {
185 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
186 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
187 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
188 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
189 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
190 // and drops the last 4 bits of the full 64 bit Morton code.
192 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
193 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
194 digit = (c1 >> (24 - 6 * i)) & 0x3f;
195 str += char_array.charAt(digit);
197 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
198 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
199 str += char_array.charAt(digit);
201 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
208 * Click handler to switch a rich text control to preview mode
210 function previewRichtext(event) {
211 var editor = $(this).parents(".richtext_container").find("textarea");
212 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
213 var width = editor.outerWidth() - preview.outerWidth() + preview.innerWidth();
214 var minHeight = editor.outerHeight() - preview.outerHeight() + preview.innerHeight();
216 if (preview.contents().length == 0) {
217 preview.oneTime(500, "loading", function () {
218 preview.addClass("loading");
221 preview.load(editor.attr("data-preview-url"), { text: editor.val() }, function () {
222 preview.stopTime("loading");
223 preview.removeClass("loading");
228 preview.width(width);
229 preview.css("min-height", minHeight + "px");
232 $(this).siblings(".richtext_doedit").prop("disabled", false);
233 $(this).prop("disabled", true);
235 event.preventDefault();
239 * Click handler to switch a rich text control to edit mode
241 function editRichtext(event) {
242 var editor = $(this).parents(".richtext_container").find("textarea");
243 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
248 $(this).siblings(".richtext_dopreview").prop("disabled", false);
249 $(this).prop("disabled", true);
251 event.preventDefault();
255 * Setup any rich text controls
257 $(document).ready(function () {
258 $(".richtext_preview").hide();
259 $(".richtext_content textarea").change(function () {
260 $(this).parents(".richtext_container").find(".richtext_preview").empty();
262 $(".richtext_doedit").prop("disabled", true);
263 $(".richtext_dopreview").prop("disabled", false);
264 $(".richtext_doedit").click(editRichtext);
265 $(".richtext_dopreview").click(previewRichtext);
269 * Forms which have been cached by rails may have he wrong
270 * authenticity token, so patch up any forms with the correct
271 * token taken from the page header.
273 $(document).ready(function () {
274 var auth_token = $("meta[name=csrf-token]").attr("content");
275 $("form input[name=authenticity_token]").val(auth_token);
279 * Enable auto expansion for all text areas
281 $(document).ready(function () {
282 $("textarea").autoGrow();