3 * List displaying all filter groups
5 * @extends OO.ui.MenuTagMultiselectWidget
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'
81 $contentWrapper
.append(
84 .addClass( 'mw-rcfilters-ui-table' )
86 // The filter list and button should appear side by side regardless of how
87 // wide the button is; the button also changes its width depending
88 // on language and its state, so the safest way to present both side
89 // by side is with a table layout
91 .addClass( 'mw-rcfilters-ui-row' )
94 .addClass( 'mw-rcfilters-ui-cell' )
95 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' ),
97 .addClass( 'mw-rcfilters-ui-cell' )
98 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
99 .append( this.resetButton
.$element
)
105 this.$handle
.append( $contentWrapper
);
106 this.emptyFilterMessage
.toggle( this.isEmpty() );
109 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
111 this.populateFromModel();
112 this.reevaluateResetRestoreState();
117 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagMultiselectWidget
, OO
.ui
.MenuTagMultiselectWidget
);
122 * Respond to menu toggle
124 * @param {boolean} isVisible Menu is visible
126 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
128 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onMenuToggle
.call( this );
131 mw
.hook( 'RcFilters.popup.open' ).fire();
133 if ( !this.getMenu().getSelectedItem() ) {
134 // If there are no selected items, scroll menu to top
135 // This has to be in a setTimeout so the menu has time
136 // to be positioned and fixed
137 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
141 this.selectTag( null );
148 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputFocus = function () {
150 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
153 this.scrollToTop( this.$element
);
159 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
161 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
163 this.emptyFilterMessage
.toggle( this.isEmpty() );
167 * Respond to model initialize event
169 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
170 this.populateFromModel();
174 * Respond to model itemUpdate event
176 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
178 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
182 this.model
.isHighlightEnabled() &&
183 item
.isHighlightSupported() &&
184 item
.getHighlightColor()
187 this.addTag( item
.getName(), item
.getLabel() );
189 this.removeTagByData( item
.getName() );
192 // Re-evaluate reset state
193 this.reevaluateResetRestoreState();
199 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
201 this.menu
.getItemFromData( data
) &&
202 !this.isDuplicateData( data
)
209 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
210 this.controller
.toggleFilterSelect( item
.model
.getName() );
212 // Select the tag if it exists, or reset selection otherwise
213 this.selectTag( this.getItemFromData( item
.model
.getName() ) );
219 * Respond to highlightChange event
221 * @param {boolean} isHighlightEnabled Highlight is enabled
223 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
224 var highlightedItems
= this.model
.getHighlightedItems();
226 if ( isHighlightEnabled
) {
227 // Add capsule widgets
228 highlightedItems
.forEach( function ( filterItem
) {
229 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
232 // Remove capsule widgets if they're not selected
233 highlightedItems
.forEach( function ( filterItem
) {
234 if ( !filterItem
.isSelected() ) {
235 this.removeTagByData( filterItem
.getName() );
244 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
246 menuOption
= this.menu
.getItemFromData( tagItem
.getData() ),
247 oldInputValue
= this.input
.getValue();
250 this.input
.setValue( '' );
253 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
255 this.menu
.selectItem( menuOption
);
256 this.selectTag( tagItem
);
258 // Scroll to the item
259 if ( oldInputValue
) {
260 // We're binding a 'once' to the itemVisibilityChange event
261 // so this happens when the menu is ready after the items
262 // are visible again, in case this is done right after the
263 // user filtered the results
265 'itemVisibilityChange',
266 function () { widget
.scrollToTop( menuOption
.$element
); }
269 this.scrollToTop( menuOption
.$element
);
274 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
275 * If no items are given, reset selection from all.
277 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
278 * omit to deselect all
280 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
281 var i
, len
, selected
;
283 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
284 selected
= this.items
[ i
] === item
;
285 if ( this.items
[ i
].isSelected() !== selected
) {
286 this.items
[ i
].toggleSelected( selected
);
293 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
295 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
297 this.controller
.clearFilter( tagItem
.getName() );
303 * Respond to click event on the reset button
305 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
306 if ( this.model
.areCurrentFiltersEmpty() ) {
307 // Reset to default filters
308 this.controller
.resetToDefaults();
310 // Reset to have no filters
311 this.controller
.emptyFilters();
316 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
318 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
319 var defaultsAreEmpty
= this.model
.areDefaultFiltersEmpty(),
320 currFiltersAreEmpty
= this.model
.areCurrentFiltersEmpty(),
321 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
323 this.resetButton
.setIcon(
324 currFiltersAreEmpty
? 'history' : 'trash'
327 this.resetButton
.setLabel(
328 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
330 this.resetButton
.setTitle(
331 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
334 this.resetButton
.toggle( !hideResetButton
);
335 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
341 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
342 return new mw
.rcfilters
.ui
.FloatingMenuSelectWidget(
346 filterFromInput
: true
352 * Populate the menu from the model
354 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.populateFromModel = function () {
359 this.getMenu().clearItems();
361 $.each( this.model
.getFilterGroups(), function ( groupName
, groupModel
) {
364 new mw
.rcfilters
.ui
.FilterMenuSectionOptionWidget(
368 $overlay
: widget
.$overlay
374 widget
.model
.getGroupFilters( groupName
).forEach( function ( filterItem
) {
376 new mw
.rcfilters
.ui
.FilterMenuOptionWidget(
380 $overlay
: widget
.$overlay
387 // Add all items to the menu
388 this.getMenu().addItems( items
);
394 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
395 var filterItem
= this.model
.getItemByName( data
);
398 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
402 $overlay
: this.$overlay
409 * Scroll the element to top within its container
412 * @param {jQuery} $element Element to position
413 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
414 * much space (in pixels) above the widget.
416 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
) {
417 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
418 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
419 containerScrollTop
= $( container
).is( 'body, html' ) ? 0 : $( container
).scrollTop();
422 $( container
).animate( {
423 scrollTop
: containerScrollTop
+ pos
.top
- ( marginFromTop
|| 0 )