3 * List displaying all filter groups
5 * @extends OO.ui.Widget
6 * @mixins OO.ui.mixin.GroupWidget
7 * @mixins OO.ui.mixin.LabelElement
10 * @param {mw.rcfilters.Controller} controller Controller
11 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
12 * @param {Object} config Configuration object
14 mw
.rcfilters
.ui
.FiltersListWidget
= function MwRcfiltersUiFiltersListWidget( controller
, model
, config
) {
15 config
= config
|| {};
18 mw
.rcfilters
.ui
.FiltersListWidget
.parent
.call( this, config
);
20 OO
.ui
.mixin
.GroupWidget
.call( this, config
);
21 OO
.ui
.mixin
.LabelElement
.call( this, $.extend( {}, config
, {
23 .addClass( 'mw-rcfilters-ui-filtersListWidget-title' )
26 this.controller
= controller
;
28 this.$overlay
= config
.$overlay
|| this.$element
;
32 this.highlightButton
= new OO
.ui
.ButtonWidget( {
33 label
: mw
.message( 'rcfilters-highlightbutton-title' ).text(),
34 classes
: [ 'mw-rcfilters-ui-filtersListWidget-hightlightButton' ]
37 this.$label
.append( this.highlightButton
.$element
);
39 this.noResultsLabel
= new OO
.ui
.LabelWidget( {
40 label
: mw
.msg( 'rcfilters-filterlist-noresults' ),
41 classes
: [ 'mw-rcfilters-ui-filtersListWidget-noresults' ]
45 this.highlightButton
.connect( this, { click
: 'onHighlightButtonClick' } );
46 this.model
.connect( this, {
47 initialize
: 'onModelInitialize',
48 highlightChange
: 'onHighlightChange'
52 this.showNoResultsMessage( false );
54 .addClass( 'mw-rcfilters-ui-filtersListWidget' )
58 .addClass( 'mw-rcfilters-ui-filtersListWidget-group' ),
59 this.noResultsLabel
.$element
65 OO
.inheritClass( mw
.rcfilters
.ui
.FiltersListWidget
, OO
.ui
.Widget
);
66 OO
.mixinClass( mw
.rcfilters
.ui
.FiltersListWidget
, OO
.ui
.mixin
.GroupWidget
);
67 OO
.mixinClass( mw
.rcfilters
.ui
.FiltersListWidget
, OO
.ui
.mixin
.LabelElement
);
72 * Respond to initialize event from the model
74 mw
.rcfilters
.ui
.FiltersListWidget
.prototype.onModelInitialize = function () {
82 Object
.keys( this.model
.getFilterGroups() ).map( function ( groupName
) {
83 var groupWidget
= new mw
.rcfilters
.ui
.FilterGroupWidget(
85 widget
.model
.getGroup( groupName
),
87 $overlay
: widget
.$overlay
91 widget
.groups
[ groupName
] = groupWidget
;
97 mw
.rcfilters
.ui
.FiltersListWidget
.prototype.onHighlightChange = function ( highlightEnabled
) {
98 this.highlightButton
.setActive( highlightEnabled
);
102 * Respond to highlight button click
104 mw
.rcfilters
.ui
.FiltersListWidget
.prototype.onHighlightButtonClick = function () {
105 this.controller
.toggleHighlight();
109 * Find the filter item widget that corresponds to the item name
111 * @param {string} itemName Filter name
112 * @return {mw.rcfilters.ui.FilterItemWidget} Filter widget
114 mw
.rcfilters
.ui
.FiltersListWidget
.prototype.getItemWidget = function ( itemName
) {
115 var filterItem
= this.model
.getItemByName( itemName
),
117 groupWidget
= this.groups
[ filterItem
.getGroupName() ];
119 // Find the item inside the group
120 return groupWidget
.getItemWidget( itemName
);
124 * Mark an item widget as selected
126 * @param {string} itemName Filter name
128 mw
.rcfilters
.ui
.FiltersListWidget
.prototype.select = function ( itemName
) {
131 if ( this.selected
!== itemName
) {
133 if ( this.selected
) {
134 filterWidget
= this.getItemWidget( this.selected
);
135 filterWidget
.toggleSelected( false );
139 this.selected
= itemName
;
140 if ( this.selected
) {
141 filterWidget
= this.getItemWidget( this.selected
);
142 filterWidget
.toggleSelected( true );
148 * Reset selection and remove selected states from all items
150 mw
.rcfilters
.ui
.FiltersListWidget
.prototype.resetSelection = function () {
151 if ( this.selected
!== null ) {
152 this.selected
= null;
153 this.getItems().forEach( function ( groupWidget
) {
154 groupWidget
.getItems().forEach( function ( filterItemWidget
) {
155 filterItemWidget
.toggleSelected( false );
162 * Switch between showing the 'no results' message for filtering results or the result list.
164 * @param {boolean} showNoResults Show no results message
166 mw
.rcfilters
.ui
.FiltersListWidget
.prototype.showNoResultsMessage = function ( showNoResults
) {
167 this.noResultsLabel
.toggle( !!showNoResults
);
168 this.$group
.toggleClass( 'oo-ui-element-hidden', !!showNoResults
);
172 * Show only the items matching with the models in the given list
174 * @param {Object} groupItems An object of items to show
175 * arranged by their group names
177 mw
.rcfilters
.ui
.FiltersListWidget
.prototype.filter = function ( groupItems
) {
178 var i
, j
, groupName
, itemWidgets
,
179 groupWidgets
= this.getItems(),
180 hasItemWithName = function ( itemArr
, name
) {
181 return !!itemArr
.filter( function ( item
) {
182 return item
.getName() === name
;
186 if ( $.isEmptyObject( groupItems
) ) {
187 // No results. Hide everything, show only 'no results'
189 this.showNoResultsMessage( true );
193 this.showNoResultsMessage( false );
194 for ( i
= 0; i
< groupWidgets
.length
; i
++ ) {
195 groupName
= groupWidgets
[ i
].getName();
197 // If this group widget is in the filtered results,
198 // show it - otherwise, hide it
199 groupWidgets
[ i
].toggle( !!groupItems
[ groupName
] );
201 if ( !groupItems
[ groupName
] ) {
202 // Continue to next group
206 // We have items to show
207 itemWidgets
= groupWidgets
[ i
].getItems();
208 for ( j
= 0; j
< itemWidgets
.length
; j
++ ) {
209 // Only show items that are in the filtered list
210 itemWidgets
[ j
].toggle(
211 hasItemWithName( groupItems
[ groupName
], itemWidgets
[ j
].getName() )
216 }( mediaWiki
, jQuery
) );