Index: trunk/parsers/wikidom/lib/synth/bases/es.AggregateArray.js |
— | — | @@ -27,6 +27,19 @@ |
28 | 28 | return null; |
29 | 29 | }; |
30 | 30 | |
| 31 | +es.AggregateArray.prototype.offsetOf = function( item ) { |
| 32 | + if ( this.length ) { |
| 33 | + var offset = 0; |
| 34 | + for( var i = 0; i < this.length; i++ ) { |
| 35 | + if ( this[i] === item ) { |
| 36 | + return offset; |
| 37 | + } |
| 38 | + offset += this[i].getLength() + 1; |
| 39 | + } |
| 40 | + } |
| 41 | + return null; |
| 42 | +}; |
| 43 | + |
31 | 44 | es.AggregateArray.prototype.rangeOf = function( item ) { |
32 | 45 | if ( this.length ) { |
33 | 46 | var i = 0, |
Index: trunk/parsers/wikidom/lib/synth/views/es.ListBlockView.js |
— | — | @@ -30,24 +30,16 @@ |
31 | 31 | return 0; |
32 | 32 | } |
33 | 33 | |
34 | | - var contentOffset, |
35 | | - itemHeight, |
36 | | - offset = 0; |
| 34 | + var listItemView = this.items[0]; |
37 | 35 | |
38 | 36 | for ( var i = 0; i < this.items.length; i++ ) { |
39 | | - contentOffset = this.items[i].$content.offset(); |
40 | | - if ( position.top >= contentOffset.top ) { |
41 | | - itemHeight = this.items[i].$.height(); |
42 | | - if ( position.top < contentOffset.top + itemHeight ) { |
43 | | - position.left -= contentOffset.left; |
44 | | - position.top -= contentOffset.top; |
45 | | - return offset + this.items[i].getContentOffset( position ); |
46 | | - } |
| 37 | + if ( this.items[i].$.offset().top >= position.top ) { |
| 38 | + break; |
47 | 39 | } |
48 | | - offset += this.items[i].getLength() + 1; |
| 40 | + listItemView = this.items[i]; |
49 | 41 | } |
50 | 42 | |
51 | | - throw 'Position coordinates are outside of the view.'; |
| 43 | + return listItemView.list.items.offsetOf( listItemView ) + listItemView.getContentOffset( position ); |
52 | 44 | }; |
53 | 45 | |
54 | 46 | /** |
Index: trunk/parsers/wikidom/lib/synth/views/es.SurfaceView.js |
— | — | @@ -238,13 +238,12 @@ |
239 | 239 | }; |
240 | 240 | |
241 | 241 | es.SurfaceView.prototype.onMouseDown = function( e ) { |
242 | | - var position = es.Position.newFromEventPagePosition( e ); |
243 | | - var offset = this.documentView.getOffsetFromPosition( position ); |
244 | | - this.from = offset; |
| 242 | + var mousePosition = es.Position.newFromEventPagePosition( e ); |
| 243 | + var contentOffset = this.documentView.getOffsetFromPosition( mousePosition ); |
| 244 | + |
| 245 | + this.from = contentOffset; |
245 | 246 | this.selecting = true; |
246 | 247 | |
247 | | - console.log(offset); |
248 | | - |
249 | 248 | if ( !this.$input.is(':focus') ) { |
250 | 249 | this.$input.focus().select(); |
251 | 250 | } |
— | — | @@ -254,10 +253,10 @@ |
255 | 254 | |
256 | 255 | es.SurfaceView.prototype.onMouseMove = function( e ) { |
257 | 256 | if (this.selecting ) { |
258 | | - var position = es.Position.newFromEventPagePosition( e ); |
259 | | - position.subtract( es.Position.newFromElementPagePosition( this.documentView.$ ) ); |
260 | | - var offset = this.documentView.getOffsetFromPosition( position ); |
261 | | - this.to = offset; |
| 257 | + var mousePosition = es.Position.newFromEventPagePosition( e ); |
| 258 | + var contentOffset = this.documentView.getOffsetFromPosition( mousePosition ); |
| 259 | + |
| 260 | + this.to = contentOffset; |
262 | 261 | this.documentView.drawSelection( new es.Range( this.from, this.to ) ); |
263 | 262 | } |
264 | 263 | // TODO: Respond to mouse move event, updating selection while painting |
Index: trunk/parsers/wikidom/lib/synth/views/es.ContentView.js |
— | — | @@ -281,6 +281,11 @@ |
282 | 282 | if ( this.model.getLength() === 0 ) { |
283 | 283 | return 0; |
284 | 284 | } |
| 285 | + |
| 286 | + // Localize position |
| 287 | + position.subtract( es.Position.newFromElementPagePosition( this.$ ) ); |
| 288 | + |
| 289 | + // |
285 | 290 | /* |
286 | 291 | * Line finding |
287 | 292 | * |
Index: trunk/parsers/wikidom/lib/synth/views/es.TableBlockRowView.js |
— | — | @@ -22,22 +22,20 @@ |
23 | 23 | * @returns {Integer} Offset nearest to position |
24 | 24 | */ |
25 | 25 | es.TableBlockRowView.prototype.getOffsetFromPosition = function( position ) { |
26 | | - var cellOffset, |
27 | | - itemWidth, |
28 | | - offset = 0; |
| 26 | + if ( this.items.length === 0 ) { |
| 27 | + return 0; |
| 28 | + } |
| 29 | + |
| 30 | + var cellView = this.items[0]; |
29 | 31 | |
30 | 32 | for ( var i = 0; i < this.items.length; i++ ) { |
31 | | - cellOffset = this.items[i].$.offset(); |
32 | | - if ( position.left >= cellOffset.left ) { |
33 | | - itemWidth = this.items[i].$.width(); |
34 | | - if ( position.left < cellOffset.left + itemWidth ) { |
35 | | - return offset + this.items[i].getOffsetFromPosition( position ); |
36 | | - } |
| 33 | + if ( this.items[i].$.offset().left >= position.left ) { |
| 34 | + break; |
37 | 35 | } |
38 | | - offset += this.items[i].getLength() + 1; |
| 36 | + cellView = this.items[i]; |
39 | 37 | } |
40 | 38 | |
41 | | - throw 'Position coordinates are outside of the view.'; |
| 39 | + return cellView.list.items.offsetOf( cellView ) + cellView.getOffsetFromPosition( position ); |
42 | 40 | }; |
43 | 41 | |
44 | 42 | /** |
Index: trunk/parsers/wikidom/lib/synth/views/es.DocumentView.js |
— | — | @@ -16,23 +16,17 @@ |
17 | 17 | if ( this.items.length === 0 ) { |
18 | 18 | return 0; |
19 | 19 | } |
20 | | - |
21 | | - var blockView = this.items[0], |
22 | | - blockLength = 0, |
23 | | - offset = 0; |
24 | 20 | |
| 21 | + var blockView = this.items[0]; |
| 22 | + |
25 | 23 | for ( var i = 0; i < this.items.length; i++ ) { |
26 | 24 | if ( this.items[i].$.offset().top >= position.top ) { |
27 | 25 | break; |
28 | 26 | } |
29 | 27 | blockView = this.items[i]; |
30 | | - blockLength = blockView.getLength(); |
31 | | - offset += blockLength + 1; |
32 | 28 | } |
33 | | - |
34 | | - offset -= blockLength + 1; |
35 | | - |
36 | | - return offset + blockView.getOffsetFromPosition( position ); |
| 29 | + |
| 30 | + return blockView.list.items.offsetOf( blockView ) + blockView.getOffsetFromPosition( position ); |
37 | 31 | }; |
38 | 32 | |
39 | 33 | /** |
Index: trunk/parsers/wikidom/lib/synth/views/es.ParagraphBlockView.js |
— | — | @@ -25,9 +25,6 @@ |
26 | 26 | * @returns {Integer} Offset nearest to position |
27 | 27 | */ |
28 | 28 | es.ParagraphBlockView.prototype.getOffsetFromPosition = function( position ) { |
29 | | - var blockPosition = this.$.offset(); |
30 | | - position.left -= blockPosition.left; |
31 | | - position.top -= blockPosition.top; |
32 | 29 | return this.contentView.getOffset( position ); |
33 | 30 | }; |
34 | 31 | |
Index: trunk/parsers/wikidom/lib/synth/views/es.TableBlockCellView.js |
— | — | @@ -21,22 +21,20 @@ |
22 | 22 | * @returns {Integer} Offset nearest to position |
23 | 23 | */ |
24 | 24 | es.TableBlockCellView.prototype.getOffsetFromPosition = function( position ) { |
25 | | - var blockOffset, |
26 | | - itemHeight, |
27 | | - offset = 0; |
| 25 | + if ( this.items.length === 0 ) { |
| 26 | + return 0; |
| 27 | + } |
28 | 28 | |
| 29 | + var blockView = this.items[0]; |
| 30 | + |
29 | 31 | for ( var i = 0; i < this.items.length; i++ ) { |
30 | | - blockOffset = this.items[i].$.offset(); |
31 | | - if ( position.top >= blockOffset.top ) { |
32 | | - itemHeight = this.items[i].$.height(); |
33 | | - if ( position.top < blockOffset.top + itemHeight ) { |
34 | | - return offset + this.items[i].getOffsetFromPosition( position ); |
35 | | - } |
| 32 | + if ( this.items[i].$.offset().top >= position.top ) { |
| 33 | + break; |
36 | 34 | } |
37 | | - offset += this.items[i].getLength() + 1; |
| 35 | + blockView = this.items[i]; |
38 | 36 | } |
39 | 37 | |
40 | | - throw 'Position coordinates are outside of the view.'; |
| 38 | + return blockView.list.items.offsetOf( blockView ) + blockView.getOffsetFromPosition( position ); |
41 | 39 | }; |
42 | 40 | |
43 | 41 | /** |
Index: trunk/parsers/wikidom/lib/synth/views/es.TableBlockView.js |
— | — | @@ -27,22 +27,16 @@ |
28 | 28 | return 0; |
29 | 29 | } |
30 | 30 | |
31 | | - var rowOffset, |
32 | | - itemHeight, |
33 | | - offset = 0; |
| 31 | + var rowView = this.items[0]; |
34 | 32 | |
35 | 33 | for ( var i = 0; i < this.items.length; i++ ) { |
36 | | - rowOffset = this.items[i].$.offset(); |
37 | | - if ( position.top >= rowOffset.top ) { |
38 | | - itemHeight = this.items[i].$.height(); |
39 | | - if ( position.top < rowOffset.top + itemHeight ) { |
40 | | - return offset + this.items[i].getOffsetFromPosition( position ); |
41 | | - } |
| 34 | + if ( this.items[i].$.offset().top >= position.top ) { |
| 35 | + break; |
42 | 36 | } |
43 | | - offset += this.items[i].getLength() + 1; |
| 37 | + rowView = this.items[i]; |
44 | 38 | } |
45 | 39 | |
46 | | - throw 'Position coordinates are outside of the view.'; |
| 40 | + return rowView.list.items.offsetOf( rowView ) + rowView.getOffsetFromPosition( position ); |
47 | 41 | }; |
48 | 42 | |
49 | 43 | /** |