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 {mw.rcfilters.dm.SavedQueriesModel} savedQueriesModel Saved queries model
12 * @param {Object} config Configuration object
13 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
15 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
= function MwRcfiltersUiFilterTagMultiselectWidget( controller
, model
, savedQueriesModel
, config
) {
17 areSavedQueriesEnabled
= mw
.config
.get( 'wgStructuredChangeFiltersEnableSaving' ),
18 title
= new OO
.ui
.LabelWidget( {
19 label
: mw
.msg( 'rcfilters-activefilters' ),
20 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-title' ]
22 $contentWrapper
= $( '<div>' )
23 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper' );
25 config
= config
|| {};
27 this.controller
= controller
;
29 this.queriesModel
= savedQueriesModel
;
30 this.$overlay
= config
.$overlay
|| this.$element
;
31 this.matchingQuery
= null;
32 this.areSavedQueriesEnabled
= areSavedQueriesEnabled
;
35 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.call( this, $.extend( true, {
36 label
: mw
.msg( 'rcfilters-filterlist-title' ),
37 placeholder
: mw
.msg( 'rcfilters-empty-filter' ),
38 inputPosition
: 'outline',
39 allowArbitrary
: false,
40 allowDisplayInvalidTags
: false,
41 allowReordering
: false,
42 $overlay
: this.$overlay
,
44 hideWhenOutOfView
: false,
49 new OO
.ui
.ButtonWidget( {
52 flags
: [ 'progressive' ],
53 label
: mw
.msg( 'rcfilters-filterlist-feedbacklink' ),
54 href
: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
60 placeholder
: mw
.msg( 'rcfilters-search-placeholder' )
64 this.savedQueryTitle
= new OO
.ui
.LabelWidget( {
66 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-savedQueryTitle' ]
69 this.resetButton
= new OO
.ui
.ButtonWidget( {
71 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-resetButton' ]
74 if ( areSavedQueriesEnabled
) {
75 this.saveQueryButton
= new mw
.rcfilters
.ui
.SaveFiltersPopupButtonWidget(
80 this.saveQueryButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
82 this.saveQueryButton
.connect( this, {
83 click
: 'onSaveQueryButtonClick',
84 saveCurrent
: 'setSavedQueryVisibility'
88 this.emptyFilterMessage
= new OO
.ui
.LabelWidget( {
89 label
: mw
.msg( 'rcfilters-empty-filter' ),
90 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
92 this.$content
.append( this.emptyFilterMessage
.$element
);
95 this.resetButton
.connect( this, { click
: 'onResetButtonClick' } );
96 // Stop propagation for mousedown, so that the widget doesn't
97 // trigger the focus on the input and scrolls up when we click the reset button
98 this.resetButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
99 this.model
.connect( this, {
100 initialize
: 'onModelInitialize',
101 itemUpdate
: 'onModelItemUpdate',
102 highlightChange
: 'onModelHighlightChange'
104 this.queriesModel
.connect( this, { itemUpdate
: 'onSavedQueriesItemUpdate' } );
106 // The filter list and button should appear side by side regardless of how
107 // wide the button is; the button also changes its width depending
108 // on language and its state, so the safest way to present both side
109 // by side is with a table layout
110 rcFiltersRow
= $( '<div>' )
111 .addClass( 'mw-rcfilters-ui-row' )
114 .addClass( 'mw-rcfilters-ui-cell' )
115 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
118 if ( areSavedQueriesEnabled
) {
121 .addClass( 'mw-rcfilters-ui-cell' )
122 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
123 .append( this.saveQueryButton
.$element
)
129 .addClass( 'mw-rcfilters-ui-cell' )
130 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
131 .append( this.resetButton
.$element
)
135 $contentWrapper
.append(
137 this.savedQueryTitle
.$element
,
139 .addClass( 'mw-rcfilters-ui-table' )
146 this.$handle
.append( $contentWrapper
);
147 this.emptyFilterMessage
.toggle( this.isEmpty() );
148 this.savedQueryTitle
.toggle( false );
151 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
153 this.populateFromModel();
154 this.reevaluateResetRestoreState();
159 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagMultiselectWidget
, OO
.ui
.MenuTagMultiselectWidget
);
164 * Respond to query button click
166 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSaveQueryButtonClick = function () {
167 this.getMenu().toggle( false );
171 * Respond to save query item change. Mainly this is done to update the label in case
172 * a query item has been edited
174 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
176 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesItemUpdate = function ( item
) {
177 if ( this.matchingQuery
=== item
) {
178 // This means we just edited the item that is currently matched
179 this.savedQueryTitle
.setLabel( item
.getLabel() );
184 * Respond to menu toggle
186 * @param {boolean} isVisible Menu is visible
188 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
190 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onMenuToggle
.call( this );
193 mw
.hook( 'RcFilters.popup.open' ).fire();
195 if ( !this.getMenu().getSelectedItem() ) {
196 // If there are no selected items, scroll menu to top
197 // This has to be in a setTimeout so the menu has time
198 // to be positioned and fixed
199 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
203 this.selectTag( null );
210 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputFocus = function () {
212 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
215 this.scrollToTop( this.$element
);
221 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
223 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
225 this.emptyFilterMessage
.toggle( this.isEmpty() );
229 * Respond to model initialize event
231 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
232 this.populateFromModel();
234 this.setSavedQueryVisibility();
238 * Set the visibility of the saved query button
240 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
241 if ( this.areSavedQueriesEnabled
) {
242 this.matchingQuery
= this.controller
.findQueryMatchingCurrentState();
244 this.savedQueryTitle
.setLabel(
245 this.matchingQuery
? this.matchingQuery
.getLabel() : ''
247 this.savedQueryTitle
.toggle( !!this.matchingQuery
);
248 this.saveQueryButton
.toggle(
255 * Respond to model itemUpdate event
257 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
259 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
263 this.model
.isHighlightEnabled() &&
264 item
.isHighlightSupported() &&
265 item
.getHighlightColor()
268 this.addTag( item
.getName(), item
.getLabel() );
270 this.removeTagByData( item
.getName() );
273 this.setSavedQueryVisibility();
275 // Re-evaluate reset state
276 this.reevaluateResetRestoreState();
282 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
284 this.menu
.getItemFromData( data
) &&
285 !this.isDuplicateData( data
)
292 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
293 this.controller
.toggleFilterSelect( item
.model
.getName() );
295 // Select the tag if it exists, or reset selection otherwise
296 this.selectTag( this.getItemFromData( item
.model
.getName() ) );
302 * Respond to highlightChange event
304 * @param {boolean} isHighlightEnabled Highlight is enabled
306 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
307 var highlightedItems
= this.model
.getHighlightedItems();
309 if ( isHighlightEnabled
) {
310 // Add capsule widgets
311 highlightedItems
.forEach( function ( filterItem
) {
312 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
315 // Remove capsule widgets if they're not selected
316 highlightedItems
.forEach( function ( filterItem
) {
317 if ( !filterItem
.isSelected() ) {
318 this.removeTagByData( filterItem
.getName() );
327 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
329 menuOption
= this.menu
.getItemFromData( tagItem
.getData() ),
330 oldInputValue
= this.input
.getValue();
333 this.input
.setValue( '' );
336 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
338 this.menu
.selectItem( menuOption
);
339 this.selectTag( tagItem
);
341 // Scroll to the item
342 if ( oldInputValue
) {
343 // We're binding a 'once' to the itemVisibilityChange event
344 // so this happens when the menu is ready after the items
345 // are visible again, in case this is done right after the
346 // user filtered the results
348 'itemVisibilityChange',
349 function () { widget
.scrollToTop( menuOption
.$element
); }
352 this.scrollToTop( menuOption
.$element
);
357 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
358 * If no items are given, reset selection from all.
360 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
361 * omit to deselect all
363 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
364 var i
, len
, selected
;
366 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
367 selected
= this.items
[ i
] === item
;
368 if ( this.items
[ i
].isSelected() !== selected
) {
369 this.items
[ i
].toggleSelected( selected
);
376 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
378 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
380 this.controller
.clearFilter( tagItem
.getName() );
386 * Respond to click event on the reset button
388 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
389 if ( this.model
.areCurrentFiltersEmpty() ) {
390 // Reset to default filters
391 this.controller
.resetToDefaults();
393 // Reset to have no filters
394 this.controller
.emptyFilters();
399 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
401 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
402 var defaultsAreEmpty
= this.model
.areDefaultFiltersEmpty(),
403 currFiltersAreEmpty
= this.model
.areCurrentFiltersEmpty(),
404 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
406 this.resetButton
.setIcon(
407 currFiltersAreEmpty
? 'history' : 'trash'
410 this.resetButton
.setLabel(
411 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
413 this.resetButton
.setTitle(
414 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
417 this.resetButton
.toggle( !hideResetButton
);
418 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
424 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
425 return new mw
.rcfilters
.ui
.MenuSelectWidget(
429 filterFromInput
: true
435 * Populate the menu from the model
437 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.populateFromModel = function () {
442 this.getMenu().clearItems();
444 $.each( this.model
.getFilterGroups(), function ( groupName
, groupModel
) {
447 new mw
.rcfilters
.ui
.FilterMenuSectionOptionWidget(
451 $overlay
: widget
.$overlay
457 widget
.model
.getGroupFilters( groupName
).forEach( function ( filterItem
) {
459 new mw
.rcfilters
.ui
.FilterMenuOptionWidget(
463 $overlay
: widget
.$overlay
470 // Add all items to the menu
471 this.getMenu().addItems( items
);
477 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
478 var filterItem
= this.model
.getItemByName( data
);
481 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
485 $overlay
: this.$overlay
492 * Scroll the element to top within its container
495 * @param {jQuery} $element Element to position
496 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
497 * much space (in pixels) above the widget.
499 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
) {
500 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
501 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
502 containerScrollTop
= $( container
).is( 'body, html' ) ? 0 : $( container
).scrollTop();
505 $( container
).animate( {
506 scrollTop
: containerScrollTop
+ pos
.top
- ( marginFromTop
|| 0 )