mediawiki-extensions-Relate.../resources/ext.relatedArticles.readMore/index.js
Jon Robson dc2e65a084 Drop outdated references to Cards extension
Previously RelatedArticles required the Cards extension
This code refers to that old state and is cleaned up.

ext.relatedArticles.cards is added a dependency of
ext.relatedArticles.readMore since it now belongs to the same
extension

Change-Id: I4a89ed4256a4ae9fd22b0191748bd47ac3ffc593
2022-03-31 08:14:23 -07:00

53 lines
1.3 KiB
JavaScript

( function () {
/**
* Renders the related articles.
*/
function main() {
var CardModel = mw.cards.CardModel,
CardView = mw.cards.CardView,
CardListView = mw.cards.CardListView;
/**
* 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 ) );
} );
}
mw.trackSubscribe( 'ext.relatedArticles.init', function ( _, pages ) {
var $readMore,
cards;
cards = new CardListView( getCards( pages ) );
$readMore = $( '<aside>' ).addClass( 'ra-read-more noprint' )
.append( $( '<h2>' ).text( mw.msg( 'relatedarticles-read-more-heading' ) ) )
.append( cards.$el );
// eslint-disable-next-line no-jquery/no-global-selector
$( '.read-more-container' ).append( $readMore );
} );
}
main();
}() );