Merge "Clarify docs for wgCookieExpiration"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.toc.js
1 /**
2 * @private
3 * @singleton
4 * @class mw.toc
5 */
6 ( function ( mw, $ ) {
7 'use strict';
8
9 // Table of contents toggle
10 mw.hook( 'wikipage.content' ).add( function ( $content ) {
11 var $toc, $tocTitle, $tocToggleLink, hideToc;
12 $toc = $content.find( '#toc' );
13 $tocTitle = $content.find( '#toctitle' );
14 $tocToggleLink = $content.find( '#togglelink' );
15
16 /**
17 * Hide/show the table of contents element
18 *
19 * @param {jQuery} $toggleLink A jQuery object of the toggle link.
20 */
21 function toggleToc( $toggleLink ) {
22 var $tocList = $toc.find( 'ul:first' );
23
24 // This function shouldn't be called if there's no TOC,
25 // but just in case...
26 if ( $tocList.length ) {
27 if ( $tocList.is( ':hidden' ) ) {
28 $tocList.slideDown( 'fast' );
29 $toggleLink.text( mw.msg( 'hidetoc' ) );
30 $toc.removeClass( 'tochidden' );
31 $.cookie( 'mw_hidetoc', null, {
32 expires: 30,
33 path: '/'
34 } );
35 } else {
36 $tocList.slideUp( 'fast' );
37 $toggleLink.text( mw.msg( 'showtoc' ) );
38 $toc.addClass( 'tochidden' );
39 $.cookie( 'mw_hidetoc', '1', {
40 expires: 30,
41 path: '/'
42 } );
43 }
44 }
45 }
46 // Only add it if there is a TOC and there is no toggle added already
47 if ( $toc.length && $tocTitle.length && !$tocToggleLink.length ) {
48 hideToc = $.cookie( 'mw_hidetoc' ) === '1';
49 $tocToggleLink = $( '<a href="#" class="internal" id="togglelink"></a>' )
50 .text( hideToc ? mw.msg( 'showtoc' ) : 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 );
62
63 if ( hideToc ) {
64 $toc.find( 'ul:first' ).hide();
65 $toc.addClass( 'tochidden' );
66 }
67 }
68 } );
69
70 }( mediaWiki, jQuery ) );