2024-04-29 22:36:48 +01:00
|
|
|
/**
|
|
|
|
* Check if the element is visible.
|
|
|
|
* @param {Element} element Dom element
|
|
|
|
* @returns boolean
|
|
|
|
*/
|
|
|
|
function isVisible(element) {
|
|
|
|
return element.offsetWidth > 0 || element.offsetHeight > 0;
|
|
|
|
}
|
|
|
|
|
2023-08-20 00:42:46 +01:00
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
2024-10-12 20:55:39 +02:00
|
|
|
scrollToActiveItem();
|
|
|
|
enableCollapsibles();
|
|
|
|
});
|
|
|
|
|
|
|
|
function enableCollapsibles() {
|
2023-08-20 00:42:46 +01:00
|
|
|
const buttons = document.querySelectorAll(".hextra-sidebar-collapsible-button");
|
|
|
|
buttons.forEach(function (button) {
|
|
|
|
button.addEventListener("click", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
const list = button.parentElement.parentElement;
|
|
|
|
if (list) {
|
2024-04-29 22:36:48 +01:00
|
|
|
list.classList.toggle("open");
|
2023-08-20 00:42:46 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2024-04-29 22:36:48 +01:00
|
|
|
|
|
|
|
const isCached = "{{- site.Params.page.sidebar.cache | default false -}}" === "true";
|
|
|
|
const currentPagePath = window.location.href;
|
|
|
|
|
|
|
|
if (isCached) {
|
|
|
|
// find the current page in the sidebar and open the parent lists
|
|
|
|
const sidebar = document.querySelector(".hextra-sidebar-container");
|
|
|
|
if (sidebar) {
|
|
|
|
// find a tags and compare href with current page path
|
|
|
|
const links = sidebar.querySelectorAll("a");
|
|
|
|
links.forEach(function (link) {
|
|
|
|
const linkPath = link.href;
|
|
|
|
|
|
|
|
if (currentPagePath === linkPath) {
|
|
|
|
// add active class to the link
|
2024-04-30 22:42:04 +01:00
|
|
|
link.classList.add("active");
|
|
|
|
link.classList.remove("inactive");
|
2024-04-29 22:36:48 +01:00
|
|
|
|
|
|
|
if (!isVisible(link)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// recursively open parent lists
|
|
|
|
let parent = link.parentElement;
|
|
|
|
while (parent && !parent.classList.contains("hextra-sidebar-container")) {
|
2024-04-30 22:44:09 +01:00
|
|
|
if (parent.tagName === "LI" && parent.classList.contains("hextra-sidebar-item")) {
|
2024-04-29 22:36:48 +01:00
|
|
|
parent.classList.add("open");
|
|
|
|
}
|
|
|
|
parent = parent.parentElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2024-10-12 20:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function scrollToActiveItem() {
|
|
|
|
const sidebarScrollbar = document.querySelector("aside.sidebar-container > .hextra-scrollbar");
|
|
|
|
const activeItems = document.querySelectorAll(".sidebar-active-item");
|
|
|
|
const visibleActiveItem = Array.from(activeItems).find(function (activeItem) {
|
|
|
|
return activeItem.getBoundingClientRect().height > 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!visibleActiveItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const yOffset = visibleActiveItem.clientHeight;
|
|
|
|
const yDistance = visibleActiveItem.getBoundingClientRect().top - sidebarScrollbar.getBoundingClientRect().top;
|
|
|
|
sidebarScrollbar.scrollTo({
|
|
|
|
behavior: "instant",
|
2024-11-11 22:59:41 +00:00
|
|
|
top: yDistance - yOffset,
|
2024-10-12 20:55:39 +02:00
|
|
|
});
|
|
|
|
}
|