2021-04-21 01:04:08 +00:00
|
|
|
/**
|
2023-04-30 22:01:53 +00:00
|
|
|
* Set up functionality of collapsable sections
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} bodyContent
|
2021-04-21 01:04:08 +00:00
|
|
|
* @return {void}
|
|
|
|
*/
|
2023-04-30 22:01:53 +00:00
|
|
|
function init( bodyContent ) {
|
|
|
|
if ( !document.body.classList.contains( 'citizen-sections-enabled' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const
|
2023-09-10 23:00:52 +00:00
|
|
|
headings = bodyContent.querySelectorAll( '.citizen-section-heading' ),
|
|
|
|
sections = bodyContent.querySelectorAll( '.citizen-section-collapsible' ),
|
2023-06-20 18:29:37 +00:00
|
|
|
editSections = bodyContent.querySelectorAll( '.mw-editsection, .mw-editsection-like' );
|
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,
|
2024-01-27 02:51:04 +00:00
|
|
|
collapsibleID = `citizen-section-collapsible-${ j }`,
|
2022-11-25 02:46:23 +00:00
|
|
|
/* T13555 */
|
2023-06-21 18:30:45 +00:00
|
|
|
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
|
|
|
|
2023-09-10 23:00:52 +00:00
|
|
|
this.classList.toggle( 'citizen-section-heading--collapsed' );
|
2022-04-30 18:47:00 +00:00
|
|
|
// .section-collapsible--collapsed
|
2022-05-13 04:17:27 +00:00
|
|
|
|
2023-09-10 23:00:52 +00:00
|
|
|
sections[ j ].classList.toggle( 'citizen-section-collapsible--collapsed' );
|
2022-04-30 18:47:00 +00:00
|
|
|
headline.setAttribute( 'aria-expanded', headline.getAttribute( 'aria-expanded' ) === 'true' ? 'false' : 'true' );
|
2021-04-21 01:04:08 +00:00
|
|
|
} );
|
|
|
|
}
|
2023-06-20 18:29:37 +00:00
|
|
|
|
|
|
|
for ( let i = 0; i < editSections.length; i++ ) {
|
2023-06-20 18:30:08 +00:00
|
|
|
editSections[ i ].addEventListener( 'click', function ( e ) {
|
2023-06-20 18:29:37 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
} );
|
|
|
|
}
|
2021-04-21 01:04:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 19:48:17 +00:00
|
|
|
module.exports = {
|
2023-04-30 22:01:53 +00:00
|
|
|
init: init
|
2022-12-09 19:48:17 +00:00
|
|
|
};
|