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,
50 // View select menu, appears on default view only
51 $element
: $( '<div>' )
52 .append( new mw
.rcfilters
.ui
.ViewSwitchWidget( this.controller
, this.model
).$element
),
57 // Feedback footer, appears on all views
58 $element
: $( '<div>' )
60 new OO
.ui
.ButtonWidget( {
63 flags
: [ 'progressive' ],
64 label
: mw
.msg( 'rcfilters-filterlist-feedbacklink' ),
65 href
: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
73 placeholder
: mw
.msg( 'rcfilters-search-placeholder' )
77 this.savedQueryTitle
= new OO
.ui
.LabelWidget( {
79 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-savedQueryTitle' ]
82 this.resetButton
= new OO
.ui
.ButtonWidget( {
84 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-resetButton' ]
87 if ( areSavedQueriesEnabled
) {
88 this.saveQueryButton
= new mw
.rcfilters
.ui
.SaveFiltersPopupButtonWidget(
93 this.saveQueryButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
95 this.saveQueryButton
.connect( this, {
96 click
: 'onSaveQueryButtonClick',
97 saveCurrent
: 'setSavedQueryVisibility'
101 this.emptyFilterMessage
= new OO
.ui
.LabelWidget( {
102 label
: mw
.msg( 'rcfilters-empty-filter' ),
103 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
105 this.$content
.append( this.emptyFilterMessage
.$element
);
108 this.resetButton
.connect( this, { click
: 'onResetButtonClick' } );
109 // Stop propagation for mousedown, so that the widget doesn't
110 // trigger the focus on the input and scrolls up when we click the reset button
111 this.resetButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
112 this.model
.connect( this, {
113 initialize
: 'onModelInitialize',
114 update
: 'onModelUpdate',
115 itemUpdate
: 'onModelItemUpdate',
116 highlightChange
: 'onModelHighlightChange'
118 this.input
.connect( this, { change
: 'onInputChange' } );
119 this.queriesModel
.connect( this, { itemUpdate
: 'onSavedQueriesItemUpdate' } );
121 // The filter list and button should appear side by side regardless of how
122 // wide the button is; the button also changes its width depending
123 // on language and its state, so the safest way to present both side
124 // by side is with a table layout
125 rcFiltersRow
= $( '<div>' )
126 .addClass( 'mw-rcfilters-ui-row' )
129 .addClass( 'mw-rcfilters-ui-cell' )
130 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
133 if ( areSavedQueriesEnabled
) {
136 .addClass( 'mw-rcfilters-ui-cell' )
137 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
138 .append( this.saveQueryButton
.$element
)
142 if ( mw
.config
.get( 'wgStructuredChangeFiltersEnableExperimentalViews' ) ) {
143 // Add a selector at the right of the input
144 this.viewsSelectWidget
= new OO
.ui
.ButtonSelectWidget( {
145 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
147 new OO
.ui
.ButtonOptionWidget( {
150 title
: mw
.msg( 'namespaces' )
152 new OO
.ui
.ButtonOptionWidget( {
155 title
: mw
.msg( 'rcfilters-view-tags' )
160 // Rearrange the UI so the select widget is at the right of the input
161 this.$element
.append(
163 .addClass( 'mw-rcfilters-ui-table' )
166 .addClass( 'mw-rcfilters-ui-row' )
169 .addClass( 'mw-rcfilters-ui-cell' )
170 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
171 .append( this.input
.$element
),
173 .addClass( 'mw-rcfilters-ui-cell' )
174 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
175 .append( this.viewsSelectWidget
.$element
)
181 this.viewsSelectWidget
.connect( this, { choose
: 'onViewsSelectWidgetChoose' } );
186 .addClass( 'mw-rcfilters-ui-cell' )
187 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
188 .append( this.resetButton
.$element
)
192 $contentWrapper
.append(
194 this.savedQueryTitle
.$element
,
196 .addClass( 'mw-rcfilters-ui-table' )
203 this.$handle
.append( $contentWrapper
);
204 this.emptyFilterMessage
.toggle( this.isEmpty() );
205 this.savedQueryTitle
.toggle( false );
208 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
210 this.reevaluateResetRestoreState();
215 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagMultiselectWidget
, OO
.ui
.MenuTagMultiselectWidget
);
220 * Respond to view select widget choose event
222 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
224 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget
) {
225 this.controller
.switchView( buttonOptionWidget
.getData() );
226 this.viewsSelectWidget
.selectItem( null );
231 * Respond to input change event
233 * @param {string} value Value of the input
235 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputChange = function ( value
) {
236 var view
= this.model
.getViewByTrigger( value
.substr( 0, 1 ) );
238 this.controller
.switchView( view
);
241 * Respond to query button click
243 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSaveQueryButtonClick = function () {
244 this.getMenu().toggle( false );
248 * Respond to save query item change. Mainly this is done to update the label in case
249 * a query item has been edited
251 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
253 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesItemUpdate = function ( item
) {
254 if ( this.matchingQuery
=== item
) {
255 // This means we just edited the item that is currently matched
256 this.savedQueryTitle
.setLabel( item
.getLabel() );
261 * Respond to menu toggle
263 * @param {boolean} isVisible Menu is visible
265 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
267 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onMenuToggle
.call( this );
270 mw
.hook( 'RcFilters.popup.open' ).fire();
272 if ( !this.getMenu().getSelectedItem() ) {
273 // If there are no selected items, scroll menu to top
274 // This has to be in a setTimeout so the menu has time
275 // to be positioned and fixed
276 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
280 this.selectTag( null );
282 // Clear input if the only thing in the input is the prefix
284 this.input
.getValue() === this.model
.getViewTrigger( this.model
.getCurrentView() )
287 this.input
.setValue( '' );
295 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputFocus = function () {
297 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
300 this.scrollToTop( this.$element
);
306 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.doInputEscape = function () {
308 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.doInputEscape
.call( this );
311 this.input
.$input
.blur();
317 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
319 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
321 this.emptyFilterMessage
.toggle( this.isEmpty() );
325 * Respond to model initialize event
327 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
328 this.setSavedQueryVisibility();
332 * Respond to model update event
334 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelUpdate = function () {
335 this.updateElementsForView();
339 * Update the elements in the widget to the current view
341 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.updateElementsForView = function () {
342 var view
= this.model
.getCurrentView(),
343 inputValue
= this.input
.getValue(),
344 inputView
= this.model
.getViewByTrigger( inputValue
.substr( 0, 1 ) );
346 if ( inputView
!== 'default' ) {
347 // We have a prefix already, remove it
348 inputValue
= inputValue
.substr( 1 );
351 if ( inputView
!== view
) {
352 // Add the correct prefix
353 inputValue
= this.model
.getViewTrigger( view
) + inputValue
;
357 this.input
.setValue( inputValue
);
361 * Set the visibility of the saved query button
363 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
364 if ( this.areSavedQueriesEnabled
) {
365 this.matchingQuery
= this.controller
.findQueryMatchingCurrentState();
367 this.savedQueryTitle
.setLabel(
368 this.matchingQuery
? this.matchingQuery
.getLabel() : ''
370 this.savedQueryTitle
.toggle( !!this.matchingQuery
);
371 this.saveQueryButton
.toggle(
379 * Respond to model itemUpdate event
381 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
383 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
387 this.model
.isHighlightEnabled() &&
388 item
.isHighlightSupported() &&
389 item
.getHighlightColor()
392 this.addTag( item
.getName(), item
.getLabel() );
394 this.removeTagByData( item
.getName() );
397 this.setSavedQueryVisibility();
399 // Re-evaluate reset state
400 this.reevaluateResetRestoreState();
406 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
408 this.model
.getItemByName( data
) &&
409 !this.isDuplicateData( data
)
416 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
417 this.controller
.toggleFilterSelect( item
.model
.getName() );
419 // Select the tag if it exists, or reset selection otherwise
420 this.selectTag( this.getItemFromData( item
.model
.getName() ) );
426 * Respond to highlightChange event
428 * @param {boolean} isHighlightEnabled Highlight is enabled
430 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
431 var highlightedItems
= this.model
.getHighlightedItems();
433 if ( isHighlightEnabled
) {
434 // Add capsule widgets
435 highlightedItems
.forEach( function ( filterItem
) {
436 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
439 // Remove capsule widgets if they're not selected
440 highlightedItems
.forEach( function ( filterItem
) {
441 if ( !filterItem
.isSelected() ) {
442 this.removeTagByData( filterItem
.getName() );
451 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
453 menuOption
= this.menu
.getItemFromModel( tagItem
.getModel() ),
454 oldInputValue
= this.input
.getValue();
457 this.input
.setValue( '' );
460 this.controller
.switchView( tagItem
.getView() );
463 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
465 this.menu
.selectItem( menuOption
);
466 this.selectTag( tagItem
);
468 // Scroll to the item
469 if ( oldInputValue
) {
470 // We're binding a 'once' to the itemVisibilityChange event
471 // so this happens when the menu is ready after the items
472 // are visible again, in case this is done right after the
473 // user filtered the results
475 'itemVisibilityChange',
476 function () { widget
.scrollToTop( menuOption
.$element
); }
479 this.scrollToTop( menuOption
.$element
);
484 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
485 * If no items are given, reset selection from all.
487 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
488 * omit to deselect all
490 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
491 var i
, len
, selected
;
493 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
494 selected
= this.items
[ i
] === item
;
495 if ( this.items
[ i
].isSelected() !== selected
) {
496 this.items
[ i
].toggleSelected( selected
);
503 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
505 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
507 this.controller
.clearFilter( tagItem
.getName() );
513 * Respond to click event on the reset button
515 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
516 if ( this.model
.areCurrentFiltersEmpty() ) {
517 // Reset to default filters
518 this.controller
.resetToDefaults();
520 // Reset to have no filters
521 this.controller
.emptyFilters();
526 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
528 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
529 var defaultsAreEmpty
= this.model
.areDefaultFiltersEmpty(),
530 currFiltersAreEmpty
= this.model
.areCurrentFiltersEmpty(),
531 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
533 this.resetButton
.setIcon(
534 currFiltersAreEmpty
? 'history' : 'trash'
537 this.resetButton
.setLabel(
538 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
540 this.resetButton
.setTitle(
541 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
544 this.resetButton
.toggle( !hideResetButton
);
545 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
551 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
552 return new mw
.rcfilters
.ui
.MenuSelectWidget(
556 filterFromInput
: true
564 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
565 var filterItem
= this.model
.getItemByName( data
);
568 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
572 $overlay
: this.$overlay
579 * Scroll the element to top within its container
582 * @param {jQuery} $element Element to position
583 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
584 * much space (in pixels) above the widget.
586 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
) {
587 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
588 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
589 containerScrollTop
= $( container
).is( 'body, html' ) ? 0 : $( container
).scrollTop();
592 $( container
).animate( {
593 scrollTop
: containerScrollTop
+ pos
.top
- ( marginFromTop
|| 0 )