Implement UserInputWidget in OOUI/MW Widgets
[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 ( $, mw ) {
8 /**
9 * Creates a mw.widgets.UserInputWidget object.
10 *
11 * @class
12 * @extends OO.ui.TextInputWidget
13 * @mixins OO.ui.mixin.LookupElement
14 *
15 * @constructor
16 * @param {Object} [config] Configuration options
17 * @cfg {number} [limit=10] Number of results to show
18 */
19 mw.widgets.UserInputWidget = function MwWidgetsUserInputWidget( config ) {
20 // Config initialization
21 config = config || {};
22
23 // Parent constructor
24 OO.ui.TextInputWidget.call( this, $.extend( {}, config, { autocomplete: false } ) );
25
26 // Mixin constructors
27 OO.ui.mixin.LookupElement.call( this, config );
28
29 // Properties
30 this.limit = config.limit || 10;
31
32 // Initialization
33 this.$element.addClass( 'mw-widget-userInputWidget' );
34 this.lookupMenu.$element.addClass( 'mw-widget-userInputWidget-menu' );
35 };
36
37 /* Inheritance */
38
39 OO.inheritClass( mw.widgets.UserInputWidget, OO.ui.TextInputWidget );
40
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 = OO.ui.TextInputWidget.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} data Response from server
93 */
94 mw.widgets.UserInputWidget.prototype.getLookupCacheDataFromResponse = function ( data ) {
95 return data.query || {};
96 };
97
98 /**
99 * Get list of menu items from a server response.
100 *
101 * @param {Object} data Query result
102 * @returns {OO.ui.MenuOptionWidget[]} Menu items
103 */
104 mw.widgets.UserInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
105 var len, i, user,
106 users = data.allusers,
107 items = [];
108
109 for ( i = 0, len = users.length; i < len; i++ ) {
110 user = users[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 }( jQuery, mediaWiki ) );