2018-11-16 15:16:25 +00:00
|
|
|
( function ( M ) {
|
2017-07-12 15:12:40 +00:00
|
|
|
var drawer,
|
|
|
|
skin = M.require( 'skins.minerva.scripts/skin' ),
|
|
|
|
page = M.getCurrentPage(),
|
2018-03-08 19:22:55 +00:00
|
|
|
router = require( 'mediawiki.router' ),
|
2018-11-08 21:34:50 +00:00
|
|
|
mobile = M.require( 'mobile.startup' ),
|
|
|
|
ReferencesGateway = mobile.ReferencesGateway,
|
|
|
|
ReferencesMobileViewGateway = mobile.ReferencesMobileViewGateway,
|
2017-07-12 15:12:40 +00:00
|
|
|
referencesMobileViewGateway = ReferencesMobileViewGateway.getSingleton(),
|
2018-11-08 21:34:50 +00:00
|
|
|
ReferencesHtmlScraperGateway = mobile.ReferencesHtmlScraperGateway,
|
|
|
|
ReferencesDrawer = mobile.ReferencesDrawer;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a ReferenceDrawer based on the currently available
|
|
|
|
* ReferenceGateway
|
|
|
|
*
|
|
|
|
* @ignore
|
|
|
|
* @return {ReferencesDrawer}
|
|
|
|
*/
|
|
|
|
function referenceDrawerFactory() {
|
|
|
|
var gateway = null;
|
|
|
|
|
|
|
|
if ( mw.config.get( 'wgMFLazyLoadReferences', false ) ) {
|
|
|
|
gateway = referencesMobileViewGateway;
|
|
|
|
} else {
|
|
|
|
gateway = new ReferencesHtmlScraperGateway( new mw.Api() );
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ReferencesDrawer( {
|
|
|
|
gateway: gateway
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event handler to show reference when a reference link is clicked
|
|
|
|
* @ignore
|
2018-07-03 14:50:09 +00:00
|
|
|
* @param {JQuery.Event} ev Click event of the reference element
|
2017-07-12 15:12:40 +00:00
|
|
|
* @param {ReferencesDrawer} drawer to show the reference in
|
|
|
|
* @param {Page} page
|
|
|
|
*/
|
|
|
|
function showReference( ev, drawer, page ) {
|
2018-03-08 19:22:55 +00:00
|
|
|
var urlComponents, result,
|
2017-07-12 15:12:40 +00:00
|
|
|
$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];
|
|
|
|
}
|
2018-03-08 19:22:55 +00:00
|
|
|
result = drawer.showReference( href, page, $dest.text() );
|
|
|
|
// Previously showReference method returns nothing so we check its truthy
|
|
|
|
// Can be removed when I5a7b23f60722eb5017a85c68f38844dd460f8b63 is merged.
|
|
|
|
if ( result ) {
|
2018-10-01 18:54:52 +00:00
|
|
|
result.then( function () {}, function ( err ) {
|
2018-03-08 19:22:55 +00:00
|
|
|
if ( err === ReferencesGateway.ERROR_NOT_EXIST ) {
|
|
|
|
router.navigate( href );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
// 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
|
2018-07-03 14:50:09 +00:00
|
|
|
* @param {JQuery.Event} ev Click event of the reference element
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
function onClickReference( ev ) {
|
|
|
|
if ( !drawer ) {
|
|
|
|
drawer = referenceDrawerFactory();
|
|
|
|
}
|
|
|
|
showReference( ev, drawer, ev.data.page );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make references clickable and show a drawer when clicked on.
|
|
|
|
* @ignore
|
|
|
|
* @param {Page} page
|
|
|
|
*/
|
|
|
|
function setup( page ) {
|
|
|
|
var $refs = page.$( 'sup.reference a' );
|
|
|
|
|
|
|
|
if ( $refs.length ) {
|
|
|
|
$refs
|
2018-05-11 16:51:50 +00:00
|
|
|
.off( 'click.references' )
|
|
|
|
.on( 'click.references', {
|
2017-07-12 15:12:40 +00:00
|
|
|
page: page
|
|
|
|
}, onClickReference );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setup( page );
|
|
|
|
// When references are lazy loaded you'll want to take care of nested references
|
|
|
|
skin.on( 'references-loaded', function ( page ) {
|
|
|
|
setup( page );
|
|
|
|
} );
|
2018-11-16 15:16:25 +00:00
|
|
|
}( mw.mobileFrontend ) );
|