Trevor Parscal has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/71560


Change subject: Select widget relative item fixes
......................................................................

Select widget relative item fixes

Changes:

ve.ui.SelectWidget.js
* Make getRelativeSelectableItem properly loop around
* Replace confusing and broken getClosestSelectableItem with 
getFirstSelectableItem since that's the only use case we had for it anyway

ve.ui.PagedDialog.js, ve.ui.LookupInputWidget.js
* Update calls to getClosestSelectableItem to use new method

Change-Id: I2399d01a45c43d1ad663ed6c6de156e796065306
---
M modules/ve/ui/dialogs/ve.ui.PagedDialog.js
M modules/ve/ui/widgets/ve.ui.LookupInputWidget.js
M modules/ve/ui/widgets/ve.ui.SelectWidget.js
3 files changed, 25 insertions(+), 31 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/60/71560/1

diff --git a/modules/ve/ui/dialogs/ve.ui.PagedDialog.js 
b/modules/ve/ui/dialogs/ve.ui.PagedDialog.js
index 7f387a5..7f45322 100644
--- a/modules/ve/ui/dialogs/ve.ui.PagedDialog.js
+++ b/modules/ve/ui/dialogs/ve.ui.PagedDialog.js
@@ -122,7 +122,7 @@
 
        // Auto-select first item when nothing is selected yet
        if ( !this.outlineWidget.getSelectedItem() ) {
-               this.outlineWidget.selectItem( 
this.outlineWidget.getClosestSelectableItem( 0 ) );
+               this.outlineWidget.selectItem( 
this.outlineWidget.getFirstSelectableItem() );
        }
 
        return this;
@@ -145,7 +145,7 @@
 
        // Auto-select first item when nothing is selected anymore
        if ( !this.outlineWidget.getSelectedItem() ) {
-               this.outlineWidget.selectItem( 
this.outlineWidget.getClosestSelectableItem( 0 ) );
+               this.outlineWidget.selectItem( 
this.outlineWidget.getFirstSelectableItem() );
        }
 
        return this;
diff --git a/modules/ve/ui/widgets/ve.ui.LookupInputWidget.js 
b/modules/ve/ui/widgets/ve.ui.LookupInputWidget.js
index 1b2b489..b9d44b3 100644
--- a/modules/ve/ui/widgets/ve.ui.LookupInputWidget.js
+++ b/modules/ve/ui/widgets/ve.ui.LookupInputWidget.js
@@ -143,7 +143,7 @@
  */
 ve.ui.LookupInputWidget.prototype.initializeLookupMenuSelection = function () {
        if ( !this.lookupMenu.getSelectedItem() ) {
-               this.lookupMenu.selectItem( 
this.lookupMenu.getClosestSelectableItem( 0 ), true );
+               this.lookupMenu.selectItem( 
this.lookupMenu.getFirstSelectableItem(), true );
        }
        this.lookupMenu.highlightItem( this.lookupMenu.getSelectedItem() );
 };
diff --git a/modules/ve/ui/widgets/ve.ui.SelectWidget.js 
b/modules/ve/ui/widgets/ve.ui.SelectWidget.js
index 633f8ae..38c8549 100644
--- a/modules/ve/ui/widgets/ve.ui.SelectWidget.js
+++ b/modules/ve/ui/widgets/ve.ui.SelectWidget.js
@@ -282,53 +282,47 @@
  * @returns {ve.ui.OptionWidget|null} Item at position, `null` if there are no 
items in the menu
  */
 ve.ui.SelectWidget.prototype.getRelativeSelectableItem = function ( item, 
direction ) {
-       var index = this.items.indexOf( item ),
-               i = direction > 0 ?
-                       // Default to 0 instead of -1, if nothing is selected 
let's start at the beginning
-                       Math.max( 0, index + direction ) :
-                       // Default to n-1 instead of -1, if nothing is selected 
let's start at the end
-                       Math.min( index + direction, this.items.length - 1 ),
+       var inc = direction > 0 ? 1 : -1,
                len = this.items.length,
-               inc = direction > 0 ? 1 : -1,
-               stopAt = i;
-       // Iterate to the next item in the sequence
-       while ( i <= len ) {
+               index = item instanceof ve.ui.OptionWidget ?
+                       ve.indexOf( item, this.items ) : ( inc > 0 ? -1 : 0 ),
+               stopAt = Math.max( Math.min( index, len - 1 ), 0 ),
+               i = inc > 0 ?
+                       // Default to 0 instead of -1, if nothing is selected 
let's start at the beginning
+                       Math.max( index, -1 ) :
+                       // Default to n-1 instead of -1, if nothing is selected 
let's start at the end
+                       Math.min( index, len );
+
+       while ( true ) {
+               i = ( i + inc + len ) % len;
                item = this.items[i];
                if ( item instanceof ve.ui.OptionWidget && item.isSelectable() 
) {
                        return item;
                }
-               // Wrap around
-               i = ( i + inc + len ) % len;
+               // Stop iterating when we've looped all the way around
                if ( i === stopAt ) {
-                       // We've looped around, I guess we're all alone
-                       return item;
+                       break;
                }
        }
        return null;
 };
 
 /**
- * Selects the next item in the menu.
+ * Get the next selectable item.
  *
  * @method
- * @param {number} index Item index
- * @returns {ve.ui.OptionWidget|null} Item, `null` if there's not an item at 
the `index`
+ * @returns {ve.ui.OptionWidget|null} Item, `null` if ther aren't any 
selectable items
  */
-ve.ui.SelectWidget.prototype.getClosestSelectableItem = function ( index ) {
-       var item,
-               i = 0,
-               len = this.items.length,
-               at = 0;
-       while ( i < len ) {
+ve.ui.SelectWidget.prototype.getFirstSelectableItem = function () {
+       var i, len, item;
+
+       for ( i = 0, len = this.items.length; i < len; i++ ) {
                item = this.items[i];
                if ( item instanceof ve.ui.OptionWidget && item.isSelectable() 
) {
-                       if ( at === index ) {
-                               return item;
-                       }
-                       at++;
+                       return item;
                }
-               i++;
        }
+
        return null;
 };
 

-- 
To view, visit https://gerrit.wikimedia.org/r/71560
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2399d01a45c43d1ad663ed6c6de156e796065306
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Trevor Parscal <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to