mediawiki-skins-MinervaNeue/resources/skins.minerva.tablet.scripts/toc.js
jdlrobson 2e7e7d3259 Only render table of contents on pages with a table of contents
Bug: T193517
Change-Id: I0ed0255280f525d7726d2e09ceab15a76e2d6026
2018-06-19 18:18:03 +00:00

40 lines
1.1 KiB
JavaScript

( function ( M, $ ) {
var TableOfContents = M.require( 'mobile.toc/TableOfContents' ),
Toggler = M.require( 'mobile.toggle/Toggler' );
/**
* Create TableOfContents if the given Page has sections and is not the main page
* and wgMFTocEnabled config variable is set to true.
* @method
* @param {Page} page for which a TOC is generated
* @ignore
*/
function init( page ) {
var toc,
sections = page.getSections(),
$toc = $( '#toc' );
toc = new TableOfContents( {
sections: sections
} );
// eslint-disable-next-line no-new
new Toggler( toc.$el, 'toc-', null, true );
// if there is a toc already, replace it
if ( $toc.length > 0 ) {
// don't show toc at end of page, when no sections there
$toc.replaceWith( toc.$el );
} else {
// otherwise append it to the lead section
toc.appendTo( page.getLeadSectionElement() );
}
}
// add a ToC only for "view" action (user is reading a page)
// provided a table of contents placeholder has been rendered
if ( mw.config.get( 'wgAction' ) === 'view' && $( '#toc' ).length > 0 ) {
init( M.getCurrentPage() );
}
}( mw.mobileFrontend, jQuery ) );