Porting tocToggle to ResourceLoader
authorKrinkle <krinkle@users.mediawiki.org>
Fri, 24 Dec 2010 00:55:20 +0000 (00:55 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Fri, 24 Dec 2010 00:55:20 +0000 (00:55 +0000)
* Path is now set to sitewide rather than current path so that it doesn't apply to just the current article but to the entire domain (fixes bug 26324)
* To avoid cookievalue being overriden by an older cookie from the legacy code (which had a more specific path and thus overrides it), using a different cookiename now ("mw_hidetoc" instead of "hidetoc")

resources/Resources.php
resources/mediawiki.util/mediawiki.util.js
skins/common/wikibits.js

index d9152b6..0949acb 100644 (file)
@@ -350,7 +350,7 @@ return array(
        ),
        'mediawiki.util' => array(
                'scripts' => 'resources/mediawiki.util/mediawiki.util.js',
-               'dependencies' => array( 'jquery.checkboxShiftClick', 'jquery.client', 'jquery.placeholder', 'jquery.makeCollapsible' ),
+               'dependencies' => array( 'jquery.checkboxShiftClick', 'jquery.client', 'jquery.cookie', 'jquery.placeholder', 'jquery.makeCollapsible' ),
                'debugScripts' => 'resources/mediawiki.util/mediawiki.util.test.js',
        ),
        'mediawiki.action.history' => array(
index 0f9c4c6..51adb0f 100644 (file)
                                // Any initialisation after the DOM is ready
                                $(function () {
 
-                                       // Shortcut
+                                       // Shortcut to client profile return
                                        var profile = $.client.profile();
 
-                                       // Set tooltipAccessKeyPrefix
+                                       /* Set tooltipAccessKeyPrefix */
 
                                        // Opera on any platform
                                        if ( profile.name == 'opera' ) {
                                                mw.util.tooltipAccessKeyPrefix = 'alt-shift-';
                                        }
 
-                                       // Enable CheckboxShiftClick
+                                       /* Enable CheckboxShiftClick */
                                        $( 'input[type=checkbox]:not(.noshiftselect)' ).checkboxShiftClick();
 
-                                       // Emulate placeholder if not supported by browser
+                                       /* Emulate placeholder if not supported by browser */
                                        if ( !( 'placeholder' in document.createElement( 'input' ) ) ) {
                                                $( 'input[placeholder]' ).placeholder();
                                        }
 
-                                       // Fill $content var
+                                       /* Fill $content var */
                                        if ( $( '#bodyContent' ).length ) {
                                                mw.util.$content = $( '#bodyContent' );
                                        } else if ( $( '#article' ).length ) {
                                        } else {
                                                mw.util.$content = $( '#content' );
                                        }
+
+                                       /* Table of Contents toggle */
+                                       var $tocContainer = $( '#toc' ),
+                                               $tocTitle = $( '#toctitle' ),
+                                               $tocToggleLink = $( '#togglelink' );
+                                       // Only add it if there is a TOC and there is no toggle added already
+                                       if ( $tocContainer.size() && $tocTitle.size() && !$tocToggleLink.size() ) {
+                                               var hideTocCookie = $.cookie( 'mw_hidetoc' ),
+                                                       $tocToggleLink = $( '<a href="#" class="internal" id="togglelink">' ).text( mw.msg( 'hidetoc' ) ).click( function(e){
+                                                               e.preventDefault();
+                                                               mw.util.toggleToc( $(this) );
+                                                       } );
+                                               $tocTitle.append( $tocToggleLink.wrap( '<span class="toctoggle">' ).parent().prepend( '&nbsp;[' ).append( ']&nbsp;' ) );
+                                               
+                                               if ( hideTocCookie == '1' ) {
+                                                       // Cookie says user want toc hidden
+                                                       $tocToggleLink.click();
+                                               }
+                                       }
                                } );
 
                                return true;
                        return s.sheet || s;
                },
 
+               /**
+                * Hide/show the table of contents element
+                *
+                * @param text String CSS to be appended
+                * @return the CSS stylesheet
+                */
+               'toggleToc' : function( $toggleLink ) {
+                       var $tocList = $( '#toc ul:first' ),
+
+                       if ( $tocList.is( ':hidden' ) ) {
+                               $tocList.slideDown( 'fast' );
+                               $toggleLink.text( mw.msg( 'hidetoc' ) );
+                               $.cookie( 'mw_hidetoc', null, {
+                                       expires: 30,
+                                       path: '/'
+                               } );
+                       } else {
+                               $tocList.slideUp( 'fast' );
+                               $toggleLink.text( mw.msg( 'showtoc' ) );
+                               $.cookie( 'mw_hidetoc', '1', {
+                                       expires: 30,
+                                       path: '/'
+                               } );
+                       }
+               },
+
                /**
                 * Get the full URL to a page name
                 *
index 91d647f..c70556e 100644 (file)
@@ -136,42 +136,6 @@ if ( 'wgBreakFrames' in window && window.wgBreakFrames ) {
        }
 }
 
-window.showTocToggle = function() {
-       if ( document.createTextNode ) {
-               // Uses DOM calls to avoid document.write + XHTML issues
-
-               var linkHolder = document.getElementById( 'toctitle' );
-               var existingLink = document.getElementById( 'togglelink' );
-               if ( !linkHolder || existingLink ) {
-                       // Don't add the toggle link twice
-                       return;
-               }
-
-               var outerSpan = document.createElement( 'span' );
-               outerSpan.className = 'toctoggle';
-
-               var toggleLink = document.createElement( 'a' );
-               toggleLink.id = 'togglelink';
-               toggleLink.className = 'internal';
-               toggleLink.href = '#';
-               addClickHandler( toggleLink, function( evt ) { toggleToc(); return killEvt( evt ); } );
-               
-               toggleLink.appendChild( document.createTextNode( mediaWiki.msg( 'hidetoc' ) ) );
-
-               outerSpan.appendChild( document.createTextNode( '[' ) );
-               outerSpan.appendChild( toggleLink );
-               outerSpan.appendChild( document.createTextNode( ']' ) );
-
-               linkHolder.appendChild( document.createTextNode( ' ' ) );
-               linkHolder.appendChild( outerSpan );
-
-               var cookiePos = document.cookie.indexOf( "hidetoc=" );
-               if ( cookiePos > -1 && document.cookie.charAt( cookiePos + 8 ) == 1 ) {
-                       toggleToc();
-               }
-       }
-};
-
 window.changeText = function( el, newText ) {
        // Safari work around
        if ( el.innerText ) {
@@ -192,25 +156,6 @@ window.killEvt = function( evt ) {
        return false; // Don't follow the link (IE)
 };
 
-window.toggleToc = function() {
-       var tocmain = document.getElementById( 'toc' );
-       var toc = document.getElementById('toc').getElementsByTagName('ul')[0];
-       var toggleLink = document.getElementById( 'togglelink' );
-
-       if ( toc && toggleLink && toc.style.display == 'none' ) {
-               changeText( toggleLink, mediaWiki.msg( 'hidetoc' ) );
-               toc.style.display = 'block';
-               document.cookie = "hidetoc=0";
-               tocmain.className = 'toc';
-       } else {
-               changeText( toggleLink, mediaWiki.msg( 'showtoc' ) );
-               toc.style.display = 'none';
-               document.cookie = "hidetoc=1";
-               tocmain.className = 'toc tochidden';
-       }
-       return false;
-};
-
 window.mwEditButtons = [];
 window.mwCustomEditButtons = []; // eg to add in MediaWiki:Common.js
 
@@ -1099,6 +1044,4 @@ hookEvent( 'load', runOnloadHook );
 
 if ( ie6_bugs ) {
        importScriptURI( stylepath + '/common/IEFixes.js' );
-}
-
-showTocToggle();
+}
\ No newline at end of file