mediawiki-skins-MinervaNeue/resources/skins.minerva.toggling/init.js
Nicholas Ray 331df226f5 Make Minerva use new PageHTMLParser.js and refactored Page.js
In I02f8645aac1d7b081eaba8f2ac92a2ef51f78182, Page.js was made into a
model and its html parsing responsibilities were moved to
PageHTMLParser. Additionally, a singleton for the current page
(currentPage.js) and a singleton for the curent page html parser
(currentPageHTMLParser.js) were introduced to replace the usage of
M.getCurrentPage().

This commit refactors Minerva to make use of these changes.

Notable changes:

* 🐛 The event bus singleton was added to references.js since it
previously relied on an instance of Skin to listen for the
`references-loaded` event. However, this event is emitted on the event
bus singleton.

* Additionally, I didn't see a reason why the `references-loaded` event
needed to also pass the current page instance when the only file
listening to it is references.js (which already has the current page
instance) so I removed that and the convoluted passing of page.js within
the file. I assumed this logic was unecessary.

* The call to drawer.showReferences in references.js now was made to
pass the currentPage instance as well as the currentPageHTMLParser. This
is to prepare for I6e858bbe73f83166476b5b2c0fdac1cca7404246 where the
getReferences() interface for ReferencesMobileViewGateway.js and
ReferencesHtmlScraperGateway.js is refactored to accept both of these
instances. Additionally, references.js was refactored to support event
delegation which gets rid of some parsing/setup logic.

Bug: T193077
Depends-On: I02f8645aac1d7b081eaba8f2ac92a2ef51f78182
Change-Id: I2f32dbcc4ebaa4bebb297cda1ecce3457922b343
2019-07-15 16:43:51 -06:00

56 lines
1.7 KiB
JavaScript

( function ( M ) {
var
// eslint-disable-next-line no-jquery/no-global-selector
$contentContainer = $( '#mw-content-text > .mw-parser-output' ),
mobile = M.require( 'mobile.startup' ),
currentPage = mobile.currentPage(),
currentPageHTMLParser = mobile.currentPageHTMLParser(),
Toggler = mobile.Toggler,
eventBus = mobile.eventBusSingleton;
if ( !currentPageHTMLParser.getLeadSectionElement() ) {
// Operating in desktop Minerva mode. Stop execution. (T172948)
return;
}
// If there was no mw-parser-output wrapper, just use the parent
if ( $contentContainer.length === 0 ) {
// eslint-disable-next-line no-jquery/no-global-selector
$contentContainer = $( '#mw-content-text' );
}
/**
* Initialises toggling code.
*
* @method
* @param {JQuery.Object} $container to enable toggling on
* @param {string} prefix a prefix to use for the id.
* @param {Page} page The current page
* @ignore
*/
function init( $container, prefix, page ) {
// distinguish headings in content from other headings
$container.find( '> h1,> h2,> h3,> h4,> h5,> h6' ).addClass( 'section-heading' )
.removeAttr( 'onclick' );
// cleanup global as it is no longer needed. We check if it's undefined because
// there is no guarantee this won't be run on other skins e.g. Vector or cached HTML
if ( window.mfTempOpenSection !== undefined ) {
delete window.mfTempOpenSection;
}
// eslint-disable-next-line no-new
new Toggler( {
$container: $container,
prefix: prefix,
page: page,
eventBus: eventBus
} );
}
// avoid this running on Watchlist
if (
!currentPage.inNamespace( 'special' ) &&
mw.config.get( 'wgAction' ) === 'view'
) {
init( $contentContainer, 'content-', currentPage );
}
}( mw.mobileFrontend ) );