Merge "Improve "selfmove" message's wording"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.toc.js
1 ( function ( mw, $ ) {
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 $tocTitle = $this.find( '.toctitle' ),
10 $tocToggleLink = $this.find( '.togglelink' ),
11 $tocList = $this.find( 'ul' ).eq( 0 );
12
13 // Hide/show the table of contents element
14 function toggleToc() {
15 if ( $tocList.is( ':hidden' ) ) {
16 $tocList.slideDown( 'fast' );
17 $tocToggleLink.text( mw.msg( 'hidetoc' ) );
18 $this.removeClass( 'tochidden' );
19 mw.cookie.set( 'hidetoc', null );
20 } else {
21 $tocList.slideUp( 'fast' );
22 $tocToggleLink.text( mw.msg( 'showtoc' ) );
23 $this.addClass( 'tochidden' );
24 mw.cookie.set( 'hidetoc', '1' );
25 }
26 }
27
28 // Only add it if there is a complete TOC and it doesn't
29 // have a toggle added already
30 if ( $tocTitle.length && $tocList.length && !$tocToggleLink.length ) {
31 hideToc = mw.cookie.get( 'hidetoc' ) === '1';
32
33 $tocToggleLink = $( '<a role="button" tabindex="0" class="togglelink"></a>' )
34 .text( mw.msg( hideToc ? 'showtoc' : 'hidetoc' ) )
35 .on( 'click keypress', function ( e ) {
36 if (
37 e.type === 'click' ||
38 e.type === 'keypress' && e.which === 13
39 ) {
40 toggleToc();
41 }
42 } );
43
44 $tocTitle.append(
45 $tocToggleLink
46 .wrap( '<span class="toctoggle"></span>' )
47 .parent()
48 .prepend( '&nbsp;[' )
49 .append( ']&nbsp;' )
50 );
51
52 if ( hideToc ) {
53 $tocList.hide();
54 $this.addClass( 'tochidden' );
55 }
56 }
57 } );
58 } );
59
60 }( mediaWiki, jQuery ) );