Index: trunk/parsers/wikidom/lib/es/es.ListBlock.js |
— | — | @@ -44,6 +44,8 @@ |
45 | 45 | this.list.renderContent( offset ); |
46 | 46 | }; |
47 | 47 | |
| 48 | +/* Public Methods */ |
| 49 | + |
48 | 50 | /** |
49 | 51 | * Gets the offset of a position. |
50 | 52 | * |
— | — | @@ -66,18 +68,19 @@ |
67 | 69 | position.top += blockOffset.top; |
68 | 70 | position.left += blockOffset.left; |
69 | 71 | |
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 | + } |
78 | 82 | } |
79 | | - offset += item.content.getLength() + 1; |
80 | | - } ); |
81 | | - |
| 83 | + offset += this.list.items[i].content.getLength() + 1; |
| 84 | + } |
82 | 85 | return offset; |
83 | 86 | }; |
84 | 87 | |
— | — | @@ -101,35 +104,64 @@ |
102 | 105 | return position; |
103 | 106 | }; |
104 | 107 | |
| 108 | +/** |
| 109 | + * Gets the flow line index within specific offset. |
| 110 | + * |
| 111 | + * @method |
| 112 | + * @param offset {Integer} Offset |
| 113 | + * @returns {Integer} Line index |
| 114 | + */ |
105 | 115 | es.ListBlock.prototype.getLineIndex = function( offset ) { |
106 | | - var globalOffset = 0, |
107 | | - lineIndex = 0, |
108 | | - itemLength; |
| 116 | + var itemLength, |
| 117 | + globalOffset = 0, |
| 118 | + lineIndex = 0; |
109 | 119 | |
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(); |
112 | 122 | 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; |
115 | 125 | } |
116 | 126 | 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 | + } |
120 | 129 | return lineIndex; |
121 | 130 | }; |
122 | 131 | |
123 | 132 | /** |
| 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 | +/** |
124 | 156 | * Gets the length of all block content. |
125 | 157 | * |
126 | 158 | * @method |
127 | 159 | * @returns {Integer} Length of content |
128 | 160 | */ |
129 | | -es.ListBlock.prototype.getLength = function( ) { |
| 161 | +es.ListBlock.prototype.getLength = function() { |
130 | 162 | 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 | + } |
134 | 166 | return length === 0 ? 0 : length - 1; |
135 | 167 | }; |
136 | 168 | |
— | — | @@ -149,33 +181,6 @@ |
150 | 182 | return ""; |
151 | 183 | }; |
152 | 184 | |
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 | | - |
180 | 185 | /* Registration */ |
181 | 186 | |
182 | 187 | /** |