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.doInputEscape = function () {
223 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.doInputEscape
.call( this );
226 this.input
.$input
.blur();
232 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
234 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
236 this.emptyFilterMessage
.toggle( this.isEmpty() );
240 * Respond to model initialize event
242 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
243 this.populateFromModel();
245 this.setSavedQueryVisibility();
249 * Set the visibility of the saved query button
251 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
252 if ( this.areSavedQueriesEnabled
) {
253 this.matchingQuery
= this.controller
.findQueryMatchingCurrentState();
255 this.savedQueryTitle
.setLabel(
256 this.matchingQuery
? this.matchingQuery
.getLabel() : ''
258 this.savedQueryTitle
.toggle( !!this.matchingQuery
);
259 this.saveQueryButton
.toggle(
266 * Respond to model itemUpdate event
268 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
270 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
274 this.model
.isHighlightEnabled() &&
275 item
.isHighlightSupported() &&
276 item
.getHighlightColor()
279 this.addTag( item
.getName(), item
.getLabel() );
281 this.removeTagByData( item
.getName() );
284 this.setSavedQueryVisibility();
286 // Re-evaluate reset state
287 this.reevaluateResetRestoreState();
293 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
295 this.menu
.getItemFromData( data
) &&
296 !this.isDuplicateData( data
)
303 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
304 this.controller
.toggleFilterSelect( item
.model
.getName() );
306 // Select the tag if it exists, or reset selection otherwise
307 this.selectTag( this.getItemFromData( item
.model
.getName() ) );
313 * Respond to highlightChange event
315 * @param {boolean} isHighlightEnabled Highlight is enabled
317 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
318 var highlightedItems
= this.model
.getHighlightedItems();
320 if ( isHighlightEnabled
) {
321 // Add capsule widgets
322 highlightedItems
.forEach( function ( filterItem
) {
323 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
326 // Remove capsule widgets if they're not selected
327 highlightedItems
.forEach( function ( filterItem
) {
328 if ( !filterItem
.isSelected() ) {
329 this.removeTagByData( filterItem
.getName() );
338 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
340 menuOption
= this.menu
.getItemFromData( tagItem
.getData() ),
341 oldInputValue
= this.input
.getValue();
344 this.input
.setValue( '' );
347 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
349 this.menu
.selectItem( menuOption
);
350 this.selectTag( tagItem
);
352 // Scroll to the item
353 if ( oldInputValue
) {
354 // We're binding a 'once' to the itemVisibilityChange event
355 // so this happens when the menu is ready after the items
356 // are visible again, in case this is done right after the
357 // user filtered the results
359 'itemVisibilityChange',
360 function () { widget
.scrollToTop( menuOption
.$element
); }
363 this.scrollToTop( menuOption
.$element
);
368 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
369 * If no items are given, reset selection from all.
371 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
372 * omit to deselect all
374 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
375 var i
, len
, selected
;
377 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
378 selected
= this.items
[ i
] === item
;
379 if ( this.items
[ i
].isSelected() !== selected
) {
380 this.items
[ i
].toggleSelected( selected
);
387 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
389 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
391 this.controller
.clearFilter( tagItem
.getName() );
397 * Respond to click event on the reset button
399 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
400 if ( this.model
.areCurrentFiltersEmpty() ) {
401 // Reset to default filters
402 this.controller
.resetToDefaults();
404 // Reset to have no filters
405 this.controller
.emptyFilters();
410 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
412 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
413 var defaultsAreEmpty
= this.model
.areDefaultFiltersEmpty(),
414 currFiltersAreEmpty
= this.model
.areCurrentFiltersEmpty(),
415 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
417 this.resetButton
.setIcon(
418 currFiltersAreEmpty
? 'history' : 'trash'
421 this.resetButton
.setLabel(
422 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
424 this.resetButton
.setTitle(
425 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
428 this.resetButton
.toggle( !hideResetButton
);
429 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
435 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
436 return new mw
.rcfilters
.ui
.MenuSelectWidget(
440 filterFromInput
: true
446 * Populate the menu from the model
448 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.populateFromModel = function () {
453 this.getMenu().clearItems();
455 $.each( this.model
.getFilterGroups(), function ( groupName
, groupModel
) {
458 new mw
.rcfilters
.ui
.FilterMenuSectionOptionWidget(
462 $overlay
: widget
.$overlay
468 widget
.model
.getGroupFilters( groupName
).forEach( function ( filterItem
) {
470 new mw
.rcfilters
.ui
.FilterMenuOptionWidget(
474 $overlay
: widget
.$overlay
481 // Add all items to the menu
482 this.getMenu().addItems( items
);
488 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
489 var filterItem
= this.model
.getItemByName( data
);
492 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
496 $overlay
: this.$overlay
503 * Scroll the element to top within its container
506 * @param {jQuery} $element Element to position
507 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
508 * much space (in pixels) above the widget.
510 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
) {
511 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
512 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
513 containerScrollTop
= $( container
).is( 'body, html' ) ? 0 : $( container
).scrollTop();
516 $( container
).animate( {
517 scrollTop
: containerScrollTop
+ pos
.top
- ( marginFromTop
|| 0 )