2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2021-04-23 20:58:44 +02:00
|
|
|
import SimpleBar from "simplebar";
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2021-02-28 00:35:35 +01:00
|
|
|
import * as common from "../common";
|
|
|
|
|
2020-08-29 04:08:14 +02:00
|
|
|
import * as google_analytics from "./google-analytics";
|
|
|
|
import {activate_correct_tab} from "./tabbed-instructions";
|
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");
|
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
$li.on("click", function () {
|
2017-10-31 06:46:35 +01:00
|
|
|
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
|
|
|
});
|
2020-07-29 12:54:39 +02:00
|
|
|
|
|
|
|
$li.on("keydown", (e) => {
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
e.target.click();
|
|
|
|
}
|
|
|
|
});
|
2017-10-31 06:46:35 +01:00
|
|
|
}
|
|
|
|
|
2017-12-24 18:22:33 +01:00
|
|
|
function highlight_current_article() {
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".help .sidebar a").removeClass("highlighted");
|
2020-07-29 07:57:06 +02:00
|
|
|
$(".help .sidebar a").attr("tabindex", "0");
|
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;
|
2021-02-03 23:23:32 +01:00
|
|
|
let article = $(`.help .sidebar a[href="${CSS.escape(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
|
2020-10-23 02:43:28 +02:00
|
|
|
// the full URL+hash pair, instead highlight an entry in the
|
|
|
|
// left sidebar that just matches the URL part.
|
2021-02-03 23:23:32 +01:00
|
|
|
article = $(`.help .sidebar a[href="${CSS.escape(path)}"]`);
|
2018-07-23 02:13:59 +02:00
|
|
|
}
|
2018-06-06 23:46:04 +02:00
|
|
|
// Highlight current article link and the heading of the same
|
2020-07-15 01:29:15 +02:00
|
|
|
article.closest("ul").css("display", "block");
|
|
|
|
article.addClass("highlighted");
|
2020-07-29 07:57:06 +02:00
|
|
|
article.attr("tabindex", "-1");
|
2017-12-24 18:22:33 +01:00
|
|
|
}
|
|
|
|
|
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();
|
2020-07-15 01:29:15 +02:00
|
|
|
if (hash !== "" && $(hash).length > 0) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 07:44:08 +02:00
|
|
|
const cache = new Map();
|
2019-11-02 00:06:25 +01:00
|
|
|
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) {
|
2020-07-02 01:45:54 +02:00
|
|
|
$.get(path, (res) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const $html = $(res).find(".markdown .content");
|
2020-07-29 07:44:08 +02:00
|
|
|
const title = $(res).filter("title").text();
|
2017-07-10 20:50:11 +02:00
|
|
|
|
2020-07-29 07:44:08 +02:00
|
|
|
callback({html: $html.html().trim(), title});
|
2019-10-25 09:15:16 +02:00
|
|
|
render_code_sections();
|
|
|
|
});
|
|
|
|
};
|
2017-07-10 20:50:11 +02:00
|
|
|
|
2020-07-29 07:44:08 +02:00
|
|
|
const update_page = function (cache, path) {
|
|
|
|
if (cache.has(path)) {
|
|
|
|
$(".markdown .content").html(cache.get(path).html);
|
|
|
|
document.title = cache.get(path).title;
|
2019-10-25 09:15:16 +02:00
|
|
|
render_code_sections();
|
|
|
|
scrollToHash(markdownSB);
|
|
|
|
} else {
|
|
|
|
loading.name = path;
|
2020-07-29 07:44:08 +02:00
|
|
|
fetch_page(path, (article) => {
|
|
|
|
cache.set(path, article);
|
|
|
|
$(".markdown .content").html(article.html);
|
2019-10-25 09:15:16 +02:00
|
|
|
loading.name = null;
|
2020-07-29 07:44:08 +02:00
|
|
|
document.title = article.title;
|
2018-07-14 19:28:12 +02:00
|
|
|
scrollToHash(markdownSB);
|
2019-10-25 09:15:16 +02:00
|
|
|
});
|
|
|
|
}
|
2020-05-08 06:37:58 +02:00
|
|
|
google_analytics.config({page_path: path});
|
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
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
$(".sidebar a").on("click", function (e) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const path = $(this).attr("href");
|
2020-07-15 01:29:15 +02:00
|
|
|
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
|
|
|
|
2020-07-29 07:44:08 +02:00
|
|
|
update_page(cache, 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
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (window.location.pathname === "/help/") {
|
2019-10-25 09:15:16 +02:00
|
|
|
// Expand the Guides user docs section in sidebar in the /help/ homepage.
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".help .sidebar h2#guides + ul").show();
|
2019-10-25 09:15:16 +02:00
|
|
|
}
|
|
|
|
// Remove ID attributes from sidebar links so they don't conflict with index page anchor links
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".help .sidebar h1, .help .sidebar h2, .help .sidebar h3").removeAttr("id");
|
2019-10-25 09:15:16 +02:00
|
|
|
|
|
|
|
// Scroll to anchor link when clicked
|
2020-07-15 00:34:28 +02:00
|
|
|
$(document).on(
|
|
|
|
"click",
|
|
|
|
".markdown .content h1, .markdown .content h2, .markdown .content h3",
|
|
|
|
function () {
|
|
|
|
window.location.hash = $(this).attr("id");
|
|
|
|
scrollToHash(markdownSB);
|
|
|
|
},
|
|
|
|
);
|
2018-06-05 23:30:15 +02:00
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
$(".hamburger").on("click", () => {
|
2019-10-25 09:15:16 +02:00
|
|
|
$(".sidebar").toggleClass("show");
|
|
|
|
});
|
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
$(".markdown").on("click", () => {
|
2019-10-25 09:15:16 +02:00
|
|
|
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);
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
window.addEventListener("popstate", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const path = window.location.pathname;
|
2020-07-29 07:44:08 +02:00
|
|
|
update_page(cache, path);
|
2019-10-25 09:15:16 +02:00
|
|
|
});
|
2018-06-05 23:30:15 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("body").addClass("noscroll");
|
2020-07-28 22:48:50 +02:00
|
|
|
|
|
|
|
$(".highlighted")[0].scrollIntoView({block: "center"});
|