r100879 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100878‎ | r100879 | r100880 >
Date:21:35, 26 October 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Moved aggregate array functionality to a shared class, es.DocumentNode
Modified paths:
  • /trunk/parsers/wikidom/lib/hype/bases/es.DocumentModelNode.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/bases/es.DocumentNode.js (added) (history)
  • /trunk/parsers/wikidom/lib/hype/bases/es.DocumentViewBranchNode.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/bases/es.DocumentViewLeafNode.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/views/es.ContentView.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/hype/bases/es.DocumentNode.js
@@ -0,0 +1,126 @@
 2+/**
 3+ * Creates an es.DocumentNode object.
 4+ */
 5+es.DocumentNode = function( nodes ) {
 6+ if ( nodes === undefined ) {
 7+ nodes = [];
 8+ }
 9+ $.extend( nodes, this );
 10+ return nodes;
 11+};
 12+
 13+/**
 14+ * Gets the range within this node that a given child node covers.
 15+ *
 16+ * @method
 17+ * @param {es.ModelNode} node
 18+ */
 19+es.DocumentNode.prototype.getRangeFromNode = function( node ) {
 20+ if ( this.length ) {
 21+ var i = 0,
 22+ length = this.length,
 23+ left = 0;
 24+ while ( i < length ) {
 25+ if ( this[i] === node ) {
 26+ return new es.Range( left, left + this[i].getElementLength() );
 27+ }
 28+ left += this[i].getElementLength() + 1;
 29+ i++;
 30+ }
 31+ }
 32+ return null;
 33+};
 34+
 35+/**
 36+ * Gets the first offset within this node of a given child node.
 37+ *
 38+ * @method
 39+ * @param {es.ModelNode} node
 40+ */
 41+es.DocumentNode.prototype.getOffsetFromNode = function( node ) {
 42+ if ( this.length ) {
 43+ var offset = 0;
 44+ for( var i = 0; i < this.length; i++ ) {
 45+ if ( this[i] === node ) {
 46+ return offset;
 47+ }
 48+ offset += this[i].getElementLength() + 1;
 49+ }
 50+ }
 51+ return null;
 52+};
 53+
 54+/**
 55+ * Gets the node which a given offset is within.
 56+ *
 57+ * @method
 58+ * @param {Integer} offset
 59+ */
 60+es.DocumentNode.prototype.getNodeFromOffset = function( offset ) {
 61+ if ( this.length ) {
 62+ var i = 0,
 63+ length = this.length,
 64+ left = 0,
 65+ right;
 66+ while ( i < length ) {
 67+ right = left + this[i].getElementLength() + 1;
 68+ if ( offset >= left && offset < right ) {
 69+ return this[i];
 70+ }
 71+ left = right;
 72+ i++;
 73+ }
 74+ }
 75+ return null;
 76+};
 77+
 78+/**
 79+ * Gets a list of nodes and their sub-ranges which are covered by a given range.
 80+ *
 81+ * @method
 82+ * @param {es.Range} range Range to select nodes within
 83+ * @param {Boolean} [off] Whether to include a list of nodes that are not covered by the range
 84+ * @returns {Object} Object with 'on' and 'off' properties, 'on' being a list of objects with 'node'
 85+ * and 'range' properties describing nodes which are covered by the range and the range within the
 86+ * node that is covered, and 'off' being a list of nodes that are not covered by the range
 87+ */
 88+es.DocumentNode.prototype.selectNodes = function( range, off ) {
 89+ range.normalize();
 90+ var result = { 'on': [], 'off': [] };
 91+ for ( var i = 0, length = this.length, left = 0, right; i < length; i++ ) {
 92+ right = left + this[i].getElementLength() + 1;
 93+ if ( range.start >= left && range.start < right ) {
 94+ if ( range.end < right ) {
 95+ result.on.push( {
 96+ 'node': this[i],
 97+ 'range': new es.Range( range.start - left, range.end - left )
 98+ } );
 99+ if ( !off ) {
 100+ break;
 101+ }
 102+ } else {
 103+ result.on.push( {
 104+ 'node': this[i],
 105+ 'range': new es.Range( range.start - left, right - left - 1 )
 106+ } );
 107+ }
 108+ } else if ( range.end >= left && range.end < right ) {
 109+ result.on.push( {
 110+ 'node': this[i],
 111+ 'range': new es.Range( 0, range.end - left )
 112+ } );
 113+ if ( !off ) {
 114+ break;
 115+ }
 116+ } else if ( left >= range.start && right <= range.end ) {
 117+ result.on.push( {
 118+ 'node': this[i],
 119+ 'range': new es.Range( 0, right - left - 1 )
 120+ } );
 121+ } else if( off ) {
 122+ result.off.push( this[i] );
 123+ }
 124+ left = right;
 125+ }
 126+ return result;
 127+};
