aboutsummaryrefslogtreecommitdiffstats
path: root/resources/src/mediawiki.toc/toc.js
blob: 6cd7efb3f0291d8d322dc25cbcb47eed1248a6b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
( function () {
	'use strict';

	function initToc( tocNode ) {
		const toggleNode = tocNode.querySelector( '.toctogglecheckbox' );

		if ( !toggleNode ) {
			return;
		}

		let hidden = false;

		toggleNode.addEventListener( 'change', () => {
			hidden = !hidden;
			mw.cookie.set( 'hidetoc', hidden ? '1' : null );
			toggleNode.setAttribute( 'aria-label', hidden ?
				mw.message( 'table-of-contents-show-button-aria-label' ).plain() :
				mw.message( 'table-of-contents-hide-button-aria-label' ).plain()
			);
		} );

		// Initial state
		if ( mw.cookie.get( 'hidetoc' ) === '1' ) {
			toggleNode.checked = true;
			hidden = true;
		}
		toggleNode.setAttribute( 'aria-label', hidden ?
			mw.message( 'table-of-contents-show-button-aria-label' ).plain() :
			mw.message( 'table-of-contents-hide-button-aria-label' ).plain()
		);
	}

	mw.hook( 'wikipage.content' ).add( ( $content ) => {
		const tocs = $content[ 0 ] ? $content[ 0 ].querySelectorAll( '.toc' ) : [];
		let i = tocs.length;
		while ( i-- ) {
			initToc( tocs[ i ] );
		}
	} );
}() );