3 //= require jquery.autogrowtextarea
4 //= require jquery.timers
5 //= require i18n/translations
10 * Called as the user scrolls/zooms around to aniplate hrefs of the
11 * view tab and various other links
13 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
14 var decimals = Math.pow(10, Math.floor(zoom/3));
17 lat = Math.round(lat * decimals) / decimals;
18 lon = Math.round(lon * decimals) / decimals;
21 minlon = Math.round(minlon * decimals) / decimals;
22 minlat = Math.round(minlat * decimals) / decimals;
23 maxlon = Math.round(maxlon * decimals) / decimals;
24 maxlat = Math.round(maxlat * decimals) / decimals;
27 $(".geolink").each(function (index, link) {
28 var args = getArgs(link.href);
30 if ($(link).hasClass("llz")) {
34 } else if (minlon && $(link).hasClass("bbox")) {
35 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
38 if (layers && $(link).hasClass("layers")) {
42 if (objtype && $(link).hasClass("object")) {
43 args[objtype] = objid;
46 var classes = $(link).attr("class").split(" ");
48 $(classes).each(function (index, classname) {
49 if (match = classname.match(/^minzoom([0-9]+)$/)) {
50 var minzoom = match[1];
51 var name = link.id.replace(/anchor$/, "");
53 $(link).off("click.minzoom");
55 if (zoom >= minzoom) {
56 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
57 $(link).removeClass("disabled");
59 $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
60 $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
61 $(link).addClass("disabled");
66 link.href = setArgs(link.href, args);
69 $("#shortlinkanchor").each(function () {
70 var args = getArgs(this.href);
71 var code = makeShortCode(lat, lon, zoom);
72 var prefix = shortlinkPrefix();
74 // Add ?{node,way,relation}=id to the arguments
75 if (objtype && objid) {
76 args[objtype] = objid;
79 // This is a hack to omit the default mapnik layer from the shortlink.
80 if (layers && layers != "M") {
87 // Here we're assuming that all parameters but ?layers= and
88 // ?{node,way,relation}= can be safely omitted from the shortlink
89 // which encodes lat/lon/zoom. If new URL parameters are added to
90 // the main slippy map this needs to be changed.
91 if (args.layers || args[objtype]) {
92 this.href = setArgs(prefix + "/go/" + code, args);
94 this.href = prefix + "/go/" + code;
100 * Get the URL prefix to use for a short link
102 function shortlinkPrefix() {
103 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
104 return "http://osm.org";
111 * Called to get the arguments from a URL as a hash.
113 function getArgs(url) {
115 var querystart = url.indexOf("?");
117 if (querystart >= 0) {
118 var querystring = url.substring(querystart + 1);
119 var queryitems = querystring.split("&");
121 for (var i = 0; i < queryitems.length; i++) {
122 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
123 args[unescape(match[1])] = unescape(match[2]);
125 args[unescape(queryitems[i])] = null;
134 * Called to set the arguments on a URL from the given hash.
136 function setArgs(url, args) {
140 if (args[arg] == null) {
141 queryitems.push(escape(arg));
143 queryitems.push(escape(arg) + "=" + escape(args[arg]));
147 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
151 * Called to interlace the bits in x and y, making a Morton code.
153 function interlace(x, y) {
154 x = (x | (x << 8)) & 0x00ff00ff;
155 x = (x | (x << 4)) & 0x0f0f0f0f;
156 x = (x | (x << 2)) & 0x33333333;
157 x = (x | (x << 1)) & 0x55555555;
159 y = (y | (y << 8)) & 0x00ff00ff;
160 y = (y | (y << 4)) & 0x0f0f0f0f;
161 y = (y | (y << 2)) & 0x33333333;
162 y = (y | (y << 1)) & 0x55555555;
168 * Called to create a short code for the short link.
170 function makeShortCode(lat, lon, zoom) {
171 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
172 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
173 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
174 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
175 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
176 // and drops the last 4 bits of the full 64 bit Morton code.
178 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
179 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
180 digit = (c1 >> (24 - 6 * i)) & 0x3f;
181 str += char_array.charAt(digit);
183 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
184 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
185 str += char_array.charAt(digit);
187 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
194 * Click handler to switch a rich text control to preview mode
196 function previewRichtext(event) {
197 var editor = $(this).parents(".richtext_container").find("textarea");
198 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
199 var width = editor.outerWidth() - preview.outerWidth() + preview.innerWidth();
200 var minHeight = editor.outerHeight() - preview.outerHeight() + preview.innerHeight();
202 if (preview.contents().length == 0) {
203 preview.oneTime(500, "loading", function () {
204 preview.addClass("loading");
207 preview.load(editor.attr("data-preview-url"), { text: editor.val() }, function () {
208 preview.stopTime("loading");
209 preview.removeClass("loading");
214 preview.width(width);
215 preview.css("min-height", minHeight + "px");
218 $(this).siblings(".richtext_doedit").prop("disabled", false);
219 $(this).prop("disabled", true);
221 event.preventDefault();
225 * Click handler to switch a rich text control to edit mode
227 function editRichtext(event) {
228 var editor = $(this).parents(".richtext_container").find("textarea");
229 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
234 $(this).siblings(".richtext_dopreview").prop("disabled", false);
235 $(this).prop("disabled", true);
237 event.preventDefault();
241 * Setup any rich text controls
243 $(document).ready(function () {
244 $(".richtext_preview").hide();
245 $(".richtext_content textarea").change(function () {
246 $(this).parents(".richtext_container").find(".richtext_preview").empty();
248 $(".richtext_doedit").prop("disabled", true);
249 $(".richtext_dopreview").prop("disabled", false);
250 $(".richtext_doedit").click(editRichtext);
251 $(".richtext_dopreview").click(previewRichtext);