2011-11-04 17:07:34 +00:00
|
|
|
/**
|
2012-02-06 23:50:56 +00:00
|
|
|
* Creates an ve.Node object.
|
2011-11-04 17:07:34 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
* @constructor
|
2012-02-06 23:50:56 +00:00
|
|
|
* @extends {ve.EventEmitter}
|
2011-11-04 17:07:34 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.Node = function() {
|
2011-11-04 17:07:34 +00:00
|
|
|
// Inheritance
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.EventEmitter.call( this );
|
2011-11-04 17:07:34 +00:00
|
|
|
|
|
|
|
// Reusable function for passing update events upstream
|
|
|
|
var _this = this;
|
|
|
|
this.emitUpdate = function() {
|
|
|
|
_this.emit( 'update' );
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the content length.
|
|
|
|
*
|
|
|
|
* @method
|
2011-11-10 19:26:02 +00:00
|
|
|
* @abstract
|
2011-11-04 17:07:34 +00:00
|
|
|
* @returns {Integer} Length of content
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.Node.prototype.getContentLength = function() {
|
2011-11-04 17:07:34 +00:00
|
|
|
throw 'DocumentNode.getContentLength not implemented in this subclass:' + this.constructor;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the element length.
|
|
|
|
*
|
|
|
|
* @method
|
2011-11-10 19:26:02 +00:00
|
|
|
* @abstract
|
2011-11-04 17:07:34 +00:00
|
|
|
* @returns {Integer} Length of content
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.Node.prototype.getElementLength = function() {
|
2011-11-04 17:07:34 +00:00
|
|
|
throw 'DocumentNode.getElementLength not implemented in this subclass:' + this.constructor;
|
|
|
|
};
|
|
|
|
|
2011-11-10 19:26:02 +00:00
|
|
|
/**
|
|
|
|
* Checks if this node has child nodes.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @abstract
|
|
|
|
* @returns {Boolean} Whether this node has children
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.Node.prototype.hasChildren = function() {
|
2011-11-10 19:26:02 +00:00
|
|
|
throw 'DocumentNode.hasChildren not implemented in this subclass:' + this.constructor;
|
|
|
|
};
|
|
|
|
|
2011-11-18 05:18:48 +00:00
|
|
|
/**
|
|
|
|
* Traverse tree of nodes (model or view) upstream and for each traversed node call callback function passing traversed node as a parameter.
|
|
|
|
* Callback function is called for node passed as node paramter as well.
|
|
|
|
*
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.Node} node Node from which to start traversing
|
2011-11-18 05:18:48 +00:00
|
|
|
* @param {function} callback Callback method to be called for every traversed node
|
|
|
|
* @method
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.Node.traverseUpstream = function( node, callback ) {
|
2011-11-18 05:18:48 +00:00
|
|
|
while ( node ) {
|
|
|
|
if ( callback ( node ) === false ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node = node.getParent();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-24 15:34:12 +00:00
|
|
|
/**
|
|
|
|
* Find the common ancestor of two equal-depth nodes, and return the
|
|
|
|
* path from each node to the common ancestor.
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.Node} node1
|
|
|
|
* @param {ve.Node} node2
|
2011-11-24 15:34:12 +00:00
|
|
|
* @returns {Object|Boolean} Object with keys 'commonAncestor', 'node1Path' and 'node2Path',
|
|
|
|
* or false if there is no common ancestor or if the nodes have unequal depth
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.Node.getCommonAncestorPaths = function( node1, node2 ) {
|
2011-11-24 15:34:12 +00:00
|
|
|
var path1 = [],
|
|
|
|
path2 = [],
|
|
|
|
n1 = node1,
|
|
|
|
n2 = node2;
|
|
|
|
|
|
|
|
// Move up from n1 and n2 simultaneously until we find the
|
|
|
|
// common ancestor
|
|
|
|
while ( n1 !== n2 ) {
|
|
|
|
// Add these nodes to their respective paths
|
|
|
|
path1.push( n1 );
|
|
|
|
path2.push( n2 );
|
|
|
|
// Move up
|
|
|
|
n1 = n1.getParent();
|
|
|
|
n2 = n2.getParent();
|
|
|
|
if ( n1 === null || n2 === null ) {
|
|
|
|
// Reached a root, so no common ancestor or unequal depth
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got here, we've found the common ancestor, and because we did
|
|
|
|
// simultaneous traversal we also know node1 and node2 have the same depth.
|
|
|
|
return { 'commonAncestor': n1, 'node1Path': path1, 'node2Path': path2 };
|
|
|
|
};
|
|
|
|
|
2011-11-04 17:07:34 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.extendClass( ve.Node, ve.EventEmitter );
|