mediawiki-skins-Citizen/resources/skins.citizen.scripts/sections.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

/**
* Set up functionality of collapsable sections
*
* @param {HTMLElement} bodyContent
* @return {void}
*/
function init( bodyContent ) {
if ( !document.body.classList.contains( 'citizen-sections-enabled' ) ) {
return;
}
const
headings = bodyContent.querySelectorAll( '.section-heading' ),
sections = bodyContent.querySelectorAll( '.section-collapsible' );
for ( let i = 0; i < headings.length; i++ ) {
const j = i + 1,
collapsibleID = `.section-collapsible-${j}`,
/* T13555 */
headline = headings[ i ].querySelector( '.mw-headline' ) ?? headings[ i ].querySelector( '.mw-heading' );
// 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
headings[ i ].addEventListener( 'click', function () {
// .section-heading--collapsed
2022-05-13 04:17:27 +00:00
this.classList.toggle( 'section-heading--collapsed' );
// .section-collapsible--collapsed
2022-05-13 04:17:27 +00:00
sections[ j ].classList.toggle( 'section-collapsible--collapsed' );
headline.setAttribute( 'aria-expanded', headline.getAttribute( 'aria-expanded' ) === 'true' ? 'false' : 'true' );
} );
}
}
module.exports = {
init: init
};