| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
let toggleAll; |
| 6 |
|
| 7 |
mw.hook( 'wikipage.content' ).add( () => { |
| 8 |
|
| 9 |
if ( toggleAll ) { |
| 10 |
return; |
| 11 |
} |
| 12 |
|
| 13 |
if ( !document.querySelector( '#mw-content-text .mw-parser-output .mw-collapsible' ) ) { |
| 14 |
return; |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
const portletLink = mw.util.addPortletLink( |
| 19 |
'p-tb', |
| 20 |
'#', |
| 21 |
mw.msg( 'collapsible-expand-all-text' ), |
| 22 |
't-collapsible-toggle-all', |
| 23 |
mw.msg( 'collapsible-expand-all-tooltip' ) |
| 24 |
); |
| 25 |
|
| 26 |
if ( !portletLink ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
toggleAll = portletLink.querySelector( 'a' ); |
| 32 |
toggleAll.setAttribute( 'role', 'button' ); |
| 33 |
|
| 34 |
|
| 35 |
toggleAll.setAttribute( 'aria-expanded', 'false' ); |
| 36 |
let allExpanded = false; |
| 37 |
|
| 38 |
|
| 39 |
toggleAll.addEventListener( 'click', ( e ) => { |
| 40 |
|
| 41 |
e.preventDefault(); |
| 42 |
|
| 43 |
if ( !allExpanded ) { |
| 44 |
const collapsed = document.querySelectorAll( '#mw-content-text .mw-parser-output .mw-made-collapsible.mw-collapsed' ); |
| 45 |
Array.prototype.forEach.call( collapsed, ( collapsible ) => { |
| 46 |
$( collapsible ).data( 'mw-collapsible' ).expand(); |
| 47 |
} ); |
| 48 |
toggleAll.textContent = mw.msg( 'collapsible-collapse-all-text' ); |
| 49 |
toggleAll.title = mw.msg( 'collapsible-collapse-all-tooltip' ); |
| 50 |
toggleAll.setAttribute( 'aria-expanded', 'true' ); |
| 51 |
allExpanded = true; |
| 52 |
|
| 53 |
} else { |
| 54 |
const expanded = document.querySelectorAll( '#mw-content-text .mw-parser-output .mw-made-collapsible:not( .mw-collapsed )' ); |
| 55 |
Array.prototype.forEach.call( expanded, ( collapsible ) => { |
| 56 |
$( collapsible ).data( 'mw-collapsible' ).collapse(); |
| 57 |
} ); |
| 58 |
toggleAll.textContent = mw.msg( 'collapsible-expand-all-text' ); |
| 59 |
toggleAll.title = mw.msg( 'collapsible-expand-all-tooltip' ); |
| 60 |
toggleAll.setAttribute( 'aria-expanded', 'false' ); |
| 61 |
allExpanded = false; |
| 62 |
} |
| 63 |
} ); |
| 64 |
} ); |