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(
94 this.saveQueryButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
96 this.saveQueryButton
.connect( this, {
97 click
: 'onSaveQueryButtonClick',
98 saveCurrent
: 'setSavedQueryVisibility'
100 this.queriesModel
.connect( this, {
101 itemUpdate
: 'onSavedQueriesItemUpdate',
102 initialize
: 'onSavedQueriesInitialize'
106 this.emptyFilterMessage
= new OO
.ui
.LabelWidget( {
107 label
: mw
.msg( 'rcfilters-empty-filter' ),
108 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
110 this.$content
.append( this.emptyFilterMessage
.$element
);
113 this.resetButton
.connect( this, { click
: 'onResetButtonClick' } );
114 // Stop propagation for mousedown, so that the widget doesn't
115 // trigger the focus on the input and scrolls up when we click the reset button
116 this.resetButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
117 this.model
.connect( this, {
118 initialize
: 'onModelInitialize',
119 update
: 'onModelUpdate',
120 itemUpdate
: 'onModelItemUpdate',
121 highlightChange
: 'onModelHighlightChange'
123 this.input
.connect( this, { change
: 'onInputChange' } );
125 // The filter list and button should appear side by side regardless of how
126 // wide the button is; the button also changes its width depending
127 // on language and its state, so the safest way to present both side
128 // by side is with a table layout
129 rcFiltersRow
= $( '<div>' )
130 .addClass( 'mw-rcfilters-ui-row' )
133 .addClass( 'mw-rcfilters-ui-cell' )
134 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
137 if ( !mw
.user
.isAnon() ) {
140 .addClass( 'mw-rcfilters-ui-cell' )
141 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
142 .append( this.saveQueryButton
.$element
)
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 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget-label' ],
155 label
: mw
.msg( 'rcfilters-view-advanced-filters-label' )
157 new OO
.ui
.ButtonOptionWidget( {
161 title
: mw
.msg( 'rcfilters-view-namespaces-tooltip' )
163 new OO
.ui
.ButtonOptionWidget( {
167 title
: mw
.msg( 'rcfilters-view-tags-tooltip' )
172 // Rearrange the UI so the select widget is at the right of the input
173 this.$element
.append(
175 .addClass( 'mw-rcfilters-ui-table' )
178 .addClass( 'mw-rcfilters-ui-row' )
181 .addClass( 'mw-rcfilters-ui-cell' )
182 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
183 .append( this.input
.$element
),
185 .addClass( 'mw-rcfilters-ui-cell' )
186 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
187 .append( this.viewsSelectWidget
.$element
)
193 this.viewsSelectWidget
.connect( this, { choose
: 'onViewsSelectWidgetChoose' } );
197 .addClass( 'mw-rcfilters-ui-cell' )
198 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
199 .append( this.resetButton
.$element
)
203 $contentWrapper
.append(
205 this.savedQueryTitle
.$element
,
207 .addClass( 'mw-rcfilters-ui-table' )
214 this.$handle
.append( $contentWrapper
);
215 this.emptyFilterMessage
.toggle( this.isEmpty() );
216 this.savedQueryTitle
.toggle( false );
219 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
221 this.reevaluateResetRestoreState();
226 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagMultiselectWidget
, OO
.ui
.MenuTagMultiselectWidget
);
231 * Respond to view select widget choose event
233 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
235 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget
) {
236 this.controller
.switchView( buttonOptionWidget
.getData() );
237 this.viewsSelectWidget
.selectItem( null );
242 * Respond to input change event
244 * @param {string} value Value of the input
246 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputChange = function ( value
) {
249 value
= value
.trim();
251 view
= this.model
.getViewByTrigger( value
.substr( 0, 1 ) );
253 this.controller
.switchView( view
);
256 * Respond to query button click
258 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSaveQueryButtonClick = function () {
259 this.getMenu().toggle( false );
263 * Respond to save query model initialization
265 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesInitialize = function () {
266 this.setSavedQueryVisibility();
270 * Respond to save query item change. Mainly this is done to update the label in case
271 * a query item has been edited
273 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
275 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesItemUpdate = function ( item
) {
276 if ( this.matchingQuery
=== item
) {
277 // This means we just edited the item that is currently matched
278 this.savedQueryTitle
.setLabel( item
.getLabel() );
283 * Respond to menu toggle
285 * @param {boolean} isVisible Menu is visible
287 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
289 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onMenuToggle
.call( this );
292 mw
.hook( 'RcFilters.popup.open' ).fire();
294 if ( !this.getMenu().getSelectedItem() ) {
295 // If there are no selected items, scroll menu to top
296 // This has to be in a setTimeout so the menu has time
297 // to be positioned and fixed
298 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
302 this.selectTag( null );
304 // Clear input if the only thing in the input is the prefix
306 this.input
.getValue().trim() === this.model
.getViewTrigger( this.model
.getCurrentView() )
309 this.input
.setValue( '' );
312 // Log filter grouping
313 this.controller
.trackFilterGroupings( 'filtermenu' );
316 this.input
.setIcon( isVisible
? 'search' : 'menu' );
322 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputFocus = function () {
324 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
326 // Only scroll to top of the viewport if:
327 // - The widget is more than 20px from the top
328 // - The widget is not above the top of the viewport (do not scroll downwards)
329 // (This isn't represented because >20 is, anyways and always, bigger than 0)
330 this.scrollToTop( this.$element
, 0, { min
: 20, max
: Infinity
} );
336 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.doInputEscape = function () {
338 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.doInputEscape
.call( this );
341 this.input
.$input
.blur();
347 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
349 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
351 this.emptyFilterMessage
.toggle( this.isEmpty() );
355 * Respond to model initialize event
357 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
358 this.setSavedQueryVisibility();
362 * Respond to model update event
364 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelUpdate = function () {
365 this.updateElementsForView();
369 * Update the elements in the widget to the current view
371 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.updateElementsForView = function () {
372 var view
= this.model
.getCurrentView(),
373 inputValue
= this.input
.getValue().trim(),
374 inputView
= this.model
.getViewByTrigger( inputValue
.substr( 0, 1 ) );
376 if ( inputView
!== 'default' ) {
377 // We have a prefix already, remove it
378 inputValue
= inputValue
.substr( 1 );
381 if ( inputView
!== view
) {
382 // Add the correct prefix
383 inputValue
= this.model
.getViewTrigger( view
) + inputValue
;
387 this.input
.setValue( inputValue
);
389 if ( this.currentView
!== view
) {
390 this.scrollToTop( this.$element
);
391 this.currentView
= view
;
396 * Set the visibility of the saved query button
398 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
399 if ( mw
.user
.isAnon() ) {
403 this.matchingQuery
= this.controller
.findQueryMatchingCurrentState();
405 this.savedQueryTitle
.setLabel(
406 this.matchingQuery
? this.matchingQuery
.getLabel() : ''
408 this.savedQueryTitle
.toggle( !!this.matchingQuery
);
409 this.saveQueryButton
.toggle( !this.matchingQuery
);
411 if ( this.matchingQuery
) {
417 * Respond to model itemUpdate event
419 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
421 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
422 if ( item
.getGroupModel().isHidden() ) {
429 this.model
.isHighlightEnabled() &&
430 item
.isHighlightSupported() &&
431 item
.getHighlightColor()
434 this.addTag( item
.getName(), item
.getLabel() );
436 this.removeTagByData( item
.getName() );
439 this.setSavedQueryVisibility();
441 // Re-evaluate reset state
442 this.reevaluateResetRestoreState();
448 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
450 this.model
.getItemByName( data
) &&
451 !this.isDuplicateData( data
)
458 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
459 this.controller
.toggleFilterSelect( item
.model
.getName() );
461 // Select the tag if it exists, or reset selection otherwise
462 this.selectTag( this.getItemFromData( item
.model
.getName() ) );
468 * Respond to highlightChange event
470 * @param {boolean} isHighlightEnabled Highlight is enabled
472 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
473 var highlightedItems
= this.model
.getHighlightedItems();
475 if ( isHighlightEnabled
) {
476 // Add capsule widgets
477 highlightedItems
.forEach( function ( filterItem
) {
478 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
481 // Remove capsule widgets if they're not selected
482 highlightedItems
.forEach( function ( filterItem
) {
483 if ( !filterItem
.isSelected() ) {
484 this.removeTagByData( filterItem
.getName() );
493 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
495 menuOption
= this.menu
.getItemFromModel( tagItem
.getModel() ),
496 oldInputValue
= this.input
.getValue().trim();
498 this.menu
.setUserSelecting( true );
501 this.input
.setValue( '' );
504 this.controller
.switchView( tagItem
.getView() );
507 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
509 this.menu
.selectItem( menuOption
);
510 this.selectTag( tagItem
);
512 // Scroll to the item
513 if ( this.model
.removeViewTriggers( oldInputValue
) ) {
514 // We're binding a 'once' to the itemVisibilityChange event
515 // so this happens when the menu is ready after the items
516 // are visible again, in case this is done right after the
517 // user filtered the results
519 'itemVisibilityChange',
521 widget
.scrollToTop( menuOption
.$element
);
522 widget
.menu
.setUserSelecting( false );
526 this.scrollToTop( menuOption
.$element
);
527 this.menu
.setUserSelecting( false );
533 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
534 * If no items are given, reset selection from all.
536 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
537 * omit to deselect all
539 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
540 var i
, len
, selected
;
542 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
543 selected
= this.items
[ i
] === item
;
544 if ( this.items
[ i
].isSelected() !== selected
) {
545 this.items
[ i
].toggleSelected( selected
);
552 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
554 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
556 this.controller
.clearFilter( tagItem
.getName() );
562 * Respond to click event on the reset button
564 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
565 if ( this.model
.areCurrentFiltersEmpty() ) {
566 // Reset to default filters
567 this.controller
.resetToDefaults();
569 // Reset to have no filters
570 this.controller
.emptyFilters();
575 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
577 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
578 var defaultsAreEmpty
= this.model
.areDefaultFiltersEmpty(),
579 currFiltersAreEmpty
= this.model
.areCurrentFiltersEmpty(),
580 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
582 this.resetButton
.setIcon(
583 currFiltersAreEmpty
? 'history' : 'trash'
586 this.resetButton
.setLabel(
587 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
589 this.resetButton
.setTitle(
590 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
593 this.resetButton
.toggle( !hideResetButton
);
594 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
600 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
601 return new mw
.rcfilters
.ui
.MenuSelectWidget(
605 filterFromInput
: true
613 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
614 var filterItem
= this.model
.getItemByName( data
);
617 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
621 $overlay
: this.$overlay
627 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.emphasize = function () {
629 !this.$handle
.hasClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' )
632 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' )
633 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
635 setTimeout( function () {
637 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' );
639 setTimeout( function () {
641 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
642 }.bind( this ), 1000 );
643 }.bind( this ), 500 );
648 * Scroll the element to top within its container
651 * @param {jQuery} $element Element to position
652 * @param {number} [marginFromTop=0] When scrolling the entire widget to the top, leave this
653 * much space (in pixels) above the widget.
654 * @param {Object} [threshold] Minimum distance from the top of the element to scroll at all
655 * @param {number} [threshold.min] Minimum distance above the element
656 * @param {number} [threshold.max] Minimum distance below the element
658 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
, threshold
) {
659 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
660 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
661 containerScrollTop
= $( container
).scrollTop(),
662 effectiveScrollTop
= $( container
).is( 'body, html' ) ? 0 : containerScrollTop
,
663 newScrollTop
= effectiveScrollTop
+ pos
.top
- ( marginFromTop
|| 0 );
667 threshold
=== undefined ||
670 threshold
.min
=== undefined ||
671 newScrollTop
- containerScrollTop
>= threshold
.min
674 threshold
.max
=== undefined ||
675 newScrollTop
- containerScrollTop
<= threshold
.max
679 $( container
).animate( {
680 scrollTop
: newScrollTop