mediawiki-extensions-Relate.../resources/ext.relatedArticles.cards/CardView.js
Jan Drewniak fcd4d1a272 Hygiene: Followup from 191fc2a
pass a collection of jQuery objects to `.append` instead of
and array.

Use `.css` instead of `.attr` to set thumbnail background image.

Change a unit to reflect the escaping pattern of the `.css` method

Bug: T219846
Change-Id: I6359be2db6c7c7a8e4b52296e0801d4ded921b46
2019-05-01 22:28:47 +02:00

84 lines
1.8 KiB
JavaScript

/* eslint-disable no-underscore-dangle */
( function () {
'use strict';
/**
* Renders a Card model and updates when it does.
*
* @class mw.cards.CardView
* @param {mw.cards.CardModel} model
*/
function CardView( model ) {
/**
* @property {mw.cards.CardModel}
*/
this.model = model;
// listen to model changes and re-render the view
this.model.on( 'change', this.render.bind( this ) );
/**
* @property {jQuery}
*/
this.$el = $( this._render() );
}
OO.initClass( CardView );
/**
* Replace the html of this.$el with a newly rendered html using the model
* attributes.
*/
CardView.prototype.render = function () {
this.$el.replaceWith( this._render() );
};
/**
* Renders the template using the model attributes.
*
* @ignore
* @return {string}
*/
CardView.prototype._render = function () {
var $listItem = $( '<li>' ),
attributes = $.extend( {}, this.model.attributes );
attributes.thumbnailUrl = CSS.escape( attributes.thumbnailUrl );
$listItem.attr( {
title: attributes.title,
class: 'ext-related-articles-card'
} );
$listItem.append(
$( '<div>' )
.addClass( 'ext-related-articles-card-thumb' )
.css( 'background-image', attributes.hasThumbnail ?
'url(' + attributes.thumbnailUrl + ')' :
null
),
$( '<a>' )
.attr( {
href: attributes.url,
'aria-hidden': 'true',
tabindex: -1
} ),
$( '<div>' )
.attr( { class: 'ext-related-articles-card-detail' } )
.append(
$( '<h3>' ).append(
$( '<a>' )
.attr( { href: attributes.url } )
.text( attributes.title )
),
$( '<p>' )
.attr( { class: 'ext-related-articles-card-extract' } )
.text( attributes.extract )
)
);
return $listItem;
};
mw.cards.CardView = CardView;
}() );