PrefixSearch: Avoid notice when no subpage exists
[mediawiki.git] / resources / src / mediawiki / mediawiki.toc.js
blobe8dd4732cfdc155fb50e1c070125d9017779201b
1 ( function ( mw, $ ) {
2         'use strict';
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 );
12                 // Hide/show the table of contents element
13                 function toggleToc() {
14                         if ( $tocList.is( ':hidden' ) ) {
15                                 $tocList.slideDown( 'fast' );
16                                 $tocToggleLink.text( mw.msg( 'hidetoc' ) );
17                                 $toc.removeClass( 'tochidden' );
18                                 $.cookie( 'mw_hidetoc', null, {
19                                         expires: 30,
20                                         path: '/'
21                                 } );
22                         } else {
23                                 $tocList.slideUp( 'fast' );
24                                 $tocToggleLink.text( mw.msg( 'showtoc' ) );
25                                 $toc.addClass( 'tochidden' );
26                                 $.cookie( 'mw_hidetoc', '1', {
27                                         expires: 30,
28                                         path: '/'
29                                 } );
30                         }
31                 }
33                 // Only add it if there is a complete TOC and it doesn't
34                 // have a toggle added already
35                 if ( $toc.length && $tocTitle.length && $tocList.length && !$tocToggleLink.length ) {
36                         hideToc = $.cookie( 'mw_hidetoc' ) === '1';
38                         $tocToggleLink = $( '<a href="#" class="internal" id="togglelink"></a>' )
39                                 .text( hideToc ? mw.msg( 'showtoc' ) : mw.msg( 'hidetoc' ) )
40                                 .click( function ( e ) {
41                                         e.preventDefault();
42                                         toggleToc();
43                                 } );
45                         $tocTitle.append(
46                                 $tocToggleLink
47                                         .wrap( '<span class="toctoggle"></span>' )
48                                         .parent()
49                                                 .prepend( '&nbsp;[' )
50                                                 .append( ']&nbsp;' )
51                         );
53                         if ( hideToc ) {
54                                 $tocList.hide();
55                                 $toc.addClass( 'tochidden' );
56                         }
57                 }
58         } );
60 }( mediaWiki, jQuery ) );