d4f0aea07016a5581fac66f81707baa94e4b7b28
3 * A floating menu widget for the filter list
5 * @extends OO.ui.MenuSelectWidget
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:
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.
24 mw
.rcfilters
.ui
.MenuSelectWidget
= function MwRcfiltersUiMenuSelectWidget( controller
, model
, config
) {
27 config
= config
|| {};
29 this.controller
= controller
;
31 this.currentView
= '';
33 this.userSelecting
= false;
35 this.menuInitialized
= false;
36 this.$overlay
= config
.$overlay
|| this.$element
;
37 this.$body
= $( '<div>' ).addClass( 'mw-rcfilters-ui-menuSelectWidget-body' );
41 mw
.rcfilters
.ui
.MenuSelectWidget
.parent
.call( this, $.extend( config
, {
42 $autoCloseIgnore
: this.$overlay
,
44 // Our filtering is done through the model
45 filterFromInput
: false
49 .addClass( 'mw-rcfilters-ui-menuSelectWidget-group' )
51 this.setClippableElement( this.$body
);
52 this.setClippableContainer( this.$element
);
54 header
= new mw
.rcfilters
.ui
.FilterMenuHeaderWidget(
58 $overlay
: this.$overlay
62 this.noResults
= new OO
.ui
.LabelWidget( {
63 label
: mw
.msg( 'rcfilters-filterlist-noresults' ),
64 classes
: [ 'mw-rcfilters-ui-menuSelectWidget-noresults' ]
68 this.model
.connect( this, {
69 initialize
: 'onModelInitialize',
70 searchChange
: 'onModelSearchChange'
75 .addClass( 'mw-rcfilters-ui-menuSelectWidget' )
76 .append( header
.$element
)
79 .append( this.$group
, this.noResults
.$element
)
82 // Append all footers; we will control their visibility
84 config
.footers
= config
.footers
|| [];
85 config
.footers
.forEach( function ( footerData
) {
86 var isSticky
= footerData
.sticky
=== undefined ? true : !!footerData
.sticky
,
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
96 if ( !footerData
.disabled
) {
97 this.footers
.push( adjustedData
);
100 this.$element
.append( adjustedData
.$element
);
102 this.$body
.append( adjustedData
.$element
);
107 // Switch to the correct view
113 OO
.inheritClass( mw
.rcfilters
.ui
.MenuSelectWidget
, OO
.ui
.MenuSelectWidget
);
118 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.onModelSearchChange = function () {
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' );
133 * lazy creation of the menu
135 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.lazyMenuCreation = function () {
139 groups
= this.model
.getFilterGroups();
141 if ( this.menuInitialized
) {
145 this.menuInitialized
= true;
147 // Create shared popup for highlight buttons
148 this.highlightPopup
= new mw
.rcfilters
.ui
.HighlightPopupWidget( this.controller
);
149 this.$overlay
.append( this.highlightPopup
.$element
);
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() ]++;
160 // eslint-disable-next-line no-restricted-properties
161 $.each( groups
, function ( groupName
, groupModel
) {
162 var currentItems
= [],
163 view
= groupModel
.getView();
165 if ( !groupModel
.isHidden() ) {
166 if ( viewGroupCount
[ view
] > 1 ) {
167 // Only add a section header if there is more than
171 new mw
.rcfilters
.ui
.FilterMenuSectionOptionWidget(
175 $overlay
: widget
.$overlay
182 widget
.model
.getGroupFilters( groupName
).forEach( function ( filterItem
) {
184 new mw
.rcfilters
.ui
.FilterMenuOptionWidget(
187 widget
.model
.getInvertModel(),
189 widget
.highlightPopup
,
191 $overlay
: widget
.$overlay
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
);
205 this.addItems( items
);
210 * Respond to model initialize event. Populate the menu from the model
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 ) );
221 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.updateView = function () {
222 var viewName
= this.model
.getCurrentView();
224 if ( this.views
[ viewName
] && this.currentView
!== viewName
) {
225 this.updateFooterVisibility( viewName
);
228 .data( 'view', viewName
)
229 .removeClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + this.currentView
)
230 .addClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + viewName
);
232 this.currentView
= viewName
;
236 this.postProcessItems();
241 * Go over the available footers and decide which should be visible
244 * @param {string} [currentView] Current view
246 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.updateFooterVisibility = function ( currentView
) {
247 currentView
= currentView
|| this.model
.getCurrentView();
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
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.
263 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.postProcessItems = function () {
265 itemWasSelected
= false,
266 items
= this.getItems();
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
++ ) {
274 !( items
[ i
] instanceof OO
.ui
.MenuSectionOptionWidget
) &&
275 items
[ i
].isVisible()
277 itemWasSelected
= true;
278 this.selectItem( items
[ i
] );
283 if ( !itemWasSelected
) {
284 this.selectItem( null );
288 this.noResults
.toggle( !this.getItems().some( function ( item
) {
289 return item
.isVisible();
294 * Get the option widget that matches the model given
296 * @param {mw.rcfilters.dm.ItemModel} model Item model
297 * @return {mw.rcfilters.ui.ItemMenuOptionWidget} Option widget
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();
309 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.onKeyDown = function ( e
) {
311 currentItem
= this.findHighlightedItem() || this.findSelectedItem();
314 mw
.rcfilters
.ui
.MenuSelectWidget
.parent
.prototype.onKeyDown
.call( this, e
);
316 // We want to select the item on arrow movement
317 // rather than just highlight it, like the menu
319 if ( !this.isDisabled() && this.isVisible() ) {
320 switch ( e
.keyCode
) {
322 case OO
.ui
.Keys
.LEFT
:
324 nextItem
= this.findRelativeSelectableItem( currentItem
, -1 );
326 case OO
.ui
.Keys
.DOWN
:
327 case OO
.ui
.Keys
.RIGHT
:
329 nextItem
= this.findRelativeSelectableItem( currentItem
, 1 );
333 nextItem
= nextItem
&& nextItem
.constructor.static.selectable
?
336 // Select the next item
337 this.selectItem( nextItem
);
342 * Scroll to the top of the menu
344 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.scrollToTop = function () {
345 this.$body
.scrollTop( 0 );
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.
354 * @param {boolean} isSelecting User is selecting
356 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.setUserSelecting = function ( isSelecting
) {
357 this.userSelecting
= !!isSelecting
;