mediawiki-skins-MinervaNeue/resources/skins.minerva.scripts/references.js
jdlrobson d1cd558821 Drop lazy loading references feature usage
This temporarily and quietly breaks references display in beta
on the assumption
that I2dcf9b0cbfe2f46eb6763e0add4ed892d1fbecd9 will be merged swiftly
afterwards.

Bug: T123328
Change-Id: I2dcf9b0cbfe2f46eb6763e0add4ed892d1fbecd9
2019-07-26 19:04:46 +00:00

90 lines
2.5 KiB
JavaScript

module.exports = function () {
var drawer,
M = mw.mobileFrontend,
router = require( 'mediawiki.router' ),
mobile = M.require( 'mobile.startup' ),
currentPage = mobile.currentPage(),
currentPageHTMLParser = mobile.currentPageHTMLParser(),
ReferencesGateway = mobile.ReferencesGateway,
ReferencesHtmlScraperGateway = mobile.ReferencesHtmlScraperGateway,
ReferencesDrawer = mobile.ReferencesDrawer;
/**
* Creates a ReferenceDrawer based on the currently available
* ReferenceGateway
*
* @ignore
* @return {ReferencesDrawer}
*/
function referenceDrawerFactory() {
var gateway = new ReferencesHtmlScraperGateway( new mw.Api() );
return new ReferencesDrawer( {
gateway: gateway
} );
}
/**
* Event handler to show reference when a reference link is clicked
* @ignore
* @param {JQuery.Event} ev Click event of the reference element
* @param {ReferencesDrawer} drawer to show the reference in
*/
function showReference( ev, drawer ) {
var urlComponents, result,
$dest = $( ev.currentTarget ),
href = $dest.attr( 'href' );
ev.preventDefault();
// If necessary strip the URL portion of the href so we are left with the
// fragment
urlComponents = href.split( '#' );
if ( urlComponents.length > 1 ) {
href = '#' + urlComponents[ 1 ];
}
result = drawer.showReference( href, currentPage, $dest.text(), currentPageHTMLParser );
// Previously showReference method returns nothing so we check its truthy
// Can be removed when I5a7b23f60722eb5017a85c68f38844dd460f8b63 is merged.
if ( result ) {
result.then( function () {}, function ( err ) {
if ( err === ReferencesGateway.ERROR_NOT_EXIST ) {
router.navigate( href );
}
} );
}
// don't hide drawer (stop propagation of click) if it is already shown
// (e.g. click another reference)
if ( drawer.isVisible() ) {
ev.stopPropagation();
} else {
// flush any existing reference information
drawer.render( {
text: undefined
} );
}
}
/**
* Event handler to show reference when a reference link is clicked.
* Delegates to `showReference` once the references drawer is ready.
*
* @ignore
* @param {JQuery.Event} ev Click event of the reference element
*/
function onClickReference( ev ) {
if ( !drawer ) {
drawer = referenceDrawerFactory();
}
showReference( ev, drawer );
}
function init() {
// Make references clickable and show a drawer when clicked on.
mobile.util.getDocument().on( 'click', 'sup.reference a', onClickReference );
}
init();
};