mediawiki-skins-MinervaNeue/resources/skins.minerva.scripts/references.js
Stephen Niedzielski 874d9c9e3b Hygiene: update JSDoc boxed and JQuery types
From TypeScript's do's and don'ts:[0]

  Don’t ever use the types Number, String, Boolean, or Object. These
  types refer to non-primitive boxed objects that are almost never used
  appropriately in JavaScript code.

Although Minerva only uses JSDocs at this time which seemingly doesn't
care about casing[1], we should endeavor to use the proper return types.

This patch lowercases typing to indicate primitive / boxed type as
appropriate.[2] As a special case, function types are uppercased for
compatibility with TypeScript type checking.

Also, JQuery types are of type "JQuery". The global JQuery object's
identifier is "jQuery". This patch uppercases J's where appropriate.

Lastly, replace unsupported type "Integer" with "number" and a comment.

[0] https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html#general-types

[1] https://github.com/jsdoc3/jsdoc/issues/1046#issuecomment-126477791

[2] find resources tests -iname \*.js|
    xargs -rd\\n sed -ri '
      s%\{\s*(number|string|boolean|object|null|undefined)%{\L\1%gi;
      s%\{\s*function%{Function%g;
      s%\{\s*jquery%{JQuery%gi;
      s%\{\s*integer\s*\}%{number} An integer.%gi
    '

Change-Id: I6cbac15940e4501aee7ede8f421b77ffd027170d
2018-07-03 11:10:12 -05:00

118 lines
3.2 KiB
JavaScript

( function ( M, $ ) {
var drawer,
skin = M.require( 'skins.minerva.scripts/skin' ),
page = M.getCurrentPage(),
router = require( 'mediawiki.router' ),
util = M.require( 'mobile.startup/util' ),
ReferencesGateway = M.require( 'mobile.references.gateway/ReferencesGateway' ),
ReferencesMobileViewGateway = M.require(
'mobile.references.gateway/ReferencesMobileViewGateway'
),
referencesMobileViewGateway = ReferencesMobileViewGateway.getSingleton(),
ReferencesHtmlScraperGateway = M.require(
'mobile.references.gateway/ReferencesHtmlScraperGateway'
),
ReferencesDrawer = M.require( 'mobile.references/ReferencesDrawer' );
/**
* 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
* @param {JQuery.Event} ev Click event of the reference element
* @param {ReferencesDrawer} drawer to show the reference in
* @param {Page} page
*/
function showReference( ev, drawer, page ) {
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, page, $dest.text() );
// Previously showReference method returns nothing so we check its truthy
// Can be removed when I5a7b23f60722eb5017a85c68f38844dd460f8b63 is merged.
if ( result ) {
result.then( util.noop, 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, 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
.off( 'click.references' )
.on( 'click.references', {
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 );
} );
}( mw.mobileFrontend, jQuery ) );