-
Notifications
You must be signed in to change notification settings - Fork 1
/
frames.js
60 lines (53 loc) · 1.6 KB
/
frames.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
'use strict';
function parseDepth(frame) {
return parseInt(frame.dataset.depth);
}
// Returns next frame element or null
function getNextFrame(frame) {
frame = frame.nextElementSibling;
while (frame && !frame.classList.contains('frame')) {
frame = frame.nextSibling;
}
return frame;
}
function getAllChildFrames(frame) {
const rootDepth = parseDepth(frame);
const childFrames = [];
frame = getNextFrame(frame);
while (frame && parseDepth(frame) > rootDepth) {
childFrames.push(frame);
frame = getNextFrame(frame);
}
return childFrames;
}
// Toggles a frame and applies same action to all its child frames
// `this` refers to frame DOM element
function toggleFrame(event) {
const expanded = this.classList.contains('expanded');
const framesToSet = [this].concat(getAllChildFrames(this));
for (const frame of framesToSet) {
if (expanded)
frame.classList.remove('expanded');
else
frame.classList.add('expanded');
}
}
function initEventListeners() {
// Frame toggles
const frames = document.getElementsByClassName('frame');
for (const frame of frames) {
frame.addEventListener('click', toggleFrame);
}
// expand-all/collapse-all buttons
const expandAllButton = document.getElementById('expand-all');
const collapseAllButton = document.getElementById('collapse-all');
expandAllButton.addEventListener('click', function expandAll(event) {
for (const frame of frames)
frame.classList.add('expanded');
});
collapseAllButton.addEventListener('click', function collapseAll(event) {
for (const frame of frames)
frame.classList.remove('expanded');
});
}
window.onload = initEventListeners;