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;
34 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.call( this, $.extend( true, {
35 label
: mw
.msg( 'rcfilters-filterlist-title' ),
36 placeholder
: mw
.msg( 'rcfilters-empty-filter' ),
37 inputPosition
: 'outline',
38 allowArbitrary
: false,
39 allowDisplayInvalidTags
: false,
40 allowReordering
: false,
41 $overlay
: this.$overlay
,
43 hideWhenOutOfView
: false,
50 // View select menu, appears on default view only
51 $element
: $( '<div>' )
52 .append( new mw
.rcfilters
.ui
.ViewSwitchWidget( this.controller
, this.model
).$element
),
57 // Feedback footer, appears on all views
58 $element
: $( '<div>' )
60 new OO
.ui
.ButtonWidget( {
63 flags
: [ 'progressive' ],
64 label
: mw
.msg( 'rcfilters-filterlist-feedbacklink' ),
65 href
: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
73 placeholder
: mw
.msg( 'rcfilters-search-placeholder' )
77 this.savedQueryTitle
= new OO
.ui
.LabelWidget( {
79 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-savedQueryTitle' ]
82 this.resetButton
= new OO
.ui
.ButtonWidget( {
84 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-resetButton' ]
87 this.saveQueryButton
= new mw
.rcfilters
.ui
.SaveFiltersPopupButtonWidget(
92 this.saveQueryButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
94 this.saveQueryButton
.connect( this, {
95 click
: 'onSaveQueryButtonClick',
96 saveCurrent
: 'setSavedQueryVisibility'
99 this.emptyFilterMessage
= new OO
.ui
.LabelWidget( {
100 label
: mw
.msg( 'rcfilters-empty-filter' ),
101 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
103 this.$content
.append( this.emptyFilterMessage
.$element
);
106 this.resetButton
.connect( this, { click
: 'onResetButtonClick' } );
107 // Stop propagation for mousedown, so that the widget doesn't
108 // trigger the focus on the input and scrolls up when we click the reset button
109 this.resetButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
110 this.model
.connect( this, {
111 initialize
: 'onModelInitialize',
112 update
: 'onModelUpdate',
113 itemUpdate
: 'onModelItemUpdate',
114 highlightChange
: 'onModelHighlightChange'
116 this.input
.connect( this, { change
: 'onInputChange' } );
117 this.queriesModel
.connect( this, { itemUpdate
: 'onSavedQueriesItemUpdate' } );
119 // The filter list and button should appear side by side regardless of how
120 // wide the button is; the button also changes its width depending
121 // on language and its state, so the safest way to present both side
122 // by side is with a table layout
123 rcFiltersRow
= $( '<div>' )
124 .addClass( 'mw-rcfilters-ui-row' )
127 .addClass( 'mw-rcfilters-ui-cell' )
128 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
133 .addClass( 'mw-rcfilters-ui-cell' )
134 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
135 .append( this.saveQueryButton
.$element
)
138 // Add a selector at the right of the input
139 this.viewsSelectWidget
= new OO
.ui
.ButtonSelectWidget( {
140 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
142 new OO
.ui
.ButtonOptionWidget( {
146 title
: mw
.msg( 'rcfilters-view-namespaces-tooltip' )
148 new OO
.ui
.ButtonOptionWidget( {
152 title
: mw
.msg( 'rcfilters-view-tags-tooltip' )
157 // Rearrange the UI so the select widget is at the right of the input
158 this.$element
.append(
160 .addClass( 'mw-rcfilters-ui-table' )
163 .addClass( 'mw-rcfilters-ui-row' )
166 .addClass( 'mw-rcfilters-ui-cell' )
167 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
168 .append( this.input
.$element
),
170 .addClass( 'mw-rcfilters-ui-cell' )
171 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
172 .append( this.viewsSelectWidget
.$element
)
178 this.viewsSelectWidget
.connect( this, { choose
: 'onViewsSelectWidgetChoose' } );
182 .addClass( 'mw-rcfilters-ui-cell' )
183 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
184 .append( this.resetButton
.$element
)
188 $contentWrapper
.append(
190 this.savedQueryTitle
.$element
,
192 .addClass( 'mw-rcfilters-ui-table' )
199 this.$handle
.append( $contentWrapper
);
200 this.emptyFilterMessage
.toggle( this.isEmpty() );
201 this.savedQueryTitle
.toggle( false );
204 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
206 this.reevaluateResetRestoreState();
211 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagMultiselectWidget
, OO
.ui
.MenuTagMultiselectWidget
);
216 * Respond to view select widget choose event
218 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
220 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget
) {
221 this.controller
.switchView( buttonOptionWidget
.getData() );
222 this.viewsSelectWidget
.selectItem( null );
227 * Respond to input change event
229 * @param {string} value Value of the input
231 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputChange = function ( value
) {
234 value
= value
.trim();
236 view
= this.model
.getViewByTrigger( value
.substr( 0, 1 ) );
238 this.controller
.switchView( view
);
241 * Respond to query button click
243 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSaveQueryButtonClick = function () {
244 this.getMenu().toggle( false );
248 * Respond to save query item change. Mainly this is done to update the label in case
249 * a query item has been edited
251 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
253 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesItemUpdate = function ( item
) {
254 if ( this.matchingQuery
=== item
) {
255 // This means we just edited the item that is currently matched
256 this.savedQueryTitle
.setLabel( item
.getLabel() );
261 * Respond to menu toggle
263 * @param {boolean} isVisible Menu is visible
265 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
267 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onMenuToggle
.call( this );
270 mw
.hook( 'RcFilters.popup.open' ).fire();
272 if ( !this.getMenu().getSelectedItem() ) {
273 // If there are no selected items, scroll menu to top
274 // This has to be in a setTimeout so the menu has time
275 // to be positioned and fixed
276 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
280 this.selectTag( null );
282 // Clear input if the only thing in the input is the prefix
284 this.input
.getValue().trim() === this.model
.getViewTrigger( this.model
.getCurrentView() )
287 this.input
.setValue( '' );
290 // Log filter grouping
291 this.controller
.trackFilterGroupings( 'filtermenu' );
294 this.input
.setIcon( isVisible
? 'search' : 'menu' );
300 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputFocus = function () {
302 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
305 this.scrollToTop( this.$element
);
311 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.doInputEscape = function () {
313 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.doInputEscape
.call( this );
316 this.input
.$input
.blur();
322 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
324 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
326 this.emptyFilterMessage
.toggle( this.isEmpty() );
330 * Respond to model initialize event
332 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
333 this.setSavedQueryVisibility();
337 * Respond to model update event
339 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelUpdate = function () {
340 this.updateElementsForView();
344 * Update the elements in the widget to the current view
346 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.updateElementsForView = function () {
347 var view
= this.model
.getCurrentView(),
348 inputValue
= this.input
.getValue().trim(),
349 inputView
= this.model
.getViewByTrigger( inputValue
.substr( 0, 1 ) );
351 if ( inputView
!== 'default' ) {
352 // We have a prefix already, remove it
353 inputValue
= inputValue
.substr( 1 );
356 if ( inputView
!== view
) {
357 // Add the correct prefix
358 inputValue
= this.model
.getViewTrigger( view
) + inputValue
;
362 this.input
.setValue( inputValue
);
366 * Set the visibility of the saved query button
368 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
369 this.matchingQuery
= this.controller
.findQueryMatchingCurrentState();
371 this.savedQueryTitle
.setLabel(
372 this.matchingQuery
? this.matchingQuery
.getLabel() : ''
374 this.savedQueryTitle
.toggle( !!this.matchingQuery
);
375 this.saveQueryButton
.toggle(
380 if ( this.matchingQuery
) {
386 * Respond to model itemUpdate event
388 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
390 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
391 if ( item
.getGroupModel().isHidden() ) {
398 this.model
.isHighlightEnabled() &&
399 item
.isHighlightSupported() &&
400 item
.getHighlightColor()
403 this.addTag( item
.getName(), item
.getLabel() );
405 this.removeTagByData( item
.getName() );
408 this.setSavedQueryVisibility();
410 // Re-evaluate reset state
411 this.reevaluateResetRestoreState();
417 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
419 this.model
.getItemByName( data
) &&
420 !this.isDuplicateData( data
)
427 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
428 this.controller
.toggleFilterSelect( item
.model
.getName() );
430 // Select the tag if it exists, or reset selection otherwise
431 this.selectTag( this.getItemFromData( item
.model
.getName() ) );
437 * Respond to highlightChange event
439 * @param {boolean} isHighlightEnabled Highlight is enabled
441 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
442 var highlightedItems
= this.model
.getHighlightedItems();
444 if ( isHighlightEnabled
) {
445 // Add capsule widgets
446 highlightedItems
.forEach( function ( filterItem
) {
447 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
450 // Remove capsule widgets if they're not selected
451 highlightedItems
.forEach( function ( filterItem
) {
452 if ( !filterItem
.isSelected() ) {
453 this.removeTagByData( filterItem
.getName() );
462 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
464 menuOption
= this.menu
.getItemFromModel( tagItem
.getModel() ),
465 oldInputValue
= this.input
.getValue().trim();
467 this.menu
.setUserSelecting( true );
470 this.input
.setValue( '' );
473 this.controller
.switchView( tagItem
.getView() );
476 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
478 this.menu
.selectItem( menuOption
);
479 this.selectTag( tagItem
);
481 // Scroll to the item
482 if ( this.model
.removeViewTriggers( oldInputValue
) ) {
483 // We're binding a 'once' to the itemVisibilityChange event
484 // so this happens when the menu is ready after the items
485 // are visible again, in case this is done right after the
486 // user filtered the results
488 'itemVisibilityChange',
490 widget
.scrollToTop( menuOption
.$element
);
491 widget
.menu
.setUserSelecting( false );
495 this.scrollToTop( menuOption
.$element
);
496 this.menu
.setUserSelecting( false );
502 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
503 * If no items are given, reset selection from all.
505 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
506 * omit to deselect all
508 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
509 var i
, len
, selected
;
511 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
512 selected
= this.items
[ i
] === item
;
513 if ( this.items
[ i
].isSelected() !== selected
) {
514 this.items
[ i
].toggleSelected( selected
);
521 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
523 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
525 this.controller
.clearFilter( tagItem
.getName() );
531 * Respond to click event on the reset button
533 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
534 if ( this.model
.areCurrentFiltersEmpty() ) {
535 // Reset to default filters
536 this.controller
.resetToDefaults();
538 // Reset to have no filters
539 this.controller
.emptyFilters();
544 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
546 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
547 var defaultsAreEmpty
= this.model
.areDefaultFiltersEmpty(),
548 currFiltersAreEmpty
= this.model
.areCurrentFiltersEmpty(),
549 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
551 this.resetButton
.setIcon(
552 currFiltersAreEmpty
? 'history' : 'trash'
555 this.resetButton
.setLabel(
556 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
558 this.resetButton
.setTitle(
559 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
562 this.resetButton
.toggle( !hideResetButton
);
563 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
569 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
570 return new mw
.rcfilters
.ui
.MenuSelectWidget(
574 filterFromInput
: true
582 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
583 var filterItem
= this.model
.getItemByName( data
);
586 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
590 $overlay
: this.$overlay
596 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.emphasize = function () {
598 !this.$handle
.hasClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' )
601 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' )
602 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
604 setTimeout( function () {
606 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' );
608 setTimeout( function () {
610 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
611 }.bind( this ), 1000 );
612 }.bind( this ), 500 );
617 * Scroll the element to top within its container
620 * @param {jQuery} $element Element to position
621 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
622 * much space (in pixels) above the widget.
624 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
) {
625 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
626 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
627 containerScrollTop
= $( container
).is( 'body, html' ) ? 0 : $( container
).scrollTop();
630 $( container
).animate( {
631 scrollTop
: containerScrollTop
+ pos
.top
- ( marginFromTop
|| 0 )