Include all of /resources/mediawiki/* in jsduck index
[mediawiki.git] / resources / mediawiki / mediawiki.toc.js
blob6eb855284ed159b4682ed7248894786fcb83e1bf
1 /**
2  * @private
3  * @singleton
4  * @class mw.toc
5  */
6 ( function ( mw, $ ) {
7         'use strict';
9         // Table of contents toggle
10         mw.hook( 'wikipage.content' ).add( function ( $content ) {
12                 /**
13                  * Hide/show the table of contents element
14                  *
15                  * @param {jQuery} $toggleLink A jQuery object of the toggle link.
16                  */
17                 function toggleToc( $toggleLink ) {
18                         var $tocList = $content.find( '#toc ul:first' );
20                         // This function shouldn't be called if there's no TOC,
21                         // but just in case...
22                         if ( $tocList.length ) {
23                                 if ( $tocList.is( ':hidden' ) ) {
24                                         $tocList.slideDown( 'fast' );
25                                         $toggleLink.text( mw.msg( 'hidetoc' ) );
26                                         $content.find( '#toc' ).removeClass( 'tochidden' );
27                                         $.cookie( 'mw_hidetoc', null, {
28                                                 expires: 30,
29                                                 path: '/'
30                                         } );
31                                 } else {
32                                         $tocList.slideUp( 'fast' );
33                                         $toggleLink.text( mw.msg( 'showtoc' ) );
34                                         $content.find( '#toc' ).addClass( 'tochidden' );
35                                         $.cookie( 'mw_hidetoc', '1', {
36                                                 expires: 30,
37                                                 path: '/'
38                                         } );
39                                 }
40                         }
41                 }
43                 var $tocTitle, $tocToggleLink, hideTocCookie;
44                 $tocTitle = $content.find( '#toctitle' );
45                 $tocToggleLink = $content.find( '#togglelink' );
46                 // Only add it if there is a TOC and there is no toggle added already
47                 if ( $content.find( '#toc' ).length && $tocTitle.length && !$tocToggleLink.length ) {
48                         hideTocCookie = $.cookie( 'mw_hidetoc' );
49                         $tocToggleLink = $( '<a href="#" class="internal" id="togglelink"></a>' )
50                                 .text( mw.msg( 'hidetoc' ) )
51                                 .click( function ( e ) {
52                                         e.preventDefault();
53                                         toggleToc( $( this ) );
54                                 } );
55                         $tocTitle.append(
56                                 $tocToggleLink
57                                         .wrap( '<span class="toctoggle"></span>' )
58                                         .parent()
59                                                 .prepend( '&nbsp;[' )
60                                                 .append( ']&nbsp;' )
61                         );
63                         if ( hideTocCookie === '1' ) {
64                                 toggleToc( $tocToggleLink );
65                         }
66                 }
67         } );
69 }( mediaWiki, jQuery ) );