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 );
327 this.scrollToTop( this.$element
);
333 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.doInputEscape = function () {
335 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.doInputEscape
.call( this );
338 this.input
.$input
.blur();
344 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
346 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
348 this.emptyFilterMessage
.toggle( this.isEmpty() );
352 * Respond to model initialize event
354 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
355 this.setSavedQueryVisibility();
359 * Respond to model update event
361 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelUpdate = function () {
362 this.updateElementsForView();
366 * Update the elements in the widget to the current view
368 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.updateElementsForView = function () {
369 var view
= this.model
.getCurrentView(),
370 inputValue
= this.input
.getValue().trim(),
371 inputView
= this.model
.getViewByTrigger( inputValue
.substr( 0, 1 ) );
373 if ( inputView
!== 'default' ) {
374 // We have a prefix already, remove it
375 inputValue
= inputValue
.substr( 1 );
378 if ( inputView
!== view
) {
379 // Add the correct prefix
380 inputValue
= this.model
.getViewTrigger( view
) + inputValue
;
384 this.input
.setValue( inputValue
);
386 if ( this.currentView
!== view
) {
387 this.scrollToTop( this.$element
);
388 this.currentView
= view
;
393 * Set the visibility of the saved query button
395 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
396 if ( mw
.user
.isAnon() ) {
400 this.matchingQuery
= this.controller
.findQueryMatchingCurrentState();
402 this.savedQueryTitle
.setLabel(
403 this.matchingQuery
? this.matchingQuery
.getLabel() : ''
405 this.savedQueryTitle
.toggle( !!this.matchingQuery
);
406 this.saveQueryButton
.toggle( !this.matchingQuery
);
408 if ( this.matchingQuery
) {
414 * Respond to model itemUpdate event
416 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
418 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
419 if ( item
.getGroupModel().isHidden() ) {
426 this.model
.isHighlightEnabled() &&
427 item
.isHighlightSupported() &&
428 item
.getHighlightColor()
431 this.addTag( item
.getName(), item
.getLabel() );
433 this.removeTagByData( item
.getName() );
436 this.setSavedQueryVisibility();
438 // Re-evaluate reset state
439 this.reevaluateResetRestoreState();
445 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
447 this.model
.getItemByName( data
) &&
448 !this.isDuplicateData( data
)
455 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
456 this.controller
.toggleFilterSelect( item
.model
.getName() );
458 // Select the tag if it exists, or reset selection otherwise
459 this.selectTag( this.getItemFromData( item
.model
.getName() ) );
465 * Respond to highlightChange event
467 * @param {boolean} isHighlightEnabled Highlight is enabled
469 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
470 var highlightedItems
= this.model
.getHighlightedItems();
472 if ( isHighlightEnabled
) {
473 // Add capsule widgets
474 highlightedItems
.forEach( function ( filterItem
) {
475 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
478 // Remove capsule widgets if they're not selected
479 highlightedItems
.forEach( function ( filterItem
) {
480 if ( !filterItem
.isSelected() ) {
481 this.removeTagByData( filterItem
.getName() );
490 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
492 menuOption
= this.menu
.getItemFromModel( tagItem
.getModel() ),
493 oldInputValue
= this.input
.getValue().trim();
495 this.menu
.setUserSelecting( true );
498 this.input
.setValue( '' );
501 this.controller
.switchView( tagItem
.getView() );
504 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
506 this.menu
.selectItem( menuOption
);
507 this.selectTag( tagItem
);
509 // Scroll to the item
510 if ( this.model
.removeViewTriggers( oldInputValue
) ) {
511 // We're binding a 'once' to the itemVisibilityChange event
512 // so this happens when the menu is ready after the items
513 // are visible again, in case this is done right after the
514 // user filtered the results
516 'itemVisibilityChange',
518 widget
.scrollToTop( menuOption
.$element
);
519 widget
.menu
.setUserSelecting( false );
523 this.scrollToTop( menuOption
.$element
);
524 this.menu
.setUserSelecting( false );
530 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
531 * If no items are given, reset selection from all.
533 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
534 * omit to deselect all
536 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
537 var i
, len
, selected
;
539 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
540 selected
= this.items
[ i
] === item
;
541 if ( this.items
[ i
].isSelected() !== selected
) {
542 this.items
[ i
].toggleSelected( selected
);
549 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
551 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
553 this.controller
.clearFilter( tagItem
.getName() );
559 * Respond to click event on the reset button
561 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
562 if ( this.model
.areCurrentFiltersEmpty() ) {
563 // Reset to default filters
564 this.controller
.resetToDefaults();
566 // Reset to have no filters
567 this.controller
.emptyFilters();
572 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
574 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
575 var defaultsAreEmpty
= this.model
.areDefaultFiltersEmpty(),
576 currFiltersAreEmpty
= this.model
.areCurrentFiltersEmpty(),
577 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
579 this.resetButton
.setIcon(
580 currFiltersAreEmpty
? 'history' : 'trash'
583 this.resetButton
.setLabel(
584 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
586 this.resetButton
.setTitle(
587 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
590 this.resetButton
.toggle( !hideResetButton
);
591 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
597 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
598 return new mw
.rcfilters
.ui
.MenuSelectWidget(
602 filterFromInput
: true
610 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
611 var filterItem
= this.model
.getItemByName( data
);
614 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
618 $overlay
: this.$overlay
624 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.emphasize = function () {
626 !this.$handle
.hasClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' )
629 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' )
630 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
632 setTimeout( function () {
634 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' );
636 setTimeout( function () {
638 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
639 }.bind( this ), 1000 );
640 }.bind( this ), 500 );
645 * Scroll the element to top within its container
648 * @param {jQuery} $element Element to position
649 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
650 * much space (in pixels) above the widget.
652 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
) {
653 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
654 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
655 containerScrollTop
= $( container
).is( 'body, html' ) ? 0 : $( container
).scrollTop();
658 $( container
).animate( {
659 scrollTop
: containerScrollTop
+ pos
.top
- ( marginFromTop
|| 0 )