2021-04-21 01:04:08 +00:00
|
|
|
/**
|
2021-04-21 15:32:44 +00:00
|
|
|
* @param {Document} document
|
2021-04-21 01:04:08 +00:00
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
function initCollapsibleSections( document ) {
|
2022-04-30 18:47:00 +00:00
|
|
|
const prefix = 'section-',
|
|
|
|
headings = document.querySelectorAll( '.' + prefix + 'heading' ),
|
|
|
|
sections = document.querySelectorAll( '.' + prefix + 'collapsible' );
|
2021-04-21 01:04:08 +00:00
|
|
|
|
|
|
|
for ( let i = 0; i < headings.length; i++ ) {
|
2022-04-30 18:47:00 +00:00
|
|
|
const j = i + 1,
|
2022-04-30 21:19:15 +00:00
|
|
|
collapsibleID = prefix + 'collapsible-' + j,
|
2022-04-30 18:47:00 +00:00
|
|
|
headline = headings[ i ].querySelector( '.mw-headline' );
|
|
|
|
|
|
|
|
// Set up ARIA
|
|
|
|
headline.setAttribute( 'tabindex', 0 );
|
|
|
|
headline.setAttribute( 'role', 'button' );
|
|
|
|
headline.setAttribute( 'aria-controls', collapsibleID );
|
|
|
|
headline.setAttribute( 'aria-expanded', true );
|
|
|
|
|
|
|
|
// TODO: Need a keyboard handler
|
2021-04-21 01:04:08 +00:00
|
|
|
headings[ i ].addEventListener( 'click', function () {
|
2022-04-30 18:47:00 +00:00
|
|
|
// .section-heading--collapsed
|
2022-05-13 04:17:27 +00:00
|
|
|
|
2022-04-30 18:47:00 +00:00
|
|
|
this.classList.toggle( prefix + 'heading--collapsed' );
|
|
|
|
// .section-collapsible--collapsed
|
2022-05-13 04:17:27 +00:00
|
|
|
|
2022-04-30 18:47:00 +00:00
|
|
|
sections[ j ].classList.toggle( prefix + 'collapsible--collapsed' );
|
|
|
|
headline.setAttribute( 'aria-expanded', headline.getAttribute( 'aria-expanded' ) === 'true' ? 'false' : 'true' );
|
2021-04-21 01:04:08 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initCollapsibleSections( document );
|