2012-04-19 20:54:34 +00:00
|
|
|
/**
|
|
|
|
* Creates an ve.dm.Node object.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
* @constructor
|
|
|
|
* @extends {ve.Node}
|
|
|
|
* @param {String} type Symbolic name of node type
|
|
|
|
* @param {Integer} [length] Length of content data in document
|
|
|
|
* @param {Object} [attributes] Reference to map of attribute key/value pairs
|
|
|
|
*/
|
|
|
|
ve.dm.Node = function( type, length, attributes ) {
|
|
|
|
// Inheritance
|
|
|
|
ve.Node.call( this, type );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.length = length || 0;
|
|
|
|
this.attributes = attributes || {};
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Abstract Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a view for this node.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* @method
|
|
|
|
* @returns {ve.ce.Node} New item view associated with this model
|
|
|
|
*/
|
|
|
|
ve.dm.Node.prototype.createView = function() {
|
2012-04-20 01:05:48 +00:00
|
|
|
throw 've.dm.Node.createView not implemented in this subclass: ' + this.constructor;
|
2012-04-19 20:54:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the inner length.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Integer} Length of the node's contents
|
|
|
|
*/
|
2012-04-19 21:17:59 +00:00
|
|
|
ve.dm.Node.prototype.getLength = function() {
|
2012-04-19 20:54:34 +00:00
|
|
|
return this.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the outer length, including any opening/closing elements.
|
|
|
|
*
|
2012-04-30 20:23:46 +00:00
|
|
|
* The default implementation is getLength() + 2. Subclasses that do not use opening/closing
|
2012-04-19 20:54:34 +00:00
|
|
|
* elements should override this.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Integer} Length of the entire node
|
|
|
|
*/
|
2012-04-19 21:17:59 +00:00
|
|
|
ve.dm.Node.prototype.getOuterLength = function() {
|
2012-04-30 20:23:46 +00:00
|
|
|
return this.length + 2;
|
2012-04-19 20:54:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the inner length.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Integer} length Length of content
|
|
|
|
* @throws Invalid content length error if length is less than 0
|
|
|
|
* @emits lengthChange (diff)
|
|
|
|
* @emits update
|
|
|
|
*/
|
2012-04-19 21:17:59 +00:00
|
|
|
ve.dm.Node.prototype.setLength = function( length ) {
|
2012-04-19 20:54:34 +00:00
|
|
|
if ( length < 0 ) {
|
|
|
|
throw 'Length cannot be negative';
|
|
|
|
}
|
|
|
|
// Compute length adjustment from old length
|
|
|
|
var diff = length - this.length;
|
|
|
|
// Set new length
|
|
|
|
this.length = length;
|
|
|
|
// Adjust the parent's length
|
|
|
|
if ( this.parent ) {
|
|
|
|
this.parent.adjustLength( diff );
|
|
|
|
}
|
|
|
|
// Emit events
|
|
|
|
this.emit( 'lengthChange', diff );
|
|
|
|
this.emit( 'update' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adjust the length.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Integer} adjustment Amount to adjust length by
|
|
|
|
* @throws Invalid adjustment error if resulting length is less than 0
|
|
|
|
* @emits lengthChange (diff)
|
|
|
|
* @emits update
|
|
|
|
*/
|
2012-04-19 21:17:59 +00:00
|
|
|
ve.dm.Node.prototype.adjustLength = function( adjustment ) {
|
2012-04-19 20:54:34 +00:00
|
|
|
this.setLength( this.length + adjustment );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an element attribute value.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Mixed} Value of attribute, or undefined if no such attribute exists
|
|
|
|
*/
|
|
|
|
ve.dm.Node.prototype.getAttribute = function( key ) {
|
|
|
|
return this.attributes[key];
|
|
|
|
};
|
|
|
|
|
2012-04-19 23:03:59 +00:00
|
|
|
/**
|
|
|
|
* Sets the root node to this and all of its children.
|
|
|
|
*
|
|
|
|
* This method is overridden by nodes with children.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.Node} root Node to use as root
|
|
|
|
*/
|
2012-04-19 23:47:40 +00:00
|
|
|
ve.dm.Node.prototype.setRoot = function( root ) {
|
2012-04-19 23:03:59 +00:00
|
|
|
// TODO events?
|
|
|
|
this.root = root;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches this node to another as a child.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.Node} parent Node to attach to
|
|
|
|
* @emits attach (parent)
|
|
|
|
*/
|
2012-04-19 23:47:40 +00:00
|
|
|
ve.dm.Node.prototype.attach = function( parent ) {
|
2012-04-19 23:03:59 +00:00
|
|
|
this.emit( 'beforeAttach', parent );
|
|
|
|
this.parent = parent;
|
|
|
|
this.setRoot( parent.getRoot() );
|
|
|
|
this.emit( 'afterAttach', parent );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detaches this node from its parent.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @emits detach
|
|
|
|
*/
|
2012-04-19 23:47:40 +00:00
|
|
|
ve.dm.Node.prototype.detach = function() {
|
2012-04-19 23:03:59 +00:00
|
|
|
this.emit( 'beforeDetach' );
|
|
|
|
this.parent = null;
|
|
|
|
this.setRoot( this );
|
|
|
|
this.emit( 'afterDetach' );
|
|
|
|
};
|
|
|
|
|
2012-04-19 20:54:34 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.extendClass( ve.dm.Node, ve.Node );
|