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;
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( {
42 $autoCloseIgnore
: this.$overlay
,
47 .addClass( 'mw-rcfilters-ui-menuSelectWidget-group' )
49 this.setClippableElement( this.$body
);
50 this.setClippableContainer( this.$element
);
52 header
= new mw
.rcfilters
.ui
.FilterMenuHeaderWidget(
56 $overlay
: this.$overlay
60 this.noResults
= new OO
.ui
.LabelWidget( {
61 label
: mw
.msg( 'rcfilters-filterlist-noresults' ),
62 classes
: [ 'mw-rcfilters-ui-menuSelectWidget-noresults' ]
66 this.model
.connect( this, {
67 update
: 'onModelUpdate',
68 initialize
: 'onModelInitialize'
73 .addClass( 'mw-rcfilters-ui-menuSelectWidget' )
74 .append( header
.$element
)
77 .append( this.$group
, this.noResults
.$element
)
80 // Append all footers; we will control their visibility
82 config
.footers
= config
.footers
|| [];
83 config
.footers
.forEach( function ( footerData
) {
84 var isSticky
= footerData
.sticky
=== undefined ? true : !!footerData
.sticky
,
86 // Wrap the element with our own footer wrapper
87 $element
: $( '<div>' )
88 .addClass( 'mw-rcfilters-ui-menuSelectWidget-footer' )
89 .addClass( 'mw-rcfilters-ui-menuSelectWidget-footer-' + footerData
.name
)
90 .append( footerData
.$element
),
91 views
: footerData
.views
94 if ( !footerData
.disabled
) {
95 this.footers
.push( adjustedData
);
98 this.$element
.append( adjustedData
.$element
);
100 this.$body
.append( adjustedData
.$element
);
105 // Switch to the correct view
106 this.switchView( this.model
.getCurrentView() );
111 OO
.inheritClass( mw
.rcfilters
.ui
.MenuSelectWidget
, OO
.ui
.MenuSelectWidget
);
116 * @event itemVisibilityChange
118 * Item visibility has changed
124 * Respond to model update event
126 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.onModelUpdate = function () {
128 this.switchView( this.model
.getCurrentView() );
132 * Respond to model initialize event. Populate the menu from the model
134 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.onModelInitialize = function () {
137 groups
= this.model
.getFilterGroups();
142 // Count groups per view
143 $.each( groups
, function ( groupName
, groupModel
) {
144 if ( !groupModel
.isHidden() ) {
145 viewGroupCount
[ groupModel
.getView() ] = viewGroupCount
[ groupModel
.getView() ] || 0;
146 viewGroupCount
[ groupModel
.getView() ]++;
150 $.each( groups
, function ( groupName
, groupModel
) {
151 var currentItems
= [],
152 view
= groupModel
.getView();
154 if ( !groupModel
.isHidden() ) {
155 if ( viewGroupCount
[ view
] > 1 ) {
156 // Only add a section header if there is more than
160 new mw
.rcfilters
.ui
.FilterMenuSectionOptionWidget(
164 $overlay
: widget
.$overlay
171 widget
.model
.getGroupFilters( groupName
).forEach( function ( filterItem
) {
173 new mw
.rcfilters
.ui
.FilterMenuOptionWidget(
177 $overlay
: widget
.$overlay
183 // Cache the items per view, so we can switch between them
184 // without rebuilding the widgets each time
185 widget
.views
[ view
] = widget
.views
[ view
] || [];
186 widget
.views
[ view
] = widget
.views
[ view
].concat( currentItems
);
190 this.switchView( this.model
.getCurrentView() );
196 * @param {string} [viewName] View name. If not given, default is used.
198 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.switchView = function ( viewName
) {
199 viewName
= viewName
|| 'default';
201 if ( this.views
[ viewName
] && this.currentView
!== viewName
) {
203 this.addItems( this.views
[ viewName
] );
204 this.updateFooterVisibility( viewName
);
207 .data( 'view', viewName
)
208 .removeClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + this.currentView
)
209 .addClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + viewName
);
211 this.currentView
= viewName
;
218 * Go over the available footers and decide which should be visible
221 * @param {string} [currentView] Current view
223 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.updateFooterVisibility = function ( currentView
) {
224 currentView
= currentView
|| this.model
.getCurrentView();
226 this.footers
.forEach( function ( data
) {
227 data
.$element
.toggle(
228 // This footer should only be shown if it is configured
229 // for all views or for this specific view
230 !data
.views
|| data
.views
.length
=== 0 || data
.views
.indexOf( currentView
) > -1
236 * @fires itemVisibilityChange
239 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.updateItemVisibility = function () {
241 itemWasSelected
= false,
242 inputVal
= this.$input
.val(),
243 items
= this.getItems();
245 // Since the method hides/shows items, we don't want to
246 // call it unless the input actually changed
248 !this.userSelecting
&&
249 this.inputValue
!== inputVal
252 mw
.rcfilters
.ui
.MenuSelectWidget
.parent
.prototype.updateItemVisibility
.call( this );
254 // Select the first item in the list
255 for ( i
= 0; i
< items
.length
; i
++ ) {
257 !( items
[ i
] instanceof OO
.ui
.MenuSectionOptionWidget
) &&
258 items
[ i
].isVisible()
260 itemWasSelected
= true;
261 this.selectItem( items
[ i
] );
266 if ( !itemWasSelected
) {
267 this.selectItem( null );
271 this.inputValue
= inputVal
;
273 this.emit( 'itemVisibilityChange' );
276 this.noResults
.toggle( !this.getItems().some( function ( item
) {
277 return item
.isVisible();
282 * Get the option widget that matches the model given
284 * @param {mw.rcfilters.dm.ItemModel} model Item model
285 * @return {mw.rcfilters.ui.ItemMenuOptionWidget} Option widget
287 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.getItemFromModel = function ( model
) {
288 return this.views
[ model
.getGroupModel().getView() ].filter( function ( item
) {
289 return item
.getName() === model
.getName();
294 * Override the item matcher to use the model's match process
298 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.getItemMatcher = function ( s
) {
299 var results
= this.model
.findMatches( s
, true );
301 return function ( item
) {
302 return results
.indexOf( item
.getModel() ) > -1;
309 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.onKeyDown = function ( e
) {
311 currentItem
= this.getHighlightedItem() || this.getSelectedItem();
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.getRelativeSelectableItem( currentItem
, -1 );
326 case OO
.ui
.Keys
.DOWN
:
327 case OO
.ui
.Keys
.RIGHT
:
329 nextItem
= this.getRelativeSelectableItem( 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
;