mediawiki-extensions-Revisi.../modules/ext.RevisionSlider.RevisionList.js
Leszek Manicki 8e2365502b Make RevisionList expect list of Revision object
Despite its documentation the "constructor" of RevisionList tends
to expect array of revision data in a format returned by API instead
of array of Revision objects.
Due to different name of ID fields in Revision object and in API array,
if "real" Revision object is passed to RevisionList, its ID is lost.

This changes RevisionList so that it only accepts an array of Revision
objects. This provides better abstraction.

This is basically a revert of I147270f28381038d05f8bcfd2317e8c269b2e458
which aimed at the same problem but suggested solution doesn't seem right.

Change-Id: Ic45cdd3e7b707a8c6a19eecf0a84d4c11696cd1f
2016-07-05 00:01:46 +02:00

91 lines
1.7 KiB
JavaScript

( function ( mw, $ ) {
/**
* @param {Revision[]} revs
* @constructor
*/
var RevisionList = function ( revs ) {
this.revisions = [];
this.initialize( revs );
this.view = new mw.libs.revisionSlider.RevisionListView( this );
};
$.extend( RevisionList.prototype, {
/**
* @type {Revision[]}
*/
revisions: [],
/**
* @type {RevisionListView}
*/
view: null,
/**
* Inititializes the RevisionList from a list of Revisions
*
* @param {Revision[]} revs
*/
initialize: function ( revs ) {
var i, rev;
for ( i = 0; i < revs.length; i++ ) {
rev = revs[ i ];
rev.setRelativeSize( i > 0 ? rev.getSize() - revs[ i - 1 ].getSize() : rev.getSize() );
this.revisions.push( rev );
}
},
/**
* @return {number}
*/
getBiggestChangeSize: function () {
var max = 0,
i;
for ( i = 0; i < this.revisions.length; i++ ) {
max = Math.max( max, Math.abs( this.revisions[ i ].getRelativeSize() ) );
}
return max;
},
/**
* @return {Revision[]}
*/
getRevisions: function () {
return this.revisions;
},
/**
* @return {number}
*/
getLength: function () {
return this.revisions.length;
},
/**
* @return {RevisionListView}
*/
getView: function () {
return this.view;
}
} );
mw.libs.revisionSlider = mw.libs.revisionSlider || {};
mw.libs.revisionSlider.RevisionList = RevisionList;
/**
* Transforms an array of revision data returned by MediaWiki API into
* an array of Revision objects
*
* @param {Array} revs
* @return {Revision[]}
*/
mw.libs.revisionSlider.makeRevisions = function ( revs ) {
return revs.map( function ( revData ) {
return new mw.libs.revisionSlider.Revision( revData );
} );
};
}( mediaWiki, jQuery ) );