-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlangtabs.js
More file actions
77 lines (64 loc) · 2.45 KB
/
langtabs.js
File metadata and controls
77 lines (64 loc) · 2.45 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
(function loadDeviconCSS() {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'https://cdn.jsdelivr.net/gh/devicons/devicon@v2.17.0/devicon.min.css';
document.head.appendChild(link);
})();
document.addEventListener('DOMContentLoaded', function() {
initLangTabs();
// Listen for theme changes to re-style tabs
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.attributeName === 'class' && mutation.target.nodeName === 'HTML') {
setTimeout(initLangTabs, 50);
}
});
});
observer.observe(document.documentElement, {
attributes: true
});
// Also handle theme changes when page hash changes (mdbook sometimes updates theme this way)
window.addEventListener('hashchange', function() {
setTimeout(initLangTabs, 100);
});
// Handle page navigation
window.addEventListener('load', function() {
setTimeout(initLangTabs, 100);
});
});
function initLangTabs() {
const langTabsContainers = document.querySelectorAll('.langtabs');
langTabsContainers.forEach(function(container) {
const tabButtons = container.querySelectorAll('.langtabs-tab');
tabButtons.forEach(function(button) {
button.removeEventListener('click', handleTabClick);
button.addEventListener('click', handleTabClick);
});
// If no tab active select first
if (!container.querySelector('.langtabs-tab.active')) {
const firstButton = tabButtons[0];
if (firstButton) {
firstButton.click();
}
}
});
}
function handleTabClick() {
const container = this.closest('.langtabs');
const lang = this.getAttribute('data-lang');
// Deactivate all tabs in this container
const tabButtons = container.querySelectorAll('.langtabs-tab');
const tabContents = container.querySelectorAll('.langtabs-code');
tabButtons.forEach(function(btn) {
btn.classList.remove('active');
});
tabContents.forEach(function(content) {
content.classList.remove('active');
});
// Activate selected tab
this.classList.add('active');
const activeContent = container.querySelector(`.langtabs-code[data-lang="${lang}"]`);
if (activeContent) {
activeContent.classList.add('active');
}
}