2018-07-12 19:18:40 +02:00
|
|
|
import SimpleBar from 'simplebar';
|
2019-01-14 20:26:32 +01:00
|
|
|
import {activate_correct_tab} from './tabbed-instructions.js';
|
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");
|
2018-06-04 21:13:07 +02:00
|
|
|
$li.filter("[data-language=" + language + "]").addClass("active");
|
2017-10-31 06:46:35 +01:00
|
|
|
|
|
|
|
$blocks.removeClass("active");
|
2018-06-04 21:13:07 +02:00
|
|
|
$blocks.filter("[data-language=" + language + "]").addClass("active");
|
2017-10-31 06:46:35 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-24 18:22:33 +01:00
|
|
|
function highlight_current_article() {
|
|
|
|
$('.help .sidebar a').removeClass('highlighted');
|
2019-11-02 00:06:25 +01:00
|
|
|
const path = window.location.pathname;
|
2017-12-24 18:22:33 +01:00
|
|
|
|
|
|
|
if (!path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const hash = window.location.hash;
|
|
|
|
let article = $('.help .sidebar a[href="' + path + hash + '"]');
|
2018-07-23 02:13:59 +02:00
|
|
|
if (!article.length) {
|
|
|
|
// If there isn't an entry in the left sidebar that matches
|
|
|
|
// the full url+hash pair, instead highlight an entry in the
|
|
|
|
// left sidebar that just matches the url part.
|
|
|
|
article = $('.help .sidebar a[href="' + path + '"]');
|
|
|
|
}
|
2018-06-06 23:46:04 +02:00
|
|
|
// Highlight current article link and the heading of the same
|
|
|
|
article.closest('ul').css('display', 'block');
|
2017-12-24 18:22:33 +01:00
|
|
|
article.addClass('highlighted');
|
|
|
|
}
|
|
|
|
|
2017-10-31 06:46:35 +01:00
|
|
|
function render_code_sections() {
|
|
|
|
$(".code-section").each(function () {
|
2019-01-14 20:26:32 +01:00
|
|
|
activate_correct_tab($(this));
|
2017-10-31 06:46:35 +01:00
|
|
|
registerCodeSection($(this));
|
|
|
|
});
|
2017-12-24 18:22:33 +01:00
|
|
|
|
|
|
|
highlight_current_article();
|
2017-12-23 06:11:25 +01:00
|
|
|
|
2019-06-10 09:22:55 +02:00
|
|
|
common.adjust_mac_shortcuts(".markdown .content code", true);
|
2018-05-26 19:02:18 +02:00
|
|
|
|
|
|
|
$("table").each(function () {
|
|
|
|
$(this).addClass("table table-striped");
|
|
|
|
});
|
2017-10-31 06:46:35 +01:00
|
|
|
}
|
|
|
|
|
2018-07-14 19:28:12 +02:00
|
|
|
function scrollToHash(simplebar) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const hash = window.location.hash;
|
|
|
|
const scrollbar = simplebar.getScrollElement();
|
2018-02-12 20:14:24 +01:00
|
|
|
if (hash !== '') {
|
2019-11-02 00:06:25 +01:00
|
|
|
const position = $(hash).position().top - $(scrollbar.firstChild).position().top;
|
2018-07-14 19:28:12 +02:00
|
|
|
scrollbar.scrollTop = position;
|
2018-02-12 20:14:24 +01:00
|
|
|
} else {
|
2018-07-14 19:28:12 +02:00
|
|
|
scrollbar.scrollTop = 0;
|
2018-02-12 20:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const html_map = {};
|
|
|
|
const loading = {
|
2019-10-25 09:15:16 +02:00
|
|
|
name: null,
|
|
|
|
};
|
2017-07-10 20:50:11 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const markdownSB = new SimpleBar($(".markdown")[0]);
|
2018-06-05 23:30:15 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const fetch_page = function (path, callback) {
|
2019-10-25 09:15:16 +02:00
|
|
|
$.get(path, function (res) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const $html = $(res).find(".markdown .content");
|
2017-07-10 20:50:11 +02:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
callback($html.html().trim());
|
|
|
|
render_code_sections();
|
|
|
|
});
|
|
|
|
};
|
2017-07-10 20:50:11 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const update_page = function (html_map, path) {
|
2019-10-25 09:15:16 +02:00
|
|
|
if (html_map[path]) {
|
|
|
|
$(".markdown .content").html(html_map[path]);
|
|
|
|
render_code_sections();
|
|
|
|
scrollToHash(markdownSB);
|
|
|
|
} else {
|
|
|
|
loading.name = path;
|
|
|
|
fetch_page(path, function (res) {
|
|
|
|
html_map[path] = res;
|
2018-06-05 23:43:56 +02:00
|
|
|
$(".markdown .content").html(html_map[path]);
|
2019-10-25 09:15:16 +02:00
|
|
|
loading.name = null;
|
2018-07-14 19:28:12 +02:00
|
|
|
scrollToHash(markdownSB);
|
2019-10-25 09:15:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2017-12-22 19:49:24 +01:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
new SimpleBar($(".sidebar")[0]);
|
2017-07-10 20:50:11 +02:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
$(".sidebar.slide h2").click(function (e) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const $next = $(e.target).next();
|
2017-07-10 20:50:11 +02:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
if ($next.is("ul")) {
|
|
|
|
// Close other article's headings first
|
|
|
|
$('.sidebar ul').not($next).hide();
|
|
|
|
// Toggle the heading
|
|
|
|
$next.slideToggle("fast", "swing");
|
|
|
|
}
|
|
|
|
});
|
2017-07-10 20:50:11 +02:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
$(".sidebar a").click(function (e) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const path = $(this).attr("href");
|
|
|
|
const path_dir = path.split('/')[1];
|
|
|
|
const current_dir = window.location.pathname.split('/')[1];
|
2017-07-10 20:50:11 +02:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
// Do not block redirecting to external URLs
|
|
|
|
if (path_dir !== current_dir) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-07 16:24:31 +02:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
if (loading.name === path) {
|
|
|
|
return;
|
2018-06-07 18:58:13 +02:00
|
|
|
}
|
2017-11-13 01:46:19 +01:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
history.pushState({}, "", path);
|
2017-11-13 01:46:19 +01:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
update_page(html_map, path);
|
2017-08-22 21:03:28 +02:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
$(".sidebar").removeClass("show");
|
2017-10-31 06:46:35 +01:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
e.preventDefault();
|
|
|
|
});
|
2018-02-14 18:26:42 +01:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
if (window.location.pathname === '/help/') {
|
|
|
|
// Expand the Guides user docs section in sidebar in the /help/ homepage.
|
|
|
|
$('.help .sidebar h2#guides + ul').show();
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
$(document).on('click', '.markdown .content h1, .markdown .content h2, .markdown .content h3', function () {
|
|
|
|
window.location.hash = $(this).attr("id");
|
2018-07-14 19:28:12 +02:00
|
|
|
scrollToHash(markdownSB);
|
2019-10-25 09:15:16 +02:00
|
|
|
});
|
2018-06-05 23:30:15 +02:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
$(".hamburger").click(function () {
|
|
|
|
$(".sidebar").toggleClass("show");
|
|
|
|
});
|
|
|
|
|
|
|
|
$(".markdown").click(function () {
|
|
|
|
if ($(".sidebar.show").length) {
|
|
|
|
$(".sidebar.show").toggleClass("show");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
render_code_sections();
|
|
|
|
|
|
|
|
// Finally, make sure if we loaded a window with a hash, we scroll
|
|
|
|
// to the right place.
|
|
|
|
scrollToHash(markdownSB);
|
|
|
|
|
|
|
|
window.addEventListener("popstate", function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const path = window.location.pathname;
|
2019-10-25 09:15:16 +02:00
|
|
|
update_page(html_map, path);
|
|
|
|
});
|
2018-06-05 23:30:15 +02:00
|
|
|
|
2019-10-25 09:15:16 +02:00
|
|
|
$('body').addClass('noscroll');
|