Clean up LookupInputWidget subclasses and use new functionality

MWCategoryInputWidget:
* Use @inheritdoc
* Don't modify data parameter in getLookupCacheItemFromData()

MWLinkTargetInputWidget:
* Remove this.choosing in favor of setLookupsDisabled()
* Explicitly close menu on choose
* Remove manual emission of change events
** This looks ridiculous, it doesn't seem to be necessary,
   and it causes infinite event loops. But I'm very curious
   why this was added in the first place.
* Remove onLookupInputChange override that is now unnecessary
* Use {} rather than [] for fake empty result
* Prevent programmatic focus from opening the menu

MWTitleInputWidget:
* On choose, close menu and disable lookups while changing value
* Use @inheritdoc
* Remove mentions of "template" from getTitle() documentation

Bug fixed:
* When choosing a suggestion in MWTitleInputWidget,
  new suggestions would be loaded and the menu would reopen

Depends on Iecae9b582 in oojs-ui.

Change-Id: I716f99bb464a5cebd4f17701197f768e4e0e02a9
This commit is contained in:
Roan Kattouw 2014-10-31 12:08:02 -07:00
parent c2dfd1ba04
commit a0167c08c7
3 changed files with 27 additions and 44 deletions

View file

@ -47,10 +47,7 @@ OO.mixinClass( ve.ui.MWCategoryInputWidget, OO.ui.LookupInputWidget );
/* Methods */
/**
* Gets a new request object of the current lookup query value.
*
* @method
* @returns {jqXHR} AJAX object without success or fail handlers attached
* @inheritdoc
*/
ve.ui.MWCategoryInputWidget.prototype.getLookupRequest = function () {
return ve.init.target.constructor.static.apiRequest( {
@ -63,16 +60,12 @@ ve.ui.MWCategoryInputWidget.prototype.getLookupRequest = function () {
};
/**
* Get lookup cache item from server response data.
*
* @method
* @param {Mixed} data Response from server
* @inheritdoc
*/
ve.ui.MWCategoryInputWidget.prototype.getLookupCacheItemFromData = function ( data ) {
var result = [], linkCacheUpdate = {};
data.query = data.query || {};
var result = [], linkCacheUpdate = {}, query = data.query || {};
$.each( data.query.allcategories || [], function ( index, category ) {
$.each( query.allcategories || [], function ( index, category ) {
result.push( category['*'] );
linkCacheUpdate['Category:' + category['*']] = { missing: false, hidden: category.hasOwnProperty( 'hidden' ) };
}.bind( this ) );
@ -83,10 +76,7 @@ ve.ui.MWCategoryInputWidget.prototype.getLookupCacheItemFromData = function ( da
};
/**
* Get list of menu items from a server response.
*
* @param {Object} data Query result
* @returns {OO.ui.MenuItemWidget[]} Menu items
* @inheritdoc
*/
ve.ui.MWCategoryInputWidget.prototype.getLookupMenuItemsFromData = function ( data ) {
var exactMatch = false,

View file

@ -27,7 +27,6 @@ ve.ui.MWLinkTargetInputWidget = function VeUiMWLinkTargetInputWidget( config ) {
// Properties
this.annotation = null;
this.choosing = false;
// Events
this.lookupMenu.connect( this, { choose: 'onLookupMenuItemChoose' } );
@ -55,30 +54,31 @@ OO.mixinClass( ve.ui.MWLinkTargetInputWidget, OO.ui.LookupInputWidget );
*
* @method
* @param {OO.ui.MenuItemWidget|null} item Selected item
* @fires change
*/
ve.ui.MWLinkTargetInputWidget.prototype.onLookupMenuItemChoose = function ( item ) {
this.closeLookupMenu();
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.setLookupsDisabled( true );
this.setAnnotation( item.getData() );
this.choosing = false;
this.setLookupsDisabled( 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();
}
ve.ui.MWLinkTargetInputWidget.prototype.focus = function () {
var retval;
// Prevent programmatic focus from opening the menu
this.setLookupsDisabled( true );
// Parent method
retval = ve.ui.MWLinkTargetInputWidget.super.prototype.focus.apply( this, arguments );
this.setLookupsDisabled( false );
return retval;
};
/**
@ -114,7 +114,7 @@ ve.ui.MWLinkTargetInputWidget.prototype.getLookupRequest = function () {
} else {
// Don't send invalid titles to the API.
// Just pretend it returned nothing so we can show the 'invalid title' section
return $.Deferred().resolve( [] ).promise( { abort: function () {
return $.Deferred().resolve( {} ).promise( { abort: function () {
// Do nothing. This is just so OOUI doesn't break due to abort being undefined.
} } );
}
@ -280,7 +280,6 @@ ve.ui.MWLinkTargetInputWidget.prototype.initializeLookupMenuSelection = function
if ( item ) {
// Set annotation directly, bypassing re-setting the value of the input
this.annotation = item.getData();
this.emit( 'change', this.getValue() );
}
};

View file

@ -52,16 +52,16 @@ OO.mixinClass( ve.ui.MWTitleInputWidget, OO.ui.LookupInputWidget );
* @param {OO.ui.MenuItemWidget} item Selected item
*/
ve.ui.MWTitleInputWidget.prototype.onLookupMenuItemChoose = function ( item ) {
this.closeLookupMenu();
if ( item ) {
this.setLookupsDisabled( true );
this.setValue( item.getData() );
this.setLookupsDisabled( false );
}
};
/**
* Gets a new request object of the current lookup query value.
*
* @method
* @returns {jQuery.Deferred} Deferred object with success and fail handlers already attached
* @inheritdoc
*/
ve.ui.MWTitleInputWidget.prototype.getLookupRequest = function () {
var value = this.value;
@ -84,20 +84,14 @@ ve.ui.MWTitleInputWidget.prototype.getLookupRequest = function () {
};
/**
* Get lookup cache item from server response data.
*
* @method
* @param {Mixed} data Response from server
* @inheritdoc
*/
ve.ui.MWTitleInputWidget.prototype.getLookupCacheItemFromData = function ( data ) {
return data[1] || [];
};
/**
* Get list of menu items from a server response.
*
* @param {Object} data Query result
* @returns {OO.ui.MenuItemWidget[]} Menu items
* @inheritdoc
*/
ve.ui.MWTitleInputWidget.prototype.getLookupMenuItemsFromData = function ( data ) {
var i, len, title, value,
@ -127,9 +121,9 @@ ve.ui.MWTitleInputWidget.prototype.getLookupMenuItemsFromData = function ( data
};
/**
* Get template title
* Get title object corresponding to #getValue
*
* @returns {mw.Title|null} Template title if valid or null
* @returns {mw.Title|null} Title object, or null if value is invalid
*/
ve.ui.MWTitleInputWidget.prototype.getTitle = function () {
var title = this.getValue(),