r97964 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r97963‎ | r97964 | r97965 >
Date:22:19, 23 September 2011
Author:inez
Status:deferred
Tags:
Comment:
Bunch of good fixes to getOffsetFromPosition
Modified paths:
  • /trunk/parsers/wikidom/lib/synth/bases/es.AggregateArray.js (modified) (history)
  • /trunk/parsers/wikidom/lib/synth/views/es.ContentView.js (modified) (history)
  • /trunk/parsers/wikidom/lib/synth/views/es.DocumentView.js (modified) (history)
  • /trunk/parsers/wikidom/lib/synth/views/es.ListBlockView.js (modified) (history)
  • /trunk/parsers/wikidom/lib/synth/views/es.ParagraphBlockView.js (modified) (history)
  • /trunk/parsers/wikidom/lib/synth/views/es.SurfaceView.js (modified) (history)
  • /trunk/parsers/wikidom/lib/synth/views/es.TableBlockCellView.js (modified) (history)
  • /trunk/parsers/wikidom/lib/synth/views/es.TableBlockRowView.js (modified) (history)
  • /trunk/parsers/wikidom/lib/synth/views/es.TableBlockView.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/synth/bases/es.AggregateArray.js
@@ -27,6 +27,19 @@
2828 return null;
2929 };
3030
 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+
3144 es.AggregateArray.prototype.rangeOf = function( item ) {
3245 if ( this.length ) {
3346 var i = 0,
Index: trunk/parsers/wikidom/lib/synth/views/es.ListBlockView.js
@@ -30,24 +30,16 @@
3131 return 0;
3232 }
3333
34 - var contentOffset,
35 - itemHeight,
36 - offset = 0;
 34+ var listItemView = this.items[0];
3735
3836 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;
4739 }
48 - offset += this.items[i].getLength() + 1;
 40+ listItemView = this.items[i];
4941 }
5042
51 - throw 'Position coordinates are outside of the view.';
 43+ return listItemView.list.items.offsetOf( listItemView ) + listItemView.getContentOffset( position );
5244 };
5345
5446 /**
Index: trunk/parsers/wikidom/lib/synth/views/es.SurfaceView.js
@@ -238,13 +238,12 @@
239239 };
240240
241241 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;
245246 this.selecting = true;
246247
247 - console.log(offset);
248 -
249248 if ( !this.$input.is(':focus') ) {
250249 this.$input.focus().select();
251250 }
@@ -254,10 +253,10 @@
255254
256255 es.SurfaceView.prototype.onMouseMove = function( e ) {
257256 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;
262261 this.documentView.drawSelection( new es.Range( this.from, this.to ) );
263262 }
264263 // TODO: Respond to mouse move event, updating selection while painting
Index: trunk/parsers/wikidom/lib/synth/views/es.ContentView.js
@@ -281,6 +281,11 @@
282282 if ( this.model.getLength() === 0 ) {
283283 return 0;
284284 }
 285+
 286+ // Localize position
 287+ position.subtract( es.Position.newFromElementPagePosition( this.$ ) );
 288+
 289+ //
285290 /*
286291 * Line finding
287292 *
Index: trunk/parsers/wikidom/lib/synth/views/es.TableBlockRowView.js
@@ -22,22 +22,20 @@
2323 * @returns {Integer} Offset nearest to position
2424 */
2525 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];
2931
3032 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;
3735 }
38 - offset += this.items[i].getLength() + 1;
 36+ cellView = this.items[i];
3937 }
4038
41 - throw 'Position coordinates are outside of the view.';
 39+ return cellView.list.items.offsetOf( cellView ) + cellView.getOffsetFromPosition( position );
4240 };
4341
4442 /**
Index: trunk/parsers/wikidom/lib/synth/views/es.DocumentView.js
@@ -16,23 +16,17 @@
1717 if ( this.items.length === 0 ) {
1818 return 0;
1919 }
20 -
21 - var blockView = this.items[0],
22 - blockLength = 0,
23 - offset = 0;
2420
 21+ var blockView = this.items[0];
 22+
2523 for ( var i = 0; i < this.items.length; i++ ) {
2624 if ( this.items[i].$.offset().top >= position.top ) {
2725 break;
2826 }
2927 blockView = this.items[i];
30 - blockLength = blockView.getLength();
31 - offset += blockLength + 1;
3228 }
33 -
34 - offset -= blockLength + 1;
35 -
36 - return offset + blockView.getOffsetFromPosition( position );
 29+
 30+ return blockView.list.items.offsetOf( blockView ) + blockView.getOffsetFromPosition( position );
3731 };
3832
3933 /**
Index: trunk/parsers/wikidom/lib/synth/views/es.ParagraphBlockView.js
@@ -25,9 +25,6 @@
2626 * @returns {Integer} Offset nearest to position
2727 */
2828 es.ParagraphBlockView.prototype.getOffsetFromPosition = function( position ) {
29 - var blockPosition = this.$.offset();
30 - position.left -= blockPosition.left;
31 - position.top -= blockPosition.top;
3229 return this.contentView.getOffset( position );
3330 };
3431
Index: trunk/parsers/wikidom/lib/synth/views/es.TableBlockCellView.js
@@ -21,22 +21,20 @@
2222 * @returns {Integer} Offset nearest to position
2323 */
2424 es.TableBlockCellView.prototype.getOffsetFromPosition = function( position ) {
25 - var blockOffset,
26 - itemHeight,
27 - offset = 0;
 25+ if ( this.items.length === 0 ) {
 26+ return 0;
 27+ }
2828
 29+ var blockView = this.items[0];
 30+
2931 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;
3634 }
37 - offset += this.items[i].getLength() + 1;
 35+ blockView = this.items[i];
3836 }
3937
40 - throw 'Position coordinates are outside of the view.';
 38+ return blockView.list.items.offsetOf( blockView ) + blockView.getOffsetFromPosition( position );
4139 };
4240
4341 /**
Index: trunk/parsers/wikidom/lib/synth/views/es.TableBlockView.js
@@ -27,22 +27,16 @@
2828 return 0;
2929 }
3030
31 - var rowOffset,
32 - itemHeight,
33 - offset = 0;
 31+ var rowView = this.items[0];
3432
3533 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;
4236 }
43 - offset += this.items[i].getLength() + 1;
 37+ rowView = this.items[i];
4438 }
4539
46 - throw 'Position coordinates are outside of the view.';
 40+ return rowView.list.items.offsetOf( rowView ) + rowView.getOffsetFromPosition( position );
4741 };
4842
4943 /**

Status & tagging log