2021-04-21 01:04:08 +00:00
|
|
|
/**
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2022-12-09 19:48:17 +00:00
|
|
|
function initCollapsibleSections() {
|
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-11-25 02:46:23 +00:00
|
|
|
/* T13555 */
|
|
|
|
headline = headings[ i ].querySelector( '.mw-headline' ) ?? headings[ i ].querySelector( '.mw-heading' );
|
2022-04-30 18:47:00 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 19:48:17 +00:00
|
|
|
module.exports = {
|
|
|
|
init: initCollapsibleSections
|
|
|
|
};
|