Merge "Improve sorting on SpecialWanted*-Pages"
[mediawiki.git] / resources / src / mediawiki.widgets / mw.widgets.UserInputWidget.js
blob2b3a59f5c032f08e84c3e2fe96177f5ecb622184
1 /*!
2  * MediaWiki Widgets - UserInputWidget class.
3  *
4  * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5  * @license The MIT License (MIT); see LICENSE.txt
6  */
7 ( function ( $, mw ) {
9         /**
10          * Creates a mw.widgets.UserInputWidget object.
11          *
12          * @class
13          * @extends OO.ui.TextInputWidget
14          * @mixins OO.ui.mixin.LookupElement
15          *
16          * @constructor
17          * @param {Object} [config] Configuration options
18          * @cfg {number} [limit=10] Number of results to show
19          */
20         mw.widgets.UserInputWidget = function MwWidgetsUserInputWidget( config ) {
21                 // Config initialization
22                 config = config || {};
24                 // Parent constructor
25                 mw.widgets.UserInputWidget.parent.call( this, $.extend( {}, config, { autocomplete: false } ) );
27                 // Mixin constructors
28                 OO.ui.mixin.LookupElement.call( this, config );
30                 // Properties
31                 this.limit = config.limit || 10;
33                 // Initialization
34                 this.$element.addClass( 'mw-widget-userInputWidget' );
35                 this.lookupMenu.$element.addClass( 'mw-widget-userInputWidget-menu' );
36         };
38         /* Setup */
40         OO.inheritClass( mw.widgets.UserInputWidget, OO.ui.TextInputWidget );
41         OO.mixinClass( mw.widgets.UserInputWidget, OO.ui.mixin.LookupElement );
43         /* Methods */
45         /**
46          * @inheritdoc
47          */
48         mw.widgets.UserInputWidget.prototype.onLookupMenuItemChoose = function ( item ) {
49                 this.closeLookupMenu();
50                 this.setLookupsDisabled( true );
51                 this.setValue( item.getData() );
52                 this.setLookupsDisabled( false );
53         };
55         /**
56          * @inheritdoc
57          */
58         mw.widgets.UserInputWidget.prototype.focus = function () {
59                 var retval;
61                 // Prevent programmatic focus from opening the menu
62                 this.setLookupsDisabled( true );
64                 // Parent method
65                 retval = mw.widgets.UserInputWidget.parent.prototype.focus.apply( this, arguments );
67                 this.setLookupsDisabled( false );
69                 return retval;
70         };
72         /**
73          * @inheritdoc
74          */
75         mw.widgets.UserInputWidget.prototype.getLookupRequest = function () {
76                 var inputValue = this.value;
78                 return new mw.Api().get( {
79                         action: 'query',
80                         list: 'allusers',
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 ),
84                         aulimit: this.limit
85                 } );
86         };
88         /**
89          * Get lookup cache item from server response data.
90          *
91          * @method
92          * @param {Mixed} response Response from server
93          * @return {Object}
94          */
95         mw.widgets.UserInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
96                 return response.query.allusers || {};
97         };
99         /**
100          * Get list of menu items from a server response.
101          *
102          * @param {Object} data Query result
103          * @return {OO.ui.MenuOptionWidget[]} Menu items
104          */
105         mw.widgets.UserInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
106                 var len, i, user,
107                         items = [];
109                 for ( i = 0, len = data.length; i < len; i++ ) {
110                         user = data[ i ] || {};
111                         items.push( new OO.ui.MenuOptionWidget( {
112                                 label: user.name,
113                                 data: user.name
114                         } ) );
115                 }
117                 return items;
118         };
120 }( jQuery, mediaWiki ) );