r94835 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94834‎ | r94835 | r94836 >
Date:22:40, 17 August 2011
Author:inez
Status:deferred
Tags:
Comment:
Refactoring for es.ListBlock
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.ListBlock.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.ListBlock.js
@@ -44,6 +44,8 @@
4545 this.list.renderContent( offset );
4646 };
4747
 48+/* Public Methods */
 49+
4850 /**
4951 * Gets the offset of a position.
5052 *
@@ -66,18 +68,19 @@
6769 position.top += blockOffset.top;
6870 position.left += blockOffset.left;
6971
70 - this.list.traverseItems( function( item, index ) {
71 - itemOffset = item.$content.offset();
72 - itemHeight = item.$content.height();
73 - if ( position.top >= itemOffset.top && position.top < itemOffset.top + itemHeight ) {
74 - position.top -= itemOffset.top;
75 - position.left -= itemOffset.left;
76 - offset += item.flow.getOffset( position );
77 - return false;
 72+ for ( var i = 0; i < this.list.items.length; i++ ) {
 73+ itemOffset = this.list.items[i].$content.offset();
 74+ if ( position.top >= itemOffset.top ) {
 75+ itemHeight = this.list.items[i].$content.height();
 76+ if ( position.top < itemOffset.top + itemHeight ) {
 77+ position.top -= itemOffset.top;
 78+ position.left -= itemOffset.left;
 79+ offset += this.list.items[i].flow.getOffset( position );
 80+ break;
 81+ }
7882 }
79 - offset += item.content.getLength() + 1;
80 - } );
81 -
 83+ offset += this.list.items[i].content.getLength() + 1;
 84+ }
8285 return offset;
8386 };
8487
@@ -101,35 +104,64 @@
102105 return position;
103106 };
104107
 108+/**
 109+ * Gets the flow line index within specific offset.
 110+ *
 111+ * @method
 112+ * @param offset {Integer} Offset
 113+ * @returns {Integer} Line index
 114+ */
105115 es.ListBlock.prototype.getLineIndex = function( offset ) {
106 - var globalOffset = 0,
107 - lineIndex = 0,
108 - itemLength;
 116+ var itemLength,
 117+ globalOffset = 0,
 118+ lineIndex = 0;
109119
110 - this.list.traverseItems( function( item, index ) {
111 - itemLength = item.content.getLength();
 120+ for ( var i = 0; i < this.list.items.length; i++ ) {
 121+ itemLength = this.list.items[i].content.getLength();
112122 if ( offset >= globalOffset && offset <= globalOffset + itemLength ) {
113 - lineIndex += item.flow.getLineIndex( offset - globalOffset );
114 - return false;
 123+ lineIndex += this.list.items[i].flow.getLineIndex( offset - globalOffset );
 124+ break;
115125 }
116126 globalOffset += itemLength + 1;
117 - lineIndex += item.flow.lines.length;
118 - } );
119 -
 127+ lineIndex += this.list.items[i].flow.lines.length; // TODO: add method getLineCount() to es.Flow
 128+ }
120129 return lineIndex;
121130 };
122131
123132 /**
 133+ * Gets a location from an offset.
 134+ *
 135+ * @method
 136+ * @param offset {Integer} Offset to get location for
 137+ * @returns {Object} Location object with item and offset, where offset is local to item
 138+ */
 139+es.ListBlock.prototype.getLocationFromOffset = function( offset ) {
 140+ var itemLength,
 141+ globalOffset = 0;
 142+ for ( var i = 0; i < this.list.items.length; i++ ) {
 143+ itemLength = this.list.items[i].content.getLength();
 144+ if ( offset >= globalOffset && offset <= globalOffset + itemLength ) {
 145+ return {
 146+ 'item' : this.list.items[i],
 147+ 'offset' : offset - globalOffset
 148+ }
 149+ }
 150+ globalOffset += itemLength + 1;
 151+ }
 152+ throw 'Offset is out of block range';
 153+};
 154+
 155+/**
124156 * Gets the length of all block content.
125157 *
126158 * @method
127159 * @returns {Integer} Length of content
128160 */
129 -es.ListBlock.prototype.getLength = function( ) {
 161+es.ListBlock.prototype.getLength = function() {
130162 var length = 0;
131 - this.list.traverseItems( function( item, index ) {
132 - length += item.content.getLength() + 1;
133 - } );
 163+ for ( var i = 0; i < this.list.items.length; i++ ) {
 164+ length += this.list.items[i].content.getLength() + 1;
 165+ }
134166 return length === 0 ? 0 : length - 1;
135167 };
136168
@@ -149,33 +181,6 @@
150182 return "";
151183 };
152184
153 -/**
154 - * Gets a location from an offset.
155 - *
156 - * @method
157 - * @param offset {Integer} Offset to get location for
158 - * @returns {Object} Location object with item and offset, where offset is local to item
159 - */
160 -es.ListBlock.prototype.getLocationFromOffset = function( offset ) {
161 - var globalOffset = 0,
162 - itemLength,
163 - location;
164 -
165 - this.list.traverseItems( function( item, index ) {
166 - itemLength = item.content.getLength();
167 - if ( offset >= globalOffset && offset <= globalOffset + itemLength ) {
168 - location = {
169 - 'item' : item,
170 - 'offset' : offset - globalOffset
171 - };
172 - return false;
173 - }
174 - globalOffset += itemLength + 1;
175 - } );
176 -
177 - return location;
178 -};
179 -
180185 /* Registration */
181186
182187 /**

Status & tagging log