3 //= require jquery.autogrowtextarea
4 //= require jquery.timers
6 //= require i18n/translations
14 * Called as the user scrolls/zooms around to aniplate hrefs of the
15 * view tab and various other links
17 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
18 var decimals = Math.pow(10, Math.floor(zoom/3));
21 lat = Math.round(lat * decimals) / decimals;
22 lon = Math.round(lon * decimals) / decimals;
25 minlon = Math.round(minlon * decimals) / decimals;
26 minlat = Math.round(minlat * decimals) / decimals;
27 maxlon = Math.round(maxlon * decimals) / decimals;
28 maxlat = Math.round(maxlat * decimals) / decimals;
31 $(".geolink").each(function (index, link) {
32 var args = getArgs(link.href);
34 if ($(link).hasClass("llz")) {
38 } else if (minlon && $(link).hasClass("bbox")) {
39 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
42 if (layers && $(link).hasClass("layers")) {
46 if (objtype && $(link).hasClass("object")) {
47 args[objtype] = objid;
50 var classes = $(link).attr("class").split(" ");
52 $(classes).each(function (index, classname) {
53 if (match = classname.match(/^minzoom([0-9]+)$/)) {
54 var minzoom = match[1];
55 var name = link.id.replace(/anchor$/, "");
57 $(link).off("click.minzoom");
59 if (zoom >= minzoom) {
60 $(link).attr("title", I18n.t("javascripts.site." + name + "_tooltip"));
61 $(link).removeClass("disabled");
63 $(link).on("click.minzoom", function () { alert(I18n.t("javascripts.site." + name + "_zoom_alert")); return false; });
64 $(link).attr("title", I18n.t("javascripts.site." + name + "_disabled_tooltip"));
65 $(link).addClass("disabled");
70 link.href = setArgs(link.href, args);
73 $("#shortlinkanchor").each(function () {
74 var args = getArgs(this.href);
75 var code = makeShortCode(lat, lon, zoom);
76 var prefix = shortlinkPrefix();
78 // Add ?{node,way,relation}=id to the arguments
79 if (objtype && objid) {
80 args[objtype] = objid;
83 // This is a hack to omit the default mapnik layer from the shortlink.
84 if (layers && layers != "M") {
91 // Here we're assuming that all parameters but ?layers= and
92 // ?{node,way,relation}= can be safely omitted from the shortlink
93 // which encodes lat/lon/zoom. If new URL parameters are added to
94 // the main slippy map this needs to be changed.
95 if (args.layers || args[objtype]) {
96 this.href = setArgs(prefix + "/go/" + code, args);
98 this.href = prefix + "/go/" + code;
104 * Get the URL prefix to use for a short link
106 function shortlinkPrefix() {
107 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
108 return "http://osm.org";
115 * Called to get the arguments from a URL as a hash.
117 function getArgs(url) {
119 var querystart = url.indexOf("?");
121 if (querystart >= 0) {
122 var querystring = url.substring(querystart + 1);
123 var queryitems = querystring.split("&");
125 for (var i = 0; i < queryitems.length; i++) {
126 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
127 args[unescape(match[1])] = unescape(match[2]);
129 args[unescape(queryitems[i])] = null;
138 * Called to set the arguments on a URL from the given hash.
140 function setArgs(url, args) {
144 if (args[arg] == null) {
145 queryitems.push(escape(arg));
147 queryitems.push(escape(arg) + "=" + escape(args[arg]));
151 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
155 * Called to interlace the bits in x and y, making a Morton code.
157 function interlace(x, y) {
158 x = (x | (x << 8)) & 0x00ff00ff;
159 x = (x | (x << 4)) & 0x0f0f0f0f;
160 x = (x | (x << 2)) & 0x33333333;
161 x = (x | (x << 1)) & 0x55555555;
163 y = (y | (y << 8)) & 0x00ff00ff;
164 y = (y | (y << 4)) & 0x0f0f0f0f;
165 y = (y | (y << 2)) & 0x33333333;
166 y = (y | (y << 1)) & 0x55555555;
172 * Called to create a short code for the short link.
174 function makeShortCode(lat, lon, zoom) {
175 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
176 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
177 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
178 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
179 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
180 // and drops the last 4 bits of the full 64 bit Morton code.
182 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
183 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
184 digit = (c1 >> (24 - 6 * i)) & 0x3f;
185 str += char_array.charAt(digit);
187 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
188 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
189 str += char_array.charAt(digit);
191 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
198 * Click handler to switch a rich text control to preview mode
200 function previewRichtext(event) {
201 var editor = $(this).parents(".richtext_container").find("textarea");
202 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
203 var width = editor.outerWidth() - preview.outerWidth() + preview.innerWidth();
204 var minHeight = editor.outerHeight() - preview.outerHeight() + preview.innerHeight();
206 if (preview.contents().length == 0) {
207 preview.oneTime(500, "loading", function () {
208 preview.addClass("loading");
211 preview.load(editor.attr("data-preview-url"), { text: editor.val() }, function () {
212 preview.stopTime("loading");
213 preview.removeClass("loading");
218 preview.width(width);
219 preview.css("min-height", minHeight + "px");
222 $(this).siblings(".richtext_doedit").prop("disabled", false);
223 $(this).prop("disabled", true);
225 event.preventDefault();
229 * Click handler to switch a rich text control to edit mode
231 function editRichtext(event) {
232 var editor = $(this).parents(".richtext_container").find("textarea");
233 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
238 $(this).siblings(".richtext_dopreview").prop("disabled", false);
239 $(this).prop("disabled", true);
241 event.preventDefault();
245 * Setup any rich text controls
247 $(document).ready(function () {
248 $(".richtext_preview").hide();
249 $(".richtext_content textarea").change(function () {
250 $(this).parents(".richtext_container").find(".richtext_preview").empty();
252 $(".richtext_doedit").prop("disabled", true);
253 $(".richtext_dopreview").prop("disabled", false);
254 $(".richtext_doedit").click(editRichtext);
255 $(".richtext_dopreview").click(previewRichtext);