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-26 03:51:50 +00:00
|
|
|
const setHeadlineAttributes = ( heading, collapsibleID, sectionIndex ) => {
|
|
|
|
const headline = heading.querySelector( '.mw-headline, .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
|
|
|
|
2024-05-26 03:51:50 +00:00
|
|
|
headline.setAttribute( 'tabindex', '0' );
|
2022-04-30 18:47:00 +00:00
|
|
|
headline.setAttribute( 'role', 'button' );
|
|
|
|
headline.setAttribute( 'aria-controls', collapsibleID );
|
2024-05-26 03:51:50 +00:00
|
|
|
headline.setAttribute( 'aria-expanded', 'true' );
|
|
|
|
headline.setAttribute( 'data-mw-citizen-section-heading-index', sectionIndex );
|
2024-05-21 22:08:28 +00:00
|
|
|
};
|
2022-04-30 18:47:00 +00:00
|
|
|
|
2024-05-26 03:51:50 +00:00
|
|
|
const setSectionAttributes = ( section ) => {
|
|
|
|
section.setAttribute( 'aria-hidden', 'false' );
|
|
|
|
};
|
|
|
|
|
|
|
|
const toggleClasses = ( heading, section ) => {
|
|
|
|
if ( section ) {
|
|
|
|
heading.classList.toggle( 'citizen-section-heading--collapsed' );
|
|
|
|
section.classList.toggle( 'citizen-section--collapsed' );
|
2024-05-21 22:46:19 +00:00
|
|
|
}
|
|
|
|
};
|
2022-05-13 04:17:27 +00:00
|
|
|
|
2024-05-26 03:51:50 +00:00
|
|
|
const toggleAriaAttribute = ( el, attribute ) => {
|
|
|
|
const isAttributeSet = el.getAttribute( attribute ) === 'true';
|
|
|
|
el.setAttribute( attribute, isAttributeSet ? 'false' : 'true' );
|
2024-05-21 22:46:19 +00:00
|
|
|
};
|
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;
|
|
|
|
}
|
|
|
|
|
2024-05-26 03:51:50 +00:00
|
|
|
const selectedHeading = target.closest( '.citizen-section-heading' );
|
2024-05-21 22:46:19 +00:00
|
|
|
|
2024-05-26 03:51:50 +00:00
|
|
|
if ( selectedHeading ) {
|
|
|
|
const selectedHeadline = selectedHeading.querySelector( '.mw-headline, .mw-heading' );
|
2024-05-21 22:46:19 +00:00
|
|
|
|
2024-05-26 03:51:50 +00:00
|
|
|
if ( selectedHeadline ) {
|
|
|
|
const sectionIndex = +selectedHeadline.dataset.mwCitizenSectionHeadingIndex;
|
|
|
|
const selectedSection = sections[ sectionIndex + 1 ];
|
|
|
|
toggleClasses( selectedHeading, selectedSection );
|
|
|
|
toggleAriaAttribute( selectedHeadline, 'aria-expanded' );
|
|
|
|
toggleAriaAttribute( selectedSection, 'aria-hidden' );
|
2024-05-21 22:46:19 +00:00
|
|
|
}
|
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
|
|
|
|
2024-05-26 03:51:50 +00:00
|
|
|
sections.forEach( ( section ) => {
|
|
|
|
setSectionAttributes( section );
|
|
|
|
} );
|
|
|
|
|
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
|
|
|
};
|