-
Notifications
You must be signed in to change notification settings - Fork 431
Expand file tree
/
Copy pathtabset.js
More file actions
162 lines (140 loc) · 5.65 KB
/
tabset.js
File metadata and controls
162 lines (140 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* @module RevealJsTabset
* @version 1.2.0
* @license MIT
* @copyright 2026 Mickaël Canouil
* @author Mickaël Canouil
*/
window.RevealJsTabset = function () {
return {
id: "RevealJsTabset",
init: function (deck) {
const TAB_SELECTOR = "ul.panel-tabset-tabby > li";
const TAB_LINK_SELECTOR = "ul.panel-tabset-tabby > li a";
/**
* Get all tab panes for a given tabset element.
* @param {Element} tabset - The tabset container element
* @returns {HTMLCollection} Collection of tab pane elements
*/
function getTabPanes(tabset) {
const tabContent = tabset.querySelector(".tab-content");
return tabContent ? tabContent.children : [];
}
/**
* Initialise tabset fragments on ready.
* This sets up fragment indices for tab content and creates invisible
* fragment triggers for tab navigation.
*/
deck.on("ready", function () {
const tabsetSlides = document.querySelectorAll(
".reveal .slides section .panel-tabset",
);
tabsetSlides.forEach(function (tabset) {
const tabs = tabset.querySelectorAll(TAB_SELECTOR);
const tabCount = tabs.length;
if (tabCount <= 1) return;
const tabPanes = getTabPanes(tabset);
const parentNode = tabset.parentNode;
let currentIndex = 0;
// Process each tab
for (let i = 0; i < tabCount; i++) {
if (tabPanes[i]) {
// Assign fragment indices to any fragments within the tab pane
const fragmentsInPane = tabPanes[i].querySelectorAll(".fragment");
fragmentsInPane.forEach(function (fragment) {
fragment.setAttribute("data-fragment-index", currentIndex);
currentIndex++;
});
}
// Create invisible fragment triggers for tab switching (except after last tab)
if (i < tabCount - 1) {
const fragmentDiv = document.createElement("div");
fragmentDiv.className = "panel-tabset-fragment fragment";
fragmentDiv.dataset.tabIndex = i + 1;
fragmentDiv.setAttribute("data-fragment-index", currentIndex);
fragmentDiv.style.display = "none";
fragmentDiv.setAttribute("aria-hidden", "true");
parentNode.appendChild(fragmentDiv);
currentIndex++;
}
}
});
});
/**
* Handle fragment shown events.
* When a tabset fragment is shown, click the corresponding tab.
*/
deck.on("fragmentshown", function (event) {
if (!event.fragment.classList.contains("panel-tabset-fragment")) return;
const tabIndex = parseInt(event.fragment.dataset.tabIndex, 10);
if (isNaN(tabIndex)) return;
const tabset = deck.getCurrentSlide().querySelector(".panel-tabset");
if (!tabset) return;
const tabLinks = tabset.querySelectorAll(TAB_LINK_SELECTOR);
if (tabLinks[tabIndex]) {
tabLinks[tabIndex].click();
}
});
/**
* Handle fragment hidden events.
* When a tabset fragment is hidden (going backwards), click the previous tab.
*/
deck.on("fragmenthidden", function (event) {
if (!event.fragment.classList.contains("panel-tabset-fragment")) return;
const tabIndex = parseInt(event.fragment.dataset.tabIndex, 10);
if (isNaN(tabIndex)) return;
const tabset = deck.getCurrentSlide().querySelector(".panel-tabset");
if (!tabset) return;
const tabLinks = tabset.querySelectorAll(TAB_LINK_SELECTOR);
const targetIndex = tabIndex > 0 ? tabIndex - 1 : 0;
if (tabLinks[targetIndex]) {
tabLinks[targetIndex].click();
}
});
/**
* Handle PDF export mode.
* Ensures the correct tab is visible based on fragment state.
*/
deck.on("pdf-ready", function () {
const slides = document.querySelectorAll(".reveal .slides section");
slides.forEach(function (slide) {
const tabset = slide.querySelector(".panel-tabset");
if (!tabset) return;
const fragments = slide.querySelectorAll(".panel-tabset-fragment");
let activeTabIndex = 0;
// Find the highest visible tab index
fragments.forEach(function (fragment) {
if (fragment.classList.contains("visible")) {
const tabIndex = parseInt(fragment.dataset.tabIndex, 10);
if (!isNaN(tabIndex) && tabIndex > activeTabIndex) {
activeTabIndex = tabIndex;
}
}
});
// Update tab states
const tabLinks = tabset.querySelectorAll(TAB_LINK_SELECTOR);
const tabPanes = getTabPanes(tabset);
const tabPanesArray = Array.from(tabPanes);
tabLinks.forEach(function (link, index) {
const li = link.parentElement;
const isActive = index === activeTabIndex;
li.classList.toggle("active", isActive);
link.setAttribute("aria-selected", isActive ? "true" : "false");
link.setAttribute("tabindex", isActive ? "0" : "-1");
});
// Update pane visibility
tabPanesArray.forEach(function (panel, index) {
const isActive = index === activeTabIndex;
panel.classList.toggle("active", isActive);
panel.style.display = isActive ? "block" : "none";
if (isActive) {
panel.removeAttribute("hidden");
} else {
panel.setAttribute("hidden", "");
}
});
});
});
},
};
};