| 1 |
/** |
| 2 |
* SimpleTOC load function. |
| 3 |
*/ |
| 4 |
const simpletocLoad = function () { |
| 5 |
const buttons = document.querySelectorAll( 'button.simpletoc-collapsible' ); |
| 6 |
|
| 7 |
buttons.forEach( ( button ) => { |
| 8 |
button.addEventListener( 'click', () => { |
| 9 |
button.classList.toggle( 'active' ); |
| 10 |
const content = button.parentElement.nextElementSibling; |
| 11 |
const isCollapsed = |
| 12 |
! content.style.maxHeight || content.style.maxHeight === '0px'; |
| 13 |
|
| 14 |
button.setAttribute( |
| 15 |
'aria-expanded', |
| 16 |
isCollapsed ? 'true' : 'false' |
| 17 |
); |
| 18 |
content.style.maxHeight = isCollapsed |
| 19 |
? `${ content.scrollHeight }px` |
| 20 |
: '0px'; |
| 21 |
} ); |
| 22 |
} ); |
| 23 |
}; |
| 24 |
|
| 25 |
// Allow others to call function if needed. |
| 26 |
window.simpletocLoad = simpletocLoad; |
| 27 |
|
| 28 |
// Check to see if the document is already loaded. |
| 29 |
if ( document.readyState === 'complete' || document.readyState !== 'loading' ) { |
| 30 |
simpletocLoad(); |
| 31 |
} else { |
| 32 |
// Fallback event if the document is not loaded. |
| 33 |
document.addEventListener( 'DOMContentLoaded', () => { |
| 34 |
simpletocLoad(); |
| 35 |
} ); |
| 36 |
} |
| 37 |
|