3 * List displaying all filter groups
6 * @extends OO.ui.MenuTagMultiselectWidget
7 * @mixins OO.ui.mixin.PendingElement
10 * @param {mw.rcfilters.Controller} controller Controller
11 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
12 * @param {mw.rcfilters.dm.SavedQueriesModel} savedQueriesModel Saved queries model
13 * @param {Object} config Configuration object
14 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
16 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
= function MwRcfiltersUiFilterTagMultiselectWidget( controller
, model
, savedQueriesModel
, config
) {
18 areSavedQueriesEnabled
= mw
.config
.get( 'wgStructuredChangeFiltersEnableSaving' ),
19 title
= new OO
.ui
.LabelWidget( {
20 label
: mw
.msg( 'rcfilters-activefilters' ),
21 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-title' ]
23 $contentWrapper
= $( '<div>' )
24 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper' );
26 config
= config
|| {};
28 this.controller
= controller
;
30 this.queriesModel
= savedQueriesModel
;
31 this.$overlay
= config
.$overlay
|| this.$element
;
32 this.matchingQuery
= null;
33 this.areSavedQueriesEnabled
= areSavedQueriesEnabled
;
36 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.call( this, $.extend( true, {
37 label
: mw
.msg( 'rcfilters-filterlist-title' ),
38 placeholder
: mw
.msg( 'rcfilters-empty-filter' ),
39 inputPosition
: 'outline',
40 allowArbitrary
: false,
41 allowDisplayInvalidTags
: false,
42 allowReordering
: false,
43 $overlay
: this.$overlay
,
45 hideWhenOutOfView
: false,
51 disabled
: !mw
.config
.get( 'wgStructuredChangeFiltersEnableExperimentalViews' ),
53 // View select menu, appears on default view only
54 $element
: $( '<div>' )
55 .append( new mw
.rcfilters
.ui
.ViewSwitchWidget( this.controller
, this.model
).$element
),
60 // Feedback footer, appears on all views
61 $element
: $( '<div>' )
63 new OO
.ui
.ButtonWidget( {
66 flags
: [ 'progressive' ],
67 label
: mw
.msg( 'rcfilters-filterlist-feedbacklink' ),
68 href
: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
76 placeholder
: mw
.msg( 'rcfilters-search-placeholder' )
80 this.savedQueryTitle
= new OO
.ui
.LabelWidget( {
82 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-savedQueryTitle' ]
85 this.resetButton
= new OO
.ui
.ButtonWidget( {
87 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-resetButton' ]
90 if ( areSavedQueriesEnabled
) {
91 this.saveQueryButton
= new mw
.rcfilters
.ui
.SaveFiltersPopupButtonWidget(
96 this.saveQueryButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
98 this.saveQueryButton
.connect( this, {
99 click
: 'onSaveQueryButtonClick',
100 saveCurrent
: 'setSavedQueryVisibility'
104 this.emptyFilterMessage
= new OO
.ui
.LabelWidget( {
105 label
: mw
.msg( 'rcfilters-empty-filter' ),
106 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
108 this.$content
.append( this.emptyFilterMessage
.$element
);
111 this.resetButton
.connect( this, { click
: 'onResetButtonClick' } );
112 // Stop propagation for mousedown, so that the widget doesn't
113 // trigger the focus on the input and scrolls up when we click the reset button
114 this.resetButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
115 this.model
.connect( this, {
116 initialize
: 'onModelInitialize',
117 update
: 'onModelUpdate',
118 itemUpdate
: 'onModelItemUpdate',
119 highlightChange
: 'onModelHighlightChange'
121 this.input
.connect( this, { change
: 'onInputChange' } );
122 this.queriesModel
.connect( this, { itemUpdate
: 'onSavedQueriesItemUpdate' } );
124 // The filter list and button should appear side by side regardless of how
125 // wide the button is; the button also changes its width depending
126 // on language and its state, so the safest way to present both side
127 // by side is with a table layout
128 rcFiltersRow
= $( '<div>' )
129 .addClass( 'mw-rcfilters-ui-row' )
132 .addClass( 'mw-rcfilters-ui-cell' )
133 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
136 if ( areSavedQueriesEnabled
) {
139 .addClass( 'mw-rcfilters-ui-cell' )
140 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
141 .append( this.saveQueryButton
.$element
)
145 if ( mw
.config
.get( 'wgStructuredChangeFiltersEnableExperimentalViews' ) ) {
146 // Add a selector at the right of the input
147 this.viewsSelectWidget
= new OO
.ui
.ButtonSelectWidget( {
148 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
150 new OO
.ui
.ButtonOptionWidget( {
154 title
: mw
.msg( 'rcfilters-view-namespaces-tooltip' )
156 new OO
.ui
.ButtonOptionWidget( {
160 title
: mw
.msg( 'rcfilters-view-tags-tooltip' )
165 // Rearrange the UI so the select widget is at the right of the input
166 this.$element
.append(
168 .addClass( 'mw-rcfilters-ui-table' )
171 .addClass( 'mw-rcfilters-ui-row' )
174 .addClass( 'mw-rcfilters-ui-cell' )
175 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
176 .append( this.input
.$element
),
178 .addClass( 'mw-rcfilters-ui-cell' )
179 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
180 .append( this.viewsSelectWidget
.$element
)
186 this.viewsSelectWidget
.connect( this, { choose
: 'onViewsSelectWidgetChoose' } );
191 .addClass( 'mw-rcfilters-ui-cell' )
192 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
193 .append( this.resetButton
.$element
)
197 $contentWrapper
.append(
199 this.savedQueryTitle
.$element
,
201 .addClass( 'mw-rcfilters-ui-table' )
208 this.$handle
.append( $contentWrapper
);
209 this.emptyFilterMessage
.toggle( this.isEmpty() );
210 this.savedQueryTitle
.toggle( false );
213 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
215 this.reevaluateResetRestoreState();
220 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagMultiselectWidget
, OO
.ui
.MenuTagMultiselectWidget
);
225 * Respond to view select widget choose event
227 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
229 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget
) {
230 this.controller
.switchView( buttonOptionWidget
.getData() );
231 this.viewsSelectWidget
.selectItem( null );
236 * Respond to input change event
238 * @param {string} value Value of the input
240 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputChange = function ( value
) {
241 var view
= this.model
.getViewByTrigger( value
.substr( 0, 1 ) );
243 this.controller
.switchView( view
);
246 * Respond to query button click
248 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSaveQueryButtonClick = function () {
249 this.getMenu().toggle( false );
253 * Respond to save query item change. Mainly this is done to update the label in case
254 * a query item has been edited
256 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
258 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesItemUpdate = function ( item
) {
259 if ( this.matchingQuery
=== item
) {
260 // This means we just edited the item that is currently matched
261 this.savedQueryTitle
.setLabel( item
.getLabel() );
266 * Respond to menu toggle
268 * @param {boolean} isVisible Menu is visible
270 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
272 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onMenuToggle
.call( this );
275 mw
.hook( 'RcFilters.popup.open' ).fire();
277 if ( !this.getMenu().getSelectedItem() ) {
278 // If there are no selected items, scroll menu to top
279 // This has to be in a setTimeout so the menu has time
280 // to be positioned and fixed
281 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
285 this.selectTag( null );
287 // Clear input if the only thing in the input is the prefix
289 this.input
.getValue() === this.model
.getViewTrigger( this.model
.getCurrentView() )
292 this.input
.setValue( '' );
295 // Log filter grouping
296 this.controller
.trackFilterGroupings( 'filtermenu' );
299 this.input
.setIcon( isVisible
? 'search' : 'menu' );
305 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputFocus = function () {
307 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
310 this.scrollToTop( this.$element
);
316 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.doInputEscape = function () {
318 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.doInputEscape
.call( this );
321 this.input
.$input
.blur();
327 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
329 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
331 this.emptyFilterMessage
.toggle( this.isEmpty() );
335 * Respond to model initialize event
337 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
338 this.setSavedQueryVisibility();
342 * Respond to model update event
344 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelUpdate = function () {
345 this.updateElementsForView();
349 * Update the elements in the widget to the current view
351 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.updateElementsForView = function () {
352 var view
= this.model
.getCurrentView(),
353 inputValue
= this.input
.getValue(),
354 inputView
= this.model
.getViewByTrigger( inputValue
.substr( 0, 1 ) );
356 if ( inputView
!== 'default' ) {
357 // We have a prefix already, remove it
358 inputValue
= inputValue
.substr( 1 );
361 if ( inputView
!== view
) {
362 // Add the correct prefix
363 inputValue
= this.model
.getViewTrigger( view
) + inputValue
;
367 this.input
.setValue( inputValue
);
371 * Set the visibility of the saved query button
373 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
374 if ( this.areSavedQueriesEnabled
) {
375 this.matchingQuery
= this.controller
.findQueryMatchingCurrentState();
377 this.savedQueryTitle
.setLabel(
378 this.matchingQuery
? this.matchingQuery
.getLabel() : ''
380 this.savedQueryTitle
.toggle( !!this.matchingQuery
);
381 this.saveQueryButton
.toggle(
386 if ( this.matchingQuery
) {
393 * Respond to model itemUpdate event
395 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
397 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
398 if ( item
.getGroupModel().isHidden() ) {
405 this.model
.isHighlightEnabled() &&
406 item
.isHighlightSupported() &&
407 item
.getHighlightColor()
410 this.addTag( item
.getName(), item
.getLabel() );
412 this.removeTagByData( item
.getName() );
415 this.setSavedQueryVisibility();
417 // Re-evaluate reset state
418 this.reevaluateResetRestoreState();
424 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
426 this.model
.getItemByName( data
) &&
427 !this.isDuplicateData( data
)
434 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
435 this.controller
.toggleFilterSelect( item
.model
.getName() );
437 // Select the tag if it exists, or reset selection otherwise
438 this.selectTag( this.getItemFromData( item
.model
.getName() ) );
444 * Respond to highlightChange event
446 * @param {boolean} isHighlightEnabled Highlight is enabled
448 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
449 var highlightedItems
= this.model
.getHighlightedItems();
451 if ( isHighlightEnabled
) {
452 // Add capsule widgets
453 highlightedItems
.forEach( function ( filterItem
) {
454 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
457 // Remove capsule widgets if they're not selected
458 highlightedItems
.forEach( function ( filterItem
) {
459 if ( !filterItem
.isSelected() ) {
460 this.removeTagByData( filterItem
.getName() );
469 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
471 menuOption
= this.menu
.getItemFromModel( tagItem
.getModel() ),
472 oldInputValue
= this.input
.getValue();
474 this.menu
.setUserSelecting( true );
477 this.input
.setValue( '' );
480 this.controller
.switchView( tagItem
.getView() );
483 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
485 this.menu
.selectItem( menuOption
);
486 this.selectTag( tagItem
);
488 // Scroll to the item
489 if ( this.model
.removeViewTriggers( oldInputValue
) ) {
490 // We're binding a 'once' to the itemVisibilityChange event
491 // so this happens when the menu is ready after the items
492 // are visible again, in case this is done right after the
493 // user filtered the results
495 'itemVisibilityChange',
497 widget
.scrollToTop( menuOption
.$element
);
498 widget
.menu
.setUserSelecting( false );
502 this.scrollToTop( menuOption
.$element
);
503 this.menu
.setUserSelecting( false );
509 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
510 * If no items are given, reset selection from all.
512 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
513 * omit to deselect all
515 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
516 var i
, len
, selected
;
518 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
519 selected
= this.items
[ i
] === item
;
520 if ( this.items
[ i
].isSelected() !== selected
) {
521 this.items
[ i
].toggleSelected( selected
);
528 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
530 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
532 this.controller
.clearFilter( tagItem
.getName() );
538 * Respond to click event on the reset button
540 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
541 if ( this.model
.areCurrentFiltersEmpty() ) {
542 // Reset to default filters
543 this.controller
.resetToDefaults();
545 // Reset to have no filters
546 this.controller
.emptyFilters();
551 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
553 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
554 var defaultsAreEmpty
= this.model
.areDefaultFiltersEmpty(),
555 currFiltersAreEmpty
= this.model
.areCurrentFiltersEmpty(),
556 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
558 this.resetButton
.setIcon(
559 currFiltersAreEmpty
? 'history' : 'trash'
562 this.resetButton
.setLabel(
563 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
565 this.resetButton
.setTitle(
566 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
569 this.resetButton
.toggle( !hideResetButton
);
570 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
576 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
577 return new mw
.rcfilters
.ui
.MenuSelectWidget(
581 filterFromInput
: true
589 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
590 var filterItem
= this.model
.getItemByName( data
);
593 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
597 $overlay
: this.$overlay
603 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.emphasize = function () {
605 !this.$handle
.hasClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' )
608 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' )
609 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
611 setTimeout( function () {
613 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' );
615 setTimeout( function () {
617 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
618 }.bind( this ), 1000 );
619 }.bind( this ), 500 );
624 * Scroll the element to top within its container
627 * @param {jQuery} $element Element to position
628 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
629 * much space (in pixels) above the widget.
631 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
) {
632 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
633 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
634 containerScrollTop
= $( container
).is( 'body, html' ) ? 0 : $( container
).scrollTop();
637 $( container
).animate( {
638 scrollTop
: containerScrollTop
+ pos
.top
- ( marginFromTop
|| 0 )