2011-11-02 21:00:55 +00:00
|
|
|
/**
|
|
|
|
* Creates an es.DocumentModelNode object.
|
|
|
|
*
|
|
|
|
* @class
|
2011-11-03 19:01:55 +00:00
|
|
|
* @abstract
|
2011-11-02 21:00:55 +00:00
|
|
|
* @constructor
|
2011-11-10 14:00:46 +00:00
|
|
|
* @extends {es.DocumentNode}
|
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
|
|
|
|
* @param {Integer} [length] Length of content data in document
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2011-11-03 21:48:40 +00:00
|
|
|
es.DocumentModelNode = function( type, element, length ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
// Inheritance
|
2011-11-04 17:07:44 +00:00
|
|
|
es.DocumentNode.call( this );
|
2011-11-03 19:01:55 +00:00
|
|
|
|
2011-11-02 21:00:55 +00:00
|
|
|
// Properties
|
2011-11-03 19:01:55 +00:00
|
|
|
this.type = type;
|
2011-11-02 21:00:55 +00:00
|
|
|
this.parent = null;
|
|
|
|
this.root = this;
|
|
|
|
this.element = element || null;
|
2011-11-04 17:07:44 +00:00
|
|
|
this.contentLength = length || 0;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Abstract Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a view for this node.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* @method
|
|
|
|
* @returns {es.DocumentViewNode} New item view associated with this model
|
|
|
|
*/
|
|
|
|
es.DocumentModelNode.prototype.createView = function() {
|
|
|
|
throw 'DocumentModelNode.createView not implemented in this subclass:' + this.constructor;
|
|
|
|
};
|
|
|
|
|
2011-11-03 19:01:55 +00:00
|
|
|
/**
|
|
|
|
* Gets a plain object representation of the document's data.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Object} Plain object representation
|
|
|
|
*/
|
|
|
|
es.DocumentModelNode.prototype.getPlainObject = function() {
|
2011-11-03 21:48:40 +00:00
|
|
|
throw 'DocumentModelNode.getPlainObject not implemented in this subclass:' + this.constructor;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
2011-11-03 21:48:40 +00:00
|
|
|
/* Methods */
|
2011-11-02 21:00:55 +00:00
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Gets the content length.
|
2011-11-02 21:00:55 +00:00
|
|
|
*
|
|
|
|
* @method
|
2011-11-04 17:07:44 +00:00
|
|
|
* @see {es.DocumentNode.prototype.getContentLength}
|
|
|
|
* @returns {Integer} Length of content
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2011-11-04 17:07:44 +00:00
|
|
|
es.DocumentModelNode.prototype.getContentLength = function() {
|
|
|
|
return this.contentLength;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Gets the element length.
|
2011-11-02 21:00:55 +00:00
|
|
|
*
|
|
|
|
* @method
|
2011-11-04 17:07:44 +00:00
|
|
|
* @see {es.DocumentNode.prototype.getElementLength}
|
|
|
|
* @returns {Integer} Length of content
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2011-11-04 17:07:44 +00:00
|
|
|
es.DocumentModelNode.prototype.getElementLength = function() {
|
|
|
|
return this.contentLength + 2;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Sets the content length.
|
2011-11-03 21:48:40 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2011-11-04 17:07:44 +00:00
|
|
|
* @param {Integer} contentLength Length of content
|
|
|
|
* @throws Invalid content length error if contentLength is less than 0
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2011-11-04 17:07:44 +00:00
|
|
|
es.DocumentModelNode.prototype.setContentLength = function( contentLength ) {
|
|
|
|
if ( contentLength < 0 ) {
|
|
|
|
throw 'Invalid content length error. Content length can not be less than 0.';
|
|
|
|
}
|
|
|
|
var diff = contentLength - this.contentLength;
|
|
|
|
this.contentLength = contentLength;
|
|
|
|
if ( this.parent ) {
|
|
|
|
this.parent.adjustContentLength( diff );
|
|
|
|
}
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Adjust the content length.
|
2011-11-03 21:48:40 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2011-11-04 17:07:44 +00:00
|
|
|
* @param {Integer} adjustment Amount to adjust content length by
|
|
|
|
* @throws Invalid adjustment error if resulting length is less than 0
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2011-11-04 17:07:44 +00:00
|
|
|
es.DocumentModelNode.prototype.adjustContentLength = function( adjustment, quiet ) {
|
|
|
|
this.contentLength += adjustment;
|
|
|
|
// Make sure the adjustment was sane
|
|
|
|
if ( this.contentLength < 0 ) {
|
|
|
|
// Reverse the adjustment
|
|
|
|
this.contentLength -= adjustment;
|
|
|
|
// Complain about it
|
|
|
|
throw 'Invalid adjustment error. Content length can not be less than 0.';
|
|
|
|
}
|
|
|
|
if ( this.parent ) {
|
|
|
|
this.parent.adjustContentLength( adjustment, true );
|
|
|
|
}
|
|
|
|
if ( !quiet ) {
|
|
|
|
this.emit( 'update' );
|
|
|
|
}
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches this node to another as a child.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {es.DocumentModelNode} parent Node to attach to
|
|
|
|
* @emits attach (parent)
|
|
|
|
*/
|
|
|
|
es.DocumentModelNode.prototype.attach = function( parent ) {
|
|
|
|
this.emit( 'beforeAttach', parent );
|
|
|
|
this.parent = parent;
|
|
|
|
this.setRoot( parent.getRoot() );
|
|
|
|
this.emit( 'afterAttach', parent );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detaches this node from it's parent.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @emits detach
|
|
|
|
*/
|
|
|
|
es.DocumentModelNode.prototype.detach = function() {
|
|
|
|
this.emit( 'beforeDetach' );
|
|
|
|
this.parent = null;
|
|
|
|
this.clearRoot();
|
|
|
|
this.emit( 'afterDetach' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Gets a reference to this node's parent.
|
2011-11-02 21:00:55 +00:00
|
|
|
*
|
|
|
|
* @method
|
2011-11-04 17:07:44 +00:00
|
|
|
* @returns {es.DocumentModelNode} Reference to this node's parent
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2011-11-04 17:07:44 +00:00
|
|
|
es.DocumentModelNode.prototype.getParent = function() {
|
|
|
|
return this.parent;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Gets the root node in the tree this node is currently attached to.
|
2011-11-02 21:00:55 +00:00
|
|
|
*
|
|
|
|
* @method
|
2011-11-04 17:07:44 +00:00
|
|
|
* @returns {es.DocumentModelNode} Root node
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2011-11-04 17:07:44 +00:00
|
|
|
es.DocumentModelNode.prototype.getRoot = function() {
|
|
|
|
return this.root;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Sets the root node to this and all of it's children.
|
|
|
|
*
|
|
|
|
* This method is overridden by nodes with children.
|
2011-11-02 21:00:55 +00:00
|
|
|
*
|
|
|
|
* @method
|
2011-11-04 17:07:44 +00:00
|
|
|
* @param {es.DocumentModelNode} root Node to use as root
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2011-11-04 17:07:44 +00:00
|
|
|
es.DocumentModelNode.prototype.setRoot = function( root ) {
|
|
|
|
this.root = root;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Clears the root node from this and all of it's children.
|
|
|
|
*
|
|
|
|
* This method is overridden by nodes with children.
|
2011-11-02 21:00:55 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
2011-11-04 17:07:44 +00:00
|
|
|
es.DocumentModelNode.prototype.clearRoot = function() {
|
|
|
|
this.root = null;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the element object.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Object} Element object in linear data model
|
|
|
|
*/
|
|
|
|
es.DocumentModelNode.prototype.getElement = function() {
|
|
|
|
return this.element;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the symbolic element type name.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {String} Symbolic name of element type
|
|
|
|
*/
|
|
|
|
es.DocumentModelNode.prototype.getElementType = function() {
|
|
|
|
return this.element.type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an element attribute value.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Mixed} Value of attribute, or null if no such attribute exists
|
|
|
|
*/
|
|
|
|
es.DocumentModelNode.prototype.getElementAttribute = function( key ) {
|
2011-11-03 19:01:55 +00:00
|
|
|
if ( this.element && this.element.attributes && key in this.element.attributes ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
return this.element.attributes[key];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2011-11-04 17:07:44 +00:00
|
|
|
es.extendClass( es.DocumentModelNode, es.DocumentNode );
|