-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcollapse.js
96 lines (90 loc) · 2.79 KB
/
collapse.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
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
(function(document) {
var allFolders = document.getElementsByTagName('ul');
// Make sure all but top level folder are collapsed by default.
(function() {
closeAll();
document.querySelector('body > .folder > ul').style.display = 'block';
// Open path to a nested link if there's one.
var nestedPath = location.hash;
if (nestedPath) {
nestedPath = nestedPath.replace(/^#\//, '');
openNestedPath(nestedPath);
}
})();
// Open and close folders when their titles are clicked.
document.addEventListener('click', function(event) {
var folder,
folderLink,
contents;
folderLink = event.target;
folder = folderLink.parentNode;
// Don't react if the event target wasn't a folder title.
if (folderLink.nodeName != 'A' || folder.className.indexOf('folder-title') === -1) {
return;
}
// Get the folder contents and show/hide them depending on their current visibility.
event.preventDefault();
contents = folder.nextElementSibling;
if (contents.style.display == 'block') {
contents.style.display = 'none';
}
else {
contents.style.display = 'block';
}
});
// Enable open/close all links.
document.getElementById('open-all-link').addEventListener('click', function(event) {
event.preventDefault();
openAll();
});
function openAll(event) {
for (var i = 0, length = allFolders.length; i < length; i++) {
var folder = allFolders[i];
if (folder.style.display != 'block') {
folder.style.display = 'block';
}
}
}
document.getElementById('close-all-link').addEventListener('click', function(event) {
event.preventDefault();
closeAll();
});
function closeAll() {
for (var i = 0, length = allFolders.length; i < length; i++) {
var folder = allFolders[i];
if (folder.style.display != 'none') {
folder.style.display = 'none';
}
}
}
function openNestedPath(path, startFolder) {
if (typeof path === 'string') {
path = path.split('/');
}
if (!startFolder) {
startFolder = document.querySelector('body > .folder > ul');
}
var pathPart = path[0],
children = startFolder.children;
foundFolder = false;
for (var i = 0, length = children.length; i < length; i++) {
var childFolder = children[i].firstChild;
if (childFolder && childFolder.getAttribute('data-folder-id') == pathPart) {
foundFolder = childFolder.querySelector('ul');
}
}
if (foundFolder) {
foundFolder.style.display = 'block';
path = path.slice(1);
if (path.length > 0) {
openNestedPath(path, foundFolder);
}
else {
foundFolder.parentNode.scrollIntoView();
}
}
else {
startFolder.parentNode.scrollIntoView();
}
}
})(document);