mediawiki-extensions-Revisi.../modules/ext.RevisionSlider.RevisionList.js
Leszek Manicki dc838bc87d Fetch more revisions as the user moves back and forward
This changes the previous behaviour of fetching always up to
500 most recent revisions.

Now the extensions fetches N revisions including the newer
revision selected to diff as the most recent revision.
N is number of revisions that would fit in the current
window when rendered as bars.
When user is close to either "end" of the slider, extensions
fetches another batch of up to N older or newer revisions,
as long as user does not reach the oldest or the newest
revision of the page.

Among others, this removes the limitations of the previous
approach: showing only 500 revisions, and failing to show
anything when any of selected revisions was older than
500 recent revisions.

This change also simplifies usage of Api class.

Bug: T135005
Change-Id: Ib3f4a6ac57ff17008f9d8784c4716bd294443096
2016-07-28 12:58:35 +02:00

154 lines
3.4 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;
},
getUserGenders: function () {
var userGenders = {};
this.revisions.forEach( function ( revision ) {
if ( revision.getUser() ) {
userGenders[ revision.getUser() ] = revision.getUserGender();
}
} );
return userGenders;
},
/**
* Adds revisions to the end of the list.
*
* @param {Revision[]} revs
*/
push: 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[ this.revisions.length - 1 ].getSize()
);
this.revisions.push( rev );
}
},
/**
* Adds revisions to the beginning of the list.
*
* @param {Revision[]} revs
* @param {number} sizeBefore optional size of the revision preceding the first of revs, defaults to 0
*/
unshift: function ( revs, sizeBefore ) {
var originalFirstRev = this.revisions[ 0 ],
i, rev;
sizeBefore = sizeBefore || 0;
originalFirstRev.setRelativeSize( originalFirstRev.getSize() - revs[ revs.length - 1 ].getSize() );
for ( i = revs.length - 1; i >= 0; i-- ) {
rev = revs[ i ];
rev.setRelativeSize( i > 0 ? rev.getSize() - revs[ i - 1 ].getSize() : rev.getSize() - sizeBefore );
this.revisions.unshift( rev );
}
},
/**
* Returns a subset of the list.
*
* @param {number} begin
* @param {number} end
* @return {RevisionList}
*/
slice: function ( begin, end ) {
var slicedList = new mw.libs.revisionSlider.RevisionList( [] );
slicedList.view = new mw.libs.revisionSlider.RevisionListView( slicedList );
slicedList.revisions = this.revisions.slice( begin, end );
return slicedList;
}
} );
mw.libs.revisionSlider = mw.libs.revisionSlider || {};
mw.libs.revisionSlider.RevisionList = RevisionList;
/**
* Transforms an array of revision data returned by MediaWiki API (including user gender information) 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 ) );