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, { itemUpdate
: 'onSavedQueriesItemUpdate' } );
103 this.emptyFilterMessage
= new OO
.ui
.LabelWidget( {
104 label
: mw
.msg( 'rcfilters-empty-filter' ),
105 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
107 this.$content
.append( this.emptyFilterMessage
.$element
);
110 this.resetButton
.connect( this, { click
: 'onResetButtonClick' } );
111 // Stop propagation for mousedown, so that the widget doesn't
112 // trigger the focus on the input and scrolls up when we click the reset button
113 this.resetButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
114 this.model
.connect( this, {
115 initialize
: 'onModelInitialize',
116 update
: 'onModelUpdate',
117 itemUpdate
: 'onModelItemUpdate',
118 highlightChange
: 'onModelHighlightChange'
120 this.input
.connect( this, { change
: 'onInputChange' } );
122 // The filter list and button should appear side by side regardless of how
123 // wide the button is; the button also changes its width depending
124 // on language and its state, so the safest way to present both side
125 // by side is with a table layout
126 rcFiltersRow
= $( '<div>' )
127 .addClass( 'mw-rcfilters-ui-row' )
130 .addClass( 'mw-rcfilters-ui-cell' )
131 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
134 if ( !mw
.user
.isAnon() ) {
137 .addClass( 'mw-rcfilters-ui-cell' )
138 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
139 .append( this.saveQueryButton
.$element
)
143 // Add a selector at the right of the input
144 this.viewsSelectWidget
= new OO
.ui
.ButtonSelectWidget( {
145 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
147 new OO
.ui
.ButtonOptionWidget( {
151 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget-label' ],
152 label
: mw
.msg( 'rcfilters-view-advanced-filters-label' )
154 new OO
.ui
.ButtonOptionWidget( {
158 title
: mw
.msg( 'rcfilters-view-namespaces-tooltip' )
160 new OO
.ui
.ButtonOptionWidget( {
164 title
: mw
.msg( 'rcfilters-view-tags-tooltip' )
169 // Rearrange the UI so the select widget is at the right of the input
170 this.$element
.append(
172 .addClass( 'mw-rcfilters-ui-table' )
175 .addClass( 'mw-rcfilters-ui-row' )
178 .addClass( 'mw-rcfilters-ui-cell' )
179 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
180 .append( this.input
.$element
),
182 .addClass( 'mw-rcfilters-ui-cell' )
183 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
184 .append( this.viewsSelectWidget
.$element
)
190 this.viewsSelectWidget
.connect( this, { choose
: 'onViewsSelectWidgetChoose' } );
194 .addClass( 'mw-rcfilters-ui-cell' )
195 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
196 .append( this.resetButton
.$element
)
200 $contentWrapper
.append(
202 this.savedQueryTitle
.$element
,
204 .addClass( 'mw-rcfilters-ui-table' )
211 this.$handle
.append( $contentWrapper
);
212 this.emptyFilterMessage
.toggle( this.isEmpty() );
213 this.savedQueryTitle
.toggle( false );
216 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
218 this.reevaluateResetRestoreState();
223 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagMultiselectWidget
, OO
.ui
.MenuTagMultiselectWidget
);
228 * Respond to view select widget choose event
230 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
232 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget
) {
233 this.controller
.switchView( buttonOptionWidget
.getData() );
234 this.viewsSelectWidget
.selectItem( null );
239 * Respond to input change event
241 * @param {string} value Value of the input
243 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputChange = function ( value
) {
246 value
= value
.trim();
248 view
= this.model
.getViewByTrigger( value
.substr( 0, 1 ) );
250 this.controller
.switchView( view
);
253 * Respond to query button click
255 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSaveQueryButtonClick = function () {
256 this.getMenu().toggle( false );
260 * Respond to save query item change. Mainly this is done to update the label in case
261 * a query item has been edited
263 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
265 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesItemUpdate = function ( item
) {
266 if ( this.matchingQuery
=== item
) {
267 // This means we just edited the item that is currently matched
268 this.savedQueryTitle
.setLabel( item
.getLabel() );
273 * Respond to menu toggle
275 * @param {boolean} isVisible Menu is visible
277 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
279 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onMenuToggle
.call( this );
282 mw
.hook( 'RcFilters.popup.open' ).fire();
284 if ( !this.getMenu().getSelectedItem() ) {
285 // If there are no selected items, scroll menu to top
286 // This has to be in a setTimeout so the menu has time
287 // to be positioned and fixed
288 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
292 this.selectTag( null );
294 // Clear input if the only thing in the input is the prefix
296 this.input
.getValue().trim() === this.model
.getViewTrigger( this.model
.getCurrentView() )
299 this.input
.setValue( '' );
302 // Log filter grouping
303 this.controller
.trackFilterGroupings( 'filtermenu' );
306 this.input
.setIcon( isVisible
? 'search' : 'menu' );
312 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputFocus = function () {
314 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
317 this.scrollToTop( this.$element
);
323 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.doInputEscape = function () {
325 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.doInputEscape
.call( this );
328 this.input
.$input
.blur();
334 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
336 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
338 this.emptyFilterMessage
.toggle( this.isEmpty() );
342 * Respond to model initialize event
344 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
345 this.setSavedQueryVisibility();
349 * Respond to model update event
351 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelUpdate = function () {
352 this.updateElementsForView();
356 * Update the elements in the widget to the current view
358 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.updateElementsForView = function () {
359 var view
= this.model
.getCurrentView(),
360 inputValue
= this.input
.getValue().trim(),
361 inputView
= this.model
.getViewByTrigger( inputValue
.substr( 0, 1 ) );
363 if ( inputView
!== 'default' ) {
364 // We have a prefix already, remove it
365 inputValue
= inputValue
.substr( 1 );
368 if ( inputView
!== view
) {
369 // Add the correct prefix
370 inputValue
= this.model
.getViewTrigger( view
) + inputValue
;
374 this.input
.setValue( inputValue
);
376 if ( this.currentView
!== view
) {
377 this.scrollToTop( this.$element
);
378 this.currentView
= view
;
383 * Set the visibility of the saved query button
385 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
386 if ( mw
.user
.isAnon() ) {
390 this.matchingQuery
= this.controller
.findQueryMatchingCurrentState();
392 this.savedQueryTitle
.setLabel(
393 this.matchingQuery
? this.matchingQuery
.getLabel() : ''
395 this.savedQueryTitle
.toggle( !!this.matchingQuery
);
396 this.saveQueryButton
.toggle(
401 if ( this.matchingQuery
) {
407 * Respond to model itemUpdate event
409 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
411 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
412 if ( item
.getGroupModel().isHidden() ) {
419 this.model
.isHighlightEnabled() &&
420 item
.isHighlightSupported() &&
421 item
.getHighlightColor()
424 this.addTag( item
.getName(), item
.getLabel() );
426 this.removeTagByData( item
.getName() );
429 this.setSavedQueryVisibility();
431 // Re-evaluate reset state
432 this.reevaluateResetRestoreState();
438 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
440 this.model
.getItemByName( data
) &&
441 !this.isDuplicateData( data
)
448 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
449 this.controller
.toggleFilterSelect( item
.model
.getName() );
451 // Select the tag if it exists, or reset selection otherwise
452 this.selectTag( this.getItemFromData( item
.model
.getName() ) );
458 * Respond to highlightChange event
460 * @param {boolean} isHighlightEnabled Highlight is enabled
462 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
463 var highlightedItems
= this.model
.getHighlightedItems();
465 if ( isHighlightEnabled
) {
466 // Add capsule widgets
467 highlightedItems
.forEach( function ( filterItem
) {
468 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
471 // Remove capsule widgets if they're not selected
472 highlightedItems
.forEach( function ( filterItem
) {
473 if ( !filterItem
.isSelected() ) {
474 this.removeTagByData( filterItem
.getName() );
483 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
485 menuOption
= this.menu
.getItemFromModel( tagItem
.getModel() ),
486 oldInputValue
= this.input
.getValue().trim();
488 this.menu
.setUserSelecting( true );
491 this.input
.setValue( '' );
494 this.controller
.switchView( tagItem
.getView() );
497 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
499 this.menu
.selectItem( menuOption
);
500 this.selectTag( tagItem
);
502 // Scroll to the item
503 if ( this.model
.removeViewTriggers( oldInputValue
) ) {
504 // We're binding a 'once' to the itemVisibilityChange event
505 // so this happens when the menu is ready after the items
506 // are visible again, in case this is done right after the
507 // user filtered the results
509 'itemVisibilityChange',
511 widget
.scrollToTop( menuOption
.$element
);
512 widget
.menu
.setUserSelecting( false );
516 this.scrollToTop( menuOption
.$element
);
517 this.menu
.setUserSelecting( false );
523 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
524 * If no items are given, reset selection from all.
526 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
527 * omit to deselect all
529 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
530 var i
, len
, selected
;
532 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
533 selected
= this.items
[ i
] === item
;
534 if ( this.items
[ i
].isSelected() !== selected
) {
535 this.items
[ i
].toggleSelected( selected
);
542 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
544 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
546 this.controller
.clearFilter( tagItem
.getName() );
552 * Respond to click event on the reset button
554 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
555 if ( this.model
.areCurrentFiltersEmpty() ) {
556 // Reset to default filters
557 this.controller
.resetToDefaults();
559 // Reset to have no filters
560 this.controller
.emptyFilters();
565 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
567 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
568 var defaultsAreEmpty
= this.model
.areDefaultFiltersEmpty(),
569 currFiltersAreEmpty
= this.model
.areCurrentFiltersEmpty(),
570 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
572 this.resetButton
.setIcon(
573 currFiltersAreEmpty
? 'history' : 'trash'
576 this.resetButton
.setLabel(
577 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
579 this.resetButton
.setTitle(
580 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
583 this.resetButton
.toggle( !hideResetButton
);
584 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
590 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
591 return new mw
.rcfilters
.ui
.MenuSelectWidget(
595 filterFromInput
: true
603 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
604 var filterItem
= this.model
.getItemByName( data
);
607 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
611 $overlay
: this.$overlay
617 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.emphasize = function () {
619 !this.$handle
.hasClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' )
622 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' )
623 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
625 setTimeout( function () {
627 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' );
629 setTimeout( function () {
631 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
632 }.bind( this ), 1000 );
633 }.bind( this ), 500 );
638 * Scroll the element to top within its container
641 * @param {jQuery} $element Element to position
642 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
643 * much space (in pixels) above the widget.
645 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
) {
646 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
647 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
648 containerScrollTop
= $( container
).is( 'body, html' ) ? 0 : $( container
).scrollTop();
651 $( container
).animate( {
652 scrollTop
: containerScrollTop
+ pos
.top
- ( marginFromTop
|| 0 )