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( '' );
291 this.input
.setIcon( isVisible
? 'search' : 'menu' );
297 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputFocus = function () {
299 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
302 this.scrollToTop( this.$element
);
308 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.doInputEscape = function () {
310 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.doInputEscape
.call( this );
313 this.input
.$input
.blur();
319 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
321 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
323 this.emptyFilterMessage
.toggle( this.isEmpty() );
327 * Respond to model initialize event
329 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
330 this.setSavedQueryVisibility();
334 * Respond to model update event
336 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelUpdate = function () {
337 this.updateElementsForView();
341 * Update the elements in the widget to the current view
343 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.updateElementsForView = function () {
344 var view
= this.model
.getCurrentView(),
345 inputValue
= this.input
.getValue(),
346 inputView
= this.model
.getViewByTrigger( inputValue
.substr( 0, 1 ) );
348 if ( inputView
!== 'default' ) {
349 // We have a prefix already, remove it
350 inputValue
= inputValue
.substr( 1 );
353 if ( inputView
!== view
) {
354 // Add the correct prefix
355 inputValue
= this.model
.getViewTrigger( view
) + inputValue
;
359 this.input
.setValue( inputValue
);
363 * Set the visibility of the saved query button
365 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
366 if ( this.areSavedQueriesEnabled
) {
367 this.matchingQuery
= this.controller
.findQueryMatchingCurrentState();
369 this.savedQueryTitle
.setLabel(
370 this.matchingQuery
? this.matchingQuery
.getLabel() : ''
372 this.savedQueryTitle
.toggle( !!this.matchingQuery
);
373 this.saveQueryButton
.toggle(
381 * Respond to model itemUpdate event
383 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
385 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
389 this.model
.isHighlightEnabled() &&
390 item
.isHighlightSupported() &&
391 item
.getHighlightColor()
394 this.addTag( item
.getName(), item
.getLabel() );
396 this.removeTagByData( item
.getName() );
399 this.setSavedQueryVisibility();
401 // Re-evaluate reset state
402 this.reevaluateResetRestoreState();
408 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
410 this.model
.getItemByName( data
) &&
411 !this.isDuplicateData( data
)
418 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
419 this.controller
.toggleFilterSelect( item
.model
.getName() );
421 // Select the tag if it exists, or reset selection otherwise
422 this.selectTag( this.getItemFromData( item
.model
.getName() ) );
428 * Respond to highlightChange event
430 * @param {boolean} isHighlightEnabled Highlight is enabled
432 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
433 var highlightedItems
= this.model
.getHighlightedItems();
435 if ( isHighlightEnabled
) {
436 // Add capsule widgets
437 highlightedItems
.forEach( function ( filterItem
) {
438 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
441 // Remove capsule widgets if they're not selected
442 highlightedItems
.forEach( function ( filterItem
) {
443 if ( !filterItem
.isSelected() ) {
444 this.removeTagByData( filterItem
.getName() );
453 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
455 menuOption
= this.menu
.getItemFromModel( tagItem
.getModel() ),
456 oldInputValue
= this.input
.getValue();
459 this.input
.setValue( '' );
462 this.controller
.switchView( tagItem
.getView() );
465 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
467 this.menu
.selectItem( menuOption
);
468 this.selectTag( tagItem
);
470 // Scroll to the item
471 if ( this.model
.removeViewTriggers( oldInputValue
) ) {
472 // We're binding a 'once' to the itemVisibilityChange event
473 // so this happens when the menu is ready after the items
474 // are visible again, in case this is done right after the
475 // user filtered the results
477 'itemVisibilityChange',
478 function () { widget
.scrollToTop( menuOption
.$element
); }
481 this.scrollToTop( menuOption
.$element
);
486 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
487 * If no items are given, reset selection from all.
489 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
490 * omit to deselect all
492 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
493 var i
, len
, selected
;
495 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
496 selected
= this.items
[ i
] === item
;
497 if ( this.items
[ i
].isSelected() !== selected
) {
498 this.items
[ i
].toggleSelected( selected
);
505 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
507 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
509 this.controller
.clearFilter( tagItem
.getName() );
515 * Respond to click event on the reset button
517 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
518 if ( this.model
.areCurrentFiltersEmpty() ) {
519 // Reset to default filters
520 this.controller
.resetToDefaults();
522 // Reset to have no filters
523 this.controller
.emptyFilters();
528 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
530 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
531 var defaultsAreEmpty
= this.model
.areDefaultFiltersEmpty(),
532 currFiltersAreEmpty
= this.model
.areCurrentFiltersEmpty(),
533 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
535 this.resetButton
.setIcon(
536 currFiltersAreEmpty
? 'history' : 'trash'
539 this.resetButton
.setLabel(
540 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
542 this.resetButton
.setTitle(
543 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
546 this.resetButton
.toggle( !hideResetButton
);
547 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
553 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
554 return new mw
.rcfilters
.ui
.MenuSelectWidget(
558 filterFromInput
: true
566 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
567 var filterItem
= this.model
.getItemByName( data
);
570 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
574 $overlay
: this.$overlay
581 * Scroll the element to top within its container
584 * @param {jQuery} $element Element to position
585 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
586 * much space (in pixels) above the widget.
588 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
) {
589 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
590 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
591 containerScrollTop
= $( container
).is( 'body, html' ) ? 0 : $( container
).scrollTop();
594 $( container
).animate( {
595 scrollTop
: containerScrollTop
+ pos
.top
- ( marginFromTop
|| 0 )