2023-06-22 09:41:37 +00:00
|
|
|
const Revision = require( './ext.RevisionSlider.Revision.js' ).Revision,
|
2020-03-29 21:15:32 +00:00
|
|
|
RevisionListView = require( './ext.RevisionSlider.RevisionListView.js' );
|
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
/**
|
|
|
|
* @class RevisionList
|
|
|
|
* @param {Revision[]} revs
|
|
|
|
* @param {Object[]} availableTags
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function RevisionList( revs, availableTags ) {
|
|
|
|
this.revisions = [];
|
|
|
|
this.availableTags = availableTags;
|
|
|
|
this.initialize( revs );
|
2020-03-29 21:15:32 +00:00
|
|
|
this.view = new RevisionListView( this );
|
2020-11-18 09:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$.extend( RevisionList.prototype, {
|
|
|
|
/**
|
|
|
|
* @type {Revision[]}
|
|
|
|
*/
|
|
|
|
revisions: [],
|
|
|
|
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
2020-11-18 09:11:12 +00:00
|
|
|
* @type {RevisionListView}
|
|
|
|
*/
|
|
|
|
view: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the RevisionList from a list of Revisions
|
|
|
|
*
|
2016-06-17 13:06:12 +00:00
|
|
|
* @param {Revision[]} revs
|
|
|
|
*/
|
2020-11-18 09:11:12 +00:00
|
|
|
initialize: function ( revs ) {
|
2023-08-25 15:47:06 +00:00
|
|
|
this.push( revs );
|
2020-11-18 09:11:12 +00:00
|
|
|
},
|
2016-05-10 12:42:05 +00:00
|
|
|
|
2020-01-24 16:21:23 +00:00
|
|
|
/**
|
2020-11-18 09:11:12 +00:00
|
|
|
* @return {number}
|
2020-01-24 16:21:23 +00:00
|
|
|
*/
|
2020-11-18 09:11:12 +00:00
|
|
|
getBiggestChangeSize: function () {
|
2023-06-22 09:41:37 +00:00
|
|
|
let max = 0;
|
2016-05-10 12:42:05 +00:00
|
|
|
|
2023-06-22 09:41:37 +00:00
|
|
|
for ( let i = 0; i < this.revisions.length; i++ ) {
|
2020-11-18 09:11:12 +00:00
|
|
|
max = Math.max( max, Math.abs( this.revisions[ i ].getRelativeSize() ) );
|
|
|
|
}
|
2016-05-10 12:42:05 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
return max;
|
|
|
|
},
|
2016-05-10 12:42:05 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
/**
|
|
|
|
* @return {Revision[]}
|
|
|
|
*/
|
|
|
|
getRevisions: function () {
|
|
|
|
return this.revisions;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {number}
|
|
|
|
*/
|
|
|
|
getLength: function () {
|
|
|
|
return this.revisions.length;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {RevisionListView}
|
|
|
|
*/
|
|
|
|
getView: function () {
|
|
|
|
return this.view;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {Object[]}
|
|
|
|
*/
|
|
|
|
getAvailableTags: function () {
|
|
|
|
return this.availableTags;
|
|
|
|
},
|
|
|
|
|
|
|
|
getUserGenders: function () {
|
2023-06-22 09:41:37 +00:00
|
|
|
const userGenders = {};
|
2020-11-18 09:11:12 +00:00
|
|
|
this.revisions.forEach( function ( revision ) {
|
|
|
|
if ( revision.getUser() ) {
|
|
|
|
userGenders[ revision.getUser() ] = revision.getUserGender();
|
2016-06-27 14:00:13 +00:00
|
|
|
}
|
2020-11-18 09:11:12 +00:00
|
|
|
} );
|
|
|
|
return userGenders;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds revisions to the end of the list.
|
|
|
|
*
|
|
|
|
* @param {Revision[]} revs
|
|
|
|
*/
|
|
|
|
push: function ( revs ) {
|
2023-08-25 15:47:06 +00:00
|
|
|
const last = this.revisions[ this.revisions.length - 1 ];
|
|
|
|
let sizeBefore = last ? last.getSize() : 0;
|
2023-06-22 09:41:37 +00:00
|
|
|
for ( let i = 0; i < revs.length; i++ ) {
|
|
|
|
const rev = revs[ i ];
|
2023-08-25 15:47:06 +00:00
|
|
|
rev.setRelativeSize( rev.getSize() - sizeBefore );
|
2020-11-18 09:11:12 +00:00
|
|
|
this.revisions.push( rev );
|
2023-08-25 15:47:06 +00:00
|
|
|
sizeBefore = rev.getSize();
|
2016-05-10 12:42:05 +00:00
|
|
|
}
|
2020-11-18 09:11:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ) {
|
2023-06-22 09:41:37 +00:00
|
|
|
const originalFirstRev = this.revisions[ 0 ];
|
2020-11-18 09:11:12 +00:00
|
|
|
sizeBefore = sizeBefore || 0;
|
|
|
|
|
|
|
|
originalFirstRev.setRelativeSize( originalFirstRev.getSize() - revs[ revs.length - 1 ].getSize() );
|
2023-06-22 09:41:37 +00:00
|
|
|
for ( let i = revs.length - 1; i >= 0; i-- ) {
|
|
|
|
const rev = revs[ i ];
|
2020-11-18 09:11:12 +00:00
|
|
|
rev.setRelativeSize( i > 0 ? rev.getSize() - revs[ i - 1 ].getSize() : rev.getSize() - sizeBefore );
|
2016-05-10 12:42:05 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
this.revisions.unshift( rev );
|
|
|
|
}
|
|
|
|
},
|
2016-07-04 20:45:34 +00:00
|
|
|
|
|
|
|
/**
|
2020-11-18 09:11:12 +00:00
|
|
|
* Returns a subset of the list.
|
2016-07-04 20:45:34 +00:00
|
|
|
*
|
2020-11-18 09:11:12 +00:00
|
|
|
* @param {number} begin
|
|
|
|
* @param {number} end
|
|
|
|
* @return {RevisionList}
|
2016-07-04 20:45:34 +00:00
|
|
|
*/
|
2020-11-18 09:11:12 +00:00
|
|
|
slice: function ( begin, end ) {
|
2023-06-22 09:41:37 +00:00
|
|
|
const slicedList = new RevisionList( [], this.getAvailableTags() );
|
2020-03-29 21:15:32 +00:00
|
|
|
slicedList.view = new RevisionListView( slicedList );
|
2020-11-18 09:11:12 +00:00
|
|
|
slicedList.revisions = this.revisions.slice( begin, end );
|
|
|
|
return slicedList;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} pos
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
isValidPosition: function ( pos ) {
|
|
|
|
return pos > 0 && pos <= this.getLength();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms an array of revision data returned by MediaWiki API (including user gender information) into
|
|
|
|
* an array of Revision objects
|
|
|
|
*
|
2023-10-24 14:08:50 +00:00
|
|
|
* @param {Object[]} revs
|
2020-11-18 09:11:12 +00:00
|
|
|
* @return {Revision[]}
|
|
|
|
*/
|
2020-03-29 21:15:32 +00:00
|
|
|
function makeRevisions( revs ) {
|
2023-10-24 14:08:50 +00:00
|
|
|
return revs.map( ( data ) => new Revision( data ) );
|
2020-03-29 21:15:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
RevisionList: RevisionList,
|
2020-11-20 10:31:12 +00:00
|
|
|
makeRevisions: makeRevisions
|
2020-11-18 09:11:12 +00:00
|
|
|
};
|