mediawiki-extensions-Popups/src/gateway/reference.js
Thiemo Kreuz f1fd3eafc4 Mark optional elements in the PreviewModel specification as such
We updated this documentation just recently via Ie370cfe. We followed
what the createModel() function does. But this is not the only way a
PreviewModel object can be created. Reference previews, for example,
don't use it.

Instead of following createModel(), I checked what the different popup
types actually use.

Bug: T214970
Change-Id: I2c4293a48387836dc30e18d10d952b4a26e6f2b5
2019-02-01 11:50:43 +01:00

42 lines
1 KiB
JavaScript

/**
* @module gateway/reference
*/
import { previewTypes } from '../preview/model';
const $ = jQuery;
/**
* @return {Gateway}
*/
export default function createReferenceGateway() {
/**
* @param {mw.Title} title
* @returns {AbortPromise<PreviewModel>}
*/
function fetchPreviewForTitle( title ) {
// Need to encode the fragment again as mw.Title returns it as decoded text
const id = title.getFragment().replace( / /g, '_' ),
$referenceText = $( `#${ $.escapeSelector( id ) } .reference-text` );
if ( !$referenceText.length ) {
return $.Deferred().reject(
'Footnote not found',
// Required to set `showNullPreview` to false and not open an error popup
{ textStatus: 'abort', xhr: { readyState: 0 } }
).promise( { abort() {} } );
}
return $.Deferred().resolve( {
// TODO: Provide different titles depending on the type of reference (e.g. "Book")
url: `#${ id }`,
extract: $referenceText.html(),
type: previewTypes.TYPE_REFERENCE
} ).promise( { abort() {} } );
}
return {
fetchPreviewForTitle
};
}