mediawiki-extensions-Revisi.../modules/ext.RevisionSlider.Revision.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

168 lines
2.9 KiB
JavaScript

( function ( mw, $ ) {
/*global moment:false */
/**
* @param {Object} data - Containing keys `id`, `size`, `comment`, `parsedcomment`, `timestamp`, `user` and `minor`
* @constructor
*/
var Revision = function ( data ) {
this.id = data.revid;
this.size = data.size;
this.timestamp = data.timestamp;
this.minor = data.hasOwnProperty( 'minor' ) && ( data.minor || data.minor === '' );
// Comments and users can be suppressed thus we must check if they exist
if ( typeof data.comment !== 'undefined' ) {
this.comment = data.comment;
}
if ( typeof data.parsedcomment !== 'undefined' ) {
this.parsedComment = data.parsedcomment;
}
if ( typeof data.user !== 'undefined' ) {
this.user = data.user;
if ( typeof data.userGender !== 'undefined' ) {
this.userGender = data.userGender;
}
}
};
$.extend( Revision.prototype, {
/**
* @type {number}
*/
id: 0,
/**
* @type {number}
*/
size: 0,
/**
* @type {string}
*/
comment: '',
/**
* @type {boolean}
*/
minor: false,
/**
* @type {string}
*/
parsedComment: '',
/**
* @type {string}
*/
timestamp: '',
/**
* @type {string}
*/
user: '',
/**
* @type {string}
*/
userGender: '',
/**
* @type {number}
*/
relativeSize: 0,
/**
* @return {number}
*/
getId: function () {
return this.id;
},
/**
* @return {number}
*/
getSize: function () {
return this.size;
},
/**
* @return {boolean}
*/
isMinor: function () {
return this.minor;
},
/**
* @return {string}
*/
getParsedComment: function () {
return this.parsedComment;
},
/**
* @return {boolean}
*/
hasEmptyComment: function () {
return this.getComment().trim().length === 0;
},
/**
* @return {string}
*/
getComment: function () {
return this.comment;
},
/**
* Uses moment.js to format the date
*
* @param {string} rawDate
* @return {string}
*/
formatDate: function ( rawDate ) {
// Moment's offset works "backwards", as the number of minutes
// behind UTC, so we need to make this number negative
var offset = -mw.libs.revisionSlider.userOffset;
return moment( rawDate ).zone( offset ).format( 'HH:mm, D MMM YYYY' );
},
/**
* @return {string}
*/
getFormattedDate: function () {
return this.formatDate( this.timestamp );
},
/**
* @return {string}
*/
getUser: function () {
return this.user;
},
/**
* @return {string}
*/
getUserGender: function () {
return this.userGender;
},
/**
* @param {number} size
*/
setRelativeSize: function ( size ) {
this.relativeSize = size;
},
/**
* @return {number}
*/
getRelativeSize: function () {
return this.relativeSize;
}
} );
mw.libs.revisionSlider = mw.libs.revisionSlider || {};
mw.libs.revisionSlider.Revision = Revision;
}( mediaWiki, jQuery ) );