Debian Codegraph @

resources/src/mediawiki.widgets/mw.widgets.UserInputWidget.js

raw symbols SCIP
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 * @classdesc User input widget.
11 *
12 * @class
13 * @extends OO.ui.TextInputWidget
14 * @mixes OO.ui.mixin.LookupElement
15 *
16 * @constructor
17 * @description Create a mw.widgets.UserInputWidget object.
18 * @param {Object} [config] Configuration options
19 * @param {number} [config.limit=10] Number of results to show
20 * @param {boolean} [config.excludenamed] Whether to exclude named users or not
21 * @param {boolean} [config.excludetemp] Whether to exclude temporary users or not
22 * @param {mw.Api} [config.api] API object to use, creates a default mw.Api instance if not specified
23 */
24 mw.widgets.UserInputWidget = function MwWidgetsUserInputWidget( config ) {
25 // Config initialization
26 config = config || {};
27
28 // Parent constructor
29 mw.widgets.UserInputWidget.super.call( this, Object.assign( {}, config, { autocomplete: false } ) );
30
31 // Mixin constructors
32 OO.ui.mixin.LookupElement.call( this, config );
33
34 // Properties
35 this.limit = config.limit || 10;
36 this.excludeNamed = config.excludenamed || false;
37 this.excludeTemp = config.excludetemp || false;
38 this.api = config.api || new mw.Api();
39
40 // Initialization
41 this.$element.addClass( 'mw-widget-userInputWidget' );
42 this.lookupMenu.$element.addClass( 'mw-widget-userInputWidget-menu' );
43 };
44
45 /* Setup */
46
47 OO.inheritClass( mw.widgets.UserInputWidget, OO.ui.TextInputWidget );
48 OO.mixinClass( mw.widgets.UserInputWidget, OO.ui.mixin.LookupElement );
49
50 /* Methods */
51
52 /**
53 * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
54 *
55 * @param {OO.ui.MenuOptionWidget} item Selected item
56 */
57 mw.widgets.UserInputWidget.prototype.onLookupMenuChoose = function ( item ) {
58 this.closeLookupMenu();
59 this.setLookupsDisabled( true );
60 this.setValue( item.getData() );
61 this.setLookupsDisabled( false );
62 };
63
64 /**
65 * @inheritdoc
66 */
67 mw.widgets.UserInputWidget.prototype.focus = function () {
68 // Prevent programmatic focus from opening the menu
69 this.setLookupsDisabled( true );
70
71 // Parent method
72 const retval = mw.widgets.UserInputWidget.super.prototype.focus.apply( this, arguments );
73
74 this.setLookupsDisabled( false );
75
76 return retval;
77 };
78
79 /**
80 * @inheritdoc
81 */
82 mw.widgets.UserInputWidget.prototype.getLookupRequest = function () {
83 return this.api.get( {
84 action: 'query',
85 list: 'allusers',
86 auprefix: this.value,
87 aulimit: this.limit,
88 auexcludenamed: this.excludeNamed,
89 auexcludetemp: this.excludeTemp
90 } );
91 };
92
93 /**
94 * Get lookup cache item from server response data.
95 *
96 * @method
97 * @param {any} response Response from server
98 * @return {Object}
99 */
100 mw.widgets.UserInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
101 return response.query.allusers || {};
102 };
103
104 /**
105 * Get list of menu items from a server response.
106 *
107 * @param {Object} data Query result
108 * @return {OO.ui.MenuOptionWidget[]} Menu items
109 */
110 mw.widgets.UserInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
111 const items = [];
112
113 for ( let i = 0, len = data.length; i < len; i++ ) {
114 const user = data[ i ] || {};
115 items.push( new OO.ui.MenuOptionWidget( {
116 label: user.name,
117 data: user.name
118 } ) );
119 }
120
121 return items;
122 };
123
124 }() );