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 $.each( groups
, function ( groupName
, groupModel
) {
153 if ( !groupModel
.isHidden() ) {
154 viewGroupCount
[ groupModel
.getView() ] = viewGroupCount
[ groupModel
.getView() ] || 0;
155 viewGroupCount
[ groupModel
.getView() ]++;
159 $.each( groups
, function ( groupName
, groupModel
) {
160 var currentItems
= [],
161 view
= groupModel
.getView();
163 if ( !groupModel
.isHidden() ) {
164 if ( viewGroupCount
[ view
] > 1 ) {
165 // Only add a section header if there is more than
169 new mw
.rcfilters
.ui
.FilterMenuSectionOptionWidget(
173 $overlay
: widget
.$overlay
180 widget
.model
.getGroupFilters( groupName
).forEach( function ( filterItem
) {
182 new mw
.rcfilters
.ui
.FilterMenuOptionWidget(
185 widget
.model
.getInvertModel(),
187 widget
.highlightPopup
,
189 $overlay
: widget
.$overlay
195 // Cache the items per view, so we can switch between them
196 // without rebuilding the widgets each time
197 widget
.views
[ view
] = widget
.views
[ view
] || [];
198 widget
.views
[ view
] = widget
.views
[ view
].concat( currentItems
);
199 items
= items
.concat( currentItems
);
203 this.addItems( items
);
208 * Respond to model initialize event. Populate the menu from the model
210 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.onModelInitialize = function () {
211 this.menuInitialized
= false;
212 // Set timeout for the menu to lazy build.
213 setTimeout( this.lazyMenuCreation
.bind( this ) );
219 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.updateView = function () {
220 var viewName
= this.model
.getCurrentView();
222 if ( this.views
[ viewName
] && this.currentView
!== viewName
) {
223 this.updateFooterVisibility( viewName
);
226 .data( 'view', viewName
)
227 .removeClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + this.currentView
)
228 .addClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + viewName
);
230 this.currentView
= viewName
;
234 this.postProcessItems();
239 * Go over the available footers and decide which should be visible
242 * @param {string} [currentView] Current view
244 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.updateFooterVisibility = function ( currentView
) {
245 currentView
= currentView
|| this.model
.getCurrentView();
247 this.footers
.forEach( function ( data
) {
248 data
.$element
.toggle(
249 // This footer should only be shown if it is configured
250 // for all views or for this specific view
251 !data
.views
|| data
.views
.length
=== 0 || data
.views
.indexOf( currentView
) > -1
257 * Post-process items after the visibility changed. Make sure
258 * that we always have an item selected, and that the no-results
259 * widget appears if the menu is empty.
261 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.postProcessItems = function () {
263 itemWasSelected
= false,
264 items
= this.getItems();
266 // If we are not already selecting an item, always make sure
267 // that the top item is selected
268 if ( !this.userSelecting
) {
269 // Select the first item in the list
270 for ( i
= 0; i
< items
.length
; i
++ ) {
272 !( items
[ i
] instanceof OO
.ui
.MenuSectionOptionWidget
) &&
273 items
[ i
].isVisible()
275 itemWasSelected
= true;
276 this.selectItem( items
[ i
] );
281 if ( !itemWasSelected
) {
282 this.selectItem( null );
286 this.noResults
.toggle( !this.getItems().some( function ( item
) {
287 return item
.isVisible();
292 * Get the option widget that matches the model given
294 * @param {mw.rcfilters.dm.ItemModel} model Item model
295 * @return {mw.rcfilters.ui.ItemMenuOptionWidget} Option widget
297 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.getItemFromModel = function ( model
) {
298 this.lazyMenuCreation();
299 return this.views
[ model
.getGroupModel().getView() ].filter( function ( item
) {
300 return item
.getName() === model
.getName();
307 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.onKeyDown = function ( e
) {
309 currentItem
= this.findHighlightedItem() || this.findSelectedItem();
312 mw
.rcfilters
.ui
.MenuSelectWidget
.parent
.prototype.onKeyDown
.call( this, e
);
314 // We want to select the item on arrow movement
315 // rather than just highlight it, like the menu
317 if ( !this.isDisabled() && this.isVisible() ) {
318 switch ( e
.keyCode
) {
320 case OO
.ui
.Keys
.LEFT
:
322 nextItem
= this.findRelativeSelectableItem( currentItem
, -1 );
324 case OO
.ui
.Keys
.DOWN
:
325 case OO
.ui
.Keys
.RIGHT
:
327 nextItem
= this.findRelativeSelectableItem( currentItem
, 1 );
331 nextItem
= nextItem
&& nextItem
.constructor.static.selectable
?
334 // Select the next item
335 this.selectItem( nextItem
);
340 * Scroll to the top of the menu
342 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.scrollToTop = function () {
343 this.$body
.scrollTop( 0 );
347 * Set whether the user is currently selecting an item.
348 * This is important when the user selects an item that is in between
349 * different views, and makes sure we do not re-select a different
350 * item (like the item on top) when this is happening.
352 * @param {boolean} isSelecting User is selecting
354 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.setUserSelecting = function ( isSelecting
) {
355 this.userSelecting
= !!isSelecting
;