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