2013-04-29 20:56:24 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface LookupInputWidget class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lookup input widget.
|
|
|
|
*
|
|
|
|
* Mixin that adds a menu showing suggested values to a text input. Subclasses must handle `select`
|
|
|
|
* events on #lookupMenu to make use of selections.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.ui.TextInputWidget} input Input widget
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2013-10-04 20:47:14 +00:00
|
|
|
* @cfg {jQuery} [$overlay=this.$$( 'body' )] Overlay layer
|
2013-04-29 20:56:24 +00:00
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget = function VeUiLookupInputWidget( input, config ) {
|
|
|
|
// Config intialization
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.lookupInput = input;
|
2013-10-04 20:47:14 +00:00
|
|
|
this.$overlay = config.$overlay || this.$$( 'body' );
|
2013-04-29 20:56:24 +00:00
|
|
|
this.lookupMenu = new ve.ui.TextInputMenuWidget( this, {
|
2013-06-27 17:25:28 +00:00
|
|
|
'$$': ve.Element.get$$( this.$overlay ),
|
2013-04-29 20:56:24 +00:00
|
|
|
'input': this.lookupInput,
|
|
|
|
'$container': config.$container
|
|
|
|
} );
|
|
|
|
this.lookupCache = {};
|
|
|
|
this.lookupQuery = null;
|
|
|
|
this.lookupRequest = null;
|
|
|
|
|
|
|
|
// Events
|
|
|
|
this.$overlay.append( this.lookupMenu.$ );
|
|
|
|
|
|
|
|
this.lookupInput.$input.on( {
|
|
|
|
'focus': ve.bind( this.onLookupInputFocus, this ),
|
|
|
|
'blur': ve.bind( this.onLookupInputBlur, this ),
|
|
|
|
'mousedown': ve.bind( this.onLookupInputMouseDown, this )
|
|
|
|
} );
|
2013-05-01 22:21:32 +00:00
|
|
|
this.lookupInput.connect( this, { 'change': 'onLookupInputChange' } );
|
2013-04-29 20:56:24 +00:00
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.$.addClass( 've-ui-lookupWidget' );
|
|
|
|
this.lookupMenu.$.addClass( 've-ui-lookupWidget-menu' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle input focus event.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {jQuery.Event} e Input focus event
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.onLookupInputFocus = function () {
|
|
|
|
this.openLookupMenu();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle input blur event.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {jQuery.Event} e Input blur event
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.onLookupInputBlur = function () {
|
|
|
|
this.lookupMenu.hide();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle input mouse down event.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {jQuery.Event} e Input mouse down event
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.onLookupInputMouseDown = function () {
|
|
|
|
this.openLookupMenu();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle input change event.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} value New input value
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.onLookupInputChange = function () {
|
|
|
|
this.openLookupMenu();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the menu.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @chainable
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.openLookupMenu = function () {
|
|
|
|
var value = this.lookupInput.getValue();
|
|
|
|
|
2013-07-09 22:13:03 +00:00
|
|
|
if ( this.lookupMenu.$input.is( ':focus' ) && $.trim( value ) !== '' ) {
|
2013-04-29 20:56:24 +00:00
|
|
|
this.populateLookupMenu();
|
|
|
|
if ( !this.lookupMenu.isVisible() ) {
|
|
|
|
this.lookupMenu.show();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.lookupMenu.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate lookup menu with current information.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @chainable
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.populateLookupMenu = function () {
|
|
|
|
var items = this.getLookupMenuItems();
|
|
|
|
|
|
|
|
this.lookupMenu.clearItems();
|
|
|
|
|
|
|
|
if ( items.length ) {
|
2013-06-18 19:34:01 +00:00
|
|
|
this.lookupMenu.show();
|
2013-04-29 20:56:24 +00:00
|
|
|
this.lookupMenu.addItems( items );
|
|
|
|
this.initializeLookupMenuSelection();
|
2013-06-18 19:34:01 +00:00
|
|
|
} else {
|
|
|
|
this.lookupMenu.hide();
|
2013-04-29 20:56:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set selection in the lookup menu with current information.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @chainable
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.initializeLookupMenuSelection = function () {
|
|
|
|
if ( !this.lookupMenu.getSelectedItem() ) {
|
2013-07-30 22:06:35 +00:00
|
|
|
this.lookupMenu.intializeSelection( this.lookupMenu.getFirstSelectableItem() );
|
2013-04-29 20:56:24 +00:00
|
|
|
}
|
|
|
|
this.lookupMenu.highlightItem( this.lookupMenu.getSelectedItem() );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get lookup menu items for the current query.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {ve.ui.MenuItemWidget[]} Menu items
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.getLookupMenuItems = function () {
|
|
|
|
var value = this.lookupInput.getValue();
|
|
|
|
|
|
|
|
if ( value && value !== this.lookupQuery ) {
|
|
|
|
// Abort current request if query has changed
|
|
|
|
if ( this.lookupRequest ) {
|
|
|
|
this.lookupRequest.abort();
|
|
|
|
this.lookupQuery = null;
|
|
|
|
this.lookupRequest = null;
|
|
|
|
}
|
|
|
|
if ( value in this.lookupCache ) {
|
|
|
|
return this.getLookupMenuItemsFromData( this.lookupCache[value] );
|
|
|
|
} else {
|
|
|
|
this.lookupQuery = value;
|
|
|
|
this.lookupRequest = this.getLookupRequest()
|
|
|
|
.always( ve.bind( function () {
|
|
|
|
this.lookupQuery = null;
|
|
|
|
this.lookupRequest = null;
|
|
|
|
}, this ) )
|
|
|
|
.done( ve.bind( function ( data ) {
|
|
|
|
this.lookupCache[value] = this.getLookupCacheItemFromData( data );
|
|
|
|
this.openLookupMenu();
|
|
|
|
}, this ) );
|
2013-06-06 00:07:51 +00:00
|
|
|
this.pushPending();
|
|
|
|
this.lookupRequest.always( ve.bind( function () {
|
|
|
|
this.popPending();
|
|
|
|
}, this ) );
|
2013-04-29 20:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a new request object of the current lookup query value.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @abstract
|
2013-06-18 17:56:56 +00:00
|
|
|
* @returns {jqXHR} jQuery AJAX object, or promise object with an .abort() method
|
2013-04-29 20:56:24 +00:00
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.getLookupRequest = function () {
|
|
|
|
// Stub, implemented in subclass
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle successful lookup request.
|
|
|
|
*
|
|
|
|
* Overriding methods should call #populateLookupMenu when results are available and cache results
|
|
|
|
* for future lookups in #lookupCache as an array of #ve.ui.MenuItemWidget objects.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @abstract
|
|
|
|
* @param {Mixed} data Response from server
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.onLookupRequestDone = function () {
|
|
|
|
// Stub, implemented in subclass
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of menu item widgets from the data stored by the lookup request's done handler.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @abstract
|
|
|
|
* @param {Mixed} data Cached result data, usually an array
|
|
|
|
* @returns {ve.ui.MenuItemWidget[]} Menu items
|
|
|
|
*/
|
|
|
|
ve.ui.LookupInputWidget.prototype.getLookupMenuItemsFromData = function () {
|
|
|
|
// Stub, implemented in subclass
|
|
|
|
return [];
|
|
|
|
};
|