2015-12-07 18:19:16 +00:00
|
|
|
( function ( $, mw ) {
|
2017-06-06 23:38:57 +00:00
|
|
|
// Make sure 'ext.relatedArticles.cards' is loaded. It may not be because of the race
|
2015-11-16 10:40:03 +00:00
|
|
|
// condition in the bootstrap file.
|
2017-06-06 23:38:57 +00:00
|
|
|
mw.loader.using( 'ext.relatedArticles.cards' ).done( function () {
|
2015-11-16 10:40:03 +00:00
|
|
|
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 $.map( pages, function ( page ) {
|
|
|
|
var result = {
|
|
|
|
title: page.title,
|
|
|
|
url: mw.util.getUrl( page.title ),
|
|
|
|
hasThumbnail: false
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( page.thumbnail ) {
|
|
|
|
result.hasThumbnail = true;
|
|
|
|
result.thumbnailUrl = page.thumbnail.source;
|
|
|
|
result.isThumbnailPortrait = page.thumbnail.height >= page.thumbnail.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
page.terms &&
|
|
|
|
page.terms.description &&
|
|
|
|
page.terms.description.length
|
|
|
|
) {
|
|
|
|
result.extract = page.terms.description[ 0 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return new CardView( new CardModel( result ) );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
mw.trackSubscribe( 'ext.relatedArticles.init', function ( _, pages ) {
|
|
|
|
var $readMore,
|
|
|
|
cards;
|
|
|
|
|
|
|
|
cards = new CardListView( getCards( pages ) );
|
|
|
|
|
2016-08-18 22:20:48 +00:00
|
|
|
$readMore = $( '<aside class="ra-read-more noprint"></aside>' )
|
2015-11-16 10:40:03 +00:00
|
|
|
.append( $( '<h2></h2>' ).text( mw.msg( 'relatedarticles-read-more-heading' ) ) )
|
|
|
|
.append( cards.$el );
|
|
|
|
|
2016-08-30 18:01:48 +00:00
|
|
|
$( '.read-more-container' ).append( $readMore );
|
2015-11-16 10:40:03 +00:00
|
|
|
|
|
|
|
// the ReadMore code is ready
|
|
|
|
mw.track( 'ext.relatedArticles.logReady', { $readMore: $readMore } );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2015-12-07 18:19:16 +00:00
|
|
|
}( jQuery, mediaWiki ) );
|