https://www.mediawiki.org/wiki/Special:Code/MediaWiki/102010

Revision: 102010
Author:   tparscal
Date:     2011-11-04 17:07:44 +0000 (Fri, 04 Nov 2011)
Log Message:
-----------
Reorganized a few methods to reduce duplication, improved documentation

Modified Paths:
--------------
    trunk/extensions/VisualEditor/demo/index.html
    trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js
    trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelBranchNode.js
    trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelLeafNode.js
    trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelNode.js
    trunk/extensions/VisualEditor/modules/es/bases/es.DocumentViewNode.js
    trunk/extensions/VisualEditor/tests/index.html

Modified: trunk/extensions/VisualEditor/demo/index.html
===================================================================
--- trunk/extensions/VisualEditor/demo/index.html       2011-11-04 17:07:34 UTC 
(rev 102009)
+++ trunk/extensions/VisualEditor/demo/index.html       2011-11-04 17:07:44 UTC 
(rev 102010)
@@ -68,6 +68,7 @@
 
                <!-- Bases -->
                <script src="../modules/es/bases/es.EventEmitter.js"></script>
+               <script src="../modules/es/bases/es.DocumentNode.js"></script>
                <script 
src="../modules/es/bases/es.DocumentBranchNode.js"></script>
                <script 
src="../modules/es/bases/es.DocumentModelNode.js"></script>
                <script 
src="../modules/es/bases/es.DocumentModelBranchNode.js"></script>

Modified: 
trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js
===================================================================
--- trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js     
2011-11-04 17:07:34 UTC (rev 102009)
+++ trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js     
2011-11-04 17:07:44 UTC (rev 102010)
@@ -4,7 +4,7 @@
  * @class
  * @abstract
  * @constructor
