2018-12-12 18:37:19 +00:00
|
|
|
/**
|
|
|
|
* @module referencePreview
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { renderPopup } from '../popup/popup';
|
|
|
|
import { escapeHTML } from '../templateUtil';
|
|
|
|
|
2019-02-16 09:13:23 +00:00
|
|
|
// Known citation type strings currently supported with icons and messages.
|
|
|
|
const KNOWN_TYPES = [ 'book', 'journal', 'news', 'web' ],
|
2019-03-13 14:31:16 +00:00
|
|
|
mw = mediaWiki,
|
|
|
|
$ = jQuery;
|
2019-01-24 15:33:29 +00:00
|
|
|
|
2018-12-12 18:37:19 +00:00
|
|
|
/**
|
2019-03-18 12:18:25 +00:00
|
|
|
* @param {ext.popups.ReferencePreviewModel} model
|
2019-02-21 11:40:03 +00:00
|
|
|
* @return {JQuery}
|
2018-12-12 18:37:19 +00:00
|
|
|
*/
|
|
|
|
export function renderReferencePreview(
|
|
|
|
model
|
|
|
|
) {
|
2019-02-16 09:13:23 +00:00
|
|
|
const type = KNOWN_TYPES.indexOf( model.referenceType ) < 0 ? 'generic' : model.referenceType,
|
2019-03-13 14:22:33 +00:00
|
|
|
// Messages:
|
|
|
|
// popups-refpreview-book
|
|
|
|
// popups-refpreview-journal
|
|
|
|
// popups-refpreview-news
|
|
|
|
// popups-refpreview-reference
|
|
|
|
// popups-refpreview-web
|
2019-03-12 16:23:46 +00:00
|
|
|
titleMsg = `popups-refpreview-${ type === 'generic' ? 'reference' : type }`,
|
2019-02-16 09:13:23 +00:00
|
|
|
title = escapeHTML( mw.msg( titleMsg ) ),
|
2019-01-28 07:54:49 +00:00
|
|
|
url = escapeHTML( model.url ),
|
2019-03-12 16:23:46 +00:00
|
|
|
linkMsg = escapeHTML( mw.msg( 'popups-refpreview-jump-to-reference' ) );
|
2018-12-12 18:37:19 +00:00
|
|
|
|
2019-01-30 18:53:50 +00:00
|
|
|
const $el = renderPopup( model.type,
|
2018-12-12 18:37:19 +00:00
|
|
|
`
|
2019-01-24 17:48:13 +00:00
|
|
|
<strong class='mwe-popups-title'>
|
2019-02-16 09:13:23 +00:00
|
|
|
<span class='mw-ui-icon mw-ui-icon-element mw-ui-icon-reference-${ type }'></span>
|
2019-01-24 17:48:13 +00:00
|
|
|
${ title }
|
|
|
|
</strong>
|
2019-02-28 17:50:36 +00:00
|
|
|
<div class='mwe-popups-extract'>
|
|
|
|
<div class='mw-parser-output'>${ model.extract }</div>
|
2019-04-26 11:35:27 +00:00
|
|
|
<div class='mwe-popups-fade' />
|
2019-02-28 17:50:36 +00:00
|
|
|
</div>
|
2018-12-12 18:37:19 +00:00
|
|
|
<footer>
|
2019-01-28 07:54:49 +00:00
|
|
|
<a href='${ url }' class='mwe-popups-read-link'>${ linkMsg }</a>
|
2018-12-12 18:37:19 +00:00
|
|
|
</footer>
|
|
|
|
`
|
2019-01-30 18:53:50 +00:00
|
|
|
);
|
2019-01-26 09:56:08 +00:00
|
|
|
|
|
|
|
// Make sure to not destroy existing targets, if any
|
|
|
|
$el.find( '.mwe-popups-extract a[href]:not([target])' ).each( ( i, a ) => {
|
|
|
|
a.target = '_blank';
|
|
|
|
// Don't let the external site access and possibly manipulate window.opener.location
|
|
|
|
a.rel = `${ a.rel ? `${ a.rel } ` : '' }noopener`;
|
|
|
|
} );
|
|
|
|
|
2019-02-04 12:08:36 +00:00
|
|
|
if ( model.sourceElementId ) {
|
|
|
|
$el.find( '.mwe-popups-read-link' ).on( 'click', ( event ) => {
|
|
|
|
event.stopPropagation();
|
2019-03-25 11:54:55 +00:00
|
|
|
$( `#${ $.escapeSelector( model.sourceElementId ) } > a:first-child` ).trigger( 'click' );
|
2019-02-04 12:08:36 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-02-28 17:50:36 +00:00
|
|
|
$el.find( '.mw-parser-output' ).on( 'scroll', function ( e ) {
|
|
|
|
const element = e.target,
|
|
|
|
scrolledToBottom = element.scrollHeight === element.scrollTop + element.clientHeight;
|
|
|
|
|
|
|
|
if ( !scrolledToBottom && element.isScrolling ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-26 11:35:27 +00:00
|
|
|
const $extract = $( element ).parent();
|
|
|
|
|
|
|
|
if ( typeof element.hasHorizontalScroll === 'undefined' ) {
|
|
|
|
element.hasHorizontalScroll = element.scrollWidth !==
|
|
|
|
element.scrollLeft + element.clientWidth;
|
|
|
|
|
|
|
|
if ( element.hasHorizontalScroll ) {
|
|
|
|
// set bottom offset to scrollbar size
|
|
|
|
$extract.find( '.mwe-popups-fade' ).css(
|
|
|
|
'bottom',
|
|
|
|
`${ element.offsetHeight - element.clientHeight }px`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 17:50:36 +00:00
|
|
|
element.isScrolling = !scrolledToBottom;
|
2019-04-26 11:35:27 +00:00
|
|
|
$extract.toggleClass( 'mwe-popups-fade-out', element.isScrolling );
|
2019-02-28 17:50:36 +00:00
|
|
|
} );
|
|
|
|
|
2019-01-26 09:56:08 +00:00
|
|
|
return $el;
|
2018-12-12 18:37:19 +00:00
|
|
|
}
|