2018-12-12 18:37:19 +00:00
|
|
|
/**
|
|
|
|
* @module gateway/reference
|
|
|
|
*/
|
|
|
|
|
2019-01-24 15:33:29 +00:00
|
|
|
import { previewTypes } from '../preview/model';
|
|
|
|
|
2019-01-24 18:24:36 +00:00
|
|
|
const $ = jQuery;
|
2018-12-12 18:37:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {Gateway}
|
|
|
|
*/
|
|
|
|
export default function createReferenceGateway() {
|
|
|
|
/**
|
|
|
|
* @param {mw.Title} title
|
2019-02-04 12:08:36 +00:00
|
|
|
* @param {Element} el
|
2018-12-12 18:37:19 +00:00
|
|
|
* @returns {AbortPromise<PreviewModel>}
|
|
|
|
*/
|
2019-02-04 12:08:36 +00:00
|
|
|
function fetchPreviewForTitle( title, el ) {
|
2019-01-24 10:37:30 +00:00
|
|
|
// Need to encode the fragment again as mw.Title returns it as decoded text
|
2019-01-25 19:44:48 +00:00
|
|
|
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() {} } );
|
|
|
|
}
|
2018-12-12 18:37:19 +00:00
|
|
|
|
|
|
|
return $.Deferred().resolve( {
|
2019-01-28 12:22:31 +00:00
|
|
|
url: `#${ id }`,
|
2019-01-25 19:44:48 +00:00
|
|
|
extract: $referenceText.html(),
|
2019-02-04 12:08:36 +00:00
|
|
|
type: previewTypes.TYPE_REFERENCE,
|
|
|
|
sourceElementId: el && el.parentNode && el.parentNode.id
|
2018-12-12 18:37:19 +00:00
|
|
|
} ).promise( { abort() {} } );
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2019-01-23 16:50:19 +00:00
|
|
|
fetchPreviewForTitle
|
2018-12-12 18:37:19 +00:00
|
|
|
};
|
|
|
|
}
|