Merge "build: Update qunitjs to 2.9.1"
[lhc/web/wiklou.git] / resources / src / mediawiki.toc / toc.js
1 ( function () {
2 'use strict';
3
4 // Table of contents toggle
5 mw.hook( 'wikipage.content' ).add( function ( $content ) {
6 $content.find( '.toc' ).addBack( '.toc' ).each( function () {
7 var hideToc,
8 $this = $( this ),
9 $tocToggleCheckbox = $this.children( '.toctogglecheckbox' ),
10 $tocTitle = $this.find( '.toctitle' ),
11 $tocToggleLink = $this.find( '.togglelink' ),
12 $tocList = $this.find( 'ul' ).eq( 0 );
13
14 // Hide/show the table of contents element
15 function toggleToc() {
16 if ( $tocList.is( ':hidden' ) ) {
17 // FIXME: Use CSS transitions
18 // eslint-disable-next-line jquery/no-slide
19 $tocList.slideDown( 'fast' );
20 $tocToggleLink.text( mw.msg( 'hidetoc' ) );
21 $this.removeClass( 'tochidden' );
22 mw.cookie.set( 'hidetoc', null );
23 } else {
24 // eslint-disable-next-line jquery/no-slide
25 $tocList.slideUp( 'fast' );
26 $tocToggleLink.text( mw.msg( 'showtoc' ) );
27 $this.addClass( 'tochidden' );
28 mw.cookie.set( 'hidetoc', '1' );
29 }
30 }
31
32 // Only add it if there is a complete TOC and it doesn't
33 // have a toggle added already
34 if ( !$tocToggleCheckbox.length && $tocTitle.length && $tocList.length && !$tocToggleLink.length ) {
35 hideToc = mw.cookie.get( 'hidetoc' ) === '1';
36
37 $tocToggleLink = $( '<a>' )
38 .attr( {
39 role: 'button',
40 tabindex: 0
41 } )
42 .addClass( 'togglelink' )
43 .text( mw.msg( hideToc ? 'showtoc' : 'hidetoc' ) )
44 .on( 'click keypress', function ( e ) {
45 if (
46 e.type === 'click' ||
47 e.type === 'keypress' && e.which === 13
48 ) {
49 toggleToc();
50 }
51 } );
52
53 $tocTitle.append(
54 $tocToggleLink
55 .wrap( '<span class="toctoggle"></span>' )
56 .parent()
57 .prepend( '&nbsp;[' )
58 .append( ']&nbsp;' )
59 );
60
61 if ( hideToc ) {
62 $tocList.hide();
63 $this.addClass( 'tochidden' );
64 }
65 }
66 } );
67 } );
68
69 }() );