107 lines
2.9 KiB
JavaScript
107 lines
2.9 KiB
JavaScript
(function () {
|
|
var toc = document.querySelector(".legal-toc");
|
|
if (!toc) return;
|
|
|
|
var layout = document.querySelector(".legal-layout");
|
|
var list = toc.querySelector(".legal-toc__list");
|
|
var items = Array.prototype.slice.call(toc.querySelectorAll(".legal-toc__item"));
|
|
var activeId = "";
|
|
var TOP = 88;
|
|
var desktopMq = window.matchMedia("(min-width: 769px)");
|
|
var slot = null;
|
|
var pinnedHeight = 0;
|
|
|
|
function ensureSlot() {
|
|
if (slot || !layout || !toc.parentNode) return;
|
|
if (toc.parentNode.classList && toc.parentNode.classList.contains("legal-toc-slot")) {
|
|
slot = toc.parentNode;
|
|
return;
|
|
}
|
|
slot = document.createElement("div");
|
|
slot.className = "legal-toc-slot";
|
|
toc.parentNode.insertBefore(slot, toc);
|
|
slot.appendChild(toc);
|
|
}
|
|
|
|
function clearPin() {
|
|
toc.classList.remove("is-fixed");
|
|
toc.style.top = "";
|
|
toc.style.left = "";
|
|
toc.style.width = "";
|
|
if (slot) slot.style.height = "";
|
|
}
|
|
|
|
function updatePin() {
|
|
if (!layout || !desktopMq.matches) {
|
|
clearPin();
|
|
return;
|
|
}
|
|
ensureSlot();
|
|
if (!slot) return;
|
|
|
|
if (!toc.classList.contains("is-fixed")) {
|
|
pinnedHeight = toc.offsetHeight;
|
|
}
|
|
|
|
var layoutRect = layout.getBoundingClientRect();
|
|
var slotRect = slot.getBoundingClientRect();
|
|
var width = slot.offsetWidth;
|
|
|
|
if (slotRect.top >= TOP) {
|
|
clearPin();
|
|
return;
|
|
}
|
|
|
|
if (!pinnedHeight) pinnedHeight = toc.offsetHeight;
|
|
slot.style.height = pinnedHeight + "px";
|
|
|
|
var top = Math.min(TOP, layoutRect.bottom - pinnedHeight);
|
|
toc.classList.add("is-fixed");
|
|
toc.style.top = top + "px";
|
|
toc.style.left = slotRect.left + "px";
|
|
toc.style.width = width + "px";
|
|
}
|
|
|
|
ensureSlot();
|
|
updatePin();
|
|
window.addEventListener("scroll", updatePin, { passive: true });
|
|
window.addEventListener("resize", function () {
|
|
clearPin();
|
|
updatePin();
|
|
});
|
|
if (typeof desktopMq.addEventListener === "function") {
|
|
desktopMq.addEventListener("change", function () {
|
|
clearPin();
|
|
updatePin();
|
|
});
|
|
} else if (typeof desktopMq.addListener === "function") {
|
|
desktopMq.addListener(function () {
|
|
clearPin();
|
|
updatePin();
|
|
});
|
|
}
|
|
|
|
function centerChip(item) {
|
|
if (!list || list.scrollWidth <= list.clientWidth) return;
|
|
var listRect = list.getBoundingClientRect();
|
|
var itemRect = item.getBoundingClientRect();
|
|
var left = list.scrollLeft + itemRect.left - listRect.left - (list.clientWidth - item.clientWidth) / 2;
|
|
list.scrollTo({ left: Math.max(0, left), behavior: "smooth" });
|
|
}
|
|
|
|
function setActive(item) {
|
|
if (!item) return;
|
|
var id = (item.getAttribute("href") || "").slice(1);
|
|
if (id === activeId) return;
|
|
activeId = id;
|
|
items.forEach(function (link) {
|
|
link.classList.toggle("is-active", link === item);
|
|
});
|
|
centerChip(item);
|
|
}
|
|
|
|
items.forEach(function (item) {
|
|
item.addEventListener("click", function () { setActive(item); });
|
|
});
|
|
})();
|