-
Notifications
You must be signed in to change notification settings - Fork 0
/
collapsible.js
77 lines (68 loc) · 2.19 KB
/
collapsible.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
const collapsibles = document.querySelectorAll(".collapsible:not([target=''])")
var col = {}
collapsibles.forEach((collapsible) => {
let target = collapsible.getAttribute("target")
if (col[target] === undefined) {
col[target] = {
sources: [],
targetNodes: document.querySelectorAll(`#${target}`)
}
}
col[target].sources.push(collapsible)
collapsible.addEventListener('click', () => {
toggleCollapsible(collapsible)
})
toggleCollapsible(collapsible, true)
})
function toggleCollapsible(collapsible, doCollapse) {
if (doCollapse === undefined) {
doCollapse = collapsible.getAttribute("collapsed") !== "true"
}
let target = collapsible.getAttribute("target")
col[target].sources.forEach((source) => {
let onCollapseNodes = collapsible.querySelectorAll(".on-collapse")
onCollapseNodes.forEach((onCollapseNode) => {
let onCollapse = onCollapseNode.getAttribute("oncollapse")
if (onCollapse !== null) {
if (doCollapse) {
onCollapseNode.classList.add(onCollapse)
}
else{
onCollapseNode.classList.remove(onCollapse)
}
}
})
source.setAttribute("collapsed", doCollapse)
})
let targetNodes = col[target].targetNodes
targetNodes.forEach((targetNode) => {
if (doCollapse) {
targetNode.classList.add("collapse-anim")
targetNode.classList.remove("expand-anim")
setTimeout(() => {
targetNode.classList.add("hidden")
}, 500);
}
else {
targetNode.classList.add("expand-anim")
targetNode.classList.remove("collapse-anim")
targetNode.classList.remove("hidden")
}
})
}
function collapse(collapsible) {
toggleCollapsible(collapsible, true)
}
function expand(collapsible) {
toggleCollapsible(collapsible, false)
}
function collapseAll() {
collapsibles.forEach((collapsible) => {
collapse(collapsible)
})
}
function expandAll() {
collapsibles.forEach((collapsible) => {
expand(collapsible)
})
}