3 //= require jquery.autogrowtextarea
4 //= require jquery.timers
7 * Called as the user scrolls/zooms around to aniplate hrefs of the
8 * view tab and various other links
10 function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
11 var decimals = Math.pow(10, Math.floor(zoom/3));
14 lat = Math.round(lat * decimals) / decimals;
15 lon = Math.round(lon * decimals) / decimals;
18 minlon = Math.round(minlon * decimals) / decimals;
19 minlat = Math.round(minlat * decimals) / decimals;
20 maxlon = Math.round(maxlon * decimals) / decimals;
21 maxlat = Math.round(maxlat * decimals) / decimals;
24 $(".geolink").each(function (index, link) {
25 var args = getArgs(link.href);
27 if ($(link).hasClass("llz")) {
31 } else if (minlon && $(link).hasClass("bbox")) {
32 args.bbox = minlon + "," + minlat + "," + maxlon + "," + maxlat;
35 if (layers && $(link).hasClass("layers")) {
39 if (objtype && $(link).hasClass("object")) {
40 args[objtype] = objid;
43 var classes = $(link).attr("class").split(" ");
45 $(classes).each(function (index, classname) {
46 if (match = classname.match(/^minzoom([0-9]+)$/)) {
47 var minzoom = match[1];
48 var name = link.id.replace(/anchor$/, "");
50 $(link).off("click.minzoom");
52 if (zoom >= minzoom) {
53 $(link).attr("title", i18n("javascripts.site." + name + "_tooltip"));
54 $(link).removeClass("disabled");
56 $(link).on("click.minzoom", function () { alert(i18n("javascripts.site." + name + "_zoom_alert")); return false; });
57 $(link).attr("title", i18n("javascripts.site." + name + "_disabled_tooltip"));
58 $(link).addClass("disabled");
63 link.href = setArgs(link.href, args);
66 $("#shortlinkanchor").each(function () {
67 var args = getArgs(this.href);
68 var code = makeShortCode(lat, lon, zoom);
69 var prefix = shortlinkPrefix();
71 // Add ?{node,way,relation}=id to the arguments
72 if (objtype && objid) {
73 args[objtype] = objid;
76 // This is a hack to omit the default mapnik layer from the shortlink.
77 if (layers && layers != "M") {
84 // Here we're assuming that all parameters but ?layers= and
85 // ?{node,way,relation}= can be safely omitted from the shortlink
86 // which encodes lat/lon/zoom. If new URL parameters are added to
87 // the main slippy map this needs to be changed.
88 if (args.layers || args[objtype]) {
89 this.href = setArgs(prefix + "/go/" + code, args);
91 this.href = prefix + "/go/" + code;
97 * Get the URL prefix to use for a short link
99 function shortlinkPrefix() {
100 if (window.location.hostname.match(/^www\.openstreetmap\.org/i)) {
101 return "http://osm.org";
108 * Called to get the arguments from a URL as a hash.
110 function getArgs(url) {
112 var querystart = url.indexOf("?");
114 if (querystart >= 0) {
115 var querystring = url.substring(querystart + 1);
116 var queryitems = querystring.split("&");
118 for (var i = 0; i < queryitems.length; i++) {
119 if (match = queryitems[i].match(/^(.*)=(.*)$/)) {
120 args[unescape(match[1])] = unescape(match[2]);
122 args[unescape(queryitems[i])] = null;
131 * Called to set the arguments on a URL from the given hash.
133 function setArgs(url, args) {
137 if (args[arg] == null) {
138 queryitems.push(escape(arg));
140 queryitems.push(escape(arg) + "=" + escape(args[arg]));
144 return url.replace(/\?.*$/, "") + "?" + queryitems.join("&");
148 * Called to interpolate JavaScript variables in strings using a
149 * similar syntax to rails I18n string interpolation - the only
150 * difference is that [[foo]] is the placeholder syntax instead
151 * of {{foo}} which allows the same string to be processed by both
152 * rails and then later by javascript.
154 function i18n(string, keys) {
155 string = i18n_strings[string] || string;
157 for (var key in keys) {
158 var re_key = '\\[\\[' + key + '\\]\\]';
159 var re = new RegExp(re_key, "g");
161 string = string.replace(re, keys[key]);
168 * Called to interlace the bits in x and y, making a Morton code.
170 function interlace(x, y) {
171 x = (x | (x << 8)) & 0x00ff00ff;
172 x = (x | (x << 4)) & 0x0f0f0f0f;
173 x = (x | (x << 2)) & 0x33333333;
174 x = (x | (x << 1)) & 0x55555555;
176 y = (y | (y << 8)) & 0x00ff00ff;
177 y = (y | (y << 4)) & 0x0f0f0f0f;
178 y = (y | (y << 2)) & 0x33333333;
179 y = (y | (y << 1)) & 0x55555555;
185 * Called to create a short code for the short link.
187 function makeShortCode(lat, lon, zoom) {
188 char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
189 var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
190 var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
191 // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
192 // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
193 // and drops the last 4 bits of the full 64 bit Morton code.
195 var c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff);
196 for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
197 digit = (c1 >> (24 - 6 * i)) & 0x3f;
198 str += char_array.charAt(digit);
200 for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
201 digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
202 str += char_array.charAt(digit);
204 for (var i = 0; i < ((zoom + 8) % 3); ++i) {
211 * Click handler to switch a rich text control to preview mode
213 function previewRichtext(event) {
214 var editor = $(this).parents(".richtext_container").find("textarea");
215 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
216 var width = editor.outerWidth() - preview.outerWidth() + preview.innerWidth();
217 var minHeight = editor.outerHeight() - preview.outerHeight() + preview.innerHeight();
219 if (preview.contents().length == 0) {
220 preview.oneTime(500, "loading", function () {
221 preview.addClass("loading");
224 preview.load(editor.attr("data-preview-url"), { text: editor.val() }, function () {
225 preview.stopTime("loading");
226 preview.removeClass("loading");
231 preview.width(width);
232 preview.css("min-height", minHeight + "px");
235 $(this).siblings(".richtext_doedit").prop("disabled", false);
236 $(this).prop("disabled", true);
238 event.preventDefault();
242 * Click handler to switch a rich text control to edit mode
244 function editRichtext(event) {
245 var editor = $(this).parents(".richtext_container").find("textarea");
246 var preview = $(this).parents(".richtext_container").find(".richtext_preview");
251 $(this).siblings(".richtext_dopreview").prop("disabled", false);
252 $(this).prop("disabled", true);
254 event.preventDefault();
258 * Setup any rich text controls
260 $(document).ready(function () {
261 $(".richtext_preview").hide();
262 $(".richtext_content textarea").change(function () {
263 $(this).parents(".richtext_container").find(".richtext_preview").empty();
265 $(".richtext_doedit").prop("disabled", true);
266 $(".richtext_dopreview").prop("disabled", false);
267 $(".richtext_doedit").click(editRichtext);
268 $(".richtext_dopreview").click(previewRichtext);