-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathcustom.js
52 lines (44 loc) · 2.08 KB
/
custom.js
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
document.addEventListener("DOMContentLoaded", function() {
// Select all <li> elements with the class "toctree-l1"
var toctreeItems = document.querySelectorAll('li.toctree-l1');
toctreeItems.forEach(function(item) {
// Find the link within the item
var link = item.querySelector('a');
var nestedList = item.querySelector('ul');
if (link && nestedList) {
// Create a span element for the "[+]" or "[-]" sign
var expandSign = document.createElement('span');
expandSign.style.cursor = 'pointer'; // Make it look clickable
// Use the link text as a unique key for localStorage
var sectionKey = 'section_' + link.textContent.trim().replace(/\s+/g, '_');
// Retrieve the saved state from localStorage
var isExpanded = localStorage.getItem(sectionKey);
// If no state is saved, default to expanded for "Learn the Basics" and collapsed for others
if (isExpanded === null) {
isExpanded = (link.textContent.trim() === 'Learn the Basics') ? 'true' : 'false';
localStorage.setItem(sectionKey, isExpanded);
}
if (isExpanded === 'true') {
nestedList.style.display = 'block'; // Expand the section
expandSign.textContent = '[-] '; // Show "[-]" since it's expanded
} else {
nestedList.style.display = 'none'; // Collapse the section
expandSign.textContent = '[+] '; // Show "[+]" since it's collapsed
}
// Add a click event to toggle the nested list
expandSign.addEventListener('click', function() {
if (nestedList.style.display === 'none') {
nestedList.style.display = 'block';
expandSign.textContent = '[-] '; // Change to "[-]" when expanded
localStorage.setItem(sectionKey, 'true'); // Save state
} else {
nestedList.style.display = 'none';
expandSign.textContent = '[+] '; // Change back to "[+]" when collapsed
localStorage.setItem(sectionKey, 'false'); // Save state
}
});
// Insert the sign before the link
link.parentNode.insertBefore(expandSign, link);
}
});
});