Index: trunk/parsers/wikidom/lib/hype/bases/es.DocumentModelNode.js
@@ -12,7 +12,7 @@
1313 */
1414 es.DocumentModelNode = function( element, contents ) {
1515 // Extension
16 - var node = $.extend( new es.ModelNode(), this );
 16+ var node = $.extend( new es.DocumentNode( new es.ModelNode() ), this );
1717
1818 // Observe add and remove operations to keep lengths up to date
1919 node.addListenerMethods( node, {
@@ -181,119 +181,3 @@
182182 }
183183 return text;
184184 };
185 -
186 -/**
187 - * Gets the range within this node that a given child node covers.
188 - *
189 - * @method
190 - * @param {es.ModelNode} node
191 - */
192 -es.DocumentModelNode.prototype.getRangeFromNode = function( node ) {
193 - if ( this.length ) {
194 - var i = 0,
195 - length = this.length,
196 - left = 0;
197 - while ( i < length ) {
198 - if ( this[i] === node ) {
199 - return new es.Range( left, left + this[i].getElementLength() );
200 - }
201 - left += this[i].getElementLength() + 1;
202 - i++;
203 - }
204 - }
205 - return null;
206 -};
207 -
208 -/**
209 - * Gets the first offset within this node of a given child node.
210 - *
211 - * @method
212 - * @param {es.ModelNode} node
213 - */
214 -es.DocumentModelNode.prototype.getOffsetFromNode = function( node ) {
215 - if ( this.length ) {
216 - var offset = 0;
217 - for( var i = 0; i < this.length; i++ ) {
218 - if ( this[i] === node ) {
219 - return offset;
220 - }
221 - offset += this[i].getElementLength() + 1;
222 - }
223 - }
224 - return null;
225 -};
226 -
227 -/**
228 - * Gets the node which a given offset is within.
229 - *
230 - * @method
231 - * @param {Integer} offset
232 - */
233 -es.DocumentModelNode.prototype.getNodeFromOffset = function( offset ) {
234 - if ( this.length ) {
235 - var i = 0,
236 - length = this.length,
237 - left = 0,
238 - right;
239 - while ( i < length ) {
240 - right = left + this[i].getElementLength() + 1;
241 - if ( offset >= left && offset < right ) {
242 - return this[i];
243 - }
244 - left = right;
245 - i++;
246 - }
247 - }
248 - return null;
249 -};
250 -
251 -/**
252 - * Gets a list of nodes and their sub-ranges which are covered by a given range.
253 - *
254 - * @method
255 - * @param {es.Range} range Range to select nodes within
256 - * @param {Boolean} [off] Whether to include a list of nodes that are not covered by the range
257 - * @returns {Object} Object with 'on' and 'off' properties, 'on' being a list of objects with 'node'
258 - * and 'range' properties describing nodes which are covered by the range and the range within the
259 - * node that is covered, and 'off' being a list of nodes that are not covered by the range
260 - */
261 -es.DocumentModelNode.prototype.selectNodes = function( range, off ) {
262 - range.normalize();
263 - var result = { 'on': [], 'off': [] };
264 - for ( var i = 0, length = this.length, left = 0, right; i < length; i++ ) {
265 - right = left + this[i].getElementLength() + 1;
266 - if ( range.start >= left && range.start < right ) {
267 - if ( range.end < right ) {
268 - result.on.push( {
269 - 'node': this[i],
270 - 'range': new es.Range( range.start - left, range.end - left )
271 - } );
272 - if ( !off ) {
273 - break;
274 - }
275 - } else {
276 - result.on.push( {
277 - 'node': this[i],
278 - 'range': new es.Range( range.start - left, right - left - 1 )
279 - } );
280 - }
281 - } else if ( range.end >= left && range.end < right ) {
282 - result.on.push( {
283 - 'node': this[i],
284 - 'range': new es.Range( 0, range.end - left )
285 - } );
286 - if ( !off ) {
287 - break;
288 - }
289 - } else if ( left >= range.start && right <= range.end ) {
290 - result.on.push( {
291 - 'node': this[i],
292 - 'range': new es.Range( 0, right - left - 1 )
293 - } );
294 - } else if( off ) {
295 - result.off.push( this[i] );
296 - }
297 - left = right;
298 - }
299 - return result;
300 -};
Index: trunk/parsers/wikidom/lib/hype/bases/es.DocumentViewLeafNode.js
@@ -9,7 +9,7 @@
1010 */
1111 es.DocumentViewLeafNode = function( model, $element ) {
1212 // Extension
13 - var node = $.extend( new es.ViewNode( model, $element ), this );
 13+ var node = $.extend( new es.DocumentNode( new es.ViewNode( model, $element ) ), this );
1414
1515 // Content
1616 node.contentView = new es.ContentView( node.$, model );
@@ -68,3 +68,13 @@
6969 es.DocumentViewLeafNode.prototype.getRenderedPositionFromOffset = function( offset ) {
7070 return this.contentView.getRenderedPositionFromOffset( offset );
7171 };
 72+
 73+/**
 74+ * Gets the length of the content in the model.
 75+ *
 76+ * @method
 77+ * @returns {Integer} Length of content
 78+ */
 79+es.DocumentViewLeafNode.prototype.getElementLength = function() {
 80+ return this.model.getElementLength();
 81+};
Index: trunk/parsers/wikidom/lib/hype/bases/es.DocumentViewBranchNode.js
@@ -9,7 +9,7 @@
1010 */
1111 es.DocumentViewBranchNode = function( model, $element ) {
1212 // Extension
13 - return $.extend( new es.ViewNode( model, $element ), this );
 13+ return $.extend( new es.DocumentNode( new es.ViewNode( model, $element ) ), this );
1414 };
1515
1616 /* Methods */
@@ -88,3 +88,13 @@
8989 }
9090 return null;
9191 };
 92+
 93+/**
 94+ * Gets the length of the content in the model.
 95+ *
 96+ * @method
 97+ * @returns {Integer} Length of content
 98+ */
 99+es.DocumentViewBranchNode.prototype.getElementLength = function() {
 100+ return this.model.getElementLength();
 101+};
Index: trunk/parsers/wikidom/lib/hype/views/es.ContentView.js
@@ -24,7 +24,7 @@
2525 */
2626 es.ContentView = function( $container, model ) {
2727 this.$ = $container;
28 - this.model = model || new es.ContentModel();
 28+ this.model = model;
2929 this.boundaries = [];
3030 this.lines = [];
3131 this.width = null;

Status & tagging log