3 * List displaying all filter groups
5 * @extends OO.ui.Widget
6 * @mixins OO.ui.mixin.PendingElement
9 * @param {mw.rcfilters.Controller} controller Controller
10 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
11 * @param {Object} config Configuration object
12 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
14 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
= function MwRcfiltersUiFilterTagMultiselectWidget( controller
, model
, config
) {
15 var title
= new OO
.ui
.LabelWidget( {
16 label
: mw
.msg( 'rcfilters-activefilters' ),
17 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-title' ]
19 $contentWrapper
= $( '<div>' )
20 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper' );
22 config
= config
|| {};
24 this.controller
= controller
;
26 this.$overlay
= config
.$overlay
|| this.$element
;
29 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.call( this, $.extend( true, {
30 label
: mw
.msg( 'rcfilters-filterlist-title' ),
31 placeholder
: mw
.msg( 'rcfilters-empty-filter' ),
32 inputPosition
: 'outline',
33 allowArbitrary
: false,
34 allowDisplayInvalidTags
: false,
35 allowReordering
: false,
36 $overlay
: this.$overlay
,
38 hideWhenOutOfView
: false,
43 new OO
.ui
.ButtonWidget( {
46 flags
: [ 'progressive' ],
47 label
: mw
.msg( 'rcfilters-filterlist-feedbacklink' ),
48 href
: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
54 placeholder
: mw
.msg( 'rcfilters-search-placeholder' )
58 this.resetButton
= new OO
.ui
.ButtonWidget( {
60 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-resetButton' ]
63 this.emptyFilterMessage
= new OO
.ui
.LabelWidget( {
64 label
: mw
.msg( 'rcfilters-empty-filter' ),
65 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
67 this.$content
.append( this.emptyFilterMessage
.$element
);
70 this.resetButton
.connect( this, { click
: 'onResetButtonClick' } );
71 // Stop propagation for mousedown, so that the widget doesn't
72 // trigger the focus on the input and scrolls up when we click the reset button
73 this.resetButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
74 this.model
.connect( this, {
75 initialize
: 'onModelInitialize',
76 itemUpdate
: 'onModelItemUpdate',
77 highlightChange
: 'onModelHighlightChange'
79 this.menu
.connect( this, { toggle
: 'onMenuToggle' } );
82 $contentWrapper
.append(
85 .addClass( 'mw-rcfilters-ui-table' )
87 // The filter list and button should appear side by side regardless of how
88 // wide the button is; the button also changes its width depending
89 // on language and its state, so the safest way to present both side
90 // by side is with a table layout
92 .addClass( 'mw-rcfilters-ui-row' )
95 .addClass( 'mw-rcfilters-ui-cell' )
96 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' ),
98 .addClass( 'mw-rcfilters-ui-cell' )
99 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
100 .append( this.resetButton
.$element
)
106 this.$handle
.append( $contentWrapper
);
107 this.emptyFilterMessage
.toggle( this.isEmpty() );
110 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
112 this.populateFromModel();
113 this.reevaluateResetRestoreState();
118 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagMultiselectWidget
, OO
.ui
.MenuTagMultiselectWidget
);
123 * Respond to menu toggle
125 * @param {boolean} isVisible Menu is visible
127 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
129 mw
.hook( 'RcFilters.popup.open' ).fire( this.getMenu().getSelectedItem() );
131 if ( !this.getMenu().getSelectedItem() ) {
132 // If there are no selected items, scroll menu to top
133 // This has to be in a setTimeout so the menu has time
134 // to be positioned and fixed
135 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
139 this.getMenu().selectItem( null );
146 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputFocus = function () {
148 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
151 this.scrollToTop( this.$element
);
157 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
159 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
161 this.emptyFilterMessage
.toggle( this.isEmpty() );
165 * Respond to model initialize event
167 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
168 this.populateFromModel();
172 * Respond to model itemUpdate event
174 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
176 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
180 this.model
.isHighlightEnabled() &&
181 item
.isHighlightSupported() &&
182 item
.getHighlightColor()
185 this.addTag( item
.getName(), item
.getLabel() );
187 this.removeTagByData( item
.getName() );
190 // Re-evaluate reset state
191 this.reevaluateResetRestoreState();
197 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
199 this.menu
.getItemFromData( data
) &&
200 !this.isDuplicateData( data
)
207 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
208 this.controller
.toggleFilterSelect( item
.model
.getName() );
212 * Respond to highlightChange event
214 * @param {boolean} isHighlightEnabled Highlight is enabled
216 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
217 var highlightedItems
= this.model
.getHighlightedItems();
219 if ( isHighlightEnabled
) {
220 // Add capsule widgets
221 highlightedItems
.forEach( function ( filterItem
) {
222 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
225 // Remove capsule widgets if they're not selected
226 highlightedItems
.forEach( function ( filterItem
) {
227 if ( !filterItem
.isSelected() ) {
228 this.removeTagByData( filterItem
.getName() );
237 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
239 menuOption
= this.menu
.getItemFromData( tagItem
.getData() ),
240 oldInputValue
= this.input
.getValue();
243 this.input
.setValue( '' );
246 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
248 this.menu
.selectItem( menuOption
);
250 // Scroll to the item
251 if ( oldInputValue
) {
252 // We're binding a 'once' to the itemVisibilityChange event
253 // so this happens when the menu is ready after the items
254 // are visible again, in case this is done right after the
255 // user filtered the results
257 'itemVisibilityChange',
258 function () { widget
.scrollToTop( menuOption
.$element
); }
261 this.scrollToTop( menuOption
.$element
);
268 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
270 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
272 this.controller
.clearFilter( tagItem
.getName() );
278 * Respond to click event on the reset button
280 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
281 if ( this.model
.areCurrentFiltersEmpty() ) {
282 // Reset to default filters
283 this.controller
.resetToDefaults();
285 // Reset to have no filters
286 this.controller
.emptyFilters();
291 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
293 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
294 var defaultsAreEmpty
= this.model
.areDefaultFiltersEmpty(),
295 currFiltersAreEmpty
= this.model
.areCurrentFiltersEmpty(),
296 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
298 this.resetButton
.setIcon(
299 currFiltersAreEmpty
? 'history' : 'trash'
302 this.resetButton
.setLabel(
303 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
305 this.resetButton
.setTitle(
306 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
309 this.resetButton
.toggle( !hideResetButton
);
310 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
316 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
317 return new mw
.rcfilters
.ui
.FilterFloatingMenuSelectWidget(
321 filterFromInput
: true
327 * Populate the menu from the model
329 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.populateFromModel = function () {
334 this.getMenu().clearItems();
336 $.each( this.model
.getFilterGroups(), function ( groupName
, groupModel
) {
339 new mw
.rcfilters
.ui
.FilterMenuSectionOptionWidget(
343 $overlay
: widget
.$overlay
349 widget
.model
.getGroupFilters( groupName
).forEach( function ( filterItem
) {
351 new mw
.rcfilters
.ui
.FilterMenuOptionWidget(
355 $overlay
: widget
.$overlay
362 // Add all items to the menu
363 this.getMenu().addItems( items
);
369 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
370 var filterItem
= this.model
.getItemByName( data
);
373 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
377 $overlay
: this.$overlay
384 * Scroll the element to top within its container
387 * @param {jQuery} $element Element to position
388 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
389 * much space (in pixels) above the widget.
391 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
) {
392 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
393 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
394 containerScrollTop
= $( container
).is( 'body, html' ) ? 0 : $( container
).scrollTop();
397 $( container
).animate( {
398 scrollTop
: containerScrollTop
+ pos
.top
- ( marginFromTop
|| 0 )