Esanders has uploaded a new change for review. https://gerrit.wikimedia.org/r/297690
Change subject: Use classes instead of IDs for TOC collapsing ...................................................................... Use classes instead of IDs for TOC collapsing One may way to have multiple TOC's on the page (e.g. in VisualEditor). Change-Id: I19701c4037b653b2944e407752e50f444861f883 --- M includes/Linker.php M resources/src/mediawiki/mediawiki.toc.js M tests/qunit/suites/resources/mediawiki/mediawiki.toc.test.js 3 files changed, 45 insertions(+), 41 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/90/297690/1 diff --git a/includes/Linker.php b/includes/Linker.php index 0b2d3a7..646784f 100644 --- a/includes/Linker.php +++ b/includes/Linker.php @@ -1652,7 +1652,7 @@ $title = wfMessage( 'toc' )->inLanguage( $lang )->escaped(); return '<div id="toc" class="toc">' - . '<div id="toctitle"><h2>' . $title . "</h2></div>\n" + . '<div id="toctitle" class="toctitle"><h2>' . $title . "</h2></div>\n" . $toc . "</ul>\n</div>\n"; } diff --git a/resources/src/mediawiki/mediawiki.toc.js b/resources/src/mediawiki/mediawiki.toc.js index 78627fc..752db5e 100644 --- a/resources/src/mediawiki/mediawiki.toc.js +++ b/resources/src/mediawiki/mediawiki.toc.js @@ -4,51 +4,55 @@ // Table of contents toggle mw.hook( 'wikipage.content' ).add( function ( $content ) { var $toc, $tocTitle, $tocToggleLink, $tocList, hideToc; - $toc = $content.find( '#toc' ); - $tocTitle = $content.find( '#toctitle' ); - $tocToggleLink = $content.find( '#togglelink' ); - $tocList = $toc.find( 'ul' ).eq( 0 ); + $toc = $content.find( '.toc' ).addBack( '.toc' ); - // Hide/show the table of contents element - function toggleToc() { - if ( $tocList.is( ':hidden' ) ) { - $tocList.slideDown( 'fast' ); - $tocToggleLink.text( mw.msg( 'hidetoc' ) ); - $toc.removeClass( 'tochidden' ); - mw.cookie.set( 'hidetoc', null ); - } else { - $tocList.slideUp( 'fast' ); - $tocToggleLink.text( mw.msg( 'showtoc' ) ); - $toc.addClass( 'tochidden' ); - mw.cookie.set( 'hidetoc', '1' ); + $toc.each( function () { + var $this = $( this ); + $tocTitle = $this.find( '.toctitle' ); + $tocToggleLink = $this.find( '.togglelink' ); + $tocList = $this.find( 'ul' ).eq( 0 ); + + // Hide/show the table of contents element + function toggleToc() { + if ( $tocList.is( ':hidden' ) ) { + $tocList.slideDown( 'fast' ); + $tocToggleLink.text( mw.msg( 'hidetoc' ) ); + $this.removeClass( 'tochidden' ); + mw.cookie.set( 'hidetoc', null ); + } else { + $tocList.slideUp( 'fast' ); + $tocToggleLink.text( mw.msg( 'showtoc' ) ); + $this.addClass( 'tochidden' ); + mw.cookie.set( 'hidetoc', '1' ); + } } - } - // 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 = mw.cookie.get( 'hidetoc' ) === '1'; + // Only add it if there is a complete TOC and it doesn't + // have a toggle added already + if ( $tocTitle.length && $tocList.length && !$tocToggleLink.length ) { + hideToc = mw.cookie.get( 'hidetoc' ) === '1'; - $tocToggleLink = $( '<a href="#" id="togglelink"></a>' ) - .text( hideToc ? mw.msg( 'showtoc' ) : mw.msg( 'hidetoc' ) ) - .click( function ( e ) { - e.preventDefault(); - toggleToc(); - } ); + $tocToggleLink = $( '<a href="#" class="togglelink"></a>' ) + .text( hideToc ? mw.msg( 'showtoc' ) : mw.msg( 'hidetoc' ) ) + .click( function ( e ) { + e.preventDefault(); + toggleToc(); + } ); - $tocTitle.append( - $tocToggleLink - .wrap( '<span class="toctoggle"></span>' ) - .parent() - .prepend( ' [' ) - .append( '] ' ) - ); + $tocTitle.append( + $tocToggleLink + .wrap( '<span class="toctoggle"></span>' ) + .parent() + .prepend( ' [' ) + .append( '] ' ) + ); - if ( hideToc ) { - $tocList.hide(); - $toc.addClass( 'tochidden' ); + if ( hideToc ) { + $tocList.hide(); + $this.addClass( 'tochidden' ); + } } - } + } ); } ); }( mediaWiki, jQuery ) ); diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.toc.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.toc.test.js index 89eb45f..0cb8961 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.toc.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.toc.test.js @@ -12,7 +12,7 @@ assert.strictEqual( $( '#toc' ).length, 0, 'There is no table of contents on the page at the beginning' ); tocHtml = '<div id="toc" class="toc">' + - '<div id="toctitle">' + + '<div id="toctitle" class="toctitle">' + '<h2>Contents</h2>' + '</div>' + '<ul><li></li></ul>' + @@ -21,7 +21,7 @@ mw.hook( 'wikipage.content' ).fire( $( '#qunit-fixture' ) ); $tocList = $( '#toc ul:first' ); - $toggleLink = $( '#togglelink' ); + $toggleLink = $( '#toc .togglelink' ); assert.strictEqual( $toggleLink.length, 1, 'Toggle link is added to the table of contents' ); -- To view, visit https://gerrit.wikimedia.org/r/297690 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I19701c4037b653b2944e407752e50f444861f883 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Esanders <esand...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits