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
15 * @cfg {jQuery} [$wrapper] A jQuery object for the wrapper of the general
16 * system. If not given, falls back to this widget's $element
17 * @cfg {boolean} [collapsed] Filter area is collapsed
19 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
= function MwRcfiltersUiFilterTagMultiselectWidget( controller
, model
, savedQueriesModel
, config
) {
21 title
= new OO
.ui
.LabelWidget( {
22 label
: mw
.msg( 'rcfilters-activefilters' ),
23 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-title' ]
25 $contentWrapper
= $( '<div>' )
26 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper' );
28 config
= config
|| {};
30 this.controller
= controller
;
32 this.queriesModel
= savedQueriesModel
;
33 this.$overlay
= config
.$overlay
|| this.$element
;
34 this.$wrapper
= config
.$wrapper
|| this.$element
;
35 this.matchingQuery
= null;
36 this.currentView
= this.model
.getCurrentView();
37 this.collapsed
= false;
40 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.call( this, $.extend( true, {
41 label
: mw
.msg( 'rcfilters-filterlist-title' ),
42 placeholder
: mw
.msg( 'rcfilters-empty-filter' ),
43 inputPosition
: 'outline',
44 allowArbitrary
: false,
45 allowDisplayInvalidTags
: false,
46 allowReordering
: false,
47 $overlay
: this.$overlay
,
49 // Our filtering is done through the model
50 filterFromInput
: false,
51 hideWhenOutOfView
: false,
58 // View select menu, appears on default view only
59 $element
: $( '<div>' )
60 .append( new mw
.rcfilters
.ui
.ViewSwitchWidget( this.controller
, this.model
).$element
),
65 // Feedback footer, appears on all views
66 $element
: $( '<div>' )
68 new OO
.ui
.ButtonWidget( {
71 flags
: [ 'progressive' ],
72 label
: mw
.msg( 'rcfilters-filterlist-feedbacklink' ),
73 href
: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
81 placeholder
: mw
.msg( 'rcfilters-search-placeholder' )
85 this.savedQueryTitle
= new OO
.ui
.LabelWidget( {
87 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-savedQueryTitle' ]
90 this.resetButton
= new OO
.ui
.ButtonWidget( {
92 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-resetButton' ]
95 this.hideShowButton
= new OO
.ui
.ButtonWidget( {
97 flags
: [ 'progressive' ],
98 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-hideshowButton' ]
100 this.toggleCollapsed( !!config
.collapsed
);
102 if ( !mw
.user
.isAnon() ) {
103 this.saveQueryButton
= new mw
.rcfilters
.ui
.SaveFiltersPopupButtonWidget(
107 $overlay
: this.$overlay
111 this.saveQueryButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
113 this.saveQueryButton
.connect( this, {
114 click
: 'onSaveQueryButtonClick',
115 saveCurrent
: 'setSavedQueryVisibility'
117 this.queriesModel
.connect( this, {
118 itemUpdate
: 'onSavedQueriesItemUpdate',
119 initialize
: 'onSavedQueriesInitialize',
120 'default': 'reevaluateResetRestoreState'
124 this.emptyFilterMessage
= new OO
.ui
.LabelWidget( {
125 label
: mw
.msg( 'rcfilters-empty-filter' ),
126 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
128 this.$content
.append( this.emptyFilterMessage
.$element
);
131 this.resetButton
.connect( this, { click
: 'onResetButtonClick' } );
132 this.hideShowButton
.connect( this, { click
: 'onHideShowButtonClick' } );
133 // Stop propagation for mousedown, so that the widget doesn't
134 // trigger the focus on the input and scrolls up when we click the reset button
135 this.resetButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
136 this.hideShowButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
137 this.model
.connect( this, {
138 initialize
: 'onModelInitialize',
139 update
: 'onModelUpdate',
140 searchChange
: 'onModelSearchChange',
141 itemUpdate
: 'onModelItemUpdate',
142 highlightChange
: 'onModelHighlightChange'
144 this.input
.connect( this, { change
: 'onInputChange' } );
146 // The filter list and button should appear side by side regardless of how
147 // wide the button is; the button also changes its width depending
148 // on language and its state, so the safest way to present both side
149 // by side is with a table layout
150 rcFiltersRow
= $( '<div>' )
151 .addClass( 'mw-rcfilters-ui-row' )
154 .addClass( 'mw-rcfilters-ui-cell' )
155 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
158 if ( !mw
.user
.isAnon() ) {
161 .addClass( 'mw-rcfilters-ui-cell' )
162 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
163 .append( this.saveQueryButton
.$element
)
167 // Add a selector at the right of the input
168 this.viewsSelectWidget
= new OO
.ui
.ButtonSelectWidget( {
169 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
171 new OO
.ui
.ButtonOptionWidget( {
175 label
: mw
.msg( 'namespaces' ),
176 title
: mw
.msg( 'rcfilters-view-namespaces-tooltip' )
178 new OO
.ui
.ButtonOptionWidget( {
182 label
: mw
.msg( 'tags-title' ),
183 title
: mw
.msg( 'rcfilters-view-tags-tooltip' )
188 // Rearrange the UI so the select widget is at the right of the input
189 this.$element
.append(
191 .addClass( 'mw-rcfilters-ui-table' )
194 .addClass( 'mw-rcfilters-ui-row' )
195 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views' )
198 .addClass( 'mw-rcfilters-ui-cell' )
199 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
200 .append( this.input
.$element
),
202 .addClass( 'mw-rcfilters-ui-cell' )
203 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
204 .append( this.viewsSelectWidget
.$element
)
210 this.viewsSelectWidget
.connect( this, { choose
: 'onViewsSelectWidgetChoose' } );
214 .addClass( 'mw-rcfilters-ui-cell' )
215 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
216 .append( this.resetButton
.$element
)
220 $contentWrapper
.append(
222 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-top' )
225 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-top-title' )
226 .append( title
.$element
),
228 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-top-queryName' )
229 .append( this.savedQueryTitle
.$element
),
231 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-top-hideshow' )
233 this.hideShowButton
.$element
237 .addClass( 'mw-rcfilters-ui-table' )
238 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-filters' )
239 .append( rcFiltersRow
)
243 this.$handle
.append( $contentWrapper
);
244 this.emptyFilterMessage
.toggle( this.isEmpty() );
245 this.savedQueryTitle
.toggle( false );
248 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
250 this.reevaluateResetRestoreState();
255 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagMultiselectWidget
, OO
.ui
.MenuTagMultiselectWidget
);
260 * Override parent method to avoid unnecessary resize events.
262 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.updateIfHeightChanged = function () { };
265 * Respond to view select widget choose event
267 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
269 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget
) {
270 this.controller
.switchView( buttonOptionWidget
.getData() );
271 this.viewsSelectWidget
.selectItem( null );
276 * Respond to model search change event
278 * @param {string} value Search value
280 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelSearchChange = function ( value
) {
281 this.input
.setValue( value
);
285 * Respond to input change event
287 * @param {string} value Value of the input
289 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputChange = function ( value
) {
290 this.controller
.setSearch( value
);
294 * Respond to query button click
296 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSaveQueryButtonClick = function () {
297 this.getMenu().toggle( false );
301 * Respond to save query model initialization
303 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesInitialize = function () {
304 this.setSavedQueryVisibility();
308 * Respond to save query item change. Mainly this is done to update the label in case
309 * a query item has been edited
311 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
313 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesItemUpdate = function ( item
) {
314 if ( this.matchingQuery
=== item
) {
315 // This means we just edited the item that is currently matched
316 this.savedQueryTitle
.setLabel( item
.getLabel() );
321 * Respond to menu toggle
323 * @param {boolean} isVisible Menu is visible
325 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
327 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onMenuToggle
.call( this );
332 mw
.hook( 'RcFilters.popup.open' ).fire();
334 if ( !this.getMenu().findSelectedItem() ) {
335 // If there are no selected items, scroll menu to top
336 // This has to be in a setTimeout so the menu has time
337 // to be positioned and fixed
338 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
342 this.selectTag( null );
345 this.controller
.setSearch( '' );
347 // Log filter grouping
348 this.controller
.trackFilterGroupings( 'filtermenu' );
353 this.input
.setIcon( isVisible
? 'search' : 'menu' );
359 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputFocus = function () {
361 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
363 // Only scroll to top of the viewport if:
364 // - The widget is more than 20px from the top
365 // - The widget is not above the top of the viewport (do not scroll downwards)
366 // (This isn't represented because >20 is, anyways and always, bigger than 0)
367 this.scrollToTop( this.$element
, 0, { min
: 20, max
: Infinity
} );
373 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.doInputEscape = function () {
375 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.doInputEscape
.call( this );
378 this.input
.$input
.blur();
384 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMouseDown = function ( e
) {
385 if ( !this.collapsed
&& !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
395 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
396 // If initialized, call parent method.
397 if ( this.controller
.isInitialized() ) {
398 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
401 this.emptyFilterMessage
.toggle( this.isEmpty() );
405 * Respond to model initialize event
407 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
408 this.setSavedQueryVisibility();
412 * Respond to model update event
414 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelUpdate = function () {
415 this.updateElementsForView();
419 * Update the elements in the widget to the current view
421 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.updateElementsForView = function () {
422 var view
= this.model
.getCurrentView(),
423 inputValue
= this.input
.getValue().trim(),
424 inputView
= this.model
.getViewByTrigger( inputValue
.substr( 0, 1 ) );
426 if ( inputView
!== 'default' ) {
427 // We have a prefix already, remove it
428 inputValue
= inputValue
.substr( 1 );
431 if ( inputView
!== view
) {
432 // Add the correct prefix
433 inputValue
= this.model
.getViewTrigger( view
) + inputValue
;
437 this.input
.setValue( inputValue
);
439 if ( this.currentView
!== view
) {
440 this.scrollToTop( this.$element
);
441 this.currentView
= view
;
446 * Set the visibility of the saved query button
448 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
449 if ( mw
.user
.isAnon() ) {
453 this.matchingQuery
= this.controller
.findQueryMatchingCurrentState();
455 this.savedQueryTitle
.setLabel(
456 this.matchingQuery
? this.matchingQuery
.getLabel() : ''
458 this.savedQueryTitle
.toggle( !!this.matchingQuery
);
459 this.saveQueryButton
.setDisabled( !!this.matchingQuery
);
460 this.saveQueryButton
.setTitle( !this.matchingQuery
?
461 mw
.msg( 'rcfilters-savedqueries-add-new-title' ) :
462 mw
.msg( 'rcfilters-savedqueries-already-saved' ) );
464 if ( this.matchingQuery
) {
470 * Respond to model itemUpdate event
471 * fixme: when a new state is applied to the model this function is called 60+ times in a row
473 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
475 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
476 if ( !item
.getGroupModel().isHidden() ) {
480 this.model
.isHighlightEnabled() &&
481 item
.getHighlightColor()
484 this.addTag( item
.getName(), item
.getLabel() );
486 // Only attempt to remove the tag if we can find an item for it (T198140, T198231)
487 if ( this.findItemFromData( item
.getName() ) !== null ) {
488 this.removeTagByData( item
.getName() );
493 this.setSavedQueryVisibility();
495 // Re-evaluate reset state
496 this.reevaluateResetRestoreState();
502 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
504 this.model
.getItemByName( data
) &&
505 !this.isDuplicateData( data
)
512 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
513 this.controller
.toggleFilterSelect( item
.model
.getName() );
515 // Select the tag if it exists, or reset selection otherwise
516 this.selectTag( this.findItemFromData( item
.model
.getName() ) );
522 * Respond to highlightChange event
524 * @param {boolean} isHighlightEnabled Highlight is enabled
526 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
527 var highlightedItems
= this.model
.getHighlightedItems();
529 if ( isHighlightEnabled
) {
530 // Add capsule widgets
531 highlightedItems
.forEach( function ( filterItem
) {
532 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
535 // Remove capsule widgets if they're not selected
536 highlightedItems
.forEach( function ( filterItem
) {
537 if ( !filterItem
.isSelected() ) {
538 // Only attempt to remove the tag if we can find an item for it (T198140, T198231)
539 if ( this.findItemFromData( filterItem
.getName() ) !== null ) {
540 this.removeTagByData( filterItem
.getName() );
546 this.setSavedQueryVisibility();
552 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
553 var menuOption
= this.menu
.getItemFromModel( tagItem
.getModel() );
555 this.menu
.setUserSelecting( true );
557 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
560 this.controller
.resetSearchForView( tagItem
.getView() );
562 this.selectTag( tagItem
);
563 this.scrollToTop( menuOption
.$element
);
565 this.menu
.setUserSelecting( false );
569 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
570 * If no items are given, reset selection from all.
572 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
573 * omit to deselect all
575 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
576 var i
, len
, selected
;
578 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
579 selected
= this.items
[ i
] === item
;
580 if ( this.items
[ i
].isSelected() !== selected
) {
581 this.items
[ i
].toggleSelected( selected
);
588 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
590 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
592 this.controller
.clearFilter( tagItem
.getName() );
598 * Respond to click event on the reset button
600 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
601 if ( this.model
.areVisibleFiltersEmpty() ) {
602 // Reset to default filters
603 this.controller
.resetToDefaults();
605 // Reset to have no filters
606 this.controller
.emptyFilters();
611 * Respond to hide/show button click
613 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onHideShowButtonClick = function () {
614 this.toggleCollapsed();
618 * Toggle the collapsed state of the filters widget
620 * @param {boolean} isCollapsed Widget is collapsed
622 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.toggleCollapsed = function ( isCollapsed
) {
623 isCollapsed
= isCollapsed
=== undefined ? !this.collapsed
: !!isCollapsed
;
625 this.collapsed
= isCollapsed
;
628 // If we are collapsing, close the menu, in case it was open
629 // We should make sure the menu closes before the rest of the elements
630 // are hidden, otherwise there is an unknown error in jQuery as ooui
631 // sets and unsets properties on the input (which is hidden at that point)
632 this.menu
.toggle( false );
634 this.input
.setDisabled( isCollapsed
);
635 this.hideShowButton
.setLabel( mw
.msg(
636 isCollapsed
? 'rcfilters-activefilters-show' : 'rcfilters-activefilters-hide'
638 this.hideShowButton
.setTitle( mw
.msg(
639 isCollapsed
? 'rcfilters-activefilters-show-tooltip' : 'rcfilters-activefilters-hide-tooltip'
642 // Toggle the wrapper class, so we have min height values correctly throughout
643 this.$wrapper
.toggleClass( 'mw-rcfilters-collapsed', isCollapsed
);
646 this.controller
.updateCollapsedState( isCollapsed
);
650 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
652 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
653 var defaultsAreEmpty
= this.controller
.areDefaultsEmpty(),
654 currFiltersAreEmpty
= this.model
.areVisibleFiltersEmpty(),
655 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
657 this.resetButton
.setIcon(
658 currFiltersAreEmpty
? 'history' : 'trash'
661 this.resetButton
.setLabel(
662 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
664 this.resetButton
.setTitle(
665 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
668 this.resetButton
.toggle( !hideResetButton
);
669 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
675 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
676 return new mw
.rcfilters
.ui
.MenuSelectWidget(
686 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
687 var filterItem
= this.model
.getItemByName( data
);
690 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
693 this.model
.getInvertModel(),
696 $overlay
: this.$overlay
702 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.emphasize = function () {
704 !this.$handle
.hasClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' )
707 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' )
708 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
710 setTimeout( function () {
712 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' );
714 setTimeout( function () {
716 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
717 }.bind( this ), 1000 );
718 }.bind( this ), 500 );
723 * Scroll the element to top within its container
726 * @param {jQuery} $element Element to position
727 * @param {number} [marginFromTop=0] When scrolling the entire widget to the top, leave this
728 * much space (in pixels) above the widget.
729 * @param {Object} [threshold] Minimum distance from the top of the element to scroll at all
730 * @param {number} [threshold.min] Minimum distance above the element
731 * @param {number} [threshold.max] Minimum distance below the element
733 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
, threshold
) {
734 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
735 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
736 containerScrollTop
= $( container
).scrollTop(),
737 effectiveScrollTop
= $( container
).is( 'body, html' ) ? 0 : containerScrollTop
,
738 newScrollTop
= effectiveScrollTop
+ pos
.top
- ( marginFromTop
|| 0 );
742 threshold
=== undefined ||
745 threshold
.min
=== undefined ||
746 newScrollTop
- containerScrollTop
>= threshold
.min
749 threshold
.max
=== undefined ||
750 newScrollTop
- containerScrollTop
<= threshold
.max
754 $( container
).animate( {
755 scrollTop
: newScrollTop