3 //= require jquery.autogrowtextarea
4 //= require jquery.timers
6 //= require i18n/translations
12 * Called as the user scrolls/zooms around to aniplate hrefs of the
13 * view tab and various other links
15 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
16 var decimals = Math.pow(10, Math.floor(zoom/3));
19 lat = Math.round(lat * decimals) / decimals;
20 lon = Math.round(lon * decimals) / decimals;
23 minlon = Math.round(minlon * decimals) / decimals;
24 minlat = Math.round(minlat * decimals) / decimals;
25 maxlon = Math.round(maxlon * decimals) / decimals;
26 maxlat = Math.round(maxlat * decimals) / decimals;
29 $(".geolink").each(function (index, link) {
30 var args = getArgs(link.href);
32 if ($(link).hasClass("llz")) {
36 } else if (minlon && $(link).hasClass("bbox")) {
37 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
40 if (layers && $(link).hasClass("layers")) {
44 if (objtype && $(link).hasClass("object")) {
45 args[objtype] = objid;
48 var classes = $(link).attr("class").split(" ");
50 $(classes).each(function (index, classname) {
51 if (match = classname.match(/^minzoom([0-9]+)$/)) {
52 var minzoom = match[1];
53 var name = link.id.replace(/anchor$/, "");
55 $(link).off("click.minzoom");
57 if (zoom >= minzoom) {
58 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
59 $(link).removeClass("disabled");
61 $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
62 $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
63 $(link).addClass("disabled");
68 link.href = setArgs(link.href, args);
71 $("#shortlinkanchor").each(function () {
72 var args = getArgs(this.href);
73 var code = makeShortCode(lat, lon, zoom);
74 var prefix = shortlinkPrefix();
76 // Add ?{node,way,relation}=id to the arguments
77 if (objtype && objid) {
78 args[objtype] = objid;
81 // This is a hack to omit the default mapnik layer from the shortlink.
82 if (layers && layers != "M") {
89 // Here we're assuming that all parameters but ?layers= and
90 // ?{node,way,relation}= can be safely omitted from the shortlink
91 // which encodes lat/lon/zoom. If new URL parameters are added to
92 // the main slippy map this needs to be changed.
93 if (args.layers || args[objtype]) {
94 this.href = setArgs(prefix + "/go/" + code, args);
96 this.href = prefix + "/go/" + code;
102 * Get the URL prefix to use for a short link
104 function shortlinkPrefix() {
105 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
106 return "http://osm.org";
113 * Called to get the arguments from a URL as a hash.
115 function getArgs(url) {
117 var querystart = url.indexOf("?");
119 if (querystart >= 0) {
120 var querystring = url.substring(querystart + 1);
121 var queryitems = querystring.split("&");
123 for (var i = 0; i < queryitems.length; i++) {
124 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
125 args[unescape(match[1])] = unescape(match[2]);
127 args[unescape(queryitems[i])] = null;
136 * Called to set the arguments on a URL from the given hash.
138 function setArgs(url, args) {
142 if (args[arg] == null) {
143 queryitems.push(escape(arg));
145 queryitems.push(escape(arg) + "=" + escape(args[arg]));
149 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
153 * Called to interlace the bits in x and y, making a Morton code.
155 function interlace(x, y) {
156 x = (x | (x << 8)) & 0x00ff00ff;
157 x = (x | (x << 4)) & 0x0f0f0f0f;
158 x = (x | (x << 2)) & 0x33333333;
159 x = (x | (x << 1)) & 0x55555555;
161 y = (y | (y << 8)) & 0x00ff00ff;
162 y = (y | (y << 4)) & 0x0f0f0f0f;
163 y = (y | (y << 2)) & 0x33333333;
164 y = (y | (y << 1)) & 0x55555555;
170 * Called to create a short code for the short link.
172 function makeShortCode(lat, lon, zoom) {
173 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
174 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
175 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
176 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
177 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
178 // and drops the last 4 bits of the full 64 bit Morton code.
180 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
181 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
182 digit = (c1 >> (24 - 6 * i)) & 0x3f;
183 str += char_array.charAt(digit);
185 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
186 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
187 str += char_array.charAt(digit);
189 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
196 * Click handler to switch a rich text control to preview mode
198 function previewRichtext(event) {
199 var editor = $(this).parents(".richtext_container").find("textarea");
200 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
201 var width = editor.outerWidth() - preview.outerWidth() + preview.innerWidth();
202 var minHeight = editor.outerHeight() - preview.outerHeight() + preview.innerHeight();
204 if (preview.contents().length == 0) {
205 preview.oneTime(500, "loading", function () {
206 preview.addClass("loading");
209 preview.load(editor.attr("data-preview-url"), { text: editor.val() }, function () {
210 preview.stopTime("loading");
211 preview.removeClass("loading");
216 preview.width(width);
217 preview.css("min-height", minHeight + "px");
220 $(this).siblings(".richtext_doedit").prop("disabled", false);
221 $(this).prop("disabled", true);
223 event.preventDefault();
227 * Click handler to switch a rich text control to edit mode
229 function editRichtext(event) {
230 var editor = $(this).parents(".richtext_container").find("textarea");
231 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
236 $(this).siblings(".richtext_dopreview").prop("disabled", false);
237 $(this).prop("disabled", true);
239 event.preventDefault();
243 * Setup any rich text controls
245 $(document).ready(function () {
246 $(".richtext_preview").hide();
247 $(".richtext_content textarea").change(function () {
248 $(this).parents(".richtext_container").find(".richtext_preview").empty();
250 $(".richtext_doedit").prop("disabled", true);
251 $(".richtext_dopreview").prop("disabled", false);
252 $(".richtext_doedit").click(editRichtext);
253 $(".richtext_dopreview").click(previewRichtext);