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;
32 this.currentView
= this.model
.getCurrentView();
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,
51 // View select menu, appears on default view only
52 $element
: $( '<div>' )
53 .append( new mw
.rcfilters
.ui
.ViewSwitchWidget( this.controller
, this.model
).$element
),
58 // Feedback footer, appears on all views
59 $element
: $( '<div>' )
61 new OO
.ui
.ButtonWidget( {
64 flags
: [ 'progressive' ],
65 label
: mw
.msg( 'rcfilters-filterlist-feedbacklink' ),
66 href
: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
74 placeholder
: mw
.msg( 'rcfilters-search-placeholder' )
78 this.savedQueryTitle
= new OO
.ui
.LabelWidget( {
80 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-savedQueryTitle' ]
83 this.resetButton
= new OO
.ui
.ButtonWidget( {
85 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-resetButton' ]
88 if ( !mw
.user
.isAnon() ) {
89 this.saveQueryButton
= new mw
.rcfilters
.ui
.SaveFiltersPopupButtonWidget(
93 $overlay
: this.$overlay
97 this.saveQueryButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
99 this.saveQueryButton
.connect( this, {
100 click
: 'onSaveQueryButtonClick',
101 saveCurrent
: 'setSavedQueryVisibility'
103 this.queriesModel
.connect( this, {
104 itemUpdate
: 'onSavedQueriesItemUpdate',
105 initialize
: 'onSavedQueriesInitialize',
106 'default': 'reevaluateResetRestoreState'
110 this.emptyFilterMessage
= new OO
.ui
.LabelWidget( {
111 label
: mw
.msg( 'rcfilters-empty-filter' ),
112 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
114 this.$content
.append( this.emptyFilterMessage
.$element
);
117 this.resetButton
.connect( this, { click
: 'onResetButtonClick' } );
118 // Stop propagation for mousedown, so that the widget doesn't
119 // trigger the focus on the input and scrolls up when we click the reset button
120 this.resetButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
121 this.model
.connect( this, {
122 initialize
: 'onModelInitialize',
123 update
: 'onModelUpdate',
124 itemUpdate
: 'onModelItemUpdate',
125 highlightChange
: 'onModelHighlightChange'
127 this.input
.connect( this, { change
: 'onInputChange' } );
129 // The filter list and button should appear side by side regardless of how
130 // wide the button is; the button also changes its width depending
131 // on language and its state, so the safest way to present both side
132 // by side is with a table layout
133 rcFiltersRow
= $( '<div>' )
134 .addClass( 'mw-rcfilters-ui-row' )
137 .addClass( 'mw-rcfilters-ui-cell' )
138 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
141 if ( !mw
.user
.isAnon() ) {
144 .addClass( 'mw-rcfilters-ui-cell' )
145 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
146 .append( this.saveQueryButton
.$element
)
150 // Add a selector at the right of the input
151 this.viewsSelectWidget
= new OO
.ui
.ButtonSelectWidget( {
152 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
154 new OO
.ui
.ButtonOptionWidget( {
158 label
: mw
.msg( 'namespaces' ),
159 title
: mw
.msg( 'rcfilters-view-namespaces-tooltip' )
161 new OO
.ui
.ButtonOptionWidget( {
165 label
: mw
.msg( 'tags-title' ),
166 title
: mw
.msg( 'rcfilters-view-tags-tooltip' )
171 // Rearrange the UI so the select widget is at the right of the input
172 this.$element
.append(
174 .addClass( 'mw-rcfilters-ui-table' )
177 .addClass( 'mw-rcfilters-ui-row' )
180 .addClass( 'mw-rcfilters-ui-cell' )
181 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
182 .append( this.input
.$element
),
184 .addClass( 'mw-rcfilters-ui-cell' )
185 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
186 .append( this.viewsSelectWidget
.$element
)
192 this.viewsSelectWidget
.connect( this, { choose
: 'onViewsSelectWidgetChoose' } );
196 .addClass( 'mw-rcfilters-ui-cell' )
197 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
198 .append( this.resetButton
.$element
)
202 $contentWrapper
.append(
204 this.savedQueryTitle
.$element
,
206 .addClass( 'mw-rcfilters-ui-table' )
213 this.$handle
.append( $contentWrapper
);
214 this.emptyFilterMessage
.toggle( this.isEmpty() );
215 this.savedQueryTitle
.toggle( false );
218 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
220 this.reevaluateResetRestoreState();
225 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagMultiselectWidget
, OO
.ui
.MenuTagMultiselectWidget
);
230 * Respond to view select widget choose event
232 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
234 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget
) {
235 this.controller
.switchView( buttonOptionWidget
.getData() );
236 this.viewsSelectWidget
.selectItem( null );
241 * Respond to input change event
243 * @param {string} value Value of the input
245 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputChange = function ( value
) {
248 value
= value
.trim();
250 view
= this.model
.getViewByTrigger( value
.substr( 0, 1 ) );
252 this.controller
.switchView( view
);
255 * Respond to query button click
257 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSaveQueryButtonClick = function () {
258 this.getMenu().toggle( false );
262 * Respond to save query model initialization
264 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesInitialize = function () {
265 this.setSavedQueryVisibility();
269 * Respond to save query item change. Mainly this is done to update the label in case
270 * a query item has been edited
272 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
274 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesItemUpdate = function ( item
) {
275 if ( this.matchingQuery
=== item
) {
276 // This means we just edited the item that is currently matched
277 this.savedQueryTitle
.setLabel( item
.getLabel() );
282 * Respond to menu toggle
284 * @param {boolean} isVisible Menu is visible
286 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
288 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onMenuToggle
.call( this );
293 mw
.hook( 'RcFilters.popup.open' ).fire();
295 if ( !this.getMenu().getSelectedItem() ) {
296 // If there are no selected items, scroll menu to top
297 // This has to be in a setTimeout so the menu has time
298 // to be positioned and fixed
299 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
305 this.selectTag( null );
307 // Clear input if the only thing in the input is the prefix
309 this.input
.getValue().trim() === this.model
.getViewTrigger( this.model
.getCurrentView() )
312 this.input
.setValue( '' );
315 // Log filter grouping
316 this.controller
.trackFilterGroupings( 'filtermenu' );
319 this.input
.setIcon( isVisible
? 'search' : 'menu' );
325 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputFocus = function () {
327 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
329 // Only scroll to top of the viewport if:
330 // - The widget is more than 20px from the top
331 // - The widget is not above the top of the viewport (do not scroll downwards)
332 // (This isn't represented because >20 is, anyways and always, bigger than 0)
333 this.scrollToTop( this.$element
, 0, { min
: 20, max
: Infinity
} );
339 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.doInputEscape = function () {
341 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.doInputEscape
.call( this );
344 this.input
.$input
.blur();
350 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMouseDown = function ( e
) {
351 if ( !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
361 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
363 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
365 this.emptyFilterMessage
.toggle( this.isEmpty() );
369 * Respond to model initialize event
371 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
372 this.setSavedQueryVisibility();
376 * Respond to model update event
378 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelUpdate = function () {
379 this.updateElementsForView();
383 * Update the elements in the widget to the current view
385 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.updateElementsForView = function () {
386 var view
= this.model
.getCurrentView(),
387 inputValue
= this.input
.getValue().trim(),
388 inputView
= this.model
.getViewByTrigger( inputValue
.substr( 0, 1 ) );
390 if ( inputView
!== 'default' ) {
391 // We have a prefix already, remove it
392 inputValue
= inputValue
.substr( 1 );
395 if ( inputView
!== view
) {
396 // Add the correct prefix
397 inputValue
= this.model
.getViewTrigger( view
) + inputValue
;
401 this.input
.setValue( inputValue
);
403 if ( this.currentView
!== view
) {
404 this.scrollToTop( this.$element
);
405 this.currentView
= view
;
410 * Set the visibility of the saved query button
412 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
413 if ( mw
.user
.isAnon() ) {
417 this.matchingQuery
= this.controller
.findQueryMatchingCurrentState();
419 this.savedQueryTitle
.setLabel(
420 this.matchingQuery
? this.matchingQuery
.getLabel() : ''
422 this.savedQueryTitle
.toggle( !!this.matchingQuery
);
423 this.saveQueryButton
.setDisabled( !!this.matchingQuery
);
424 this.saveQueryButton
.setTitle( !this.matchingQuery
?
425 mw
.msg( 'rcfilters-savedqueries-add-new-title' ) :
426 mw
.msg( 'rcfilters-savedqueries-already-saved' ) );
428 if ( this.matchingQuery
) {
434 * Respond to model itemUpdate event
435 * fixme: when a new state is applied to the model this function is called 60+ times in a row
437 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
439 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
440 if ( !item
.getGroupModel().isHidden() ) {
444 this.model
.isHighlightEnabled() &&
445 item
.getHighlightColor()
448 this.addTag( item
.getName(), item
.getLabel() );
450 this.removeTagByData( item
.getName() );
454 this.setSavedQueryVisibility();
456 // Re-evaluate reset state
457 this.reevaluateResetRestoreState();
463 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
465 this.model
.getItemByName( data
) &&
466 !this.isDuplicateData( data
)
473 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
474 this.controller
.toggleFilterSelect( item
.model
.getName() );
476 // Select the tag if it exists, or reset selection otherwise
477 this.selectTag( this.getItemFromData( item
.model
.getName() ) );
483 * Respond to highlightChange event
485 * @param {boolean} isHighlightEnabled Highlight is enabled
487 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
488 var highlightedItems
= this.model
.getHighlightedItems();
490 if ( isHighlightEnabled
) {
491 // Add capsule widgets
492 highlightedItems
.forEach( function ( filterItem
) {
493 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
496 // Remove capsule widgets if they're not selected
497 highlightedItems
.forEach( function ( filterItem
) {
498 if ( !filterItem
.isSelected() ) {
499 this.removeTagByData( filterItem
.getName() );
504 this.setSavedQueryVisibility();
510 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
512 menuOption
= this.menu
.getItemFromModel( tagItem
.getModel() ),
513 oldInputValue
= this.input
.getValue().trim();
515 this.menu
.setUserSelecting( true );
518 this.input
.setValue( '' );
521 this.controller
.switchView( tagItem
.getView() );
524 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
526 this.menu
.selectItem( menuOption
);
527 this.selectTag( tagItem
);
529 // Scroll to the item
530 if ( this.model
.removeViewTriggers( oldInputValue
) ) {
531 // We're binding a 'once' to the itemVisibilityChange event
532 // so this happens when the menu is ready after the items
533 // are visible again, in case this is done right after the
534 // user filtered the results
536 'itemVisibilityChange',
538 widget
.scrollToTop( menuOption
.$element
);
539 widget
.menu
.setUserSelecting( false );
543 this.scrollToTop( menuOption
.$element
);
544 this.menu
.setUserSelecting( false );
550 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
551 * If no items are given, reset selection from all.
553 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
554 * omit to deselect all
556 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
557 var i
, len
, selected
;
559 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
560 selected
= this.items
[ i
] === item
;
561 if ( this.items
[ i
].isSelected() !== selected
) {
562 this.items
[ i
].toggleSelected( selected
);
569 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
571 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
573 this.controller
.clearFilter( tagItem
.getName() );
579 * Respond to click event on the reset button
581 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
582 if ( this.model
.areVisibleFiltersEmpty() ) {
583 // Reset to default filters
584 this.controller
.resetToDefaults();
586 // Reset to have no filters
587 this.controller
.emptyFilters();
592 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
594 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
595 var defaultsAreEmpty
= this.controller
.areDefaultsEmpty(),
596 currFiltersAreEmpty
= this.model
.areVisibleFiltersEmpty(),
597 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
599 this.resetButton
.setIcon(
600 currFiltersAreEmpty
? 'history' : 'trash'
603 this.resetButton
.setLabel(
604 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
606 this.resetButton
.setTitle(
607 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
610 this.resetButton
.toggle( !hideResetButton
);
611 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
617 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
618 return new mw
.rcfilters
.ui
.MenuSelectWidget(
622 filterFromInput
: true
630 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
631 var filterItem
= this.model
.getItemByName( data
);
634 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
637 this.model
.getInvertModel(),
640 $overlay
: this.$overlay
646 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.emphasize = function () {
648 !this.$handle
.hasClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' )
651 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' )
652 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
654 setTimeout( function () {
656 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' );
658 setTimeout( function () {
660 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
661 }.bind( this ), 1000 );
662 }.bind( this ), 500 );
667 * Scroll the element to top within its container
670 * @param {jQuery} $element Element to position
671 * @param {number} [marginFromTop=0] When scrolling the entire widget to the top, leave this
672 * much space (in pixels) above the widget.
673 * @param {Object} [threshold] Minimum distance from the top of the element to scroll at all
674 * @param {number} [threshold.min] Minimum distance above the element
675 * @param {number} [threshold.max] Minimum distance below the element
677 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
, threshold
) {
678 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
679 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
680 containerScrollTop
= $( container
).scrollTop(),
681 effectiveScrollTop
= $( container
).is( 'body, html' ) ? 0 : containerScrollTop
,
682 newScrollTop
= effectiveScrollTop
+ pos
.top
- ( marginFromTop
|| 0 );
686 threshold
=== undefined ||
689 threshold
.min
=== undefined ||
690 newScrollTop
- containerScrollTop
>= threshold
.min
693 threshold
.max
=== undefined ||
694 newScrollTop
- containerScrollTop
<= threshold
.max
698 $( container
).animate( {
699 scrollTop
: newScrollTop