-
Notifications
You must be signed in to change notification settings - Fork 0
/
section-toggle.js
132 lines (125 loc) · 3.64 KB
/
section-toggle.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
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
/*
on button click, it toggles its own css class.
css class toggle means .desc is no longer hidden.
but wait, some screen reader users have vision.
*/
var kConfirm = { 13: 1, 32: 1 };
/* Resets the ARIA label for the specified topic/chapter/resource. */
function fSetNewARIALabel( hRelabeledElement, sReplacementLabel )
{
sReplacementLabel += " the ";
sReplacementLabel += hRelabeledElement.textContent || hRelabeledElement.innerText;
sReplacementLabel += " details";
hRelabeledElement.setAttribute( "aria-label", sReplacementLabel );
}
/* Toggles the folding of a topic/chapter/resource. */
function fToggleFoldingTopicChapterResource( hElement )
{
let sNewARIALabel = "";
if( hElement.classList.contains( "state-open" ) )
{
hElement.classList.remove( "state-open" );
if( hElement.classList.contains( "button-chapter-name" ) )
{
hElement.parentNode.classList.remove( "state-open" );
}
else if( hElement.classList.contains( "chapter" ) )
{
hElement = hElement.getElementsByClassName( "button-chapter-name" )[ 0 ];
hElement.classList.remove( "state-open" );
}
sNewARIALabel = "Expand";
}
else
{
hElement.classList.add( "state-open" );
if( hElement.classList.contains( "button-chapter-name" ) )
{
hElement.parentNode.classList.add( "state-open" );
}
else if( hElement.classList.contains( "chapter" ) )
{
hElement = hElement.getElementsByClassName( "button-chapter-name" )[ 0 ];
hElement.classList.add( "state-open" );
}
sNewARIALabel = "Collapse";
}
if( hElement.classList.contains( "button-chapter-name" ) )
{
fSetNewARIALabel( hElement, sNewARIALabel );
}
else
{
hElement.setAttribute( "aria-label", sNewARIALabel );
}
}
/* Handles key input for a a resource. */
function fHandleKeyUpResource( event )
{
if( kConfirm[ event.keyCode ] )
{
let sNodeTagName = document.activeElement.nodeName;
if( sNodeTagName != "A" )
{
fToggleFoldingTopicChapterResource( this );
}
}
}
/* Handles key input for a a resource. */
function fHandleClickResource( event )
{
let sNodeTagName = event.target.nodeName;
if( sNodeTagName != "SPAN" && sNodeTagName != "A" )
{
fToggleFoldingTopicChapterResource( this );
}
}
/* A wrapper function for toggling a chapter and focusing its first child. */
function fWrapperToggleChapter( hTargetChapter )
{
let aByClassChapter = document.getElementsByClassName( "chapter" );
var aSlicedChapters = Array.prototype.slice.call( aByClassChapter );
/// Note to self: Why does this for loop need to happen?
for( let i = 0; i < aSlicedChapters.length; i++ )
{
if( hTargetChapter == aSlicedChapters[ i ] )
{
fToggleFoldingTopicChapterResource( aSlicedChapters[ i ] );
i = aSlicedChapters.length;
}
}
if( hTargetChapter.classList.contains( "state-open" ) == false && hTargetChapter.classList.contains( "resource" ) == false )
{
if( hTargetChapter.classList.contains( "button-chapter-name" ) )
{
hTargetChapter = hTargetChapter.parentNode;
}
hTargetChapter = hTargetChapter.getElementsByTagName( "section" )[ 0 ];
hTargetChapter = hTargetChapter.getElementsByTagName( "div" )[ 0 ];
setTimeout( function()
{
hTargetChapter.focus();
}, 1 // Needs one tick to render.
);
}
}
/* Functions for key and mouse interactions with chapters. */
function fHandleClickChapter( event )
{
if( event.type == "mousedown" || kConfirm[ event.keyCode ] )
{
fWrapperToggleChapter( event.target );
}
}
function fHandleKeyUpChapter( event )
{
if( kConfirm[ event.keyCode ] )
{
fWrapperToggleChapter( document.activeElement );
}
}
/* Handles mouse input on the chapter folding toggle button. */
function fHandleClickChapterButton( event )
{
fToggleFoldingTopicChapterResource( this );
}