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 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;
34 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.call( this, $.extend( true, {
35 label
: mw
.msg( 'rcfilters-filterlist-title' ),
36 placeholder
: mw
.msg( 'rcfilters-empty-filter' ),
37 inputPosition
: 'outline',
38 allowArbitrary
: false,
39 allowDisplayInvalidTags
: false,
40 allowReordering
: false,
41 $overlay
: this.$overlay
,
43 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 this.saveQueryButton
= new mw
.rcfilters
.ui
.SaveFiltersPopupButtonWidget(
92 this.saveQueryButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
94 this.saveQueryButton
.connect( this, {
95 click
: 'onSaveQueryButtonClick',
96 saveCurrent
: 'setSavedQueryVisibility'
99 this.emptyFilterMessage
= new OO
.ui
.LabelWidget( {
100 label
: mw
.msg( 'rcfilters-empty-filter' ),
101 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
103 this.$content
.append( this.emptyFilterMessage
.$element
);
106 this.resetButton
.connect( this, { click
: 'onResetButtonClick' } );
107 // Stop propagation for mousedown, so that the widget doesn't
108 // trigger the focus on the input and scrolls up when we click the reset button
109 this.resetButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
110 this.model
.connect( this, {
111 initialize
: 'onModelInitialize',
112 update
: 'onModelUpdate',
113 itemUpdate
: 'onModelItemUpdate',
114 highlightChange
: 'onModelHighlightChange'
116 this.input
.connect( this, { change
: 'onInputChange' } );
117 this.queriesModel
.connect( this, { itemUpdate
: 'onSavedQueriesItemUpdate' } );
119 // The filter list and button should appear side by side regardless of how
120 // wide the button is; the button also changes its width depending
121 // on language and its state, so the safest way to present both side
122 // by side is with a table layout
123 rcFiltersRow
= $( '<div>' )
124 .addClass( 'mw-rcfilters-ui-row' )
127 .addClass( 'mw-rcfilters-ui-cell' )
128 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
133 .addClass( 'mw-rcfilters-ui-cell' )
134 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
135 .append( this.saveQueryButton
.$element
)
138 // Add a selector at the right of the input
139 this.viewsSelectWidget
= new OO
.ui
.ButtonSelectWidget( {
140 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
142 new OO
.ui
.ButtonOptionWidget( {
146 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget-label' ],
147 label
: mw
.msg( 'rcfilters-view-advanced-filters-label' )
149 new OO
.ui
.ButtonOptionWidget( {
153 title
: mw
.msg( 'rcfilters-view-namespaces-tooltip' )
155 new OO
.ui
.ButtonOptionWidget( {
159 title
: mw
.msg( 'rcfilters-view-tags-tooltip' )
164 // Rearrange the UI so the select widget is at the right of the input
165 this.$element
.append(
167 .addClass( 'mw-rcfilters-ui-table' )
170 .addClass( 'mw-rcfilters-ui-row' )
173 .addClass( 'mw-rcfilters-ui-cell' )
174 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
175 .append( this.input
.$element
),
177 .addClass( 'mw-rcfilters-ui-cell' )
178 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
179 .append( this.viewsSelectWidget
.$element
)
185 this.viewsSelectWidget
.connect( this, { choose
: 'onViewsSelectWidgetChoose' } );
189 .addClass( 'mw-rcfilters-ui-cell' )
190 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
191 .append( this.resetButton
.$element
)
195 $contentWrapper
.append(
197 this.savedQueryTitle
.$element
,
199 .addClass( 'mw-rcfilters-ui-table' )
206 this.$handle
.append( $contentWrapper
);
207 this.emptyFilterMessage
.toggle( this.isEmpty() );
208 this.savedQueryTitle
.toggle( false );
211 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
213 this.reevaluateResetRestoreState();
218 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagMultiselectWidget
, OO
.ui
.MenuTagMultiselectWidget
);
223 * Respond to view select widget choose event
225 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
227 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget
) {
228 this.controller
.switchView( buttonOptionWidget
.getData() );
229 this.viewsSelectWidget
.selectItem( null );
234 * Respond to input change event
236 * @param {string} value Value of the input
238 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputChange = function ( value
) {
241 value
= value
.trim();
243 view
= this.model
.getViewByTrigger( value
.substr( 0, 1 ) );
245 this.controller
.switchView( view
);
248 * Respond to query button click
250 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSaveQueryButtonClick = function () {
251 this.getMenu().toggle( false );
255 * Respond to save query item change. Mainly this is done to update the label in case
256 * a query item has been edited
258 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
260 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesItemUpdate = function ( item
) {
261 if ( this.matchingQuery
=== item
) {
262 // This means we just edited the item that is currently matched
263 this.savedQueryTitle
.setLabel( item
.getLabel() );
268 * Respond to menu toggle
270 * @param {boolean} isVisible Menu is visible
272 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
274 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onMenuToggle
.call( this );
277 mw
.hook( 'RcFilters.popup.open' ).fire();
279 if ( !this.getMenu().getSelectedItem() ) {
280 // If there are no selected items, scroll menu to top
281 // This has to be in a setTimeout so the menu has time
282 // to be positioned and fixed
283 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
287 this.selectTag( null );
289 // Clear input if the only thing in the input is the prefix
291 this.input
.getValue().trim() === this.model
.getViewTrigger( this.model
.getCurrentView() )
294 this.input
.setValue( '' );
297 // Log filter grouping
298 this.controller
.trackFilterGroupings( 'filtermenu' );
301 this.input
.setIcon( isVisible
? 'search' : 'menu' );
307 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputFocus = function () {
309 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
312 this.scrollToTop( this.$element
);
318 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.doInputEscape = function () {
320 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.doInputEscape
.call( this );
323 this.input
.$input
.blur();
329 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
331 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
333 this.emptyFilterMessage
.toggle( this.isEmpty() );
337 * Respond to model initialize event
339 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
340 this.setSavedQueryVisibility();
344 * Respond to model update event
346 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelUpdate = function () {
347 this.updateElementsForView();
351 * Update the elements in the widget to the current view
353 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.updateElementsForView = function () {
354 var view
= this.model
.getCurrentView(),
355 inputValue
= this.input
.getValue().trim(),
356 inputView
= this.model
.getViewByTrigger( inputValue
.substr( 0, 1 ) );
358 if ( inputView
!== 'default' ) {
359 // We have a prefix already, remove it
360 inputValue
= inputValue
.substr( 1 );
363 if ( inputView
!== view
) {
364 // Add the correct prefix
365 inputValue
= this.model
.getViewTrigger( view
) + inputValue
;
369 this.input
.setValue( inputValue
);
373 * Set the visibility of the saved query button
375 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
376 this.matchingQuery
= this.controller
.findQueryMatchingCurrentState();
378 this.savedQueryTitle
.setLabel(
379 this.matchingQuery
? this.matchingQuery
.getLabel() : ''
381 this.savedQueryTitle
.toggle( !!this.matchingQuery
);
382 this.saveQueryButton
.toggle(
387 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().trim();
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 )