Merge "Improve beginMasterChanges and make methods for DeferredUpdates"
[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 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 );
11
12 // Hide/show the table of contents element
13 function toggleToc( e ) {
14 e.preventDefault();
15 if ( $tocList.is( ':hidden' ) ) {
16 $tocList.slideDown( 'fast' );
17 $tocToggleLink.text( mw.msg( 'hidetoc' ) );
18 $toc.removeClass( 'tochidden' );
19 mw.cookie.set( 'hidetoc', null );
20 } else {
21 $tocList.slideUp( 'fast' );
22 $tocToggleLink.text( mw.msg( 'showtoc' ) );
23 $toc.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 ( $toc.length && $tocTitle.length && $tocList.length && !$tocToggleLink.length ) {
31 hideToc = mw.cookie.get( 'hidetoc' ) === '1';
32
33 $tocToggleLink = $( '<a href="#" id="togglelink"></a>' )
34 .text( mw.msg( hideToc ? 'showtoc' : 'hidetoc' ) )
35 .click( toggleToc );
36
37 $tocTitle.append(
38 $tocToggleLink
39 .wrap( '<span class="toctoggle"></span>' )
40 .parent()
41 .prepend( '&nbsp;[' )
42 .append( ']&nbsp;' )
43 );
44
45 if ( hideToc ) {
46 $tocList.hide();
47 $toc.addClass( 'tochidden' );
48 }
49 }
50 } );
51
52 }( mediaWiki, jQuery ) );