Merge "resourceloader: Introduce metric for backend response timing"
[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.inputValue = '';
37 this.$overlay = config.$overlay || this.$element;
38 this.$body = $( '<div>' ).addClass( 'mw-rcfilters-ui-menuSelectWidget-body' );
39 this.footers = [];
40
41 // Parent
42 mw.rcfilters.ui.MenuSelectWidget.parent.call( this, $.extend( {
43 $autoCloseIgnore: this.$overlay,
44 width: 650
45 }, config ) );
46 this.setGroupElement(
47 $( '<div>' )
48 .addClass( 'mw-rcfilters-ui-menuSelectWidget-group' )
49 );
50 this.setClippableElement( this.$body );
51 this.setClippableContainer( this.$element );
52
53 header = new mw.rcfilters.ui.FilterMenuHeaderWidget(
54 this.controller,
55 this.model,
56 {
57 $overlay: this.$overlay
58 }
59 );
60
61 this.noResults = new OO.ui.LabelWidget( {
62 label: mw.msg( 'rcfilters-filterlist-noresults' ),
63 classes: [ 'mw-rcfilters-ui-menuSelectWidget-noresults' ]
64 } );
65
66 // Events
67 this.model.connect( this, {
68 update: 'onModelUpdate',
69 initialize: 'onModelInitialize'
70 } );
71
72 // Initialization
73 this.$element
74 .addClass( 'mw-rcfilters-ui-menuSelectWidget' )
75 .append( header.$element )
76 .append(
77 this.$body
78 .append( this.$group, this.noResults.$element )
79 );
80
81 // Append all footers; we will control their visibility
82 // based on view
83 config.footers = config.footers || [];
84 config.footers.forEach( function ( footerData ) {
85 var isSticky = footerData.sticky === undefined ? true : !!footerData.sticky,
86 adjustedData = {
87 // Wrap the element with our own footer wrapper
88 $element: $( '<div>' )
89 .addClass( 'mw-rcfilters-ui-menuSelectWidget-footer' )
90 .addClass( 'mw-rcfilters-ui-menuSelectWidget-footer-' + footerData.name )
91 .append( footerData.$element ),
92 views: footerData.views
93 };
94
95 if ( !footerData.disabled ) {
96 this.footers.push( adjustedData );
97
98 if ( isSticky ) {
99 this.$element.append( adjustedData.$element );
100 } else {
101 this.$body.append( adjustedData.$element );
102 }
103 }
104 }.bind( this ) );
105
106 // Switch to the correct view
107 this.switchView( this.model.getCurrentView() );
108 };
109
110 /* Initialize */
111
112 OO.inheritClass( mw.rcfilters.ui.MenuSelectWidget, OO.ui.MenuSelectWidget );
113
114 /* Events */
115
116 /**
117 * @event itemVisibilityChange
118 *
119 * Item visibility has changed
120 */
121
122 /* Methods */
123
124 /**
125 * Respond to model update event
126 */
127 mw.rcfilters.ui.MenuSelectWidget.prototype.onModelUpdate = function () {
128 // Change view
129 this.switchView( this.model.getCurrentView() );
130 };
131
132 /**
133 * @inheritdoc
134 */
135 mw.rcfilters.ui.MenuSelectWidget.prototype.toggle = function ( show ) {
136 this.lazyMenuCreation();
137 mw.rcfilters.ui.MenuSelectWidget.parent.prototype.toggle.call( this, show );
138 };
139
140 /**
141 * lazy creation of the menu
142 */
143 mw.rcfilters.ui.MenuSelectWidget.prototype.lazyMenuCreation = function () {
144 var widget = this,
145 viewGroupCount = {},
146 groups = this.model.getFilterGroups();
147
148 if ( this.menuInitialized ) {
149 return;
150 }
151
152 this.menuInitialized = true;
153 // Reset
154 this.clearItems();
155
156 // Count groups per view
157 $.each( groups, function ( groupName, groupModel ) {
158 if ( !groupModel.isHidden() ) {
159 viewGroupCount[ groupModel.getView() ] = viewGroupCount[ groupModel.getView() ] || 0;
160 viewGroupCount[ groupModel.getView() ]++;
161 }
162 } );
163
164 $.each( groups, function ( groupName, groupModel ) {
165 var currentItems = [],
166 view = groupModel.getView();
167
168 if ( !groupModel.isHidden() ) {
169 if ( viewGroupCount[ view ] > 1 ) {
170 // Only add a section header if there is more than
171 // one group
172 currentItems.push(
173 // Group section
174 new mw.rcfilters.ui.FilterMenuSectionOptionWidget(
175 widget.controller,
176 groupModel,
177 {
178 $overlay: widget.$overlay
179 }
180 )
181 );
182 }
183
184 // Add items
185 widget.model.getGroupFilters( groupName ).forEach( function ( filterItem ) {
186 currentItems.push(
187 new mw.rcfilters.ui.FilterMenuOptionWidget(
188 widget.controller,
189 widget.model.getInvertModel(),
190 filterItem,
191 {
192 $overlay: widget.$overlay
193 }
194 )
195 );
196 } );
197
198 // Cache the items per view, so we can switch between them
199 // without rebuilding the widgets each time
200 widget.views[ view ] = widget.views[ view ] || [];
201 widget.views[ view ] = widget.views[ view ].concat( currentItems );
202 }
203 } );
204
205 this.switchView( this.model.getCurrentView() );
206 };
207
208 /**
209 * Respond to model initialize event. Populate the menu from the model
210 */
211 mw.rcfilters.ui.MenuSelectWidget.prototype.onModelInitialize = function () {
212 this.menuInitialized = false;
213 };
214
215 /**
216 * Switch view
217 *
218 * @param {string} [viewName] View name. If not given, default is used.
219 */
220 mw.rcfilters.ui.MenuSelectWidget.prototype.switchView = function ( viewName ) {
221 viewName = viewName || 'default';
222
223 if ( this.views[ viewName ] && this.currentView !== viewName ) {
224 this.clearItems();
225 this.addItems( this.views[ viewName ] );
226 this.updateFooterVisibility( viewName );
227
228 this.$element
229 .data( 'view', viewName )
230 .removeClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + this.currentView )
231 .addClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + viewName );
232
233 this.currentView = viewName;
234 this.scrollToTop();
235 this.clip();
236 }
237 };
238
239 /**
240 * Go over the available footers and decide which should be visible
241 * for this view
242 *
243 * @param {string} [currentView] Current view
244 */
245 mw.rcfilters.ui.MenuSelectWidget.prototype.updateFooterVisibility = function ( currentView ) {
246 currentView = currentView || this.model.getCurrentView();
247
248 this.footers.forEach( function ( data ) {
249 data.$element.toggle(
250 // This footer should only be shown if it is configured
251 // for all views or for this specific view
252 !data.views || data.views.length === 0 || data.views.indexOf( currentView ) > -1
253 );
254 } );
255 };
256
257 /**
258 * @fires itemVisibilityChange
259 * @inheritdoc
260 */
261 mw.rcfilters.ui.MenuSelectWidget.prototype.updateItemVisibility = function () {
262 var i,
263 itemWasSelected = false,
264 inputVal = this.$input.val(),
265 items = this.getItems();
266
267 // Since the method hides/shows items, we don't want to
268 // call it unless the input actually changed
269 if (
270 !this.userSelecting &&
271 this.inputValue !== inputVal
272 ) {
273 // Parent method
274 mw.rcfilters.ui.MenuSelectWidget.parent.prototype.updateItemVisibility.call( this );
275
276 // Select the first item in the list
277 for ( i = 0; i < items.length; i++ ) {
278 if (
279 !( items[ i ] instanceof OO.ui.MenuSectionOptionWidget ) &&
280 items[ i ].isVisible()
281 ) {
282 itemWasSelected = true;
283 this.selectItem( items[ i ] );
284 break;
285 }
286 }
287
288 if ( !itemWasSelected ) {
289 this.selectItem( null );
290 }
291
292 // Cache value
293 this.inputValue = inputVal;
294
295 this.emit( 'itemVisibilityChange' );
296 }
297
298 this.noResults.toggle( !this.getItems().some( function ( item ) {
299 return item.isVisible();
300 } ) );
301 };
302
303 /**
304 * Get the option widget that matches the model given
305 *
306 * @param {mw.rcfilters.dm.ItemModel} model Item model
307 * @return {mw.rcfilters.ui.ItemMenuOptionWidget} Option widget
308 */
309 mw.rcfilters.ui.MenuSelectWidget.prototype.getItemFromModel = function ( model ) {
310 this.lazyMenuCreation();
311 return this.views[ model.getGroupModel().getView() ].filter( function ( item ) {
312 return item.getName() === model.getName();
313 } )[ 0 ];
314 };
315
316 /**
317 * Override the item matcher to use the model's match process
318 *
319 * @inheritdoc
320 */
321 mw.rcfilters.ui.MenuSelectWidget.prototype.getItemMatcher = function ( s ) {
322 var results = this.model.findMatches( s, true );
323
324 return function ( item ) {
325 return results.indexOf( item.getModel() ) > -1;
326 };
327 };
328
329 /**
330 * @inheritdoc
331 */
332 mw.rcfilters.ui.MenuSelectWidget.prototype.onKeyDown = function ( e ) {
333 var nextItem,
334 currentItem = this.getHighlightedItem() || this.getSelectedItem();
335
336 // Call parent
337 mw.rcfilters.ui.MenuSelectWidget.parent.prototype.onKeyDown.call( this, e );
338
339 // We want to select the item on arrow movement
340 // rather than just highlight it, like the menu
341 // does by default
342 if ( !this.isDisabled() && this.isVisible() ) {
343 switch ( e.keyCode ) {
344 case OO.ui.Keys.UP:
345 case OO.ui.Keys.LEFT:
346 // Get the next item
347 nextItem = this.findRelativeSelectableItem( currentItem, -1 );
348 break;
349 case OO.ui.Keys.DOWN:
350 case OO.ui.Keys.RIGHT:
351 // Get the next item
352 nextItem = this.findRelativeSelectableItem( currentItem, 1 );
353 break;
354 }
355
356 nextItem = nextItem && nextItem.constructor.static.selectable ?
357 nextItem : null;
358
359 // Select the next item
360 this.selectItem( nextItem );
361 }
362 };
363
364 /**
365 * Scroll to the top of the menu
366 */
367 mw.rcfilters.ui.MenuSelectWidget.prototype.scrollToTop = function () {
368 this.$body.scrollTop( 0 );
369 };
370
371 /**
372 * Set whether the user is currently selecting an item.
373 * This is important when the user selects an item that is in between
374 * different views, and makes sure we do not re-select a different
375 * item (like the item on top) when this is happening.
376 *
377 * @param {boolean} isSelecting User is selecting
378 */
379 mw.rcfilters.ui.MenuSelectWidget.prototype.setUserSelecting = function ( isSelecting ) {
380 this.userSelecting = !!isSelecting;
381 };
382 }( mediaWiki ) );