2018-03-16 20:49:58 +01:00
|
|
|
const perfectScrollbar = require('perfect-scrollbar');
|
2017-10-17 02:22:24 +02:00
|
|
|
|
2017-10-31 06:46:35 +01:00
|
|
|
function registerCodeSection($codeSection) {
|
|
|
|
const $li = $codeSection.find("ul.nav li");
|
|
|
|
const $blocks = $codeSection.find(".blocks div");
|
|
|
|
|
|
|
|
$li.click(function () {
|
|
|
|
const language = this.dataset.language;
|
|
|
|
|
|
|
|
$li.removeClass("active");
|
|
|
|
$li.filter("[data-language="+language+"]").addClass("active");
|
|
|
|
|
|
|
|
$blocks.removeClass("active");
|
|
|
|
$blocks.filter("[data-language="+language+"]").addClass("active");
|
|
|
|
});
|
|
|
|
|
|
|
|
$li.eq(0).click();
|
|
|
|
}
|
|
|
|
|
2017-12-24 18:22:33 +01:00
|
|
|
function highlight_current_article() {
|
|
|
|
$('.help .sidebar a').removeClass('highlighted');
|
2017-12-28 19:12:14 +01:00
|
|
|
var path = window.location.href.match(/\/(help|api)\/.*/);
|
2017-12-24 18:22:33 +01:00
|
|
|
|
|
|
|
if (!path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var article = $('.help .sidebar a[href="' + path[0] + '"]');
|
|
|
|
article.addClass('highlighted');
|
|
|
|
}
|
|
|
|
|
2017-12-23 06:11:25 +01:00
|
|
|
function adjust_mac_shortcuts() {
|
|
|
|
var keys_map = new Map([
|
|
|
|
['Backspace', 'Delete'],
|
|
|
|
['Enter', 'Return'],
|
|
|
|
['Home', 'Fn + ⇽'],
|
|
|
|
['End', 'Fn + ⇾'],
|
|
|
|
['PgUp', 'Fn + ↑'],
|
|
|
|
['PgDn', 'Fn + ↓'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$(".markdown .content code").each(function () {
|
|
|
|
var text = $(this).text();
|
|
|
|
|
|
|
|
if (!keys_map.has(text)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var key_string = keys_map.get(text);
|
|
|
|
var keys = key_string.match(/[^\s\+]+/g);
|
|
|
|
|
|
|
|
_.each(keys, function (key) {
|
|
|
|
key_string = key_string.replace(key, '<code>' + key + '</code>');
|
|
|
|
});
|
|
|
|
|
|
|
|
$(this).replaceWith(key_string);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-31 06:46:35 +01:00
|
|
|
function render_code_sections() {
|
|
|
|
$(".code-section").each(function () {
|
|
|
|
registerCodeSection($(this));
|
|
|
|
});
|
2017-12-24 18:22:33 +01:00
|
|
|
|
|
|
|
highlight_current_article();
|
2017-12-23 06:11:25 +01:00
|
|
|
|
|
|
|
if (/Mac/i.test(navigator.userAgent)) {
|
|
|
|
adjust_mac_shortcuts();
|
|
|
|
}
|
2017-10-31 06:46:35 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 20:14:24 +01:00
|
|
|
function scrollToHash(container) {
|
|
|
|
var hash = window.location.hash;
|
|
|
|
if (hash !== '') {
|
|
|
|
container.scrollTop = $(hash).position().top - $('.markdown .content').position().top;
|
|
|
|
} else {
|
|
|
|
container.scrollTop = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 20:50:11 +02:00
|
|
|
(function () {
|
|
|
|
var html_map = {};
|
|
|
|
var loading = {
|
|
|
|
name: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
var fetch_page = function (path, callback) {
|
|
|
|
$.get(path, function (res) {
|
|
|
|
var $html = $(res).find(".markdown .content");
|
|
|
|
$html.find(".back-to-home").remove();
|
|
|
|
|
|
|
|
callback($html.html().trim());
|
2017-10-31 06:53:41 +01:00
|
|
|
render_code_sections();
|
2017-07-10 20:50:11 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-11-08 21:48:45 +01:00
|
|
|
$(".sidebar.slide h2").click(function (e) {
|
2017-10-04 01:38:47 +02:00
|
|
|
var $next = $(e.target).next();
|
|
|
|
|
|
|
|
if ($next.is("ul")) {
|
2017-10-17 02:22:24 +02:00
|
|
|
$next.slideToggle("fast", "swing", function () {
|
2018-03-16 20:49:58 +01:00
|
|
|
perfectScrollbar.update($(".markdown")[0]);
|
2017-10-17 02:22:24 +02:00
|
|
|
});
|
2017-10-04 01:38:47 +02:00
|
|
|
}
|
2017-10-01 23:00:00 +02:00
|
|
|
});
|
|
|
|
|
2017-07-10 20:50:11 +02:00
|
|
|
$(".sidebar a").click(function (e) {
|
|
|
|
var path = $(this).attr("href");
|
2017-12-22 19:49:24 +01:00
|
|
|
var path_dir = path.split('/')[1];
|
|
|
|
var current_dir = window.location.pathname.split('/')[1];
|
|
|
|
|
|
|
|
// Do not block redirecting to external URLs
|
|
|
|
if (path_dir !== current_dir) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-17 02:22:24 +02:00
|
|
|
var container = $(".markdown")[0];
|
|
|
|
|
2017-07-10 20:50:11 +02:00
|
|
|
if (loading.name === path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
history.pushState({}, "", path);
|
|
|
|
|
|
|
|
if (html_map[path]) {
|
|
|
|
$(".markdown .content").html(html_map[path]);
|
2018-03-16 20:49:58 +01:00
|
|
|
perfectScrollbar.update(container);
|
2017-10-31 06:53:41 +01:00
|
|
|
render_code_sections();
|
2018-02-12 20:14:24 +01:00
|
|
|
scrollToHash(container);
|
2017-07-10 20:50:11 +02:00
|
|
|
} else {
|
|
|
|
loading.name = path;
|
|
|
|
|
|
|
|
fetch_page(path, function (res) {
|
|
|
|
html_map[path] = res;
|
|
|
|
$(".markdown .content").html(html_map[path]);
|
|
|
|
loading.name = null;
|
2018-03-16 20:49:58 +01:00
|
|
|
perfectScrollbar.update(container);
|
2018-02-12 20:14:24 +01:00
|
|
|
scrollToHash(container);
|
2017-07-10 20:50:11 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$(".sidebar").removeClass("show");
|
|
|
|
|
|
|
|
e.preventDefault();
|
2017-10-17 02:22:24 +02:00
|
|
|
});
|
2017-08-07 16:24:31 +02:00
|
|
|
|
2017-11-13 01:46:19 +01:00
|
|
|
// Show Guides user docs in sidebar by default
|
|
|
|
$('.help .sidebar h2#guides + ul').css('display', 'block');
|
|
|
|
|
|
|
|
// Remove ID attributes from sidebar links so they don't conflict with index page anchor links
|
|
|
|
$('.help .sidebar h1, .help .sidebar h2, .help .sidebar h3').removeAttr('id');
|
|
|
|
|
|
|
|
// Scroll to anchor link when clicked
|
|
|
|
$('.markdown .content h1, .markdown .content h2, .markdown .content h3').on('click', function () {
|
|
|
|
window.location.href = window.location.href.replace(/#.*/, '') + '#' + $(this).attr("id");
|
|
|
|
});
|
|
|
|
|
2018-03-16 20:49:58 +01:00
|
|
|
perfectScrollbar.initialize($(".markdown")[0], {
|
2017-10-17 02:22:24 +02:00
|
|
|
suppressScrollX: true,
|
|
|
|
useKeyboard: false,
|
|
|
|
wheelSpeed: 0.68,
|
|
|
|
});
|
|
|
|
|
2018-03-16 20:49:58 +01:00
|
|
|
perfectScrollbar.initialize($(".sidebar")[0], {
|
2017-10-17 02:22:24 +02:00
|
|
|
suppressScrollX: true,
|
|
|
|
useKeyboard: false,
|
|
|
|
wheelSpeed: 0.68,
|
2017-07-10 20:50:11 +02:00
|
|
|
});
|
|
|
|
|
2017-10-17 02:22:24 +02:00
|
|
|
window.onresize = function () {
|
2018-03-16 20:49:58 +01:00
|
|
|
perfectScrollbar.update($(".markdown")[0]);
|
2017-10-17 02:22:24 +02:00
|
|
|
};
|
|
|
|
|
2017-07-10 20:50:11 +02:00
|
|
|
window.addEventListener("popstate", function () {
|
|
|
|
var path = window.location.pathname;
|
|
|
|
$(".markdown .content").html(html_map[path]);
|
|
|
|
});
|
|
|
|
|
|
|
|
$(".hamburger").click(function () {
|
|
|
|
$(".sidebar").toggleClass("show");
|
|
|
|
});
|
2017-08-22 21:03:28 +02:00
|
|
|
|
|
|
|
$(".markdown").click(function () {
|
|
|
|
if ($(".sidebar.show").length) {
|
|
|
|
$(".sidebar.show").toggleClass("show");
|
|
|
|
}
|
|
|
|
});
|
2017-10-31 06:46:35 +01:00
|
|
|
|
|
|
|
render_code_sections();
|
2018-02-14 18:26:42 +01:00
|
|
|
|
|
|
|
// Finally, make sure if we loaded a window with a hash, we scroll
|
|
|
|
// to the right place.
|
|
|
|
var container = $(".markdown")[0];
|
|
|
|
scrollToHash(container);
|
2017-07-10 20:50:11 +02:00
|
|
|
}());
|