From c446a164b1941957bbd883da51b2ffed77ed94bc Mon Sep 17 00:00:00 2001 From: Trevor Parscal Date: Thu, 21 Aug 2014 17:08:35 -0700 Subject: [PATCH] Select the correct annotation in MWLinkTargetInputWidget The solution to this problem was two-fold: * Initialize the menu selection based on current annotation data * Don't re-open the LinkTargetInput suggestions menu after choosing It unfortunately involves assuming that setAnnotation will always synchronously emit 'change' events. Bug: 65343 Change-Id: Ia92751add5ee59ba581141a31c8433c5e7e521a5 --- .../widgets/ve.ui.MWLinkTargetInputWidget.js | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/modules/ve-mw/ui/widgets/ve.ui.MWLinkTargetInputWidget.js b/modules/ve-mw/ui/widgets/ve.ui.MWLinkTargetInputWidget.js index e145c64627..96fc170ddd 100644 --- a/modules/ve-mw/ui/widgets/ve.ui.MWLinkTargetInputWidget.js +++ b/modules/ve-mw/ui/widgets/ve.ui.MWLinkTargetInputWidget.js @@ -27,6 +27,7 @@ ve.ui.MWLinkTargetInputWidget = function VeUiMWLinkTargetInputWidget( config ) { // Properties this.annotation = null; + this.choosing = false; // Events this.lookupMenu.connect( this, { 'choose': 'onLookupMenuItemChoose' } ); @@ -58,13 +59,28 @@ OO.mixinClass( ve.ui.MWLinkTargetInputWidget, OO.ui.LookupInputWidget ); */ ve.ui.MWLinkTargetInputWidget.prototype.onLookupMenuItemChoose = function ( item ) { if ( item ) { + // WARNING: This assumes that #setAnnotation will emit `change` events synchronously + // TODO: Consider how this trick can be solved better, and possibly pushed upstream to + // OO.ui.LookupInputWidget so others don't fall into this trap + this.choosing = true; this.setAnnotation( item.getData() ); + this.choosing = false; } else if ( this.annotation ) { this.annotation = null; this.emit( 'change', this.getValue() ); } }; +/** + * @inheritdoc + */ +ve.ui.MWLinkTargetInputWidget.prototype.onLookupInputChange = function () { + // WARNING: See #onLookupMenuItemChoose for why this is fragile + if ( !this.choosing ) { + this.openLookupMenu(); + } +}; + /** * @inheritdoc */ @@ -237,8 +253,15 @@ ve.ui.MWLinkTargetInputWidget.prototype.getLookupMenuItemsFromData = function ( ve.ui.MWLinkTargetInputWidget.prototype.initializeLookupMenuSelection = function () { var item; - // Parent method - OO.ui.LookupInputWidget.prototype.initializeLookupMenuSelection.call( this ); + if ( this.annotation ) { + this.lookupMenu.selectItem( this.lookupMenu.getItemFromData( this.annotation ) ); + } + + item = this.lookupMenu.getSelectedItem(); + if ( !item ) { + // Parent method + OO.ui.LookupInputWidget.prototype.initializeLookupMenuSelection.call( this ); + } // Update annotation to match selected item item = this.lookupMenu.getSelectedItem();