Localisation updates from https://translatewiki.net.
[mediawiki.git] / resources / src / mediawiki.widgets / mw.widgets.UserInputWidget.js
blob86d684ca7c3e5531f4e1863d87e4821806e4abaf
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 () {
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 || {};
28                 // Parent constructor
29                 mw.widgets.UserInputWidget.super.call( this, Object.assign( {}, config, { autocomplete: false } ) );
31                 // Mixin constructors
32                 OO.ui.mixin.LookupElement.call( this, config );
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();
40                 // Initialization
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 );
49                 }
50         };
52         /* Setup */
54         OO.inheritClass( mw.widgets.UserInputWidget, OO.ui.TextInputWidget );
55         OO.mixinClass( mw.widgets.UserInputWidget, OO.ui.mixin.LookupElement );
57         /**
58          * Disable or re-enable lookups, but does not apply the re-enabling of lookups if
59          * this.alwaysDisableLookups is set to true.
60          *
61          * @param {boolean} [disabled=false] Disable lookups
62          */
63         mw.widgets.UserInputWidget.prototype.setLookupsDisabled = function ( disabled ) {
64                 this.lookupsDisabled = !!disabled || this.alwaysDisableLookups;
65         };
67         /* Methods */
69         /**
70          * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
71          *
72          * @param {OO.ui.MenuOptionWidget} item Selected item
73          */
74         mw.widgets.UserInputWidget.prototype.onLookupMenuChoose = function ( item ) {
75                 this.closeLookupMenu();
76                 this.setLookupsDisabled( true );
77                 this.setValue( item.getData() );
78                 this.setLookupsDisabled( false );
79         };
81         /**
82          * @inheritdoc
83          */
84         mw.widgets.UserInputWidget.prototype.focus = function () {
85                 // Prevent programmatic focus from opening the menu
86                 this.setLookupsDisabled( true );
88                 // Parent method
89                 const retval = mw.widgets.UserInputWidget.super.prototype.focus.apply( this, arguments );
91                 this.setLookupsDisabled( false );
93                 return retval;
94         };
96         /**
97          * @inheritdoc
98          */
99         mw.widgets.UserInputWidget.prototype.getLookupRequest = function () {
100                 return this.api.get( {
101                         action: 'query',
102                         list: 'allusers',
103                         auprefix: this.value,
104                         aulimit: this.limit,
105                         auexcludenamed: this.excludeNamed,
106                         auexcludetemp: this.excludeTemp
107                 } );
108         };
110         /**
111          * Get lookup cache item from server response data.
112          *
113          * @method
114          * @param {any} response Response from server
115          * @return {Object}
116          */
117         mw.widgets.UserInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
118                 return response.query.allusers || {};
119         };
121         /**
122          * Get list of menu items from a server response.
123          *
124          * @param {Object} data Query result
125          * @return {OO.ui.MenuOptionWidget[]} Menu items
126          */
127         mw.widgets.UserInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
128                 const items = [];
130                 for ( let i = 0, len = data.length; i < len; i++ ) {
131                         const user = data[ i ] || {};
132                         items.push( new OO.ui.MenuOptionWidget( {
133                                 label: user.name,
134                                 data: user.name
135                         } ) );
136                 }
138                 return items;
139         };
141 }() );