2012-04-19 20:54:34 +00:00
|
|
|
/**
|
2012-04-30 23:58:41 +00:00
|
|
|
* Generic node.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-19 20:54:34 +00:00
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
* @constructor
|
|
|
|
* @extends {ve.EventEmitter}
|
|
|
|
* @param {String} type Symbolic name of node type
|
|
|
|
*/
|
|
|
|
ve.Node = function( type ) {
|
|
|
|
// Inheritance
|
|
|
|
ve.EventEmitter.call( this );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.type = type;
|
|
|
|
this.parent = null;
|
|
|
|
this.root = this;
|
2012-05-10 04:11:09 +00:00
|
|
|
this.doc = null;
|
2012-04-19 20:54:34 +00:00
|
|
|
|
2012-05-10 04:11:09 +00:00
|
|
|
// Convenience function for emitting update events - context is bound by enclosing this scope
|
|
|
|
// making it easy to pass through other functions as a callback
|
2012-04-19 20:54:34 +00:00
|
|
|
var _this = this;
|
|
|
|
this.emitUpdate = function() {
|
|
|
|
_this.emit( 'update' );
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-05-10 04:11:09 +00:00
|
|
|
/* Abstract Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if node can have children.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-10 04:11:09 +00:00
|
|
|
* @method
|
|
|
|
* @abstract
|
|
|
|
* @returns {Boolean} Node can have children
|
|
|
|
* @throws {Error} if not overridden
|
|
|
|
*/
|
|
|
|
ve.Node.prototype.canHaveChildren = function() {
|
|
|
|
throw 've.Node.canHaveChildren must be overridden in subclass';
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if node can have grandchildren.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-10 04:11:09 +00:00
|
|
|
* @method
|
|
|
|
* @abstract
|
|
|
|
* @returns {Boolean} Node can have grandchildren
|
|
|
|
* @throws {Error} if not overridden
|
|
|
|
*/
|
|
|
|
ve.Node.prototype.canHaveGrandchildren = function() {
|
|
|
|
throw 've.Node.canHaveGrandchildren must be overridden in subclass';
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if node represents a wrapped element.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-10 04:11:09 +00:00
|
|
|
* @method
|
|
|
|
* @abstract
|
|
|
|
* @returns {Boolean} Node represents a wrapped element
|
|
|
|
* @throws {Error} if not overridden
|
|
|
|
*/
|
|
|
|
ve.Node.prototype.isWrapped = function() {
|
|
|
|
throw 've.Node.isWrapped must be overridden in subclass';
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets node length.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-10 04:11:09 +00:00
|
|
|
* @method
|
|
|
|
* @abstract
|
|
|
|
* @returns {Integer} Node length
|
|
|
|
* @throws {Error} if not overridden
|
|
|
|
*/
|
|
|
|
ve.Node.prototype.getLength = function() {
|
|
|
|
throw 've.Node.getLength must be overridden in subclass';
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets node outer length.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-10 04:11:09 +00:00
|
|
|
* @method
|
|
|
|
* @abstract
|
|
|
|
* @returns {Integer} Node outer length
|
|
|
|
* @throws {Error} if not overridden
|
|
|
|
*/
|
|
|
|
ve.Node.prototype.getOuterLength = function() {
|
|
|
|
throw 've.Node.getOuterLength must be overridden in subclass';
|
|
|
|
};
|
|
|
|
|
2012-04-19 20:54:34 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2012-04-19 21:43:31 +00:00
|
|
|
/**
|
|
|
|
* Gets the symbolic node type name.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-19 21:43:31 +00:00
|
|
|
* @method
|
|
|
|
* @returns {String} Symbolic name of element type
|
|
|
|
*/
|
|
|
|
ve.Node.prototype.getType = function() {
|
|
|
|
return this.type;
|
|
|
|
};
|
|
|
|
|
2012-04-19 23:47:40 +00:00
|
|
|
/**
|
|
|
|
* Gets a reference to this node's parent.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-19 23:47:40 +00:00
|
|
|
* @method
|
|
|
|
* @returns {ve.Node} Reference to this node's parent
|
|
|
|
*/
|
|
|
|
ve.Node.prototype.getParent = function() {
|
|
|
|
return this.parent;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the root node in the tree this node is currently attached to.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-19 23:47:40 +00:00
|
|
|
* @method
|
|
|
|
* @returns {ve.Node} Root node
|
|
|
|
*/
|
|
|
|
ve.Node.prototype.getRoot = function() {
|
|
|
|
return this.root;
|
|
|
|
};
|
|
|
|
|
2012-05-10 04:11:09 +00:00
|
|
|
/**
|
|
|
|
* Sets the root node this node is a descendent of.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-10 04:11:09 +00:00
|
|
|
* This method is overridden by nodes with children.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-10 04:11:09 +00:00
|
|
|
* @method
|
|
|
|
* @param {ve.Node} root Node to use as root
|
|
|
|
*/
|
|
|
|
ve.Node.prototype.setRoot = function( root ) {
|
|
|
|
this.root = root;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the document this node is a part of.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-10 04:11:09 +00:00
|
|
|
* @method
|
|
|
|
* @returns {ve.Document} Document this node is a part of
|
|
|
|
*/
|
|
|
|
ve.Node.prototype.getDocument = function( root ) {
|
|
|
|
return this.doc;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the document this node is a part of.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-10 04:11:09 +00:00
|
|
|
* This method is overridden by nodes with children.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-10 04:11:09 +00:00
|
|
|
* @method
|
|
|
|
* @param {ve.Document} doc Document this node is a part of
|
|
|
|
*/
|
|
|
|
ve.Node.prototype.setDocument = function( doc ) {
|
|
|
|
this.doc = doc;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches this node to another as a child.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-10 04:11:09 +00:00
|
|
|
* @method
|
|
|
|
* @param {ve.Node} parent Node to attach to
|
|
|
|
* @emits attach (parent)
|
|
|
|
*/
|
|
|
|
ve.Node.prototype.attach = function( parent ) {
|
|
|
|
this.parent = parent;
|
|
|
|
this.setRoot( parent.getRoot() );
|
|
|
|
this.setDocument( parent.getDocument() );
|
|
|
|
this.emit( 'attach', parent );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detaches this node from its parent.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-10 04:11:09 +00:00
|
|
|
* @method
|
|
|
|
* @emits detach
|
|
|
|
*/
|
|
|
|
ve.Node.prototype.detach = function() {
|
|
|
|
this.parent = null;
|
|
|
|
this.setRoot( this );
|
|
|
|
this.setDocument();
|
|
|
|
this.emit( 'detach' );
|
|
|
|
};
|
|
|
|
|
2012-04-19 20:54:34 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.extendClass( ve.Node, ve.EventEmitter );
|