2011-11-02 21:00:55 +00:00
|
|
|
/**
|
2012-02-06 23:50:56 +00:00
|
|
|
* Creates an ve.dm.Transaction object.
|
2011-11-02 21:00:55 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {Object[]} operations List of operations
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Transaction = function( operations ) {
|
|
|
|
this.operations = ve.isArray( operations ) ? operations : [];
|
2011-11-22 22:57:23 +00:00
|
|
|
this.lengthDifference = 0;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2011-11-04 21:06:06 +00:00
|
|
|
/**
|
|
|
|
* Gets a list of all operations.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Object[]} List of operations
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Transaction.prototype.getOperations = function() {
|
2011-11-17 14:43:11 +00:00
|
|
|
return this.operations;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
2011-11-17 22:42:18 +00:00
|
|
|
/**
|
|
|
|
* Gets the difference in content length this transaction will cause if applied.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Integer} Difference in content length
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Transaction.prototype.getLengthDifference = function() {
|
2011-11-22 22:57:23 +00:00
|
|
|
return this.lengthDifference;
|
2011-11-17 22:42:18 +00:00
|
|
|
};
|
|
|
|
|
2011-11-04 21:06:06 +00:00
|
|
|
/**
|
|
|
|
* Adds a retain operation.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Integer} length Length of content data to retain
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Transaction.prototype.pushRetain = function( length ) {
|
2011-11-23 23:54:36 +00:00
|
|
|
var end = this.operations.length - 1;
|
|
|
|
if ( this.operations.length && this.operations[end].type === 'retain' ) {
|
|
|
|
this.operations[end].length += length;
|
|
|
|
} else {
|
|
|
|
this.operations.push( {
|
|
|
|
'type': 'retain',
|
|
|
|
'length': length
|
|
|
|
} );
|
|
|
|
}
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
2011-11-04 21:06:06 +00:00
|
|
|
/**
|
|
|
|
* Adds an insertion operation.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Array} data Data to retain
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Transaction.prototype.pushInsert = function( data ) {
|
2011-11-23 23:54:36 +00:00
|
|
|
var end = this.operations.length - 1;
|
|
|
|
if ( this.operations.length && this.operations[end].type === 'insert' ) {
|
|
|
|
this.operations[end].data = this.operations[end].data.concat( data );
|
|
|
|
} else {
|
|
|
|
this.operations.push( {
|
|
|
|
'type': 'insert',
|
|
|
|
'data': data
|
|
|
|
} );
|
|
|
|
}
|
2011-11-22 22:57:23 +00:00
|
|
|
this.lengthDifference += data.length;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
2011-11-04 21:06:06 +00:00
|
|
|
/**
|
|
|
|
* Adds a removal operation.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Array} data Data to remove
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Transaction.prototype.pushRemove = function( data ) {
|
2011-11-23 23:54:36 +00:00
|
|
|
var end = this.operations.length - 1;
|
|
|
|
if ( this.operations.length && this.operations[end].type === 'remove' ) {
|
|
|
|
this.operations[end].data = this.operations[end].data.concat( data );
|
|
|
|
} else {
|
|
|
|
this.operations.push( {
|
|
|
|
'type': 'remove',
|
|
|
|
'data': data
|
|
|
|
} );
|
|
|
|
}
|
2011-11-22 22:57:23 +00:00
|
|
|
this.lengthDifference -= data.length;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
2012-03-08 23:21:17 +00:00
|
|
|
/**
|
|
|
|
* Adds a replace operation
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Array} remove Data to remove
|
|
|
|
* @param {Array] replacement Data to replace 'remove' with
|
|
|
|
*/
|
|
|
|
ve.dm.Transaction.prototype.pushReplace = function( remove, replacement ) {
|
|
|
|
this.operations.push( {
|
|
|
|
'type': 'replace',
|
|
|
|
'remove': remove,
|
|
|
|
'replacement': replacement
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2011-11-04 21:06:06 +00:00
|
|
|
/**
|
|
|
|
* Adds an element attribute change operation.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {String} method Method to use, either "set" or "clear"
|
|
|
|
* @param {String} key Name of attribute to change
|
2012-02-22 21:23:28 +00:00
|
|
|
* @param {Mixed} from Value change attribute from
|
|
|
|
* @param {Mixed} to Value to change attribute to
|
2011-11-04 21:06:06 +00:00
|
|
|
*/
|
2012-02-22 21:23:28 +00:00
|
|
|
ve.dm.Transaction.prototype.pushReplaceElementAttribute = function( key, from, to ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
this.operations.push( {
|
|
|
|
'type': 'attribute',
|
|
|
|
'key': key,
|
2012-02-22 21:23:28 +00:00
|
|
|
'from': from,
|
|
|
|
'to': to
|
2011-11-02 21:00:55 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2011-11-04 21:06:06 +00:00
|
|
|
/**
|
|
|
|
* Adds a start annotating operation.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {String} method Method to use, either "set" or "clear"
|
|
|
|
* @param {Object} annotation Annotation object to start setting or clearing from content data
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Transaction.prototype.pushStartAnnotating = function( method, annotation ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
this.operations.push( {
|
|
|
|
'type': 'annotate',
|
|
|
|
'method': method,
|
|
|
|
'bias': 'start',
|
|
|
|
'annotation': annotation
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2011-11-04 21:06:06 +00:00
|
|
|
/**
|
|
|
|
* Adds a stop annotating operation.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {String} method Method to use, either "set" or "clear"
|
|
|
|
* @param {Object} annotation Annotation object to stop setting or clearing from content data
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Transaction.prototype.pushStopAnnotating = function( method, annotation ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
this.operations.push( {
|
|
|
|
'type': 'annotate',
|
|
|
|
'method': method,
|
|
|
|
'bias': 'stop',
|
|
|
|
'annotation': annotation
|
|
|
|
} );
|
|
|
|
};
|