mediawiki.toc: Clean up left overs
authorTimo Tijhof <krinklemail@gmail.com>
Tue, 6 May 2014 23:29:37 +0000 (01:29 +0200)
committerOri.livneh <ori@wikimedia.org>
Wed, 7 May 2014 07:40:19 +0000 (07:40 +0000)
Follows-up d7a40c8 which creates the mediawiki.toc module and
moved the tocToggle method from mediawiki.util.

The tocToggle callback used to be a public method and therefore
had two things about that that are obsolete:

* Taking the toggle as parameter.
  This is a private method now, and we already have a reference
  to the toggle.

* Asserting that the #toc has an <ul>.
  There is no need to defer this check this far in, nor to repeat
  it on each click. The old code checked it earlier was well, but
  we used to repeat the check here as the method was public.
  Now that it is a private helper method we can simply return
  early before setting it all up in the first place.

Also:

* Removed jsduck-like construction. This file is being indexed,
  as part of src/mediawiki/, but turning this into a class seems
  odd. It doesn't really have any structure to it.
  It's just UI glue. Turned it into an ignored file (no doc blocks).

Change-Id: I21d4f735032b96f00291502e06d9d35a74648143

resources/src/mediawiki/mediawiki.toc.js

index dfdedb6..e8dd473 100644 (file)
@@ -1,57 +1,47 @@
-/**
- * @private
- * @singleton
- * @class mw.toc
- */
 ( function ( mw, $ ) {
        'use strict';
 
        // Table of contents toggle
        mw.hook( 'wikipage.content' ).add( function ( $content ) {
-               var $toc, $tocTitle, $tocToggleLink, hideToc;
+               var $toc, $tocTitle, $tocToggleLink, $tocList, hideToc;
                $toc = $content.find( '#toc' );
                $tocTitle = $content.find( '#toctitle' );
                $tocToggleLink = $content.find( '#togglelink' );
+               $tocList = $toc.find( 'ul' ).eq( 0 );
 
-               /**
-                * Hide/show the table of contents element
-                *
-                * @param {jQuery} $toggleLink A jQuery object of the toggle link.
-                */
-               function toggleToc( $toggleLink ) {
-                       var $tocList = $toc.find( 'ul:first' );
-
-                       // This function shouldn't be called if there's no TOC,
-                       // but just in case...
-                       if ( $tocList.length ) {
-                               if ( $tocList.is( ':hidden' ) ) {
-                                       $tocList.slideDown( 'fast' );
-                                       $toggleLink.text( mw.msg( 'hidetoc' ) );
-                                       $toc.removeClass( 'tochidden' );
-                                       $.cookie( 'mw_hidetoc', null, {
-                                               expires: 30,
-                                               path: '/'
-                                       } );
-                               } else {
-                                       $tocList.slideUp( 'fast' );
-                                       $toggleLink.text( mw.msg( 'showtoc' ) );
-                                       $toc.addClass( 'tochidden' );
-                                       $.cookie( 'mw_hidetoc', '1', {
-                                               expires: 30,
-                                               path: '/'
-                                       } );
-                               }
+               // Hide/show the table of contents element
+               function toggleToc() {
+                       if ( $tocList.is( ':hidden' ) ) {
+                               $tocList.slideDown( 'fast' );
+                               $tocToggleLink.text( mw.msg( 'hidetoc' ) );
+                               $toc.removeClass( 'tochidden' );
+                               $.cookie( 'mw_hidetoc', null, {
+                                       expires: 30,
+                                       path: '/'
+                               } );
+                       } else {
+                               $tocList.slideUp( 'fast' );
+                               $tocToggleLink.text( mw.msg( 'showtoc' ) );
+                               $toc.addClass( 'tochidden' );
+                               $.cookie( 'mw_hidetoc', '1', {
+                                       expires: 30,
+                                       path: '/'
+                               } );
                        }
                }
-               // Only add it if there is a TOC and there is no toggle added already
-               if ( $toc.length && $tocTitle.length && !$tocToggleLink.length ) {
+
+               // Only add it if there is a complete TOC and it doesn't
+               // have a toggle added already
+               if ( $toc.length && $tocTitle.length && $tocList.length && !$tocToggleLink.length ) {
                        hideToc = $.cookie( 'mw_hidetoc' ) === '1';
+
                        $tocToggleLink = $( '<a href="#" class="internal" id="togglelink"></a>' )
                                .text( hideToc ? mw.msg( 'showtoc' ) : mw.msg( 'hidetoc' ) )
                                .click( function ( e ) {
                                        e.preventDefault();
-                                       toggleToc( $( this ) );
+                                       toggleToc();
                                } );
+
                        $tocTitle.append(
                                $tocToggleLink
                                        .wrap( '<span class="toctoggle"></span>' )
@@ -61,7 +51,7 @@
                        );
 
                        if ( hideToc ) {
-                               $toc.find( 'ul:first' ).hide();
+                               $tocList.hide();
                                $toc.addClass( 'tochidden' );
                        }
                }