CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
pytorch

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: pytorch/tutorials
Path: blob/main/_static/js/custom.js
Views: 713
1
document.addEventListener("DOMContentLoaded", function() {
2
// Select all <li> elements with the class "toctree-l1"
3
var toctreeItems = document.querySelectorAll('li.toctree-l1');
4
5
toctreeItems.forEach(function(item) {
6
// Find the link within the item
7
var link = item.querySelector('a');
8
var nestedList = item.querySelector('ul');
9
10
if (link && nestedList) {
11
// Create a span element for the "[+]" or "[-]" sign
12
var expandSign = document.createElement('span');
13
expandSign.style.cursor = 'pointer'; // Make it look clickable
14
15
// Use the link text as a unique key for localStorage
16
var sectionKey = 'section_' + link.textContent.trim().replace(/\s+/g, '_');
17
18
// Retrieve the saved state from localStorage
19
var isExpanded = localStorage.getItem(sectionKey);
20
21
// If no state is saved, default to expanded for "Learn the Basics" and collapsed for others
22
if (isExpanded === null) {
23
isExpanded = (link.textContent.trim() === 'Learn the Basics') ? 'true' : 'false';
24
localStorage.setItem(sectionKey, isExpanded);
25
}
26
27
if (isExpanded === 'true') {
28
nestedList.style.display = 'block'; // Expand the section
29
expandSign.textContent = '[-] '; // Show "[-]" since it's expanded
30
} else {
31
nestedList.style.display = 'none'; // Collapse the section
32
expandSign.textContent = '[+] '; // Show "[+]" since it's collapsed
33
}
34
35
// Add a click event to toggle the nested list
36
expandSign.addEventListener('click', function() {
37
if (nestedList.style.display === 'none') {
38
nestedList.style.display = 'block';
39
expandSign.textContent = '[-] '; // Change to "[-]" when expanded
40
localStorage.setItem(sectionKey, 'true'); // Save state
41
} else {
42
nestedList.style.display = 'none';
43
expandSign.textContent = '[+] '; // Change back to "[+]" when collapsed
44
localStorage.setItem(sectionKey, 'false'); // Save state
45
}
46
});
47
48
// Insert the sign before the link
49
link.parentNode.insertBefore(expandSign, link);
50
}
51
});
52
});
53
54