2011-11-03 21:48:40 +00:00
|
|
|
/**
|
2012-02-06 23:50:56 +00:00
|
|
|
* Creates an ve.dm.BranchNode object.
|
2011-11-03 21:48:40 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
* @constructor
|
2012-02-06 23:50:56 +00:00
|
|
|
* @extends {ve.BranchNode}
|
|
|
|
* @extends {ve.dm.Node}
|
2011-11-03 21:48:40 +00:00
|
|
|
* @param {String} type Symbolic name of node type
|
2011-11-04 17:07:44 +00:00
|
|
|
* @param {Object} element Element object in document data
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.dm.BranchNode[]} [contents] List of child nodes to append
|
2011-11-03 21:48:40 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.BranchNode = function( type, element, contents ) {
|
2011-11-03 21:48:40 +00:00
|
|
|
// Inheritance
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.BranchNode.call( this );
|
|
|
|
ve.dm.Node.call( this, type, element, 0 );
|
2011-11-03 21:48:40 +00:00
|
|
|
|
|
|
|
// Child nodes
|
2012-02-06 23:50:56 +00:00
|
|
|
if ( ve.isArray( contents ) ) {
|
2011-11-03 21:48:40 +00:00
|
|
|
for ( var i = 0; i < contents.length; i++ ) {
|
|
|
|
this.push( contents[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a plain object representation of the document's data.
|
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @see {ve.dm.Node.getPlainObject}
|
|
|
|
* @see {ve.dm.DocumentNode.newFromPlainObject}
|
2011-11-03 21:48:40 +00:00
|
|
|
* @returns {Object} Plain object representation
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.BranchNode.prototype.getPlainObject = function() {
|
2011-11-03 21:48:40 +00:00
|
|
|
var obj = { 'type': this.type };
|
|
|
|
if ( this.element && this.element.attributes ) {
|
2012-02-06 23:50:56 +00:00
|
|
|
obj.attributes = ve.copyObject( this.element.attributes );
|
2011-11-03 21:48:40 +00:00
|
|
|
}
|
|
|
|
obj.children = [];
|
|
|
|
for ( var i = 0; i < this.children.length; i++ ) {
|
|
|
|
obj.children.push( this.children[i].getPlainObject() );
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a node to the end of this node's children.
|
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.dm.BranchNode} childModel Item to add
|
2011-11-03 21:48:40 +00:00
|
|
|
* @returns {Integer} New number of children
|
|
|
|
* @emits beforePush (childModel)
|
|
|
|
* @emits afterPush (childModel)
|
|
|
|
* @emits update
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.BranchNode.prototype.push = function( childModel ) {
|
2011-11-03 21:48:40 +00:00
|
|
|
this.emit( 'beforePush', childModel );
|
|
|
|
childModel.attach( this );
|
|
|
|
childModel.on( 'update', this.emitUpdate );
|
|
|
|
this.children.push( childModel );
|
|
|
|
this.adjustContentLength( childModel.getElementLength(), true );
|
|
|
|
this.emit( 'afterPush', childModel );
|
|
|
|
this.emit( 'update' );
|
|
|
|
return this.children.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a node to the beginning of this node's children.
|
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.dm.BranchNode} childModel Item to add
|
2011-11-03 21:48:40 +00:00
|
|
|
* @returns {Integer} New number of children
|
|
|
|
* @emits beforeUnshift (childModel)
|
|
|
|
* @emits afterUnshift (childModel)
|
|
|
|
* @emits update
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.BranchNode.prototype.unshift = function( childModel ) {
|
2011-11-03 21:48:40 +00:00
|
|
|
this.emit( 'beforeUnshift', childModel );
|
|
|
|
childModel.attach( this );
|
|
|
|
childModel.on( 'update', this.emitUpdate );
|
|
|
|
this.children.unshift( childModel );
|
|
|
|
this.adjustContentLength( childModel.getElementLength(), true );
|
|
|
|
this.emit( 'afterUnshift', childModel );
|
|
|
|
this.emit( 'update' );
|
|
|
|
return this.children.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a node from the end of this node's children
|
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @returns {ve.dm.BranchNode} Removed childModel
|
2011-11-03 21:48:40 +00:00
|
|
|
* @emits beforePop
|
|
|
|
* @emits afterPop
|
|
|
|
* @emits update
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.BranchNode.prototype.pop = function() {
|
2011-11-03 21:48:40 +00:00
|
|
|
if ( this.children.length ) {
|
|
|
|
this.emit( 'beforePop' );
|
|
|
|
var childModel = this.children[this.children.length - 1];
|
|
|
|
childModel.detach();
|
|
|
|
childModel.removeListener( 'update', this.emitUpdate );
|
|
|
|
this.children.pop();
|
|
|
|
this.adjustContentLength( -childModel.getElementLength(), true );
|
|
|
|
this.emit( 'afterPop' );
|
|
|
|
this.emit( 'update' );
|
|
|
|
return childModel;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a node from the beginning of this node's children
|
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @returns {ve.dm.BranchNode} Removed childModel
|
2011-11-03 21:48:40 +00:00
|
|
|
* @emits beforeShift
|
|
|
|
* @emits afterShift
|
|
|
|
* @emits update
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.BranchNode.prototype.shift = function() {
|
2011-11-03 21:48:40 +00:00
|
|
|
if ( this.children.length ) {
|
|
|
|
this.emit( 'beforeShift' );
|
|
|
|
var childModel = this.children[0];
|
|
|
|
childModel.detach();
|
|
|
|
childModel.removeListener( 'update', this.emitUpdate );
|
|
|
|
this.children.shift();
|
|
|
|
this.adjustContentLength( -childModel.getElementLength(), true );
|
|
|
|
this.emit( 'afterShift' );
|
|
|
|
this.emit( 'update' );
|
|
|
|
return childModel;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds and removes nodes from this node's children.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Integer} index Index to remove and or insert nodes at
|
|
|
|
* @param {Integer} howmany Number of nodes to remove
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.dm.BranchNode} [...] Variadic list of nodes to insert
|
|
|
|
* @returns {ve.dm.BranchNode[]} Removed nodes
|
2011-11-03 21:48:40 +00:00
|
|
|
* @emits beforeSplice (index, howmany, [...])
|
|
|
|
* @emits afterSplice (index, howmany, [...])
|
|
|
|
* @emits update
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.BranchNode.prototype.splice = function( index, howmany ) {
|
2011-11-03 21:48:40 +00:00
|
|
|
var i,
|
|
|
|
length,
|
|
|
|
args = Array.prototype.slice.call( arguments, 0 ),
|
|
|
|
diff = 0;
|
|
|
|
this.emit.apply( this, ['beforeSplice'].concat( args ) );
|
|
|
|
if ( args.length >= 3 ) {
|
|
|
|
for ( i = 2, length = args.length; i < length; i++ ) {
|
|
|
|
args[i].attach( this );
|
2011-11-14 19:47:44 +00:00
|
|
|
args[i].on( 'update', this.emitUpdate );
|
|
|
|
diff += args[i].getElementLength();
|
2011-11-03 21:48:40 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-14 19:47:44 +00:00
|
|
|
var removals = this.children.splice.apply( this.children, args );
|
|
|
|
for ( i = 0, length = removals.length; i < length; i++ ) {
|
|
|
|
removals[i].detach();
|
|
|
|
removals[i].removeListener( 'update', this.emitUpdate );
|
|
|
|
diff -= removals[i].getElementLength();
|
2011-11-03 21:48:40 +00:00
|
|
|
}
|
|
|
|
this.adjustContentLength( diff, true );
|
|
|
|
this.emit.apply( this, ['afterSplice'].concat( args ) );
|
|
|
|
this.emit( 'update' );
|
2011-11-14 19:47:44 +00:00
|
|
|
return removals;
|
2011-11-03 21:48:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sorts this node's children.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Function} sortfunc Function to use when sorting
|
|
|
|
* @emits beforeSort (sortfunc)
|
|
|
|
* @emits afterSort (sortfunc)
|
|
|
|
* @emits update
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.BranchNode.prototype.sort = function( sortfunc ) {
|
2011-11-03 21:48:40 +00:00
|
|
|
this.emit( 'beforeSort', sortfunc );
|
|
|
|
this.children.sort( sortfunc );
|
|
|
|
this.emit( 'afterSort', sortfunc );
|
|
|
|
this.emit( 'update' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverses the order of this node's children.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @emits beforeReverse
|
|
|
|
* @emits afterReverse
|
|
|
|
* @emits update
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.BranchNode.prototype.reverse = function() {
|
2011-11-03 21:48:40 +00:00
|
|
|
this.emit( 'beforeReverse' );
|
|
|
|
this.children.reverse();
|
|
|
|
this.emit( 'afterReverse' );
|
|
|
|
this.emit( 'update' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the root node to this and all of it's children.
|
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @see {ve.dm.Node.prototype.setRoot}
|
|
|
|
* @param {ve.dm.Node} root Node to use as root
|
2011-11-03 21:48:40 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.BranchNode.prototype.setRoot = function( root ) {
|
2011-11-03 21:48:40 +00:00
|
|
|
this.root = root;
|
|
|
|
for ( var i = 0; i < this.children.length; i++ ) {
|
|
|
|
this.children[i].setRoot( root );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the root node from this and all of it's children.
|
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @see {ve.dm.Node.prototype.clearRoot}
|
2011-11-03 21:48:40 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.BranchNode.prototype.clearRoot = function() {
|
2011-11-03 21:48:40 +00:00
|
|
|
this.root = null;
|
|
|
|
for ( var i = 0; i < this.children.length; i++ ) {
|
|
|
|
this.children[i].clearRoot();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.extendClass( ve.dm.BranchNode, ve.BranchNode );
|
|
|
|
ve.extendClass( ve.dm.BranchNode, ve.dm.Node );
|