From 532c5c33b44db79c31dbde34b4824e702b77577c Mon Sep 17 00:00:00 2001 From: Krinkle Date: Fri, 24 Dec 2010 00:55:20 +0000 Subject: [PATCH] Porting tocToggle to ResourceLoader * 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 | 2 +- resources/mediawiki.util/mediawiki.util.js | 55 ++++++++++++++++++-- skins/common/wikibits.js | 59 +--------------------- 3 files changed, 52 insertions(+), 64 deletions(-) diff --git a/resources/Resources.php b/resources/Resources.php index d9152b6e84..0949acb472 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -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( diff --git a/resources/mediawiki.util/mediawiki.util.js b/resources/mediawiki.util/mediawiki.util.js index 0f9c4c6039..51adb0f305 100644 --- a/resources/mediawiki.util/mediawiki.util.js +++ b/resources/mediawiki.util/mediawiki.util.js @@ -15,10 +15,10 @@ // 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' ) { @@ -49,15 +49,15 @@ 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 ) { @@ -65,6 +65,25 @@ } 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 = $( '' ).text( mw.msg( 'hidetoc' ) ).click( function(e){ + e.preventDefault(); + mw.util.toggleToc( $(this) ); + } ); + $tocTitle.append( $tocToggleLink.wrap( '' ).parent().prepend( ' [' ).append( '] ' ) ); + + if ( hideTocCookie == '1' ) { + // Cookie says user want toc hidden + $tocToggleLink.click(); + } + } } ); return true; @@ -117,6 +136,32 @@ 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 * diff --git a/skins/common/wikibits.js b/skins/common/wikibits.js index 91d647fd70..c70556e278 100644 --- a/skins/common/wikibits.js +++ b/skins/common/wikibits.js @@ -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 -- 2.20.1