all files / rebaser/src/dm/ ve.dm.RebaseDocState.js

50% Statements 8/16
0% Branches 0/2
33.33% Functions 2/6
50% Lines 8/16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76                                                                                                                                       
/*!
 * VisualEditor DataModel rebase document state class.
 *
 * @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * DataModel rebase document state
 *
 * @class
 *
 * @constructor
 */
ve.dm.RebaseDocState = function VeDmRebaseDocState() {
	/**
	 * @property {ve.dm.Change} history History as one big change
	 */
	this.history = new ve.dm.Change();
 
	/**
	 * @property {Map.<number, Object>} authors Information about each author
	 */
	this.authors = new Map();
};
 
/* Inheritance */
 
OO.initClass( ve.dm.RebaseDocState );
 
/* Static Methods */
 
/**
 * Get new empty author data object
 *
 * @return {Object} New empty author data object
 * @return {string} return.name Name
 * @return {string} return.color Color
 * @return {number} return.rejections Number of unacknowledged rejections
 * @return {ve.dm.Change|null} return.continueBase Continue base
 * @return {string} return.token Secret token for usurping sessions
 * @return {boolean} return.active Whether the author is active
 */
ve.dm.RebaseDocState.static.newAuthorData = function () {
	return {
		name: '',
		color: '',
		rejections: 0,
		continueBase: null,
		// TODO use cryptographic randomness here and convert to hex
		token: Math.random().toString(),
		active: true
	};
};
 
/* Methods */
 
ve.dm.RebaseDocState.prototype.getActiveAuthors = function () {
	var result = {};
	this.authors.forEach( function ( authorData, authorId ) {
		if ( authorData.active ) {
			result[ authorId ] = {
				name: authorData.name,
				color: authorData.color
			};
		}
	} );
	return result;
};
 
ve.dm.RebaseDocState.prototype.clearHistory = function () {
	this.history = new ve.dm.Change();
	this.authors.forEach( function ( authorData ) {
		authorData.continueBase = null;
	} );
};