3 * Filter-specific CapsuleMultiselectWidget
6 * @extends OO.ui.CapsuleMultiselectWidget
9 * @param {mw.rcfilters.Controller} controller RCFilters controller
10 * @param {mw.rcfilters.dm.FiltersViewModel} model RCFilters view model
11 * @param {OO.ui.InputWidget} filterInput A filter input that focuses the capsule widget
12 * @param {Object} config Configuration object
13 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
15 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
= function MwRcfiltersUiFilterCapsuleMultiselectWidget( controller
, model
, filterInput
, config
) {
16 var title
= new OO
.ui
.LabelWidget( {
17 label
: mw
.msg( 'rcfilters-activefilters' ),
18 classes
: [ 'mw-rcfilters-ui-filterCapsuleMultiselectWidget-wrapper-content-title' ]
20 $contentWrapper
= $( '<div>' )
21 .addClass( 'mw-rcfilters-ui-filterCapsuleMultiselectWidget-wrapper' );
23 this.$overlay
= config
.$overlay
|| this.$element
;
26 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.parent
.call( this, $.extend( true, {
28 $autoCloseIgnore
: filterInput
.$element
.add( this.$overlay
),
29 $floatableContainer
: filterInput
.$element
33 this.controller
= controller
;
35 this.filterInput
= filterInput
;
36 this.isSelecting
= false;
39 this.resetButton
= new OO
.ui
.ButtonWidget( {
42 title
: mw
.msg( 'rcfilters-clear-all-filters' ),
43 classes
: [ 'mw-rcfilters-ui-filterCapsuleMultiselectWidget-resetButton' ]
46 this.emptyFilterMessage
= new OO
.ui
.LabelWidget( {
47 label
: mw
.msg( 'rcfilters-empty-filter' ),
48 classes
: [ 'mw-rcfilters-ui-filterCapsuleMultiselectWidget-emptyFilters' ]
50 this.$content
.append( this.emptyFilterMessage
.$element
);
53 this.resetButton
.connect( this, { click
: 'onResetButtonClick' } );
54 this.model
.connect( this, {
55 itemUpdate
: 'onModelItemUpdate',
56 highlightChange
: 'onModelHighlightChange'
58 this.aggregate( { click
: 'capsuleItemClick' } );
60 // Add the filterInput as trigger
61 this.filterInput
.$input
62 .on( 'focus', this.focus
.bind( this ) );
65 $contentWrapper
.append(
68 .addClass( 'mw-rcfilters-ui-table' )
70 // The filter list and button should appear side by side regardless of how
71 // wide the button is; the button also changes its width depending
72 // on language and its state, so the safest way to present both side
73 // by side is with a table layout
75 .addClass( 'mw-rcfilters-ui-row' )
78 .addClass( 'mw-rcfilters-ui-cell' )
79 .addClass( 'mw-rcfilters-ui-filterCapsuleMultiselectWidget-cell-filters' ),
81 .addClass( 'mw-rcfilters-ui-cell' )
82 .addClass( 'mw-rcfilters-ui-filterCapsuleMultiselectWidget-cell-reset' )
83 .append( this.resetButton
.$element
)
89 this.$handle
.append( $contentWrapper
);
92 .addClass( 'mw-rcfilters-ui-filterCapsuleMultiselectWidget' );
94 this.reevaluateResetRestoreState();
99 OO
.inheritClass( mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
, OO
.ui
.CapsuleMultiselectWidget
);
105 * @param {string[]} filters Array of names of removed filters
107 * Filters were removed
113 * Respond to model itemUpdate event
115 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
117 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
121 this.model
.isHighlightEnabled() &&
122 item
.isHighlightSupported() &&
123 item
.getHighlightColor()
126 this.addItemByName( item
.getName() );
128 this.removeItemByName( item
.getName() );
131 // Re-evaluate reset state
132 this.reevaluateResetRestoreState();
136 * Respond to highlightChange event
138 * @param {boolean} isHighlightEnabled Highlight is enabled
140 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
141 var highlightedItems
= this.model
.getHighlightedItems();
143 if ( isHighlightEnabled
) {
144 // Add capsule widgets
145 highlightedItems
.forEach( function ( filterItem
) {
146 this.addItemByName( filterItem
.getName() );
149 // Remove capsule widgets if they're not selected
150 highlightedItems
.forEach( function ( filterItem
) {
151 if ( !filterItem
.isSelected() ) {
152 this.removeItemByName( filterItem
.getName() );
159 * Respond to click event on the reset button
161 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.onResetButtonClick = function () {
162 if ( this.model
.areCurrentFiltersEmpty() ) {
163 // Reset to default filters
164 this.controller
.resetToDefaults();
166 // Reset to have no filters
167 this.controller
.emptyFilters();
172 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
174 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
175 var defaultsAreEmpty
= this.model
.areDefaultFiltersEmpty(),
176 currFiltersAreEmpty
= this.model
.areCurrentFiltersEmpty(),
177 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
179 this.resetButton
.setIcon(
180 currFiltersAreEmpty
? 'history' : 'trash'
183 this.resetButton
.setLabel(
184 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
187 this.resetButton
.toggle( !hideResetButton
);
188 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
192 * Mark an item widget as selected
194 * @param {mw.rcfilters.ui.CapsuleItemWidget} item Capsule widget
196 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.select = function ( item
) {
197 if ( this.selected
!== item
) {
199 if ( this.selected
) {
200 this.selected
.toggleSelected( false );
204 this.selected
= item
;
205 if ( this.selected
) {
206 item
.toggleSelected( true );
212 * Reset selection and remove selected states from all items
214 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.resetSelection = function () {
215 if ( this.selected
!== null ) {
216 this.selected
= null;
217 this.getItems().forEach( function ( capsuleWidget
) {
218 capsuleWidget
.toggleSelected( false );
226 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.createItemWidget = function ( data
) {
227 var item
= this.model
.getItemByName( data
);
233 return new mw
.rcfilters
.ui
.CapsuleItemWidget(
236 { $overlay
: this.$overlay
}
241 * Add items by their filter name
243 * @param {string} name Filter name
245 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.addItemByName = function ( name
) {
246 var item
= this.model
.getItemByName( name
);
252 // Check that the item isn't already added
253 if ( !this.getItemFromData( name
) ) {
254 this.addItems( [ this.createItemWidget( name
) ] );
259 * Remove items by their filter name
261 * @param {string} name Filter name
263 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.removeItemByName = function ( name
) {
264 this.removeItemsFromData( [ name
] );
270 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.focus = function () {
271 // Override this method; we don't want to focus on the popup, and we
272 // don't want to bind the size to the handle.
273 if ( !this.isDisabled() ) {
274 this.popup
.toggle( true );
275 this.filterInput
.$input
.get( 0 ).focus();
283 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.onFocusForPopup = function () {
284 // HACK can be removed once I21b8cff4048 is merged in oojs-ui
291 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.onKeyDown = function () {};
296 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.onPopupFocusOut = function () {};
301 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.clearInput = function () {
302 if ( this.filterInput
) {
303 this.filterInput
.setValue( '' );
305 this.menu
.toggle( false );
306 this.menu
.selectItem();
307 this.menu
.highlightItem();
313 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.removeItems = function ( items
) {
315 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.parent
.prototype.removeItems
.call( this, items
);
317 // Destroy the item widget when it is removed
318 // This is done because we re-add items by recreating them, rather than hiding them
319 // and items include popups, that will just continue to be created and appended
321 items
.forEach( function ( widget
) {
327 * Override 'editItem' since it tries to use $input which does
328 * not exist when a popup is available.
330 mw
.rcfilters
.ui
.FilterCapsuleMultiselectWidget
.prototype.editItem = function () {};
331 }( mediaWiki
, jQuery
) );