r102010 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r102009‎ | r102010 | r102011 >
Date:17:07, 4 November 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Reorganized a few methods to reduce duplication, improved documentation
Modified paths:
  • /trunk/extensions/VisualEditor/demo/index.html (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelBranchNode.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelLeafNode.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelNode.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/bases/es.DocumentViewNode.js (modified) (history)
  • /trunk/extensions/VisualEditor/tests/index.html (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/tests/index.html
@@ -22,6 +22,7 @@
2323
2424 <!-- Bases -->
2525 <script src="../modules/es/bases/es.EventEmitter.js"></script>
 26+ <script src="../modules/es/bases/es.DocumentNode.js"></script>
2627 <script src="../modules/es/bases/es.DocumentBranchNode.js"></script>
2728 <script src="../modules/es/bases/es.DocumentModelNode.js"></script>
2829 <script src="../modules/es/bases/es.DocumentModelBranchNode.js"></script>
Index: trunk/extensions/VisualEditor/demo/index.html
@@ -68,6 +68,7 @@
6969
7070 <!-- Bases -->
7171 <script src="../modules/es/bases/es.EventEmitter.js"></script>
 72+ <script src="../modules/es/bases/es.DocumentNode.js"></script>
7273 <script src="../modules/es/bases/es.DocumentBranchNode.js"></script>
7374 <script src="../modules/es/bases/es.DocumentModelNode.js"></script>
7475 <script src="../modules/es/bases/es.DocumentModelBranchNode.js"></script>
Index: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentViewNode.js
@@ -1,55 +1,45 @@
22 /**
33 * Creates an es.DocumentViewNode object.
44 *
5 - * es.DocumentViewNode extends native JavaScript Array objects, without changing Array.prototype by
6 - * dynamically extending an array literal with the methods of es.DocumentViewNode.
7 - *
8 - * View nodes follow the operations performed on model nodes and keep elements in the DOM in sync.
9 - *
10 - * Child objects must extend es.DocumentViewNode.
11 - *
125 * @class
136 * @abstract
147 * @constructor
158 * @extends {es.EventEmitter}
16 - * @param model {es.ModelNode} Model to observe
17 - * @param {jQuery} [$element=New DIV element] Element to use as a container
18 - * @property {es.ModelItem} model Model being observed
19 - * @property {jQuery} $ Container element
 9+ * @param {es.DocumentModelNode} model Model to observe
 10+ * @param {jQuery} [$element=$( '<div></div>' )] Element to use as a container
2011 */
2112 es.DocumentViewNode = function( model, $element ) {
2213 // Inheritance
23 - es.EventEmitter.call( this );
 14+ es.DocumentNode.call( this );
2415
2516 // Properties
2617 this.model = model;
 18+ this.parent = null;
2719 this.$ = $element || $( '<div/>' );
28 -
29 - // Reusable function for passing update events upstream
30 - var _this = this;
31 - this.emitUpdate = function() {
32 - _this.emit( 'update' );
33 - };
3420 };
3521
 22+/* Methods */
 23+
3624 /**
37 - * Gets a reference to the model this node observes.
 25+ * Gets the length of the element in the model.
3826 *
3927 * @method
40 - * @returns {es.ModelNode} Reference to the model this node observes
 28+ * @see {es.DocumentNode.prototype.getElementLength}
 29+ * @returns {Integer} Length of content
4130 */
42 -es.DocumentViewNode.prototype.getModel = function() {
43 - return this.model;
 31+es.DocumentViewNode.prototype.getElementLength = function() {
 32+ return this.model.getElementLength();
4433 };
4534
4635 /**
47 - * Gets a reference to this node's parent.
 36+ * Gets the length of the content in the model.
4837 *
4938 * @method
50 - * @returns {es.DocumentViewNode} Reference to this node's parent
 39+ * @see {es.DocumentNode.prototype.getContentLength}
 40+ * @returns {Integer} Length of content
5141 */
52 -es.DocumentViewNode.prototype.getParent = function() {
53 - return this.parent;
 42+es.DocumentViewNode.prototype.getContentLength = function() {
 43+ return this.model.getContentLength();
5444 };
5545
5646 /**
@@ -77,25 +67,25 @@
7868 };
7969
8070 /**
81 - * Gets the length of the element in the model.
 71+ * Gets a reference to this node's parent.
8272 *
8373 * @method
84 - * @returns {Integer} Length of content
 74+ * @returns {es.DocumentViewNode} Reference to this node's parent
8575 */
86 -es.DocumentViewNode.prototype.getElementLength = function() {
87 - return this.model.getElementLength();
 76+es.DocumentViewNode.prototype.getParent = function() {
 77+ return this.parent;
8878 };
8979
9080 /**
91 - * Gets the length of the content in the model.
 81+ * Gets a reference to the model this node observes.
9282 *
9383 * @method
94 - * @returns {Integer} Length of content
 84+ * @returns {es.DocumentModelNode} Reference to the model this node observes
9585 */
96 -es.DocumentViewNode.prototype.getContentLength = function() {
97 - return this.model.getContentLength();
 86+es.DocumentViewNode.prototype.getModel = function() {
 87+ return this.model;
9888 };
9989
10090 /* Inheritance */
10191
102 -es.extendClass( es.DocumentViewNode, es.EventEmitter );
 92+es.extendClass( es.DocumentViewNode, es.DocumentNode );
Index: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js
@@ -4,7 +4,7 @@
55 * @class
66 * @abstract
77 * @constructor
8 - * @param {es.DocumentNode[]} nodes List of document nodes to initially add
 8+ * @param {es.DocumentNode[]} nodes List of document nodes to add
99 */
1010 es.DocumentBranchNode = function( nodes ) {
1111 this.children = es.isArray( nodes ) ? nodes : [];
Index: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelBranchNode.js
@@ -7,7 +7,8 @@
88 * @extends {es.DocumentModelNode}
99 * @extends {es.DocumentBranchNode}
1010 * @param {String} type Symbolic name of node type
11 - * @param {es.DocumentModelBranchNode[]} contents List of child nodes to append
 11+ * @param {Object} element Element object in document data
 12+ * @param {es.DocumentModelBranchNode[]} [contents] List of child nodes to append
1213 */
1314 es.DocumentModelBranchNode = function( type, element, contents ) {
1415 // Inheritance
@@ -27,9 +28,9 @@
2829 /**
2930 * Gets a plain object representation of the document's data.
3031 *
31 - * The resulting object is compatible with es.DocumentModel.newFromPlainObject.
32 - *
3332 * @method
 33+ * @see {es.DocumentModelNode.getPlainObject}
 34+ * @see {es.DocumentModel.newFromPlainObject}
3435 * @returns {Object} Plain object representation
3536 */
3637 es.DocumentModelBranchNode.prototype.getPlainObject = function() {
@@ -214,6 +215,7 @@
215216 * Sets the root node to this and all of it's children.
216217 *
217218 * @method
 219+ * @see {es.DocumentModelNode.prototype.setRoot}
218220 * @param {es.DocumentModelNode} root Node to use as root
219221 */
220222 es.DocumentModelBranchNode.prototype.setRoot = function( root ) {
@@ -227,6 +229,7 @@
228230 * Clears the root node from this and all of it's children.
229231 *
230232 * @method
 233+ * @see {es.DocumentModelNode.prototype.clearRoot}
231234 */
232235 es.DocumentModelBranchNode.prototype.clearRoot = function() {
233236 this.root = null;
Index: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelNode.js
@@ -6,25 +6,19 @@
77 * @constructor
88 * @extends {es.EventEmitter}
99 * @param {String} type Symbolic name of node type
10 - * @param {Integer|Array} contents Either Length of content or array of child nodes to append
11 - * @property {Integer} contentLength Length of content
 10+ * @param {Object} element Element object in document data
 11+ * @param {Integer} [length] Length of content data in document
1212 */
1313 es.DocumentModelNode = function( type, element, length ) {
1414 // Inheritance
15 - es.EventEmitter.call( this );
 15+ es.DocumentNode.call( this );
1616
17 - // Reusable function for passing update events upstream
18 - var _this = this;
19 - this.emitUpdate = function() {
20 - _this.emit( 'update' );
21 - };
22 -
2317 // Properties
2418 this.type = type;
2519 this.parent = null;
2620 this.root = this;
2721 this.element = element || null;
28 - this.contentLength = length;
 22+ this.contentLength = length || 0;
2923 };
3024
3125 /* Abstract Methods */
@@ -43,8 +37,6 @@
4438 /**
4539 * Gets a plain object representation of the document's data.
4640 *
47 - * The resulting object is compatible with es.DocumentModel.newFromPlainObject.
48 - *
4941 * @method
5042 * @returns {Object} Plain object representation
5143 */
@@ -55,46 +47,67 @@
5648 /* Methods */
5749
5850 /**
59 - * Gets a reference to this node's parent.
 51+ * Gets the content length.
6052 *
6153 * @method
62 - * @returns {es.DocumentModelNode} Reference to this node's parent
 54+ * @see {es.DocumentNode.prototype.getContentLength}
 55+ * @returns {Integer} Length of content
6356 */
64 -es.DocumentModelNode.prototype.getParent = function() {
65 - return this.parent;
 57+es.DocumentModelNode.prototype.getContentLength = function() {
 58+ return this.contentLength;
6659 };
6760
6861 /**
69 - * Gets the root node in the tree this node is currently attached to.
 62+ * Gets the element length.
7063 *
7164 * @method
72 - * @returns {es.DocumentModelNode} Root node
 65+ * @see {es.DocumentNode.prototype.getElementLength}
 66+ * @returns {Integer} Length of content
7367 */
74 -es.DocumentModelNode.prototype.getRoot = function() {
75 - return this.root;
 68+es.DocumentModelNode.prototype.getElementLength = function() {
 69+ return this.contentLength + 2;
7670 };
7771
7872 /**
79 - * Sets the root node to this and all of it's children.
 73+ * Sets the content length.
8074 *
81 - * This method is overridden by nodes with children.
82 - *
8375 * @method
84 - * @param {es.DocumentModelNode} root Node to use as root
 76+ * @param {Integer} contentLength Length of content
 77+ * @throws Invalid content length error if contentLength is less than 0
8578 */
86 -es.DocumentModelNode.prototype.setRoot = function( root ) {
87 - this.root = root;
 79+es.DocumentModelNode.prototype.setContentLength = function( contentLength ) {
 80+ if ( contentLength < 0 ) {
 81+ throw 'Invalid content length error. Content length can not be less than 0.';
 82+ }
 83+ var diff = contentLength - this.contentLength;
 84+ this.contentLength = contentLength;
 85+ if ( this.parent ) {
 86+ this.parent.adjustContentLength( diff );
 87+ }
8888 };
8989
9090 /**
91 - * Clears the root node from this and all of it's children.
 91+ * Adjust the content length.
9292 *
93 - * This method is overridden by nodes with children.
94 - *
9593 * @method
 94+ * @param {Integer} adjustment Amount to adjust content length by
 95+ * @throws Invalid adjustment error if resulting length is less than 0
9696 */
97 -es.DocumentModelNode.prototype.clearRoot = function() {
98 - this.root = null;
 97+es.DocumentModelNode.prototype.adjustContentLength = function( adjustment, quiet ) {
 98+ this.contentLength += adjustment;
 99+ // Make sure the adjustment was sane
 100+ if ( this.contentLength < 0 ) {
 101+ // Reverse the adjustment
 102+ this.contentLength -= adjustment;
 103+ // Complain about it
 104+ throw 'Invalid adjustment error. Content length can not be less than 0.';
 105+ }
 106+ if ( this.parent ) {
 107+ this.parent.adjustContentLength( adjustment, true );
 108+ }
 109+ if ( !quiet ) {
 110+ this.emit( 'update' );
 111+ }
99112 };
100113
101114 /**
@@ -125,65 +138,46 @@
126139 };
127140
128141 /**
129 - * Sets the content length.
 142+ * Gets a reference to this node's parent.
130143 *
131144 * @method
132 - * @param {Integer} contentLength Length of content
133 - * @throws Invalid content length error if contentLength is less than 0
 145+ * @returns {es.DocumentModelNode} Reference to this node's parent
134146 */
135 -es.DocumentModelNode.prototype.setContentLength = function( contentLength ) {
136 - if ( contentLength < 0 ) {
137 - throw 'Invalid content length error. Content length can not be less than 0.';
138 - }
139 - var diff = contentLength - this.contentLength;
140 - this.contentLength = contentLength;
141 - if ( this.parent ) {
142 - this.parent.adjustContentLength( diff );
143 - }
 147+es.DocumentModelNode.prototype.getParent = function() {
 148+ return this.parent;
144149 };
145150
146151 /**
147 - * Adjust the content length.
 152+ * Gets the root node in the tree this node is currently attached to.
148153 *
149154 * @method
150 - * @param {Integer} adjustment Amount to adjust content length by
151 - * @throws Invalid adjustment error if resulting length is less than 0
 155+ * @returns {es.DocumentModelNode} Root node
152156 */
153 -es.DocumentModelNode.prototype.adjustContentLength = function( adjustment, quiet ) {
154 - this.contentLength += adjustment;
155 - // Make sure the adjustment was sane
156 - if ( this.contentLength < 0 ) {
157 - // Reverse the adjustment
158 - this.contentLength -= adjustment;
159 - // Complain about it
160 - throw 'Invalid adjustment error. Content length can not be less than 0.';
161 - }
162 - if ( this.parent ) {
163 - this.parent.adjustContentLength( adjustment, true );
164 - }
165 - if ( !quiet ) {
166 - this.emit( 'update' );
167 - }
 157+es.DocumentModelNode.prototype.getRoot = function() {
 158+ return this.root;
168159 };
169160
170161 /**
171 - * Gets the content length.
 162+ * Sets the root node to this and all of it's children.
172163 *
 164+ * This method is overridden by nodes with children.
 165+ *
173166 * @method
174 - * @returns {Integer} Length of content
 167+ * @param {es.DocumentModelNode} root Node to use as root
175168 */
176 -es.DocumentModelNode.prototype.getContentLength = function() {
177 - return this.contentLength;
 169+es.DocumentModelNode.prototype.setRoot = function( root ) {
 170+ this.root = root;
178171 };
179172
180173 /**
181 - * Gets the element length.
 174+ * Clears the root node from this and all of it's children.
182175 *
 176+ * This method is overridden by nodes with children.
 177+ *
183178 * @method
184 - * @returns {Integer} Length of content
185179 */
186 -es.DocumentModelNode.prototype.getElementLength = function() {
187 - return this.contentLength + 2;
 180+es.DocumentModelNode.prototype.clearRoot = function() {
 181+ this.root = null;
188182 };
189183
190184 /**
@@ -221,4 +215,4 @@
222216
223217 /* Inheritance */
224218
225 -es.extendClass( es.DocumentModelNode, es.EventEmitter );
 219+es.extendClass( es.DocumentModelNode, es.DocumentNode );
Index: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelLeafNode.js
@@ -7,14 +7,15 @@
88 * @extends {es.DocumentModelNode}
99 * @extends {es.DocumentNode}
1010 * @param {String} type Symbolic name of node type
11 - * @param {Integer} length Length of content data in document
 11+ * @param {Object} element Element object in document data
 12+ * @param {Integer} [length] Length of content data in document
1213 */
1314 es.DocumentModelLeafNode = function( type, element, length ) {
1415 // Inheritance
1516 es.DocumentModelNode.call( this, type, element, length );
1617
1718 // Properties
18 - this.contentLength = length;
 19+ this.contentLength = length || 0;
1920 };
2021
2122 /* Methods */
@@ -22,10 +23,10 @@
2324 /**
2425 * Gets a plain object representation of the document's data.
2526 *
26 - * The resulting object is compatible with es.DocumentModel.newFromPlainObject.
27 - *
2827 * @method
29 - * @returns {Object} Plain object representation
 28+ * @see {es.DocumentModelNode.getPlainObject}
 29+ * @see {es.DocumentModel.newFromPlainObject}
 30+ * @returns {Object} Plain object representation,
3031 */
3132 es.DocumentModelLeafNode.prototype.getPlainObject = function() {
3233 var obj = { 'type': this.type };

Status & tagging log