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;
|
|
|
|
}
|
|
|
|
|
2024-05-21 22:08:28 +00:00
|
|
|
const headings = bodyContent.querySelectorAll( '.citizen-section-heading' );
|
2024-05-21 22:46:19 +00:00
|
|
|
const sections = bodyContent.querySelectorAll( '.citizen-section' );
|
2021-04-21 01:04:08 +00:00
|
|
|
|
2024-05-21 22:46:19 +00:00
|
|
|
const setHeadlineAttributes = ( heading, collapsibleID, i ) => {
|
|
|
|
const headline = heading.querySelector( '.mw-headline' ) ||
|
|
|
|
heading.querySelector( '.mw-heading' );
|
2024-05-21 22:08:28 +00:00
|
|
|
|
2024-05-21 22:46:19 +00:00
|
|
|
if ( !headline ) {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-30 18:47:00 +00:00
|
|
|
|
|
|
|
headline.setAttribute( 'tabindex', 0 );
|
|
|
|
headline.setAttribute( 'role', 'button' );
|
|
|
|
headline.setAttribute( 'aria-controls', collapsibleID );
|
|
|
|
headline.setAttribute( 'aria-expanded', true );
|
2024-05-21 22:46:19 +00:00
|
|
|
headline.setAttribute( 'data-mw-citizen-section-heading-index', i );
|
2024-05-21 22:08:28 +00:00
|
|
|
};
|
2022-04-30 18:47:00 +00:00
|
|
|
|
2024-05-21 22:46:19 +00:00
|
|
|
const toggleClasses = ( i ) => {
|
|
|
|
if ( sections[ i + 1 ] ) {
|
|
|
|
headings[ i ].classList.toggle( 'citizen-section-heading--collapsed' );
|
|
|
|
sections[ i + 1 ].classList.toggle( 'citizen-section--collapsed' );
|
|
|
|
}
|
|
|
|
};
|
2022-05-13 04:17:27 +00:00
|
|
|
|
2024-05-21 22:46:19 +00:00
|
|
|
const toggleAriaExpanded = ( el ) => {
|
|
|
|
const isExpanded = el.getAttribute( 'aria-expanded' ) === 'true';
|
|
|
|
el.setAttribute( 'aria-expanded', isExpanded ? 'false' : 'true' );
|
|
|
|
};
|
2022-05-13 04:17:27 +00:00
|
|
|
|
2024-05-21 22:46:19 +00:00
|
|
|
const onEditSectionClick = ( e ) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
};
|
2023-06-20 18:29:37 +00:00
|
|
|
|
2024-05-21 22:46:19 +00:00
|
|
|
const handleClick = ( e ) => {
|
|
|
|
const target = e.target;
|
|
|
|
const isEditSection = target.closest( '.mw-editsection, .mw-editsection-like' );
|
|
|
|
|
|
|
|
if ( isEditSection ) {
|
|
|
|
onEditSectionClick( e );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const heading = target.closest( '.citizen-section-heading' );
|
|
|
|
|
|
|
|
if ( heading ) {
|
|
|
|
const headline = heading.querySelector( '.mw-headline' ) ||
|
|
|
|
heading.querySelector( '.mw-heading' );
|
|
|
|
|
|
|
|
if ( headline ) {
|
2024-05-22 03:15:53 +00:00
|
|
|
const i = +headline.getAttribute( 'data-mw-citizen-section-heading-index' );
|
2024-05-21 22:46:19 +00:00
|
|
|
toggleClasses( i );
|
|
|
|
toggleAriaExpanded( headline );
|
|
|
|
}
|
2024-05-21 22:08:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-21 22:46:19 +00:00
|
|
|
const headingsLength = headings.length;
|
|
|
|
for ( let i = 0; i < headingsLength; i++ ) {
|
|
|
|
setHeadlineAttributes( headings[ i ], `citizen-section-${ i + 1 }`, i );
|
2023-06-20 18:29:37 +00:00
|
|
|
}
|
2024-05-21 22:46:19 +00:00
|
|
|
|
|
|
|
bodyContent.addEventListener( 'click', handleClick, false );
|
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
|
|
|
};
|