22c176f2825ee1849d73eb6e1bec6f2ca604afc3
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;
37 this.$overlay
= config
.$overlay
|| this.$element
;
38 this.$body
= $( '<div>' ).addClass( 'mw-rcfilters-ui-menuSelectWidget-body' );
42 mw
.rcfilters
.ui
.MenuSelectWidget
.parent
.call( this, $.extend( {
43 $autoCloseIgnore
: this.$overlay
,
48 .addClass( 'mw-rcfilters-ui-menuSelectWidget-group' )
50 this.setClippableElement( this.$body
);
51 this.setClippableContainer( this.$element
);
53 header
= new mw
.rcfilters
.ui
.FilterMenuHeaderWidget(
57 $overlay
: this.$overlay
61 this.noResults
= new OO
.ui
.LabelWidget( {
62 label
: mw
.msg( 'rcfilters-filterlist-noresults' ),
63 classes
: [ 'mw-rcfilters-ui-menuSelectWidget-noresults' ]
67 this.model
.connect( this, {
68 update
: 'onModelUpdate',
69 initialize
: 'onModelInitialize'
74 .addClass( 'mw-rcfilters-ui-menuSelectWidget' )
75 .append( header
.$element
)
78 .append( this.$group
, this.noResults
.$element
)
81 // Append all footers; we will control their visibility
83 config
.footers
= config
.footers
|| [];
84 config
.footers
.forEach( function ( footerData
) {
85 var isSticky
= footerData
.sticky
=== undefined ? true : !!footerData
.sticky
,
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
95 if ( !footerData
.disabled
) {
96 this.footers
.push( adjustedData
);
99 this.$element
.append( adjustedData
.$element
);
101 this.$body
.append( adjustedData
.$element
);
106 // Switch to the correct view
107 this.switchView( this.model
.getCurrentView() );
112 OO
.inheritClass( mw
.rcfilters
.ui
.MenuSelectWidget
, OO
.ui
.MenuSelectWidget
);
117 * @event itemVisibilityChange
119 * Item visibility has changed
125 * Respond to model update event
127 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.onModelUpdate = function () {
129 this.switchView( this.model
.getCurrentView() );
135 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.toggle = function ( show
) {
136 this.lazyMenuCreation();
137 mw
.rcfilters
.ui
.MenuSelectWidget
.parent
.prototype.toggle
.call( this, show
);
141 * lazy creation of the menu
143 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.lazyMenuCreation = function () {
146 groups
= this.model
.getFilterGroups();
148 if ( this.menuInitialized
) {
152 this.menuInitialized
= true;
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() ]++;
164 $.each( groups
, function ( groupName
, groupModel
) {
165 var currentItems
= [],
166 view
= groupModel
.getView();
168 if ( !groupModel
.isHidden() ) {
169 if ( viewGroupCount
[ view
] > 1 ) {
170 // Only add a section header if there is more than
174 new mw
.rcfilters
.ui
.FilterMenuSectionOptionWidget(
178 $overlay
: widget
.$overlay
185 widget
.model
.getGroupFilters( groupName
).forEach( function ( filterItem
) {
187 new mw
.rcfilters
.ui
.FilterMenuOptionWidget(
190 widget
.model
.getInvertModel(),
193 $overlay
: widget
.$overlay
199 // Cache the items per view, so we can switch between them
200 // without rebuilding the widgets each time
201 widget
.views
[ view
] = widget
.views
[ view
] || [];
202 widget
.views
[ view
] = widget
.views
[ view
].concat( currentItems
);
206 this.switchView( this.model
.getCurrentView() );
210 * Respond to model initialize event. Populate the menu from the model
212 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.onModelInitialize = function () {
213 this.menuInitialized
= false;
219 * @param {string} [viewName] View name. If not given, default is used.
221 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.switchView = function ( viewName
) {
222 viewName
= viewName
|| 'default';
224 if ( this.views
[ viewName
] && this.currentView
!== viewName
) {
226 this.addItems( this.views
[ viewName
] );
227 this.updateFooterVisibility( viewName
);
230 .data( 'view', viewName
)
231 .removeClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + this.currentView
)
232 .addClass( 'mw-rcfilters-ui-menuSelectWidget-view-' + viewName
);
234 this.currentView
= viewName
;
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 * @fires itemVisibilityChange
262 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.updateItemVisibility = function () {
264 itemWasSelected
= false,
265 inputVal
= this.$input
.val(),
266 items
= this.getItems();
268 // Since the method hides/shows items, we don't want to
269 // call it unless the input actually changed
271 !this.userSelecting
&&
272 this.inputValue
!== inputVal
275 mw
.rcfilters
.ui
.MenuSelectWidget
.parent
.prototype.updateItemVisibility
.call( this );
277 // Select the first item in the list
278 for ( i
= 0; i
< items
.length
; i
++ ) {
280 !( items
[ i
] instanceof OO
.ui
.MenuSectionOptionWidget
) &&
281 items
[ i
].isVisible()
283 itemWasSelected
= true;
284 this.selectItem( items
[ i
] );
289 if ( !itemWasSelected
) {
290 this.selectItem( null );
294 this.inputValue
= inputVal
;
296 this.emit( 'itemVisibilityChange' );
299 this.noResults
.toggle( !this.getItems().some( function ( item
) {
300 return item
.isVisible();
305 * Get the option widget that matches the model given
307 * @param {mw.rcfilters.dm.ItemModel} model Item model
308 * @return {mw.rcfilters.ui.ItemMenuOptionWidget} Option widget
310 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.getItemFromModel = function ( model
) {
311 this.lazyMenuCreation();
312 return this.views
[ model
.getGroupModel().getView() ].filter( function ( item
) {
313 return item
.getName() === model
.getName();
318 * Override the item matcher to use the model's match process
322 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.getItemMatcher = function ( s
) {
323 var results
= this.model
.findMatches( s
, true );
325 return function ( item
) {
326 return results
.indexOf( item
.getModel() ) > -1;
333 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.onKeyDown = function ( e
) {
335 currentItem
= this.getHighlightedItem() || this.getSelectedItem();
338 mw
.rcfilters
.ui
.MenuSelectWidget
.parent
.prototype.onKeyDown
.call( this, e
);
340 // We want to select the item on arrow movement
341 // rather than just highlight it, like the menu
343 if ( !this.isDisabled() && this.isVisible() ) {
344 switch ( e
.keyCode
) {
346 case OO
.ui
.Keys
.LEFT
:
348 nextItem
= this.findRelativeSelectableItem( currentItem
, -1 );
350 case OO
.ui
.Keys
.DOWN
:
351 case OO
.ui
.Keys
.RIGHT
:
353 nextItem
= this.findRelativeSelectableItem( currentItem
, 1 );
357 nextItem
= nextItem
&& nextItem
.constructor.static.selectable
?
360 // Select the next item
361 this.selectItem( nextItem
);
366 * Scroll to the top of the menu
368 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.scrollToTop = function () {
369 this.$body
.scrollTop( 0 );
373 * Set whether the user is currently selecting an item.
374 * This is important when the user selects an item that is in between
375 * different views, and makes sure we do not re-select a different
376 * item (like the item on top) when this is happening.
378 * @param {boolean} isSelecting User is selecting
380 mw
.rcfilters
.ui
.MenuSelectWidget
.prototype.setUserSelecting = function ( isSelecting
) {
381 this.userSelecting
= !!isSelecting
;