-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsidebarDropdown.js
More file actions
65 lines (60 loc) · 2.48 KB
/
Copy pathsidebarDropdown.js
File metadata and controls
65 lines (60 loc) · 2.48 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
// this keeps the dropdown open when one of the dropdown link is active
var dropdown = document.getElementsByClassName("dropdown-btn");
function dropdownParts(button, fallbackIndex) {
var suffix = button.id ? button.id.replace("dropdown-btn-", "") : (fallbackIndex + 1).toString();
return {
content: document.getElementById("dropdown-list-" + suffix),
arrowDown: document.getElementById("arrow-down-" + suffix),
arrowUp: document.getElementById("arrow-up-" + suffix),
suffix: suffix
};
}
for (let i = 0; i < dropdown.length; i++) {
var parts = dropdownParts(dropdown[i], i);
var dropdownContent = parts.content;
var arrowDown = parts.arrowDown;
var arrowUp = parts.arrowUp;
if (!dropdownContent || !arrowDown || !arrowUp) {
continue;
}
var dropdownContent_children = dropdownContent.children[0].children;
for (let j = 0; j < dropdownContent_children.length; j++) {
if (dropdownContent_children[j].className.includes("active")) {
if (dropdownContent.style.display === "block") {
dropdownContent.style.display = "none";
arrowDown.style.display = "block";
arrowUp.style.display = "none";
} else {
dropdownContent.style.display = "block";
arrowUp.style.display = "block";
arrowDown.style.display = "none";
const element = document.getElementById("nav-item-" + (j + 1) + "-" + parts.suffix);
if (element) {
element.scrollIntoView({ behavior: "instant", block: "center", inline: "nearest" });
}
}
}
}
}
// this opens the dropdown when button is clicked
for (let i = 0; i < dropdown.length; i++) {
dropdown[i].addEventListener("click", function (event) {
event.preventDefault();
var parts = dropdownParts(dropdown[i], i);
var dropdownContent = parts.content;
var arrowDown = parts.arrowDown;
var arrowUp = parts.arrowUp;
if (!dropdownContent || !arrowDown || !arrowUp) {
return;
}
if (dropdownContent.style.display === "block") {
dropdownContent.style.display = "none";
arrowDown.style.display = "block";
arrowUp.style.display = "none";
} else {
dropdownContent.style.display = "block";
arrowUp.style.display = "block";
arrowDown.style.display = "none";
}
});
}