2012-05-03 20:21:13 +00:00
|
|
|
/**
|
|
|
|
* DataModel surface.
|
2012-05-03 23:02:21 +00:00
|
|
|
*
|
2012-05-03 20:21:13 +00:00
|
|
|
* @class
|
|
|
|
* @constructor
|
2012-05-03 23:02:21 +00:00
|
|
|
* @extends {ve.EventEmitter}
|
2012-05-03 20:21:13 +00:00
|
|
|
* @param {ve.dm.Document} doc Document model to create surface for
|
|
|
|
*/
|
|
|
|
ve.dm.Surface = function( doc ) {
|
2012-05-03 23:02:21 +00:00
|
|
|
// Inheritance
|
|
|
|
ve.EventEmitter.call( this );
|
2012-05-03 20:21:13 +00:00
|
|
|
// Properties
|
|
|
|
this.documentModel = doc;
|
2012-05-03 23:02:21 +00:00
|
|
|
this.selection = null;
|
2012-06-06 18:27:23 +00:00
|
|
|
this.bigStack = []; // NOTE: HistoryButtonTool depends on bigStack
|
2012-05-03 20:21:13 +00:00
|
|
|
};
|
2012-05-03 23:02:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the document model of the surface.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {ve.dm.DocumentNode} Document model of the surface
|
|
|
|
*/
|
|
|
|
ve.dm.Surface.prototype.getDocument = function() {
|
2012-05-04 22:20:14 +00:00
|
|
|
return this.documentModel;
|
2012-05-03 23:02:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the selection
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {ve.Range} Current selection
|
|
|
|
*/
|
|
|
|
ve.dm.Surface.prototype.getSelection = function() {
|
|
|
|
return this.selection;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the selection
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.Range} selection
|
|
|
|
*/
|
2012-05-04 22:29:47 +00:00
|
|
|
ve.dm.Surface.prototype.setSelection = function( selection ) {
|
2012-05-03 23:02:21 +00:00
|
|
|
selection.normalize();
|
2012-05-04 22:29:47 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
( !this.selection ) ||
|
|
|
|
( !this.selection.equals ( selection ) )
|
|
|
|
)
|
|
|
|
{
|
2012-05-03 23:02:21 +00:00
|
|
|
this.selection = selection;
|
2012-05-04 22:29:47 +00:00
|
|
|
}
|
2012-06-07 00:05:32 +00:00
|
|
|
this.emit ('select', this.selection.clone() );
|
2012-05-03 23:02:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies a series of transactions to the content data.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.Transaction} transactions Tranasction to apply to the document
|
|
|
|
*/
|
|
|
|
ve.dm.Surface.prototype.transact = function( transaction ) {
|
2012-06-04 21:29:27 +00:00
|
|
|
ve.dm.TransactionProcessor.commit( this.getDocument(), transaction );
|
2012-05-03 23:02:21 +00:00
|
|
|
this.emit( 'transact', transaction );
|
|
|
|
};
|
|
|
|
|
2012-06-04 22:06:37 +00:00
|
|
|
/**
|
|
|
|
* Applies an annotation to the current selection
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {String} annotation action: toggle, clear, set
|
|
|
|
* @param {Object} annotation object to apply.
|
|
|
|
*/
|
2012-06-04 21:29:27 +00:00
|
|
|
ve.dm.Surface.prototype.annotate = function( method, annotation ) {
|
|
|
|
var selection = this.getSelection();
|
|
|
|
|
|
|
|
if ( method === 'toggle' ) {
|
|
|
|
var annotations = this.getDocument().getAnnotationsFromRange( selection );
|
|
|
|
if ( annotation in annotations ) {
|
|
|
|
method = 'clear';
|
|
|
|
} else {
|
|
|
|
method = 'set';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( this.selection.getLength() ) {
|
|
|
|
var tx = ve.dm.Transaction.newFromAnnotation(
|
|
|
|
this.getDocument(), selection, method, annotation
|
|
|
|
);
|
|
|
|
this.transact( tx );
|
2012-06-07 00:05:32 +00:00
|
|
|
// emit selection for dev purposes
|
|
|
|
this.setSelection ( selection );
|
2012-06-04 21:29:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-03 23:02:21 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.extendClass( ve.dm.Surface, ve.EventEmitter );
|