2 * MediaWiki Widgets - UserInputWidget class.
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
10 * Creates a mw.widgets.UserInputWidget object.
13 * @extends OO.ui.TextInputWidget
14 * @mixins OO.ui.mixin.LookupElement
17 * @param {Object} [config] Configuration options
18 * @cfg {number} [limit=10] Number of results to show
20 mw.widgets.UserInputWidget = function MwWidgetsUserInputWidget( config ) {
21 // Config initialization
22 config = config || {};
25 mw.widgets.UserInputWidget.parent.call( this, $.extend( {}, config, { autocomplete: false } ) );
28 OO.ui.mixin.LookupElement.call( this, config );
31 this.limit = config.limit || 10;
34 this.$element.addClass( 'mw-widget-userInputWidget' );
35 this.lookupMenu.$element.addClass( 'mw-widget-userInputWidget-menu' );
40 OO.inheritClass( mw.widgets.UserInputWidget, OO.ui.TextInputWidget );
41 OO.mixinClass( mw.widgets.UserInputWidget, OO.ui.mixin.LookupElement );
48 mw.widgets.UserInputWidget.prototype.onLookupMenuItemChoose = function ( item ) {
49 this.closeLookupMenu();
50 this.setLookupsDisabled( true );
51 this.setValue( item.getData() );
52 this.setLookupsDisabled( false );
58 mw.widgets.UserInputWidget.prototype.focus = function () {
61 // Prevent programmatic focus from opening the menu
62 this.setLookupsDisabled( true );
65 retval = mw.widgets.UserInputWidget.parent.prototype.focus.apply( this, arguments );
67 this.setLookupsDisabled( false );
75 mw.widgets.UserInputWidget.prototype.getLookupRequest = function () {
76 var inputValue = this.value;
78 return new mw.Api().get( {
81 // Prefix of list=allusers is case sensitive. Normalise first
82 // character to uppercase so that "fo" may yield "Foo".
83 auprefix: inputValue[ 0 ].toUpperCase() + inputValue.slice( 1 ),
89 * Get lookup cache item from server response data.
92 * @param {Mixed} response Response from server
95 mw.widgets.UserInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
96 return response.query.allusers || {};
100 * Get list of menu items from a server response.
102 * @param {Object} data Query result
103 * @return {OO.ui.MenuOptionWidget[]} Menu items
105 mw.widgets.UserInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
109 for ( i = 0, len = data.length; i < len; i++ ) {
110 user = data[ i ] || {};
111 items.push( new OO.ui.MenuOptionWidget( {
120 }( jQuery, mediaWiki ) );