Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / resources / src / mediawiki.widgets / mw.widgets.UserInputWidget.js
blob164fd20d0a99d70887adafd6497cf95d331c15b0
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          */
94         mw.widgets.UserInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
95                 return response.query.allusers || {};
96         };
98         /**
99          * Get list of menu items from a server response.
100          *
101          * @param {Object} data Query result
102          * @return {OO.ui.MenuOptionWidget[]} Menu items
103          */
104         mw.widgets.UserInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
105                 var len, i, user,
106                         items = [];
108                 for ( i = 0, len = data.length; i < len; i++ ) {
109                         user = data[ i ] || {};
110                         items.push( new OO.ui.MenuOptionWidget( {
111                                 label: user.name,
112                                 data: user.name
113                         } ) );
114                 }
116                 return items;
117         };
119 }( jQuery, mediaWiki ) );