mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/RelatedArticles
synced 2024-12-22 04:44:26 +00:00
dfce5b1cb7
Instead export a render method Bug: T149317 Change-Id: I2b88690d381f896d6a646046574528a2312c91c2
49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
var CardModel = require( './CardModel.js' ),
|
|
CardView = require( './CardView.js' ),
|
|
CardListView = require( './CardListView.js' );
|
|
/**
|
|
* Generates `mw.cards.CardView`s from pages
|
|
*
|
|
* @param {Object[]} pages
|
|
* @return {mw.cards.CardView[]}
|
|
*/
|
|
function getCards( pages ) {
|
|
return pages.map( function ( page ) {
|
|
var result = {
|
|
title: page.title,
|
|
url: mw.util.getUrl( page.title ),
|
|
hasThumbnail: false,
|
|
extract: ( page.description || page.extract ||
|
|
( page.pageprops ? page.pageprops.description : '' ) )
|
|
};
|
|
|
|
if ( page.thumbnail ) {
|
|
result.hasThumbnail = true;
|
|
result.thumbnailUrl = page.thumbnail.source;
|
|
result.isThumbnailPortrait = page.thumbnail.height >= page.thumbnail.width;
|
|
}
|
|
|
|
return new CardView( new CardModel( result ) );
|
|
} );
|
|
}
|
|
|
|
/**
|
|
* Renders the related articles.
|
|
*
|
|
* @param {Object[]} pages
|
|
* @param {Element} el
|
|
*/
|
|
function render( pages, el ) {
|
|
var cards = new CardListView( getCards( pages ) ),
|
|
$readMore = $( '<aside>' ).addClass( 'ra-read-more noprint' )
|
|
.append( $( '<h2>' ).text( mw.msg( 'relatedarticles-read-more-heading' ) ) )
|
|
.append( cards.$el );
|
|
|
|
$( el ).append( $readMore );
|
|
}
|
|
|
|
module.exports = {
|
|
render: render,
|
|
getCards: getCards
|
|
};
|