Debian Codegraph @

resources/src/mediawiki.page.ready/toggleAllCollapsibles.js

raw symbols SCIP
1 /*!
2 * Add portlet link to toggle all collapsibles created by
3 * the jquery.makeCollapsible module.
4 */
5 let toggleAll;
6
7 mw.hook( 'wikipage.content' ).add( () => {
8 // return early if the link was already added
9 if ( toggleAll ) {
10 return;
11 }
12 // return early if there are no collapsibles within the parsed page content
13 if ( !document.querySelector( '#mw-content-text .mw-parser-output .mw-collapsible' ) ) {
14 return;
15 }
16
17 // create portlet link for expand/collapse all
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 // return early if no link was added (e.g. no toolbox)
26 if ( !portletLink ) {
27 return;
28 }
29
30 // set up the toggle link
31 toggleAll = portletLink.querySelector( 'a' );
32 toggleAll.setAttribute( 'role', 'button' );
33
34 // initially treat as collapsed
35 toggleAll.setAttribute( 'aria-expanded', 'false' );
36 let allExpanded = false;
37
38 // on click, expand/collapse all collapsibles, then prepare to do the opposite on the next click
39 toggleAll.addEventListener( 'click', ( e ) => {
40 // Prevent scrolling
41 e.preventDefault();
42 // expand
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 // collapse
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 } );