resources: Strip '$' and 'mw' from file closures
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets / mw.widgets.UserInputWidget.js
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 () {
8
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 || {};
23
24 // Parent constructor
25 mw.widgets.UserInputWidget.parent.call( this, $.extend( {}, config, { autocomplete: false } ) );
26
27 // Mixin constructors
28 OO.ui.mixin.LookupElement.call( this, config );
29
30 // Properties
31 this.limit = config.limit || 10;
32
33 // Initialization
34 this.$element.addClass( 'mw-widget-userInputWidget' );
35 this.lookupMenu.$element.addClass( 'mw-widget-userInputWidget-menu' );
36 };
37
38 /* Setup */
39
40 OO.inheritClass( mw.widgets.UserInputWidget, OO.ui.TextInputWidget );
41 OO.mixinClass( mw.widgets.UserInputWidget, OO.ui.mixin.LookupElement );
42
43 /* Methods */
44
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 };
54
55 /**
56 * @inheritdoc
57 */
58 mw.widgets.UserInputWidget.prototype.focus = function () {
59 var retval;
60
61 // Prevent programmatic focus from opening the menu
62 this.setLookupsDisabled( true );
63
64 // Parent method
65 retval = mw.widgets.UserInputWidget.parent.prototype.focus.apply( this, arguments );
66
67 this.setLookupsDisabled( false );
68
69 return retval;
70 };
71
72 /**
73 * @inheritdoc
74 */
75 mw.widgets.UserInputWidget.prototype.getLookupRequest = function () {
76 var inputValue = this.value;
77
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 };
87
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 };
98
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 = [];
108
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 }
116
117 return items;
118 };
119
120 }() );