Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / resources / src / mediawiki / mediawiki.toc.js
blob0955c23f067b260cb2f06b2affb37c74bc694ca7
1 ( function ( mw, $ ) {
2         'use strict';
4         // Table of contents toggle
5         mw.hook( 'wikipage.content' ).add( function ( $content ) {
6                 var $toc, $tocTitle, $tocToggleLink, $tocList, hideToc;
7                 $toc = $content.find( '#toc' );
8                 $tocTitle = $content.find( '#toctitle' );
9                 $tocToggleLink = $content.find( '#togglelink' );
10                 $tocList = $toc.find( 'ul' ).eq( 0 );
12                 // Hide/show the table of contents element
13                 function toggleToc() {
14                         if ( $tocList.is( ':hidden' ) ) {
15                                 $tocList.slideDown( 'fast' );
16                                 $tocToggleLink.text( mw.msg( 'hidetoc' ) );
17                                 $toc.removeClass( 'tochidden' );
18                                 mw.cookie.set( 'hidetoc', null );
19                         } else {
20                                 $tocList.slideUp( 'fast' );
21                                 $tocToggleLink.text( mw.msg( 'showtoc' ) );
22                                 $toc.addClass( 'tochidden' );
23                                 mw.cookie.set( 'hidetoc', '1' );
24                         }
25                 }
27                 // Only add it if there is a complete TOC and it doesn't
28                 // have a toggle added already
29                 if ( $toc.length && $tocTitle.length && $tocList.length && !$tocToggleLink.length ) {
30                         hideToc = mw.cookie.get( 'hidetoc' ) === '1';
32                         $tocToggleLink = $( '<a role="button" tabindex="0" id="togglelink"></a>' )
33                                 .text( mw.msg( hideToc ? 'showtoc' : 'hidetoc' ) )
34                                 .on( 'click keypress', function ( e ) {
35                                         if (
36                                                 e.type === 'click' ||
37                                                 e.type === 'keypress' && e.which === 13
38                                         ) {
39                                                 toggleToc();
40                                         }
41                                 } );
43                         $tocTitle.append(
44                                 $tocToggleLink
45                                         .wrap( '<span class="toctoggle"></span>' )
46                                         .parent()
47                                                 .prepend( '&nbsp;[' )
48                                                 .append( ']&nbsp;' )
49                         );
51                         if ( hideToc ) {
52                                 $tocList.hide();
53                                 $toc.addClass( 'tochidden' );
54                         }
55                 }
56         } );
58 }( mediaWiki, jQuery ) );