-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollapsibleSections.js
More file actions
176 lines (144 loc) · 5.75 KB
/
Copy pathcollapsibleSections.js
File metadata and controls
176 lines (144 loc) · 5.75 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
window.addEventListener("DOMContentLoaded", () => {
const sections = document.querySelectorAll(".collapsibleSection");
const LAST_OPEN_SECTION_KEY = "pdfConverter.lastOpenSection";
const saveLastOpenSection = (sectionId) => {
if (!sectionId) return;
try { localStorage.setItem(LAST_OPEN_SECTION_KEY, sectionId); } catch (_) {}
};
const clearLastOpenSection = () => {
try { localStorage.removeItem(LAST_OPEN_SECTION_KEY); } catch (_) {}
};
const getLastOpenSection = () => {
try { return localStorage.getItem(LAST_OPEN_SECTION_KEY); } catch (_) { return null; }
};
const sectionControls = new Map();
sections.forEach((section) => {
const summary = section.querySelector(":scope > summary");
const content = section.querySelector(":scope > .collapsibleContent");
if (!summary || !content) return;
let isAnimating = false;
let revealTimer = null;
const cards = section.querySelectorAll(":scope > .collapsibleContent .optionCard");
cards.forEach((card, index) => {
const delay = Math.min((index + 1) * 40, 280);
card.style.setProperty("--card-delay", `${delay}ms`);
});
const clearRevealTimer = () => {
if (revealTimer) { clearTimeout(revealTimer); revealTimer = null; }
};
const hideCards = () => {
clearRevealTimer();
section.classList.remove("cardsVisible");
};
const showCardsDelayed = () => {
clearRevealTimer();
// Next frame: links must receive clicks immediately (120ms felt like "nothing happens")
revealTimer = setTimeout(() => {
section.classList.add("cardsVisible");
}, 0);
};
const setExpandedState = () => {
content.style.height = "auto";
content.style.opacity = "1";
};
const setCollapsedState = () => {
content.style.height = "0px";
content.style.opacity = "0";
};
if (section.hasAttribute("open")) {
setExpandedState();
section.classList.add("cardsVisible");
} else {
setCollapsedState();
section.classList.remove("cardsVisible");
}
const finishAnimation = (handler) => {
const onTransitionEnd = (event) => {
if (event.propertyName !== "height") return;
content.removeEventListener("transitionend", onTransitionEnd);
handler();
isAnimating = false;
};
content.addEventListener("transitionend", onTransitionEnd);
};
const expand = () => {
if (isAnimating) return;
isAnimating = true;
section.setAttribute("open", "");
hideCards();
content.style.height = "0px";
content.style.opacity = "0";
saveLastOpenSection(section.id);
requestAnimationFrame(() => {
content.style.height = `${content.scrollHeight}px`;
content.style.opacity = "1";
});
showCardsDelayed();
finishAnimation(() => {
content.style.height = "auto";
});
};
const collapse = () => {
if (isAnimating) return;
isAnimating = true;
hideCards();
setCollapsedState();
section.removeAttribute("open");
clearLastOpenSection();
isAnimating = false;
};
const collapseInstant = () => {
clearRevealTimer();
section.classList.remove("cardsVisible");
content.style.height = "0px";
content.style.opacity = "0";
section.removeAttribute("open");
isAnimating = false;
};
sectionControls.set(section, { expand, collapse, collapseInstant });
summary.addEventListener("click", (event) => {
event.preventDefault();
if (isAnimating) return;
if (section.hasAttribute("open")) {
collapse();
} else {
sections.forEach((otherSection) => {
if (otherSection !== section && otherSection.hasAttribute("open")) {
sectionControls.get(otherSection)?.collapseInstant();
}
});
expand();
}
});
});
const categoryTiles = document.querySelectorAll(".categoryTile");
categoryTiles.forEach((tile) => {
tile.addEventListener("click", () => {
const sectionName = tile.getAttribute("data-section");
const targetSection = document.getElementById(`${sectionName}Section`);
if (!targetSection) return;
const isCurrentlyOpen = targetSection.hasAttribute("open");
const targetControls = sectionControls.get(targetSection);
sections.forEach((section) => {
if (section !== targetSection && section.hasAttribute("open")) {
sectionControls.get(section)?.collapseInstant();
}
});
if (!isCurrentlyOpen) {
targetControls?.expand();
}
setTimeout(() => {
targetSection.scrollIntoView({ behavior: "smooth", block: "start" });
}, 100);
});
});
const lastOpenSectionId = getLastOpenSection();
if (lastOpenSectionId) {
const lastSection = document.getElementById(lastOpenSectionId);
if (lastSection && !lastSection.hasAttribute("open")) {
sectionControls.get(lastSection)?.expand();
} else if (!lastSection) {
clearLastOpenSection();
}
}
});