1 var ViewSwitchWidget
= require( './ViewSwitchWidget.js' ),
2 SaveFiltersPopupButtonWidget
= require( './SaveFiltersPopupButtonWidget.js' ),
3 MenuSelectWidget
= require( './MenuSelectWidget.js' ),
4 FilterTagItemWidget
= require( './FilterTagItemWidget.js' ),
5 FilterTagMultiselectWidget
;
8 * List displaying all filter groups
10 * @class mw.rcfilters.ui.FilterTagMultiselectWidget
11 * @extends OO.ui.MenuTagMultiselectWidget
12 * @mixins OO.ui.mixin.PendingElement
15 * @param {mw.rcfilters.Controller} controller Controller
16 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
17 * @param {mw.rcfilters.dm.SavedQueriesModel} savedQueriesModel Saved queries model
18 * @param {Object} config Configuration object
19 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
20 * @cfg {jQuery} [$wrapper] A jQuery object for the wrapper of the general
21 * system. If not given, falls back to this widget's $element
22 * @cfg {boolean} [collapsed] Filter area is collapsed
24 FilterTagMultiselectWidget
= function MwRcfiltersUiFilterTagMultiselectWidget( controller
, model
, savedQueriesModel
, config
) {
26 title
= new OO
.ui
.LabelWidget( {
27 label
: mw
.msg( 'rcfilters-activefilters' ),
28 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-title' ]
30 $contentWrapper
= $( '<div>' )
31 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper' );
33 config
= config
|| {};
35 this.controller
= controller
;
37 this.queriesModel
= savedQueriesModel
;
38 this.$overlay
= config
.$overlay
|| this.$element
;
39 this.$wrapper
= config
.$wrapper
|| this.$element
;
40 this.matchingQuery
= null;
41 this.currentView
= this.model
.getCurrentView();
42 this.collapsed
= false;
45 FilterTagMultiselectWidget
.parent
.call( this, $.extend( true, {
46 label
: mw
.msg( 'rcfilters-filterlist-title' ),
47 placeholder
: mw
.msg( 'rcfilters-empty-filter' ),
48 inputPosition
: 'outline',
49 allowArbitrary
: false,
50 allowDisplayInvalidTags
: false,
51 allowReordering
: false,
52 $overlay
: this.$overlay
,
54 // Our filtering is done through the model
55 filterFromInput
: false,
56 hideWhenOutOfView
: false,
63 // View select menu, appears on default view only
64 $element
: $( '<div>' )
65 .append( new ViewSwitchWidget( this.controller
, this.model
).$element
),
70 // Feedback footer, appears on all views
71 $element
: $( '<div>' )
73 new OO
.ui
.ButtonWidget( {
76 flags
: [ 'progressive' ],
77 label
: mw
.msg( 'rcfilters-filterlist-feedbacklink' ),
78 href
: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
86 placeholder
: mw
.msg( 'rcfilters-search-placeholder' )
90 this.savedQueryTitle
= new OO
.ui
.LabelWidget( {
92 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-savedQueryTitle' ]
95 this.resetButton
= new OO
.ui
.ButtonWidget( {
97 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-resetButton' ]
100 this.hideShowButton
= new OO
.ui
.ButtonWidget( {
102 flags
: [ 'progressive' ],
103 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-hideshowButton' ]
105 this.toggleCollapsed( !!config
.collapsed
);
107 if ( !mw
.user
.isAnon() ) {
108 this.saveQueryButton
= new SaveFiltersPopupButtonWidget(
112 $overlay
: this.$overlay
116 this.saveQueryButton
.$element
.on( 'mousedown', function ( e
) {
120 this.saveQueryButton
.connect( this, {
121 click
: 'onSaveQueryButtonClick',
122 saveCurrent
: 'setSavedQueryVisibility'
124 this.queriesModel
.connect( this, {
125 itemUpdate
: 'onSavedQueriesItemUpdate',
126 initialize
: 'onSavedQueriesInitialize',
127 default: 'reevaluateResetRestoreState'
131 this.emptyFilterMessage
= new OO
.ui
.LabelWidget( {
132 label
: mw
.msg( 'rcfilters-empty-filter' ),
133 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
135 this.$content
.append( this.emptyFilterMessage
.$element
);
138 this.resetButton
.connect( this, { click
: 'onResetButtonClick' } );
139 this.hideShowButton
.connect( this, { click
: 'onHideShowButtonClick' } );
140 // Stop propagation for mousedown, so that the widget doesn't
141 // trigger the focus on the input and scrolls up when we click the reset button
142 this.resetButton
.$element
.on( 'mousedown', function ( e
) {
145 this.hideShowButton
.$element
.on( 'mousedown', function ( e
) {
148 this.model
.connect( this, {
149 initialize
: 'onModelInitialize',
150 update
: 'onModelUpdate',
151 searchChange
: 'onModelSearchChange',
152 itemUpdate
: 'onModelItemUpdate',
153 highlightChange
: 'onModelHighlightChange'
155 this.input
.connect( this, { change
: 'onInputChange' } );
157 // The filter list and button should appear side by side regardless of how
158 // wide the button is; the button also changes its width depending
159 // on language and its state, so the safest way to present both side
160 // by side is with a table layout
161 rcFiltersRow
= $( '<div>' )
162 .addClass( 'mw-rcfilters-ui-row' )
165 .addClass( 'mw-rcfilters-ui-cell' )
166 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
169 if ( !mw
.user
.isAnon() ) {
172 .addClass( 'mw-rcfilters-ui-cell' )
173 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
174 .append( this.saveQueryButton
.$element
)
178 // Add a selector at the right of the input
179 this.viewsSelectWidget
= new OO
.ui
.ButtonSelectWidget( {
180 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
182 new OO
.ui
.ButtonOptionWidget( {
186 label
: mw
.msg( 'namespaces' ),
187 title
: mw
.msg( 'rcfilters-view-namespaces-tooltip' )
189 new OO
.ui
.ButtonOptionWidget( {
193 label
: mw
.msg( 'tags-title' ),
194 title
: mw
.msg( 'rcfilters-view-tags-tooltip' )
199 // Rearrange the UI so the select widget is at the right of the input
200 this.$element
.append(
202 .addClass( 'mw-rcfilters-ui-table' )
205 .addClass( 'mw-rcfilters-ui-row' )
206 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views' )
209 .addClass( 'mw-rcfilters-ui-cell' )
210 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
211 .append( this.input
.$element
),
213 .addClass( 'mw-rcfilters-ui-cell' )
214 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
215 .append( this.viewsSelectWidget
.$element
)
221 this.viewsSelectWidget
.connect( this, { choose
: 'onViewsSelectWidgetChoose' } );
225 .addClass( 'mw-rcfilters-ui-cell' )
226 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
227 .append( this.resetButton
.$element
)
231 $contentWrapper
.append(
233 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-top' )
236 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-top-title' )
237 .append( title
.$element
),
239 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-top-queryName' )
240 .append( this.savedQueryTitle
.$element
),
242 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-top-hideshow' )
244 this.hideShowButton
.$element
248 .addClass( 'mw-rcfilters-ui-table' )
249 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-filters' )
250 .append( rcFiltersRow
)
254 this.$handle
.append( $contentWrapper
);
255 this.emptyFilterMessage
.toggle( this.isEmpty() );
256 this.savedQueryTitle
.toggle( false );
259 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
261 this.reevaluateResetRestoreState();
266 OO
.inheritClass( FilterTagMultiselectWidget
, OO
.ui
.MenuTagMultiselectWidget
);
271 * Respond to view select widget choose event
273 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
275 FilterTagMultiselectWidget
.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget
) {
276 this.controller
.switchView( buttonOptionWidget
.getData() );
277 this.viewsSelectWidget
.selectItem( null );
282 * Respond to model search change event
284 * @param {string} value Search value
286 FilterTagMultiselectWidget
.prototype.onModelSearchChange = function ( value
) {
287 this.input
.setValue( value
);
291 * Respond to input change event
293 * @param {string} value Value of the input
295 FilterTagMultiselectWidget
.prototype.onInputChange = function ( value
) {
296 this.controller
.setSearch( value
);
300 * Respond to query button click
302 FilterTagMultiselectWidget
.prototype.onSaveQueryButtonClick = function () {
303 this.getMenu().toggle( false );
307 * Respond to save query model initialization
309 FilterTagMultiselectWidget
.prototype.onSavedQueriesInitialize = function () {
310 this.setSavedQueryVisibility();
314 * Respond to save query item change. Mainly this is done to update the label in case
315 * a query item has been edited
317 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
319 FilterTagMultiselectWidget
.prototype.onSavedQueriesItemUpdate = function ( item
) {
320 if ( this.matchingQuery
=== item
) {
321 // This means we just edited the item that is currently matched
322 this.savedQueryTitle
.setLabel( item
.getLabel() );
327 * Respond to menu toggle
329 * @param {boolean} isVisible Menu is visible
331 FilterTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
333 FilterTagMultiselectWidget
.parent
.prototype.onMenuToggle
.call( this );
338 mw
.hook( 'RcFilters.popup.open' ).fire();
340 if ( !this.getMenu().findSelectedItem() ) {
341 // If there are no selected items, scroll menu to top
342 // This has to be in a setTimeout so the menu has time
343 // to be positioned and fixed
346 this.getMenu().scrollToTop();
352 this.selectTag( null );
355 this.controller
.setSearch( '' );
357 // Log filter grouping
358 this.controller
.trackFilterGroupings( 'filtermenu' );
363 this.input
.setIcon( isVisible
? 'search' : 'menu' );
369 FilterTagMultiselectWidget
.prototype.onInputFocus = function () {
371 FilterTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
373 // Only scroll to top of the viewport if:
374 // - The widget is more than 20px from the top
375 // - The widget is not above the top of the viewport (do not scroll downwards)
376 // (This isn't represented because >20 is, anyways and always, bigger than 0)
377 this.scrollToTop( this.$element
, 0, { min
: 20, max
: Infinity
} );
383 FilterTagMultiselectWidget
.prototype.doInputEscape = function () {
385 FilterTagMultiselectWidget
.parent
.prototype.doInputEscape
.call( this );
388 this.input
.$input
.trigger( 'blur' );
394 FilterTagMultiselectWidget
.prototype.onMouseDown = function ( e
) {
395 if ( !this.collapsed
&& !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
405 FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
406 // If initialized, call parent method.
407 if ( this.controller
.isInitialized() ) {
408 FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
411 this.emptyFilterMessage
.toggle( this.isEmpty() );
415 * Respond to model initialize event
417 FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
418 this.setSavedQueryVisibility();
422 * Respond to model update event
424 FilterTagMultiselectWidget
.prototype.onModelUpdate = function () {
425 this.updateElementsForView();
429 * Update the elements in the widget to the current view
431 FilterTagMultiselectWidget
.prototype.updateElementsForView = function () {
432 var view
= this.model
.getCurrentView(),
433 inputValue
= this.input
.getValue().trim(),
434 inputView
= this.model
.getViewByTrigger( inputValue
.substr( 0, 1 ) );
436 if ( inputView
!== 'default' ) {
437 // We have a prefix already, remove it
438 inputValue
= inputValue
.substr( 1 );
441 if ( inputView
!== view
) {
442 // Add the correct prefix
443 inputValue
= this.model
.getViewTrigger( view
) + inputValue
;
447 this.input
.setValue( inputValue
);
449 if ( this.currentView
!== view
) {
450 this.scrollToTop( this.$element
);
451 this.currentView
= view
;
456 * Set the visibility of the saved query button
458 FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
459 if ( mw
.user
.isAnon() ) {
463 this.matchingQuery
= this.controller
.findQueryMatchingCurrentState();
465 this.savedQueryTitle
.setLabel(
466 this.matchingQuery
? this.matchingQuery
.getLabel() : ''
468 this.savedQueryTitle
.toggle( !!this.matchingQuery
);
469 this.saveQueryButton
.setDisabled( !!this.matchingQuery
);
470 this.saveQueryButton
.setTitle( !this.matchingQuery
?
471 mw
.msg( 'rcfilters-savedqueries-add-new-title' ) :
472 mw
.msg( 'rcfilters-savedqueries-already-saved' ) );
474 if ( this.matchingQuery
) {
480 * Respond to model itemUpdate event
481 * fixme: when a new state is applied to the model this function is called 60+ times in a row
483 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
485 FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
486 if ( !item
.getGroupModel().isHidden() ) {
490 this.model
.isHighlightEnabled() &&
491 item
.getHighlightColor()
494 this.addTag( item
.getName(), item
.getLabel() );
496 // Only attempt to remove the tag if we can find an item for it (T198140, T198231)
497 if ( this.findItemFromData( item
.getName() ) !== null ) {
498 this.removeTagByData( item
.getName() );
503 this.setSavedQueryVisibility();
505 // Re-evaluate reset state
506 this.reevaluateResetRestoreState();
512 FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
514 this.model
.getItemByName( data
) &&
515 !this.isDuplicateData( data
)
522 FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
523 this.controller
.toggleFilterSelect( item
.model
.getName() );
525 // Select the tag if it exists, or reset selection otherwise
526 this.selectTag( this.findItemFromData( item
.model
.getName() ) );
532 * Respond to highlightChange event
534 * @param {boolean} isHighlightEnabled Highlight is enabled
536 FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
537 var highlightedItems
= this.model
.getHighlightedItems();
539 if ( isHighlightEnabled
) {
540 // Add capsule widgets
541 highlightedItems
.forEach( function ( filterItem
) {
542 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
545 // Remove capsule widgets if they're not selected
546 highlightedItems
.forEach( function ( filterItem
) {
547 if ( !filterItem
.isSelected() ) {
548 // Only attempt to remove the tag if we can find an item for it (T198140, T198231)
549 if ( this.findItemFromData( filterItem
.getName() ) !== null ) {
550 this.removeTagByData( filterItem
.getName() );
556 this.setSavedQueryVisibility();
562 FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
563 var menuOption
= this.menu
.getItemFromModel( tagItem
.getModel() );
565 this.menu
.setUserSelecting( true );
567 FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
570 this.controller
.resetSearchForView( tagItem
.getView() );
572 this.selectTag( tagItem
);
573 this.scrollToTop( menuOption
.$element
);
575 this.menu
.setUserSelecting( false );
579 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
580 * If no items are given, reset selection from all.
582 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
583 * omit to deselect all
585 FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
586 var i
, len
, selected
;
588 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
589 selected
= this.items
[ i
] === item
;
590 if ( this.items
[ i
].isSelected() !== selected
) {
591 this.items
[ i
].toggleSelected( selected
);
598 FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
600 FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
602 this.controller
.clearFilter( tagItem
.getName() );
608 * Respond to click event on the reset button
610 FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
611 if ( this.model
.areVisibleFiltersEmpty() ) {
612 // Reset to default filters
613 this.controller
.resetToDefaults();
615 // Reset to have no filters
616 this.controller
.emptyFilters();
621 * Respond to hide/show button click
623 FilterTagMultiselectWidget
.prototype.onHideShowButtonClick = function () {
624 this.toggleCollapsed();
628 * Toggle the collapsed state of the filters widget
630 * @param {boolean} isCollapsed Widget is collapsed
632 FilterTagMultiselectWidget
.prototype.toggleCollapsed = function ( isCollapsed
) {
633 isCollapsed
= isCollapsed
=== undefined ? !this.collapsed
: !!isCollapsed
;
635 this.collapsed
= isCollapsed
;
638 // If we are collapsing, close the menu, in case it was open
639 // We should make sure the menu closes before the rest of the elements
640 // are hidden, otherwise there is an unknown error in jQuery as ooui
641 // sets and unsets properties on the input (which is hidden at that point)
642 this.menu
.toggle( false );
644 this.input
.setDisabled( isCollapsed
);
645 this.hideShowButton
.setLabel( mw
.msg(
646 isCollapsed
? 'rcfilters-activefilters-show' : 'rcfilters-activefilters-hide'
648 this.hideShowButton
.setTitle( mw
.msg(
649 isCollapsed
? 'rcfilters-activefilters-show-tooltip' : 'rcfilters-activefilters-hide-tooltip'
652 // Toggle the wrapper class, so we have min height values correctly throughout
653 this.$wrapper
.toggleClass( 'mw-rcfilters-collapsed', isCollapsed
);
656 this.controller
.updateCollapsedState( isCollapsed
);
660 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
662 FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
663 var defaultsAreEmpty
= this.controller
.areDefaultsEmpty(),
664 currFiltersAreEmpty
= this.model
.areVisibleFiltersEmpty(),
665 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
667 this.resetButton
.setIcon(
668 currFiltersAreEmpty
? 'history' : 'trash'
671 this.resetButton
.setLabel(
672 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
674 this.resetButton
.setTitle(
675 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
678 this.resetButton
.toggle( !hideResetButton
);
679 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
685 FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
686 return new MenuSelectWidget(
696 FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
697 var filterItem
= this.model
.getItemByName( data
);
700 return new FilterTagItemWidget(
703 this.model
.getInvertModel(),
706 $overlay
: this.$overlay
712 FilterTagMultiselectWidget
.prototype.emphasize = function () {
714 // eslint-disable-next-line no-jquery/no-class-state
715 !this.$handle
.hasClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' )
718 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' )
719 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
721 setTimeout( function () {
723 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' );
725 setTimeout( function () {
727 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
728 }.bind( this ), 1000 );
729 }.bind( this ), 500 );
734 * Scroll the element to top within its container
737 * @param {jQuery} $element Element to position
738 * @param {number} [marginFromTop=0] When scrolling the entire widget to the top, leave this
739 * much space (in pixels) above the widget.
740 * @param {Object} [threshold] Minimum distance from the top of the element to scroll at all
741 * @param {number} [threshold.min] Minimum distance above the element
742 * @param {number} [threshold.max] Minimum distance below the element
744 FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
, threshold
) {
745 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
746 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
747 containerScrollTop
= $( container
).scrollTop(),
748 effectiveScrollTop
= $( container
).is( 'body, html' ) ? 0 : containerScrollTop
,
749 newScrollTop
= effectiveScrollTop
+ pos
.top
- ( marginFromTop
|| 0 );
753 threshold
=== undefined ||
756 threshold
.min
=== undefined ||
757 newScrollTop
- containerScrollTop
>= threshold
.min
760 threshold
.max
=== undefined ||
761 newScrollTop
- containerScrollTop
<= threshold
.max
765 $( container
).animate( {
766 scrollTop
: newScrollTop
771 module
.exports
= FilterTagMultiselectWidget
;