@@ -10,7 +10,9 @@ let toggleChevron = `
10
10
11
11
var initToggleItems = ( ) => {
12
12
var itemsToToggle = document . querySelectorAll ( togglebuttonSelector ) ;
13
- console . log ( `[togglebutton]: Adding toggle buttons to ${ itemsToToggle . length } items` )
13
+ console . log (
14
+ `[togglebutton]: Adding toggle buttons to ${ itemsToToggle . length } items`
15
+ ) ;
14
16
// Add the button to each admonition and hook up a callback to toggle visibility
15
17
itemsToToggle . forEach ( ( item , index ) => {
16
18
if ( item . classList . contains ( "admonition" ) ) {
@@ -19,30 +21,40 @@ var initToggleItems = () => {
19
21
var toggleID = `toggle-${ index } ` ;
20
22
var buttonID = `button-${ toggleID } ` ;
21
23
22
- item . setAttribute ( 'id' , toggleID ) ;
23
- if ( ! item . classList . contains ( "toggle" ) ) {
24
+ item . setAttribute ( "id" , toggleID ) ;
25
+ if ( ! item . classList . contains ( "toggle" ) ) {
24
26
item . classList . add ( "toggle" ) ;
25
27
}
26
28
// This is the button that will be added to each item to trigger the toggle
27
29
var collapseButton = `
28
- <button type="button" id="${ buttonID } " class="toggle-button" data-target="${ toggleID } " data-button="${ buttonID } " data-toggle-hint="${ toggleHintShow } " aria-label="Toggle hidden content">
30
+ <button type="button"
31
+ id="${ buttonID } "
32
+ class="toggle-button"
33
+ data-target="${ toggleID } "
34
+ data-button="${ buttonID } "
35
+ data-toggle-hint="${ toggleHintShow } "
36
+ aria-label="Toggle hidden content"
37
+ aria-expanded="false"
38
+ >
29
39
${ toggleChevron }
30
40
</button>` ;
31
41
32
- title = item . querySelector ( ".admonition-title" )
42
+ title = item . querySelector ( ".admonition-title" ) ;
33
43
title . insertAdjacentHTML ( "beforeend" , collapseButton ) ;
34
44
thisButton = document . getElementById ( buttonID ) ;
35
45
36
46
// Add click handlers for the button + admonition title (if admonition)
37
- admonitionTitle = document . querySelector ( `#${ toggleID } > .admonition-title` )
47
+ admonitionTitle = document . querySelector (
48
+ `#${ toggleID } > .admonition-title`
49
+ ) ;
38
50
if ( admonitionTitle ) {
39
51
// If an admonition, then make the whole title block clickable
40
- admonitionTitle . addEventListener ( ' click' , toggleClickHandler ) ;
41
- admonitionTitle . dataset . target = toggleID
42
- admonitionTitle . dataset . button = buttonID
52
+ admonitionTitle . addEventListener ( " click" , toggleClickHandler ) ;
53
+ admonitionTitle . dataset . target = toggleID ;
54
+ admonitionTitle . dataset . button = buttonID ;
43
55
} else {
44
56
// If not an admonition then we'll listen for the button click
45
- thisButton . addEventListener ( ' click' , toggleClickHandler ) ;
57
+ thisButton . addEventListener ( " click" , toggleClickHandler ) ;
46
58
}
47
59
48
60
// Now hide the item for this toggle button unless explicitly noted to show
@@ -62,12 +74,12 @@ var initToggleItems = () => {
62
74
item . insertAdjacentHTML ( "beforebegin" , detailsBlock ) ;
63
75
64
76
// Now move the toggle-able content inside of the details block
65
- details = item . previousElementSibling
66
- details . appendChild ( item )
67
- item . classList . add ( "toggle-details__container" )
77
+ details = item . previousElementSibling ;
78
+ details . appendChild ( item ) ;
79
+ item . classList . add ( "toggle-details__container" ) ;
68
80
69
81
// Set up a click trigger to change the text as needed
70
- details . addEventListener ( ' click' , ( click ) => {
82
+ details . addEventListener ( " click" , ( click ) => {
71
83
let parent = click . target . parentElement ;
72
84
if ( parent . tagName . toLowerCase ( ) == "details" ) {
73
85
summary = parent . querySelector ( "summary" ) ;
@@ -78,35 +90,38 @@ var initToggleItems = () => {
78
90
}
79
91
// Update the inner text for the proper hint
80
92
if ( details . open ) {
81
- summary . querySelector ( "span.toggle-details__summary-text" ) . innerText = toggleHintShow ;
93
+ summary . querySelector ( "span.toggle-details__summary-text" ) . innerText =
94
+ toggleHintShow ;
82
95
} else {
83
- summary . querySelector ( "span.toggle-details__summary-text" ) . innerText = toggleHintHide ;
96
+ summary . querySelector ( "span.toggle-details__summary-text" ) . innerText =
97
+ toggleHintHide ;
84
98
}
85
-
86
99
} ) ;
87
100
88
101
// If we have a toggle-shown class, open details block should be open
89
102
if ( item . classList . contains ( "toggle-shown" ) ) {
90
103
details . click ( ) ;
91
104
}
92
105
}
93
- } )
106
+ } ) ;
94
107
} ;
95
108
96
109
// This should simply add / remove the collapsed class and change the button text
97
110
var toggleHidden = ( button ) => {
98
- target = button . dataset [ ' target' ]
111
+ target = button . dataset [ " target" ] ;
99
112
var itemToToggle = document . getElementById ( target ) ;
100
113
if ( itemToToggle . classList . contains ( "toggle-hidden" ) ) {
101
114
itemToToggle . classList . remove ( "toggle-hidden" ) ;
102
115
button . classList . remove ( "toggle-button-hidden" ) ;
103
116
button . dataset . toggleHint = toggleHintHide ;
117
+ button . setAttribute ( "aria-expanded" , true ) ;
104
118
} else {
105
119
itemToToggle . classList . add ( "toggle-hidden" ) ;
106
120
button . classList . add ( "toggle-button-hidden" ) ;
107
121
button . dataset . toggleHint = toggleHintShow ;
122
+ button . setAttribute ( "aria-expanded" , false ) ;
108
123
}
109
- }
124
+ } ;
110
125
111
126
var toggleClickHandler = ( click ) => {
112
127
// Be cause the admonition title is clickable and extends to the whole admonition
@@ -124,36 +139,36 @@ var toggleClickHandler = (click) => {
124
139
// We've clicked the button itself and so don't need to do anything
125
140
button = click . target ;
126
141
} else {
127
- console . log ( `[togglebutton]: Couldn't find button for ${ click . target } ` )
142
+ console . log ( `[togglebutton]: Couldn't find button for ${ click . target } ` ) ;
128
143
}
129
- target = document . getElementById ( button . dataset [ ' button' ] ) ;
144
+ target = document . getElementById ( button . dataset [ " button" ] ) ;
130
145
toggleHidden ( target ) ;
131
- }
146
+ } ;
132
147
133
148
// If we want to blanket-add toggle classes to certain cells
134
149
var addToggleToSelector = ( ) => {
135
150
const selector = "" ;
136
151
if ( selector . length > 0 ) {
137
152
document . querySelectorAll ( selector ) . forEach ( ( item ) => {
138
153
item . classList . add ( "toggle" ) ;
139
- } )
154
+ } ) ;
140
155
}
141
- }
156
+ } ;
142
157
143
158
// Helper function to run when the DOM is finished
144
- const sphinxToggleRunWhenDOMLoaded = cb => {
145
- if ( document . readyState != ' loading' ) {
146
- cb ( )
159
+ const sphinxToggleRunWhenDOMLoaded = ( cb ) => {
160
+ if ( document . readyState != " loading" ) {
161
+ cb ( ) ;
147
162
} else if ( document . addEventListener ) {
148
- document . addEventListener ( ' DOMContentLoaded' , cb )
163
+ document . addEventListener ( " DOMContentLoaded" , cb ) ;
149
164
} else {
150
- document . attachEvent ( ' onreadystatechange' , function ( ) {
151
- if ( document . readyState == ' complete' ) cb ( )
152
- } )
165
+ document . attachEvent ( " onreadystatechange" , function ( ) {
166
+ if ( document . readyState == " complete" ) cb ( ) ;
167
+ } ) ;
153
168
}
154
- }
155
- sphinxToggleRunWhenDOMLoaded ( addToggleToSelector )
156
- sphinxToggleRunWhenDOMLoaded ( initToggleItems )
169
+ } ;
170
+ sphinxToggleRunWhenDOMLoaded ( addToggleToSelector ) ;
171
+ sphinxToggleRunWhenDOMLoaded ( initToggleItems ) ;
157
172
158
173
/** Toggle details blocks to be open when printing */
159
174
if ( toggleOpenOnPrint == "true" ) {
@@ -163,21 +178,23 @@ if (toggleOpenOnPrint == "true") {
163
178
el . dataset [ "togglestatus" ] = el . open ;
164
179
el . open = true ;
165
180
} ) ;
166
-
181
+
167
182
// Open the admonitions
168
- document . querySelectorAll ( ".admonition.toggle.toggle-hidden" ) . forEach ( ( el ) => {
169
- console . log ( el ) ;
170
- el . querySelector ( "button.toggle-button" ) . click ( ) ;
171
- el . dataset [ "toggle_after_print" ] = "true" ;
172
- } ) ;
183
+ document
184
+ . querySelectorAll ( ".admonition.toggle.toggle-hidden" )
185
+ . forEach ( ( el ) => {
186
+ console . log ( el ) ;
187
+ el . querySelector ( "button.toggle-button" ) . click ( ) ;
188
+ el . dataset [ "toggle_after_print" ] = "true" ;
189
+ } ) ;
173
190
} ) ;
174
191
window . addEventListener ( "afterprint" , ( ) => {
175
192
// Re-close the details that were closed
176
193
document . querySelectorAll ( "details.toggle-details" ) . forEach ( ( el ) => {
177
194
el . open = el . dataset [ "togglestatus" ] == "true" ;
178
195
delete el . dataset [ "togglestatus" ] ;
179
196
} ) ;
180
-
197
+
181
198
// Re-close the admonition toggle buttons
182
199
document . querySelectorAll ( ".admonition.toggle" ) . forEach ( ( el ) => {
183
200
if ( el . dataset [ "toggle_after_print" ] == "true" ) {
0 commit comments