Merge "Add deprecated annotation to Article::doEditContent()"
[mediawiki.git] / resources / src / mediawiki.widgets / mw.widgets.UserInputWidget.js
blob2b3a59f5c032f08e84c3e2fe96177f5ecb622184
1 /*!
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
6 */
7 ( function ( $, mw ) {
9 /**
10 * Creates a mw.widgets.UserInputWidget object.
12 * @class
13 * @extends OO.ui.TextInputWidget
14 * @mixins OO.ui.mixin.LookupElement
16 * @constructor
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 || {};
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' );
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
48 mw.widgets.UserInputWidget.prototype.onLookupMenuItemChoose = function ( item ) {
49 this.closeLookupMenu();
50 this.setLookupsDisabled( true );
51 this.setValue( item.getData() );
52 this.setLookupsDisabled( false );
55 /**
56 * @inheritdoc
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;
72 /**
73 * @inheritdoc
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 } );
88 /**
89 * Get lookup cache item from server response data.
91 * @method
92 * @param {Mixed} response Response from server
93 * @return {Object}
95 mw.widgets.UserInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
96 return response.query.allusers || {};
99 /**
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 ) {
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 } ) );
117 return items;
120 }( jQuery, mediaWiki ) );