mediawiki-extensions-Visual.../modules/ve/ve.Node.js
Timo Tijhof b1d9c83b5d Object management: Object create/inherit/clone utilities
* For the most common case:
  - replace ve.extendClass with ve.inheritClass (chose slightly
    different names to detect usage of the old/new one, and I
    like 'inherit' better).
  - move it up to below the constructor, see doc block for why.

* Cases where more than 2 arguments were passed to
  ve.extendClass are handled differently depending on the case.

  In case of a longer inheritance tree, the other arguments
  could be omitted (like in "ve.ce.FooBar, ve.FooBar,
  ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
  because ve.ce.FooBar inherits from ve.Bar).

  In the case of where it previously had two mixins with
  ve.extendClass(), either one becomes inheritClass and one
  a mixin, both to mixinClass().

  No visible changes should come from this commit as the
  instances still all have the same visible properties in the
  end. No more or less than before.

* Misc.:
 - Be consistent in calling parent constructors in the
   same order as the inheritance.
 - Add missing @extends and @param documentation.
 - Replace invalid {Integer} type hint with {Number}.
 - Consistent doc comments order:
   @class, @abstract, @constructor, @extends, @params.
 - Fix indentation errors
   A fairly common mistake was a superfluous space before the
   identifier on the assignment line directly below the
   documentation comment.
   $ ack "^ [^*]" --js modules/ve
 - Typo "Inhertiance" -> "Inheritance".
 - Replacing the other confusing comment "Inheritance" (inside
   the constructor) with "Parent constructor".
 - Add missing @abstract for ve.ui.Tool.
 - Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
 - Add function names to all @constructor functions. Now that we
   have inheritance it is important and useful to have these
   functions not be anonymous.

   Example of debug shot: http://cl.ly/image/1j3c160w3D45

   Makes the difference between

   < documentNode;
   > ve_dm_DocumentNode
     ...
     : ve_dm_BranchNode
       ...
       : ve_dm_Node
         ...
         : ve_dm_Node
           ...
           : Object
             ...

   without names (current situation):

   < documentNode;
   > Object
     ...
     : Object
       ...
       : Object
         ...
         : Object
           ...
           : Object
             ...

   though before this commit, it really looks like this
   (flattened since ve.extendClass really did a mixin):

   < documentNode;
   > Object
     ...
     ...
     ...

   Pattern in Sublime (case-sensitive) to find nameless
   constructor functions:
   "^ve\..*\.([A-Z])([^\.]+) = function \("

Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-06 15:29:31 -07:00

211 lines
4.5 KiB
JavaScript

/**
* VisualEditor Node class.
*
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Generic node.
*
* @class
* @abstract
* @constructor
* @extends {ve.EventEmitter}
* @param {String} type Symbolic name of node type
*/
ve.Node = function ve_Node( type ) {
// Parent constructor
ve.EventEmitter.call( this );
// Properties
this.type = type;
this.parent = null;
this.root = this;
this.doc = null;
// Convenience function for emitting update events - context is bound by enclosing this scope
// making it easy to pass through other functions as a callback
var node = this;
this.emitUpdate = function () {
node.emit( 'update' );
};
};
/* Inheritance */
ve.inheritClass( ve.Node, ve.EventEmitter );
/* Abstract Methods */
/**
* Checks if node can have children.
*
* @method
* @abstract
* @returns {Boolean} Node can have children
* @throws {Error} if not overridden
*/
ve.Node.prototype.canHaveChildren = function () {
throw new Error( 've.Node.canHaveChildren must be overridden in subclass' );
};
/**
* Checks if node can have grandchildren.
*
* @method
* @abstract
* @returns {Boolean} Node can have grandchildren
* @throws {Error} if not overridden
*/
ve.Node.prototype.canHaveGrandchildren = function () {
throw new Error( 've.Node.canHaveGrandchildren must be overridden in subclass' );
};
/**
* Checks if node represents a wrapped element.
*
* @method
* @abstract
* @returns {Boolean} Node represents a wrapped element
* @throws {Error} if not overridden
*/
ve.Node.prototype.isWrapped = function () {
throw new Error( 've.Node.isWrapped must be overridden in subclass' );
};
/**
* Gets node length.
*
* @method
* @abstract
* @returns {Number} Node length
* @throws {Error} if not overridden
*/
ve.Node.prototype.getLength = function () {
throw new Error( 've.Node.getLength must be overridden in subclass' );
};
/**
* Gets node outer length.
*
* @method
* @abstract
* @returns {Number} Node outer length
* @throws {Error} if not overridden
*/
ve.Node.prototype.getOuterLength = function () {
throw new Error( 've.Node.getOuterLength must be overridden in subclass' );
};
/* Methods */
/**
* Gets the symbolic node type name.
*
* @method
* @returns {String} Symbolic name of element type
*/
ve.Node.prototype.getType = function () {
return this.type;
};
/**
* Gets a reference to this node's parent.
*
* @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.
*
* @method
* @returns {ve.Node} Root node
*/
ve.Node.prototype.getRoot = function () {
return this.root;
};
/**
* Sets the root node this node is a descendent of.
*
* This method is overridden by nodes with children.
*
* @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.
*
* @method
* @returns {ve.Document} Document this node is a part of
*/
ve.Node.prototype.getDocument = function () {
return this.doc;
};
/**
* Sets the document this node is a part of.
*
* This method is overridden by nodes with children.
*
* @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.
*
* @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.
*
* @method
* @emits detach
*/
ve.Node.prototype.detach = function () {
var parent = this.parent;
this.parent = null;
this.setRoot( this );
this.setDocument();
this.emit( 'detach', parent );
};
/**
* 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.
*
* @param {ve.Node} node Node from which to start traversing
* @param {function} callback Callback method to be called for every traversed node
* @method
*/
ve.Node.traverseUpstream = function ( node, callback ) {
while ( node ) {
if ( callback ( node ) === false ) {
break;
}
node = node.getParent();
}
};