2018-11-16 15:16:25 +00:00
|
|
|
( function ( M ) {
|
2017-07-12 15:12:40 +00:00
|
|
|
var TableOfContents = M.require( 'mobile.toc/TableOfContents' ),
|
2018-12-04 22:13:38 +00:00
|
|
|
Toggler = M.require( 'mobile.toggle/Toggler' ),
|
|
|
|
eventBus = M.require( 'mobile.startup/eventBusSingleton' );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(),
|
2018-03-28 18:36:17 +00:00
|
|
|
$toc = $( '#toc' );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-03-28 18:36:17 +00:00
|
|
|
toc = new TableOfContents( {
|
|
|
|
sections: sections
|
|
|
|
} );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-03-28 18:36:17 +00:00
|
|
|
// eslint-disable-next-line no-new
|
2018-12-04 22:13:38 +00:00
|
|
|
new Toggler( {
|
|
|
|
$container: toc.$el,
|
|
|
|
prefix: 'toc-',
|
|
|
|
page: null,
|
|
|
|
isClosed: true,
|
|
|
|
eventBus: eventBus
|
|
|
|
} );
|
2018-03-28 18:36:17 +00:00
|
|
|
// 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() );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a ToC only for "view" action (user is reading a page)
|
2018-06-11 21:12:46 +00:00
|
|
|
// provided a table of contents placeholder has been rendered
|
|
|
|
if ( mw.config.get( 'wgAction' ) === 'view' && $( '#toc' ).length > 0 ) {
|
2017-07-12 15:12:40 +00:00
|
|
|
init( M.getCurrentPage() );
|
|
|
|
}
|
|
|
|
|
2018-11-16 15:16:25 +00:00
|
|
|
}( mw.mobileFrontend ) );
|