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 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' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-03-12 00:07:33 +00:00
|
|
|
* Sets the root node to this and all of its descendants.
|
2011-11-03 21:48:40 +00:00
|
|
|
*
|
|
|
|
* @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 ) {
|
2012-03-12 00:07:33 +00:00
|
|
|
if ( root == this.root ) {
|
|
|
|
// Nothing to do, don't recurse into all descendants
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 );
|