Merge "Use ParserCache in CategoryMembershipChangeJob"
[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 $tocList.slideDown( 'fast' );
18 $tocToggleLink.text( mw.msg( 'hidetoc' ) );
19 $this.removeClass( 'tochidden' );
20 mw.cookie.set( 'hidetoc', null );
21 } else {
22 $tocList.slideUp( 'fast' );
23 $tocToggleLink.text( mw.msg( 'showtoc' ) );
24 $this.addClass( 'tochidden' );
25 mw.cookie.set( 'hidetoc', '1' );
26 }
27 }
28
29 // Only add it if there is a complete TOC and it doesn't
30 // have a toggle added already
31 if ( !$tocToggleCheckbox.length && $tocTitle.length && $tocList.length && !$tocToggleLink.length ) {
32 hideToc = mw.cookie.get( 'hidetoc' ) === '1';
33
34 $tocToggleLink = $( '<a>' )
35 .attr( {
36 role: 'button',
37 tabindex: 0
38 } )
39 .addClass( 'togglelink' )
40 .text( mw.msg( hideToc ? 'showtoc' : 'hidetoc' ) )
41 .on( 'click keypress', function ( e ) {
42 if (
43 e.type === 'click' ||
44 e.type === 'keypress' && e.which === 13
45 ) {
46 toggleToc();
47 }
48 } );
49
50 $tocTitle.append(
51 $tocToggleLink
52 .wrap( '<span class="toctoggle"></span>' )
53 .parent()
54 .prepend( '&nbsp;[' )
55 .append( ']&nbsp;' )
56 );
57
58 if ( hideToc ) {
59 $tocList.hide();
60 $this.addClass( 'tochidden' );
61 }
62 }
63 } );
64 } );
65
66 }() );