- * @param {es.DocumentNode[]} nodes List of document nodes to initially add
+ * @param {es.DocumentNode[]} nodes List of document nodes to add
  */
 es.DocumentBranchNode = function( nodes ) {
        this.children = es.isArray( nodes ) ? nodes : [];

Modified: 
trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelBranchNode.js
===================================================================
--- 
trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelBranchNode.js    
    2011-11-04 17:07:34 UTC (rev 102009)
+++ 
trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelBranchNode.js    
    2011-11-04 17:07:44 UTC (rev 102010)
@@ -7,7 +7,8 @@
  * @extends {es.DocumentModelNode}
  * @extends {es.DocumentBranchNode}
  * @param {String} type Symbolic name of node type
- * @param {es.DocumentModelBranchNode[]} contents List of child nodes to append
+ * @param {Object} element Element object in document data
+ * @param {es.DocumentModelBranchNode[]} [contents] List of child nodes to 
append
  */
 es.DocumentModelBranchNode = function( type, element, contents ) {
        // Inheritance
@@ -27,9 +28,9 @@
 /**
  * Gets a plain object representation of the document's data.
  * 
- * The resulting object is compatible with es.DocumentModel.newFromPlainObject.
- * 
  * @method
+ * @see {es.DocumentModelNode.getPlainObject}
+ * @see {es.DocumentModel.newFromPlainObject}
  * @returns {Object} Plain object representation
  */
 es.DocumentModelBranchNode.prototype.getPlainObject = function() {
@@ -214,6 +215,7 @@
  * Sets the root node to this and all of it's children.
  * 
  * @method
+ * @see {es.DocumentModelNode.prototype.setRoot}
  * @param {es.DocumentModelNode} root Node to use as root
  */
 es.DocumentModelBranchNode.prototype.setRoot = function( root ) {
@@ -227,6 +229,7 @@
  * Clears the root node from this and all of it's children.
  * 
  * @method
+ * @see {es.DocumentModelNode.prototype.clearRoot}
  */
 es.DocumentModelBranchNode.prototype.clearRoot = function() {
        this.root = null;

Modified: 
trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelLeafNode.js
===================================================================
--- trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelLeafNode.js  
2011-11-04 17:07:34 UTC (rev 102009)
+++ trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelLeafNode.js  
2011-11-04 17:07:44 UTC (rev 102010)
@@ -7,14 +7,15 @@
  * @extends {es.DocumentModelNode}
  * @extends {es.DocumentNode}
  * @param {String} type Symbolic name of node type
- * @param {Integer} length Length of content data in document
+ * @param {Object} element Element object in document data
+ * @param {Integer} [length] Length of content data in document
  */
 es.DocumentModelLeafNode = function( type, element, length ) {
        // Inheritance
        es.DocumentModelNode.call( this, type, element, length );
 
        // Properties
-       this.contentLength = length;
+       this.contentLength = length || 0;
 };
 
 /* Methods */
@@ -22,10 +23,10 @@
 /**
  * Gets a plain object representation of the document's data.
  * 
- * The resulting object is compatible with es.DocumentModel.newFromPlainObject.
- * 
  * @method
- * @returns {Object} Plain object representation
+ * @see {es.DocumentModelNode.getPlainObject}
+ * @see {es.DocumentModel.newFromPlainObject}
+ * @returns {Object} Plain object representation, 
  */
 es.DocumentModelLeafNode.prototype.getPlainObject = function() {
        var obj = { 'type': this.type };

Modified: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelNode.js
===================================================================
--- trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelNode.js      
2011-11-04 17:07:34 UTC (rev 102009)
+++ trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelNode.js      
2011-11-04 17:07:44 UTC (rev 102010)
@@ -6,25 +6,19 @@
  * @constructor
  * @extends {es.EventEmitter}
  * @param {String} type Symbolic name of node type
- * @param {Integer|Array} contents Either Length of content or array of child 
nodes to append
- * @property {Integer} contentLength Length of content
+ * @param {Object} element Element object in document data
+ * @param {Integer} [length] Length of content data in document
  */
 es.DocumentModelNode = function( type, element, length ) {
        // Inheritance
-       es.EventEmitter.call( this );
+       es.DocumentNode.call( this );
 
-       // Reusable function for passing update events upstream
-       var _this = this;
-       this.emitUpdate = function() {
-               _this.emit( 'update' );
-       };
-
        // Properties
        this.type = type;
        this.parent = null;
        this.root = this;
        this.element = element || null;
-       this.contentLength = length;
+       this.contentLength = length || 0;
 };
 
 /* Abstract Methods */
@@ -43,8 +37,6 @@
 /**
  * Gets a plain object representation of the document's data.
  * 
- * The resulting object is compatible with es.DocumentModel.newFromPlainObject.
- * 
  * @method
  * @returns {Object} Plain object representation
  */
@@ -55,46 +47,67 @@
 /* Methods */
 
 /**
- * Gets a reference to this node's parent.
+ * Gets the content length.
  * 
  * @method
- * @returns {es.DocumentModelNode} Reference to this node's parent
+ * @see {es.DocumentNode.prototype.getContentLength}
+ * @returns {Integer} Length of content
  */
-es.DocumentModelNode.prototype.getParent = function() {
-       return this.parent;
+es.DocumentModelNode.prototype.getContentLength = function() {
+       return this.contentLength;
 };
 
 /**
- * Gets the root node in the tree this node is currently attached to.
+ * Gets the element length.
  * 
  * @method
- * @returns {es.DocumentModelNode} Root node
+ * @see {es.DocumentNode.prototype.getElementLength}
+ * @returns {Integer} Length of content
  */
-es.DocumentModelNode.prototype.getRoot = function() {
-       return this.root;
+es.DocumentModelNode.prototype.getElementLength = function() {
+       return this.contentLength + 2;
 };
 
 /**
- * Sets the root node to this and all of it's children.
+ * Sets the content length.
  * 
- * This method is overridden by nodes with children.
- * 
  * @method
- * @param {es.DocumentModelNode} root Node to use as root
+ * @param {Integer} contentLength Length of content
+ * @throws Invalid content length error if contentLength is less than 0
  */
-es.DocumentModelNode.prototype.setRoot = function( root ) {
-       this.root = root;
+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 );
+       }
 };
 
 /**
- * Clears the root node from this and all of it's children.
+ * Adjust the content length.
  * 
- * This method is overridden by nodes with children.
- * 
  * @method
+ * @param {Integer} adjustment Amount to adjust content length by
+ * @throws Invalid adjustment error if resulting length is less than 0
  */
-es.DocumentModelNode.prototype.clearRoot = function() {
-       this.root = null;
+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' );
+       }
 };
 
 /**
@@ -125,65 +138,46 @@
 };
 
 /**
- * Sets the content length.
+ * Gets a reference to this node's parent.
  * 
  * @method
- * @param {Integer} contentLength Length of content
- * @throws Invalid content length error if contentLength is less than 0
+ * @returns {es.DocumentModelNode} Reference to this node's parent
  */
-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 );
-       }
+es.DocumentModelNode.prototype.getParent = function() {
+       return this.parent;
 };
 
 /**
- * Adjust the content length.
+ * Gets the root node in the tree this node is currently attached to.
  * 
  * @method
- * @param {Integer} adjustment Amount to adjust content length by
- * @throws Invalid adjustment error if resulting length is less than 0
+ * @returns {es.DocumentModelNode} Root node
  */
-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' );
-       }
+es.DocumentModelNode.prototype.getRoot = function() {
+       return this.root;
 };
 
 /**
- * Gets the content length.
+ * Sets the root node to this and all of it's children.
  * 
+ * This method is overridden by nodes with children.
+ * 
  * @method
- * @returns {Integer} Length of content
+ * @param {es.DocumentModelNode} root Node to use as root
  */
-es.DocumentModelNode.prototype.getContentLength = function() {
-       return this.contentLength;
+es.DocumentModelNode.prototype.setRoot = function( root ) {
+       this.root = root;
 };
 
 /**
- * Gets the element length.
+ * Clears the root node from this and all of it's children.
  * 
+ * This method is overridden by nodes with children.
+ * 
  * @method
- * @returns {Integer} Length of content
  */
-es.DocumentModelNode.prototype.getElementLength = function() {
-       return this.contentLength + 2;
+es.DocumentModelNode.prototype.clearRoot = function() {
+       this.root = null;
 };
 
 /**
@@ -221,4 +215,4 @@
 
 /* Inheritance */
 
-es.extendClass( es.DocumentModelNode, es.EventEmitter );
+es.extendClass( es.DocumentModelNode, es.DocumentNode );

Modified: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentViewNode.js
===================================================================
--- trunk/extensions/VisualEditor/modules/es/bases/es.DocumentViewNode.js       
2011-11-04 17:07:34 UTC (rev 102009)
+++ trunk/extensions/VisualEditor/modules/es/bases/es.DocumentViewNode.js       
2011-11-04 17:07:44 UTC (rev 102010)
@@ -1,55 +1,45 @@
 /**
  * Creates an es.DocumentViewNode object.
  * 
- * es.DocumentViewNode extends native JavaScript Array objects, without 
changing Array.prototype by
- * dynamically extending an array literal with the methods of 
es.DocumentViewNode.
- * 
- * View nodes follow the operations performed on model nodes and keep elements 
in the DOM in sync.
- * 
- * Child objects must extend es.DocumentViewNode.
- * 
  * @class
  * @abstract
  * @constructor
  * @extends {es.EventEmitter}
- * @param model {es.ModelNode} Model to observe
- * @param {jQuery} [$element=New DIV element] Element to use as a container
- * @property {es.ModelItem} model Model being observed
- * @property {jQuery} $ Container element
+ * @param {es.DocumentModelNode} model Model to observe
+ * @param {jQuery} [$element=$( '<div></div>' )] Element to use as a container
  */
 es.DocumentViewNode = function( model, $element ) {
        // Inheritance
-       es.EventEmitter.call( this );
+       es.DocumentNode.call( this );
        
        // Properties
        this.model = model;
+       this.parent = null;
        this.$ = $element || $( '<div/>' );
-
-       // Reusable function for passing update events upstream
-       var _this = this;
-       this.emitUpdate = function() {
-               _this.emit( 'update' );
-       };
 };
 
+/* Methods */
+
 /**
- * Gets a reference to the model this node observes.
+ * Gets the length of the element in the model.
  * 
  * @method
- * @returns {es.ModelNode} Reference to the model this node observes
+ * @see {es.DocumentNode.prototype.getElementLength}
+ * @returns {Integer} Length of content
  */
-es.DocumentViewNode.prototype.getModel = function() {
-       return this.model;
+es.DocumentViewNode.prototype.getElementLength = function() {
+       return this.model.getElementLength();
 };
 
 /**
- * Gets a reference to this node's parent.
+ * Gets the length of the content in the model.
  * 
  * @method
- * @returns {es.DocumentViewNode} Reference to this node's parent
+ * @see {es.DocumentNode.prototype.getContentLength}
+ * @returns {Integer} Length of content
  */
-es.DocumentViewNode.prototype.getParent = function() {
-       return this.parent;
+es.DocumentViewNode.prototype.getContentLength = function() {
+       return this.model.getContentLength();
 };
 
 /**
@@ -77,25 +67,25 @@
 };
 
 /**
- * Gets the length of the element in the model.
+ * Gets a reference to this node's parent.
  * 
  * @method
- * @returns {Integer} Length of content
+ * @returns {es.DocumentViewNode} Reference to this node's parent
  */
-es.DocumentViewNode.prototype.getElementLength = function() {
-       return this.model.getElementLength();
+es.DocumentViewNode.prototype.getParent = function() {
+       return this.parent;
 };
 
 /**
- * Gets the length of the content in the model.
+ * Gets a reference to the model this node observes.
  * 
  * @method
- * @returns {Integer} Length of content
+ * @returns {es.DocumentModelNode} Reference to the model this node observes
  */
-es.DocumentViewNode.prototype.getContentLength = function() {
-       return this.model.getContentLength();
+es.DocumentViewNode.prototype.getModel = function() {
+       return this.model;
 };
 
 /* Inheritance */
 
-es.extendClass( es.DocumentViewNode, es.EventEmitter );
+es.extendClass( es.DocumentViewNode, es.DocumentNode );

Modified: trunk/extensions/VisualEditor/tests/index.html
===================================================================
--- trunk/extensions/VisualEditor/tests/index.html      2011-11-04 17:07:34 UTC 
(rev 102009)
+++ trunk/extensions/VisualEditor/tests/index.html      2011-11-04 17:07:44 UTC 
(rev 102010)
@@ -22,6 +22,7 @@
 
                <!-- Bases -->
                <script src="../modules/es/bases/es.EventEmitter.js"></script>
+               <script src="../modules/es/bases/es.DocumentNode.js"></script>
                <script 
src="../modules/es/bases/es.DocumentBranchNode.js"></script>
                <script 
src="../modules/es/bases/es.DocumentModelNode.js"></script>
                <script 
src="../modules/es/bases/es.DocumentModelBranchNode.js"></script>


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to