Stirling-PDF/src/main/resources/static/js/tab-container.js

39 lines
1.2 KiB
JavaScript
Raw Normal View History

TabContainer = {
initTabGroups() {
const groups = document.querySelectorAll(".tab-group");
const unloadedGroups = [...groups].filter((g) => !g.initialised);
unloadedGroups.forEach((group) => {
const containers = group.querySelectorAll(".tab-container");
const tabTitles = [...containers].map((c) => c.getAttribute("title"));
2024-02-11 17:47:00 +01:00
const tabList = document.createElement("div");
tabList.classList.add("tab-buttons");
tabTitles.forEach((title) => {
const tabButton = document.createElement("button");
tabButton.innerHTML = title;
tabButton.onclick = (e) => {
this.setActiveTab(e.target);
};
tabList.appendChild(tabButton);
});
group.prepend(tabList);
this.setActiveTab(tabList.firstChild);
group.initialised = true;
});
},
setActiveTab(tabButton) {
const group = tabButton.closest(".tab-group");
group.querySelectorAll(".active").forEach((el) => el.classList.remove("active"));
tabButton.classList.add("active");
group.querySelector(`[title="${tabButton.innerHTML}"]`).classList.add("active");
},
};
document.addEventListener("DOMContentLoaded", () => {
TabContainer.initTabGroups();
});