r94400 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94399‎ | r94400 | r94401 >
Date:00:24, 13 August 2011
Author:inez
Status:deferred
Tags:
Comment:
Implement and use nice traverseItems function instead of repeating the same loop over and over
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.ListBlock.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.ListBlockList.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.ListBlockList.js
@@ -14,6 +14,14 @@
1515 }
1616 };
1717
 18+es.ListBlockList.prototype.traverseItems = function( callback ) {
 19+ for( var i = 0; i < this.items.length; i++ ) {
 20+ if ( callback( this.items[i], i ) === false ) {
 21+ return false;
 22+ }
 23+ }
 24+};
 25+
1826 es.ListBlockList.flattenList = function( wikidomList, items, level ) {
1927 for ( var i = 0; i < wikidomList.items.length; i++ ) {
2028 items.push(
Index: trunk/parsers/wikidom/lib/es/es.ListBlock.js
@@ -57,28 +57,30 @@
5858 } else if ( position.top >= this.$.height() ) {
5959 return this.getLength();
6060 }
61 -
62 - var globalOffset = 0,
63 - i,
 61+
 62+ var offset = 0,
6463 itemOffset,
6564 itemHeight,
6665 blockOffset = this.$.offset();
6766
6867 position.top += blockOffset.top;
6968 position.left += blockOffset.left;
70 -
71 - for( i = 0; i < this.list.items.length; i++ ) {
72 - itemOffset = this.list.items[i].$content.offset();
73 - itemHeight = this.list.items[i].$content.height();
74 -
 69+
 70+ this.list.traverseItems( function( item, index ) {
 71+ itemOffset = item.$content.offset();
 72+ itemHeight = item.$content.height();
 73+
7574 if ( position.top >= itemOffset.top && position.top < itemOffset.top + itemHeight ) {
7675 position.top -= itemOffset.top;
7776 position.left -= itemOffset.left;
78 - return globalOffset + this.list.items[i].flow.getOffset( position );
 77+ offset += item.flow.getOffset( position );
 78+ return false;
7979 }
8080
81 - globalOffset += this.list.items[i].content.getLength() + 1;
82 - }
 81+ offset += item.content.getLength() + 1;
 82+ } );
 83+
 84+ return offset;
8385 };
8486
8587 /**
@@ -90,28 +92,25 @@
9193 */
9294 es.ListBlock.prototype.getPosition = function( offset ) {
9395 var globalOffset = 0,
94 - i,
9596 itemLength,
9697 position,
97 - contentOffset,
9898 blockOffset = this.$.offset();
9999
100 - for( i = 0; i < this.list.items.length; i++ ) {
101 - itemLength = this.list.items[i].content.getLength();
 100+ this.list.traverseItems( function( item, index ) {
 101+ itemLength = item.content.getLength();
102102 if ( offset >= globalOffset && offset < globalOffset + itemLength ) {
103 - position = this.list.items[i].flow.getPosition( offset - globalOffset );
104 - contentOffset = this.list.items[i].$content.offset();
105 -
 103+ position = item.flow.getPosition( offset - globalOffset );
 104+ contentOffset = item.$content.offset();
106105 position.top += contentOffset.top - blockOffset.top;
107106 position.left += contentOffset.left - blockOffset.left;
108107 position.bottom += contentOffset.top - blockOffset.top;
109 -
110 - position.line = i;
111 -
112 - return position;
 108+ position.line = index;
 109+ return false;
113110 }
114111 globalOffset += itemLength + 1;
115 - }
 112+ } );
 113+
 114+ return position;
116115 };
117116
118117 /* Registration */

Status & tagging log