Merge "ApiQueryInfo: fix query limits for testactions"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.MenuSelectWidget.js
1 ( function ( mw ) {
2 /**
3 * A floating menu widget for the filter list
4 *
5 * @extends OO.ui.MenuSelectWidget
6 *
7 * @constructor
8 * @param {mw.rcfilters.Controller} controller Controller
9 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
10 * @param {Object} [config] Configuration object
11 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
12 * @cfg {Object[]} [footers] An array of objects defining the footers for
13 * this menu, with a definition whether they appear per specific views.
14 * The expected structure is:
15 * [
16 * {
17 * name: {string} A unique name for the footer object
18 * $element: {jQuery} A jQuery object for the content of the footer
19 * views: {string[]} Optional. An array stating which views this footer is
20 * active on. Use null or omit to display this on all views.
21 * }
22 * ]
23 */
24 mw.rcfilters.ui.MenuSelectWidget = function MwRcfiltersUiMenuSelectWidget( controller, model, config ) {
25 var header;
26
27 config = config || {};
28
29 this.controller = controller;
30 this.model = model;
31 this.currentView = '';
32 this.views = {};
33 this.userSelecting = false;
34
35 this.menuInitialized = false;
36 this.$overlay = config.$overlay || this.$element;
37 this.$body = $( '<div>' ).addClass( 'mw-rcfilters-ui-menuSelectWidget-body' );
38 this.footers = [];
39
40 // Parent
41 mw.rcfilters.ui.MenuSelectWidget.parent.call( this, $.extend( config, {
42 $autoCloseIgnore: this.$overlay,
43 width: 650,
44 // Our filtering is done through the model
45 filterFromInput: false
46 } ) );
47 this.setGroupElement(
48 $( '<div>' )
49 .addClass( 'mw-rcfilters-ui-menuSelectWidget-group' )
50 );
51 this.setClippableElement( this.$body );
52 this.setClippableContainer( this.$element );
53
54 header = new mw.rcfilters.ui.FilterMenuHeaderWidget(
55 this.controller,
56 this.model,
57 {
58 $overlay: this.$overlay
59 }
60 );
61
62 this.noResults = new OO.ui.LabelWidget( {
63 label: mw.msg( 'rcfilters-filterlist-noresults' ),
64 classes: [ 'mw-rcfilters-ui-menuSelectWidget-noresults' ]
65 } );
66
67 // Events
68 this.model.connect( this, {
69 initialize: 'onModelInitialize',
70 searchChange: 'onModelSearchChange'
71 } );
72
73 // Initialization
74 this.$element
75 .addClass( 'mw-rcfilters-ui-menuSelectWidget' )
76 .append( header.$element )
77 .append(
78 this.$body
79 .append( this.$group, this.noResults.$element )
80 );
81
82 // Append all footers; we will control their visibility
83 // based on view
84 config.footers = config.footers || [];
85 config.footers.forEach( function ( footerData ) {
86 var isSticky = footerData.sticky === undefined ? true : !!footerData.sticky,
87 adjustedData = {
88 // Wrap the element with our own footer wrapper
89 $element: $( '<div>' )
90 .addClass( 'mw-rcfilters-ui-menuSelectWidget-footer' )
91 .addClass( 'mw-rcfilters-ui-menuSelectWidget-footer-' + footerData.name )
92 .append( footerData.$element ),
93 views: footerData.views
94 };
95
96 if ( !footerData.disabled ) {
97 this.footers.push( adjustedData );
98
99 if ( isSticky ) {
100 this.$element.append( adjustedData.$element );
101 } else {
102 this.$body.append( adjustedData.$element );
103 }
104 }
105 }.bind( this ) );
106
107 // Switch to the correct view
108 this.updateView();
109 };
110
111 /* Initialize */
112
113 OO.inheritClass( mw.rcfilters.ui.MenuSelectWidget, OO.ui.MenuSelectWidget );
114
115 /* Events */
116
117 /* Methods */
118 mw.rcfilters.ui.MenuSelectWidget.prototype.onModelSearchChange = function () {
119 this.updateView();
120 };
121
122 /**
123 * @inheritdoc
124 */
125 mw.rcfilters.ui.MenuSelectWidget.prototype.toggle = function ( show ) {
126 this.lazyMenuCreation();
127 mw.rcfilters.ui.MenuSelectWidget.parent.prototype.toggle.call( this, show );
128 // Always open this menu downwards. FilterTagMultiselectWidget scrolls it into view.
129 this.setVerticalPosition( 'below' );
130 };
131
132 /**
133 * lazy creation of the menu
134 */
135 mw.rcfilters.ui.MenuSelectWidget.prototype.lazyMenuCreation = function () {
136 var widget = this,
137 items = [],
138 viewGroupCount = {},
139 groups = this.model.getFilterGroups();
140
141 if ( this.menuInitialized ) {
142 return;
143 }
144
145 this.menuInitialized = true;
146
147 // Create shared popup for highlight buttons
148 this.highlightPopup = new mw.rcfilters.ui.HighlightPopupWidget( this.controller );
149 this.$overlay.append( this.highlightPopup.$element );
150
151 // Count groups per view
152 // eslint-disable-next-line no-restricted-properties
153 $.each( groups, function ( groupName, groupModel ) {
154 if ( !groupModel.isHidden() ) {
155 viewGroupCount[ groupModel.getView() ] = viewGroupCount[ groupModel.getView() ] || 0;
156 viewGroupCount[ groupModel.getView() ]++;
157 }
158 } );
159
160 // eslint-disable-next-line no-restricted-properties
161 $.each( groups, function ( groupName, groupModel ) {
162 var currentItems = [],
163 view = groupModel.getView();
164
165 if ( !groupModel.isHidden() ) {
166 if ( viewGroupCount[ view ] > 1 ) {
167 // Only add a section header if there is more than
168 // one group
169 currentItems.push(
170 // Group section
171 new mw.rcfilters.ui.FilterMenuSectionOptionWidget(
172 widget.controller,
173 groupModel,
174 {
175 $overlay: widget.$overlay
176 }
177 )
178 );
179 }
180
181 // Add items
182 widget.model.getGroupFilters( groupName ).forEach( function ( filterItem ) {
183 currentItems.push(
184 new mw.rcfilters.ui.FilterMenuOptionWidget(
185 widget.controller,
186 widget.model,
187 widget.model.getInvertModel(),
188 filterItem,
189 widget.highlightPopup,
190 {
191 $overlay: widget.$overlay
192 }
193 )
194 );
195 } );
196
197 // Cache the items per view, so we can switch between them
198 // without rebuilding the widgets each time
199 widget.views[ view ] = widget.views[ view ] || [];
200 widget.views[ view ] = widget.views[ view ].concat( currentItems );
201 items = items.concat( currentItems );
202 }
203 } );
204
205 this.addItems( items );
206 this.updateView();
207 };
208
209 /**
210 * Respond to model initialize event. Populate the menu from the model
211 */
212 mw.rcfilters.ui.MenuSelectWidget.prototype.onModelInitialize = function () {
213 this.menuInitialized = false;
214 // Set timeout for the menu to lazy build.
215 setTimeout( this.lazyMenuCreation.bind( this ) );
216 };
217
218 /**
219 * Update view
220 */
221 mw.rcfilters.ui.MenuSelectWidget.prototype.updateView = function () {
222 var viewName = this.model.getCurrentView();
223
224 if ( this.views[ viewName ] && this.currentView !== viewName ) {
225 this.updateFooterVisibility( viewName );
226
227 this.$element
228 .data( 'view', viewName )
229 .removeClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + this.currentView )
230 .addClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + viewName );
231
232 this.currentView = viewName;
233 this.scrollToTop();
234 }
235
236 this.postProcessItems();
237 this.clip();
238 };
239
240 /**
241 * Go over the available footers and decide which should be visible
242 * for this view
243 *
244 * @param {string} [currentView] Current view
245 */
246 mw.rcfilters.ui.MenuSelectWidget.prototype.updateFooterVisibility = function ( currentView ) {
247 currentView = currentView || this.model.getCurrentView();
248
249 this.footers.forEach( function ( data ) {
250 data.$element.toggle(
251 // This footer should only be shown if it is configured
252 // for all views or for this specific view
253 !data.views || data.views.length === 0 || data.views.indexOf( currentView ) > -1
254 );
255 } );
256 };
257
258 /**
259 * Post-process items after the visibility changed. Make sure
260 * that we always have an item selected, and that the no-results
261 * widget appears if the menu is empty.
262 */
263 mw.rcfilters.ui.MenuSelectWidget.prototype.postProcessItems = function () {
264 var i,
265 itemWasSelected = false,
266 items = this.getItems();
267
268 // If we are not already selecting an item, always make sure
269 // that the top item is selected
270 if ( !this.userSelecting ) {
271 // Select the first item in the list
272 for ( i = 0; i < items.length; i++ ) {
273 if (
274 !( items[ i ] instanceof OO.ui.MenuSectionOptionWidget ) &&
275 items[ i ].isVisible()
276 ) {
277 itemWasSelected = true;
278 this.selectItem( items[ i ] );
279 break;
280 }
281 }
282
283 if ( !itemWasSelected ) {
284 this.selectItem( null );
285 }
286 }
287
288 this.noResults.toggle( !this.getItems().some( function ( item ) {
289 return item.isVisible();
290 } ) );
291 };
292
293 /**
294 * Get the option widget that matches the model given
295 *
296 * @param {mw.rcfilters.dm.ItemModel} model Item model
297 * @return {mw.rcfilters.ui.ItemMenuOptionWidget} Option widget
298 */
299 mw.rcfilters.ui.MenuSelectWidget.prototype.getItemFromModel = function ( model ) {
300 this.lazyMenuCreation();
301 return this.views[ model.getGroupModel().getView() ].filter( function ( item ) {
302 return item.getName() === model.getName();
303 } )[ 0 ];
304 };
305
306 /**
307 * @inheritdoc
308 */
309 mw.rcfilters.ui.MenuSelectWidget.prototype.onKeyDown = function ( e ) {
310 var nextItem,
311 currentItem = this.findHighlightedItem() || this.findSelectedItem();
312
313 // Call parent
314 mw.rcfilters.ui.MenuSelectWidget.parent.prototype.onKeyDown.call( this, e );
315
316 // We want to select the item on arrow movement
317 // rather than just highlight it, like the menu
318 // does by default
319 if ( !this.isDisabled() && this.isVisible() ) {
320 switch ( e.keyCode ) {
321 case OO.ui.Keys.UP:
322 case OO.ui.Keys.LEFT:
323 // Get the next item
324 nextItem = this.findRelativeSelectableItem( currentItem, -1 );
325 break;
326 case OO.ui.Keys.DOWN:
327 case OO.ui.Keys.RIGHT:
328 // Get the next item
329 nextItem = this.findRelativeSelectableItem( currentItem, 1 );
330 break;
331 }
332
333 nextItem = nextItem && nextItem.constructor.static.selectable ?
334 nextItem : null;
335
336 // Select the next item
337 this.selectItem( nextItem );
338 }
339 };
340
341 /**
342 * Scroll to the top of the menu
343 */
344 mw.rcfilters.ui.MenuSelectWidget.prototype.scrollToTop = function () {
345 this.$body.scrollTop( 0 );
346 };
347
348 /**
349 * Set whether the user is currently selecting an item.
350 * This is important when the user selects an item that is in between
351 * different views, and makes sure we do not re-select a different
352 * item (like the item on top) when this is happening.
353 *
354 * @param {boolean} isSelecting User is selecting
355 */
356 mw.rcfilters.ui.MenuSelectWidget.prototype.setUserSelecting = function ( isSelecting ) {
357 this.userSelecting = !!isSelecting;
358 };
359 }( mediaWiki ) );