Index: trunk/extensions/VisualEditor/tests/index.html |
— | — | @@ -22,6 +22,7 @@ |
23 | 23 | |
24 | 24 | <!-- Bases --> |
25 | 25 | <script src="../modules/es/bases/es.EventEmitter.js"></script> |
| 26 | + <script src="../modules/es/bases/es.DocumentNode.js"></script> |
26 | 27 | <script src="../modules/es/bases/es.DocumentBranchNode.js"></script> |
27 | 28 | <script src="../modules/es/bases/es.DocumentModelNode.js"></script> |
28 | 29 | <script src="../modules/es/bases/es.DocumentModelBranchNode.js"></script> |
Index: trunk/extensions/VisualEditor/demo/index.html |
— | — | @@ -68,6 +68,7 @@ |
69 | 69 | |
70 | 70 | <!-- Bases --> |
71 | 71 | <script src="../modules/es/bases/es.EventEmitter.js"></script> |
| 72 | + <script src="../modules/es/bases/es.DocumentNode.js"></script> |
72 | 73 | <script src="../modules/es/bases/es.DocumentBranchNode.js"></script> |
73 | 74 | <script src="../modules/es/bases/es.DocumentModelNode.js"></script> |
74 | 75 | <script src="../modules/es/bases/es.DocumentModelBranchNode.js"></script> |
Index: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentViewNode.js |
— | — | @@ -1,55 +1,45 @@ |
2 | 2 | /** |
3 | 3 | * Creates an es.DocumentViewNode object. |
4 | 4 | * |
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 | | - * |
12 | 5 | * @class |
13 | 6 | * @abstract |
14 | 7 | * @constructor |
15 | 8 | * @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 |
20 | 11 | */ |
21 | 12 | es.DocumentViewNode = function( model, $element ) { |
22 | 13 | // Inheritance |
23 | | - es.EventEmitter.call( this ); |
| 14 | + es.DocumentNode.call( this ); |
24 | 15 | |
25 | 16 | // Properties |
26 | 17 | this.model = model; |
| 18 | + this.parent = null; |
27 | 19 | 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 | | - }; |
34 | 20 | }; |
35 | 21 | |
| 22 | +/* Methods */ |
| 23 | + |
36 | 24 | /** |
37 | | - * Gets a reference to the model this node observes. |
| 25 | + * Gets the length of the element in the model. |
38 | 26 | * |
39 | 27 | * @method |
40 | | - * @returns {es.ModelNode} Reference to the model this node observes |
| 28 | + * @see {es.DocumentNode.prototype.getElementLength} |
| 29 | + * @returns {Integer} Length of content |
41 | 30 | */ |
42 | | -es.DocumentViewNode.prototype.getModel = function() { |
43 | | - return this.model; |
| 31 | +es.DocumentViewNode.prototype.getElementLength = function() { |
| 32 | + return this.model.getElementLength(); |
44 | 33 | }; |
45 | 34 | |
46 | 35 | /** |
47 | | - * Gets a reference to this node's parent. |
| 36 | + * Gets the length of the content in the model. |
48 | 37 | * |
49 | 38 | * @method |
50 | | - * @returns {es.DocumentViewNode} Reference to this node's parent |
| 39 | + * @see {es.DocumentNode.prototype.getContentLength} |
| 40 | + * @returns {Integer} Length of content |
51 | 41 | */ |
52 | | -es.DocumentViewNode.prototype.getParent = function() { |
53 | | - return this.parent; |
| 42 | +es.DocumentViewNode.prototype.getContentLength = function() { |
| 43 | + return this.model.getContentLength(); |
54 | 44 | }; |
55 | 45 | |
56 | 46 | /** |
— | — | @@ -77,25 +67,25 @@ |
78 | 68 | }; |
79 | 69 | |
80 | 70 | /** |
81 | | - * Gets the length of the element in the model. |
| 71 | + * Gets a reference to this node's parent. |
82 | 72 | * |
83 | 73 | * @method |
84 | | - * @returns {Integer} Length of content |
| 74 | + * @returns {es.DocumentViewNode} Reference to this node's parent |
85 | 75 | */ |
86 | | -es.DocumentViewNode.prototype.getElementLength = function() { |
87 | | - return this.model.getElementLength(); |
| 76 | +es.DocumentViewNode.prototype.getParent = function() { |
| 77 | + return this.parent; |
88 | 78 | }; |
89 | 79 | |
90 | 80 | /** |
91 | | - * Gets the length of the content in the model. |
| 81 | + * Gets a reference to the model this node observes. |
92 | 82 | * |
93 | 83 | * @method |
94 | | - * @returns {Integer} Length of content |
| 84 | + * @returns {es.DocumentModelNode} Reference to the model this node observes |
95 | 85 | */ |
96 | | -es.DocumentViewNode.prototype.getContentLength = function() { |
97 | | - return this.model.getContentLength(); |
| 86 | +es.DocumentViewNode.prototype.getModel = function() { |
| 87 | + return this.model; |
98 | 88 | }; |
99 | 89 | |
100 | 90 | /* Inheritance */ |
101 | 91 | |
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 @@ |
5 | 5 | * @class |
6 | 6 | * @abstract |
7 | 7 | * @constructor |
8 | | - * @param {es.DocumentNode[]} nodes List of document nodes to initially add |
| 8 | + * @param {es.DocumentNode[]} nodes List of document nodes to add |
9 | 9 | */ |
10 | 10 | es.DocumentBranchNode = function( nodes ) { |
11 | 11 | this.children = es.isArray( nodes ) ? nodes : []; |
Index: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelBranchNode.js |
— | — | @@ -7,7 +7,8 @@ |
8 | 8 | * @extends {es.DocumentModelNode} |
9 | 9 | * @extends {es.DocumentBranchNode} |
10 | 10 | * @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 |
12 | 13 | */ |
13 | 14 | es.DocumentModelBranchNode = function( type, element, contents ) { |
14 | 15 | // Inheritance |
— | — | @@ -27,9 +28,9 @@ |
28 | 29 | /** |
29 | 30 | * Gets a plain object representation of the document's data. |
30 | 31 | * |
31 | | - * The resulting object is compatible with es.DocumentModel.newFromPlainObject. |
32 | | - * |
33 | 32 | * @method |
| 33 | + * @see {es.DocumentModelNode.getPlainObject} |
| 34 | + * @see {es.DocumentModel.newFromPlainObject} |
34 | 35 | * @returns {Object} Plain object representation |
35 | 36 | */ |
36 | 37 | es.DocumentModelBranchNode.prototype.getPlainObject = function() { |
— | — | @@ -214,6 +215,7 @@ |
215 | 216 | * Sets the root node to this and all of it's children. |
216 | 217 | * |
217 | 218 | * @method |
| 219 | + * @see {es.DocumentModelNode.prototype.setRoot} |
218 | 220 | * @param {es.DocumentModelNode} root Node to use as root |
219 | 221 | */ |
220 | 222 | es.DocumentModelBranchNode.prototype.setRoot = function( root ) { |
— | — | @@ -227,6 +229,7 @@ |
228 | 230 | * Clears the root node from this and all of it's children. |
229 | 231 | * |
230 | 232 | * @method |
| 233 | + * @see {es.DocumentModelNode.prototype.clearRoot} |
231 | 234 | */ |
232 | 235 | es.DocumentModelBranchNode.prototype.clearRoot = function() { |
233 | 236 | this.root = null; |
Index: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelNode.js |
— | — | @@ -6,25 +6,19 @@ |
7 | 7 | * @constructor |
8 | 8 | * @extends {es.EventEmitter} |
9 | 9 | * @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 |
12 | 12 | */ |
13 | 13 | es.DocumentModelNode = function( type, element, length ) { |
14 | 14 | // Inheritance |
15 | | - es.EventEmitter.call( this ); |
| 15 | + es.DocumentNode.call( this ); |
16 | 16 | |
17 | | - // Reusable function for passing update events upstream |
18 | | - var _this = this; |
19 | | - this.emitUpdate = function() { |
20 | | - _this.emit( 'update' ); |
21 | | - }; |
22 | | - |
23 | 17 | // Properties |
24 | 18 | this.type = type; |
25 | 19 | this.parent = null; |
26 | 20 | this.root = this; |
27 | 21 | this.element = element || null; |
28 | | - this.contentLength = length; |
| 22 | + this.contentLength = length || 0; |
29 | 23 | }; |
30 | 24 | |
31 | 25 | /* Abstract Methods */ |
— | — | @@ -43,8 +37,6 @@ |
44 | 38 | /** |
45 | 39 | * Gets a plain object representation of the document's data. |
46 | 40 | * |
47 | | - * The resulting object is compatible with es.DocumentModel.newFromPlainObject. |
48 | | - * |
49 | 41 | * @method |
50 | 42 | * @returns {Object} Plain object representation |
51 | 43 | */ |
— | — | @@ -55,46 +47,67 @@ |
56 | 48 | /* Methods */ |
57 | 49 | |
58 | 50 | /** |
59 | | - * Gets a reference to this node's parent. |
| 51 | + * Gets the content length. |
60 | 52 | * |
61 | 53 | * @method |
62 | | - * @returns {es.DocumentModelNode} Reference to this node's parent |
| 54 | + * @see {es.DocumentNode.prototype.getContentLength} |
| 55 | + * @returns {Integer} Length of content |
63 | 56 | */ |
64 | | -es.DocumentModelNode.prototype.getParent = function() { |
65 | | - return this.parent; |
| 57 | +es.DocumentModelNode.prototype.getContentLength = function() { |
| 58 | + return this.contentLength; |
66 | 59 | }; |
67 | 60 | |
68 | 61 | /** |
69 | | - * Gets the root node in the tree this node is currently attached to. |
| 62 | + * Gets the element length. |
70 | 63 | * |
71 | 64 | * @method |
72 | | - * @returns {es.DocumentModelNode} Root node |
| 65 | + * @see {es.DocumentNode.prototype.getElementLength} |
| 66 | + * @returns {Integer} Length of content |
73 | 67 | */ |
74 | | -es.DocumentModelNode.prototype.getRoot = function() { |
75 | | - return this.root; |
| 68 | +es.DocumentModelNode.prototype.getElementLength = function() { |
| 69 | + return this.contentLength + 2; |
76 | 70 | }; |
77 | 71 | |
78 | 72 | /** |
79 | | - * Sets the root node to this and all of it's children. |
| 73 | + * Sets the content length. |
80 | 74 | * |
81 | | - * This method is overridden by nodes with children. |
82 | | - * |
83 | 75 | * @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 |
85 | 78 | */ |
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 | + } |
88 | 88 | }; |
89 | 89 | |
90 | 90 | /** |
91 | | - * Clears the root node from this and all of it's children. |
| 91 | + * Adjust the content length. |
92 | 92 | * |
93 | | - * This method is overridden by nodes with children. |
94 | | - * |
95 | 93 | * @method |
| 94 | + * @param {Integer} adjustment Amount to adjust content length by |
| 95 | + * @throws Invalid adjustment error if resulting length is less than 0 |
96 | 96 | */ |
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 | + } |
99 | 112 | }; |
100 | 113 | |
101 | 114 | /** |
— | — | @@ -125,65 +138,46 @@ |
126 | 139 | }; |
127 | 140 | |
128 | 141 | /** |
129 | | - * Sets the content length. |
| 142 | + * Gets a reference to this node's parent. |
130 | 143 | * |
131 | 144 | * @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 |
134 | 146 | */ |
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; |
144 | 149 | }; |
145 | 150 | |
146 | 151 | /** |
147 | | - * Adjust the content length. |
| 152 | + * Gets the root node in the tree this node is currently attached to. |
148 | 153 | * |
149 | 154 | * @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 |
152 | 156 | */ |
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; |
168 | 159 | }; |
169 | 160 | |
170 | 161 | /** |
171 | | - * Gets the content length. |
| 162 | + * Sets the root node to this and all of it's children. |
172 | 163 | * |
| 164 | + * This method is overridden by nodes with children. |
| 165 | + * |
173 | 166 | * @method |
174 | | - * @returns {Integer} Length of content |
| 167 | + * @param {es.DocumentModelNode} root Node to use as root |
175 | 168 | */ |
176 | | -es.DocumentModelNode.prototype.getContentLength = function() { |
177 | | - return this.contentLength; |
| 169 | +es.DocumentModelNode.prototype.setRoot = function( root ) { |
| 170 | + this.root = root; |
178 | 171 | }; |
179 | 172 | |
180 | 173 | /** |
181 | | - * Gets the element length. |
| 174 | + * Clears the root node from this and all of it's children. |
182 | 175 | * |
| 176 | + * This method is overridden by nodes with children. |
| 177 | + * |
183 | 178 | * @method |
184 | | - * @returns {Integer} Length of content |
185 | 179 | */ |
186 | | -es.DocumentModelNode.prototype.getElementLength = function() { |
187 | | - return this.contentLength + 2; |
| 180 | +es.DocumentModelNode.prototype.clearRoot = function() { |
| 181 | + this.root = null; |
188 | 182 | }; |
189 | 183 | |
190 | 184 | /** |
— | — | @@ -221,4 +215,4 @@ |
222 | 216 | |
223 | 217 | /* Inheritance */ |
224 | 218 | |
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 @@ |
8 | 8 | * @extends {es.DocumentModelNode} |
9 | 9 | * @extends {es.DocumentNode} |
10 | 10 | * @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 |
12 | 13 | */ |
13 | 14 | es.DocumentModelLeafNode = function( type, element, length ) { |
14 | 15 | // Inheritance |
15 | 16 | es.DocumentModelNode.call( this, type, element, length ); |
16 | 17 | |
17 | 18 | // Properties |
18 | | - this.contentLength = length; |
| 19 | + this.contentLength = length || 0; |
19 | 20 | }; |
20 | 21 | |
21 | 22 | /* Methods */ |
— | — | @@ -22,10 +23,10 @@ |
23 | 24 | /** |
24 | 25 | * Gets a plain object representation of the document's data. |
25 | 26 | * |
26 | | - * The resulting object is compatible with es.DocumentModel.newFromPlainObject. |
27 | | - * |
28 | 27 | * @method |
29 | | - * @returns {Object} Plain object representation |
| 28 | + * @see {es.DocumentModelNode.getPlainObject} |
| 29 | + * @see {es.DocumentModel.newFromPlainObject} |
| 30 | + * @returns {Object} Plain object representation, |
30 | 31 | */ |
31 | 32 | es.DocumentModelLeafNode.prototype.getPlainObject = function() { |
32 | 33 | var obj = { 'type': this.type }; |