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
10 * @classdesc User input widget.
13 * @extends OO.ui.TextInputWidget
14 * @mixes OO.ui.mixin.LookupElement
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
24 mw.widgets.UserInputWidget = function MwWidgetsUserInputWidget( config ) {
25 // Config initialization
26 config = config || {};
29 mw.widgets.UserInputWidget.super.call( this, Object.assign( {}, config, { autocomplete: false } ) );
32 OO.ui.mixin.LookupElement.call( this, config );
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();
41 this.$element.addClass( 'mw-widget-userInputWidget' );
42 this.lookupMenu.$element.addClass( 'mw-widget-userInputWidget-menu' );
44 // Disable autocompletion if this widget only accepts IPs or IP ranges,
45 // since the allusers API won't yield results in this case.
46 this.alwaysDisableLookups = this.excludeNamed && this.excludeTemp;
47 if ( this.alwaysDisableLookups ) {
48 this.setLookupsDisabled( true );
54 OO.inheritClass( mw.widgets.UserInputWidget, OO.ui.TextInputWidget );
55 OO.mixinClass( mw.widgets.UserInputWidget, OO.ui.mixin.LookupElement );
58 * Disable or re-enable lookups, but does not apply the re-enabling of lookups if
59 * this.alwaysDisableLookups is set to true.
61 * @param {boolean} [disabled=false] Disable lookups
63 mw.widgets.UserInputWidget.prototype.setLookupsDisabled = function ( disabled ) {
64 this.lookupsDisabled = !!disabled || this.alwaysDisableLookups;
70 * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
72 * @param {OO.ui.MenuOptionWidget} item Selected item
74 mw.widgets.UserInputWidget.prototype.onLookupMenuChoose = function ( item ) {
75 this.closeLookupMenu();
76 this.setLookupsDisabled( true );
77 this.setValue( item.getData() );
78 this.setLookupsDisabled( false );
84 mw.widgets.UserInputWidget.prototype.focus = function () {
85 // Prevent programmatic focus from opening the menu
86 this.setLookupsDisabled( true );
89 const retval = mw.widgets.UserInputWidget.super.prototype.focus.apply( this, arguments );
91 this.setLookupsDisabled( false );
99 mw.widgets.UserInputWidget.prototype.getLookupRequest = function () {
100 return this.api.get( {
103 auprefix: this.value,
105 auexcludenamed: this.excludeNamed,
106 auexcludetemp: this.excludeTemp
111 * Get lookup cache item from server response data.
114 * @param {any} response Response from server
117 mw.widgets.UserInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
118 return response.query.allusers || {};
122 * Get list of menu items from a server response.
124 * @param {Object} data Query result
125 * @return {OO.ui.MenuOptionWidget[]} Menu items
127 mw.widgets.UserInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
130 for ( let i = 0, len = data.length; i < len; i++ ) {
131 const user = data[ i ] || {};
132 items.push( new OO.ui.MenuOptionWidget( {