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',
103 'default': 'reevaluateResetRestoreState'
107 this.emptyFilterMessage
= new OO
.ui
.LabelWidget( {
108 label
: mw
.msg( 'rcfilters-empty-filter' ),
109 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
111 this.$content
.append( this.emptyFilterMessage
.$element
);
114 this.resetButton
.connect( this, { click
: 'onResetButtonClick' } );
115 // Stop propagation for mousedown, so that the widget doesn't
116 // trigger the focus on the input and scrolls up when we click the reset button
117 this.resetButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
118 this.model
.connect( this, {
119 initialize
: 'onModelInitialize',
120 update
: 'onModelUpdate',
121 itemUpdate
: 'onModelItemUpdate',
122 highlightChange
: 'onModelHighlightChange'
124 this.input
.connect( this, { change
: 'onInputChange' } );
126 // The filter list and button should appear side by side regardless of how
127 // wide the button is; the button also changes its width depending
128 // on language and its state, so the safest way to present both side
129 // by side is with a table layout
130 rcFiltersRow
= $( '<div>' )
131 .addClass( 'mw-rcfilters-ui-row' )
134 .addClass( 'mw-rcfilters-ui-cell' )
135 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
138 if ( !mw
.user
.isAnon() ) {
141 .addClass( 'mw-rcfilters-ui-cell' )
142 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
143 .append( this.saveQueryButton
.$element
)
147 // Add a selector at the right of the input
148 this.viewsSelectWidget
= new OO
.ui
.ButtonSelectWidget( {
149 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
151 new OO
.ui
.ButtonOptionWidget( {
155 label
: mw
.msg( 'namespaces' ),
156 title
: mw
.msg( 'rcfilters-view-namespaces-tooltip' )
158 new OO
.ui
.ButtonOptionWidget( {
162 label
: mw
.msg( 'tags-title' ),
163 title
: mw
.msg( 'rcfilters-view-tags-tooltip' )
168 // Rearrange the UI so the select widget is at the right of the input
169 this.$element
.append(
171 .addClass( 'mw-rcfilters-ui-table' )
174 .addClass( 'mw-rcfilters-ui-row' )
177 .addClass( 'mw-rcfilters-ui-cell' )
178 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
179 .append( this.input
.$element
),
181 .addClass( 'mw-rcfilters-ui-cell' )
182 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
183 .append( this.viewsSelectWidget
.$element
)
189 this.viewsSelectWidget
.connect( this, { choose
: 'onViewsSelectWidgetChoose' } );
193 .addClass( 'mw-rcfilters-ui-cell' )
194 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
195 .append( this.resetButton
.$element
)
199 $contentWrapper
.append(
201 this.savedQueryTitle
.$element
,
203 .addClass( 'mw-rcfilters-ui-table' )
210 this.$handle
.append( $contentWrapper
);
211 this.emptyFilterMessage
.toggle( this.isEmpty() );
212 this.savedQueryTitle
.toggle( false );
215 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
217 this.reevaluateResetRestoreState();
222 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagMultiselectWidget
, OO
.ui
.MenuTagMultiselectWidget
);
227 * Respond to view select widget choose event
229 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
231 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget
) {
232 this.controller
.switchView( buttonOptionWidget
.getData() );
233 this.viewsSelectWidget
.selectItem( null );
238 * Respond to input change event
240 * @param {string} value Value of the input
242 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputChange = function ( value
) {
245 value
= value
.trim();
247 view
= this.model
.getViewByTrigger( value
.substr( 0, 1 ) );
249 this.controller
.switchView( view
);
252 * Respond to query button click
254 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSaveQueryButtonClick = function () {
255 this.getMenu().toggle( false );
259 * Respond to save query model initialization
261 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesInitialize = function () {
262 this.setSavedQueryVisibility();
266 * Respond to save query item change. Mainly this is done to update the label in case
267 * a query item has been edited
269 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
271 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesItemUpdate = function ( item
) {
272 if ( this.matchingQuery
=== item
) {
273 // This means we just edited the item that is currently matched
274 this.savedQueryTitle
.setLabel( item
.getLabel() );
279 * Respond to menu toggle
281 * @param {boolean} isVisible Menu is visible
283 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
285 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onMenuToggle
.call( this );
290 mw
.hook( 'RcFilters.popup.open' ).fire();
292 if ( !this.getMenu().getSelectedItem() ) {
293 // If there are no selected items, scroll menu to top
294 // This has to be in a setTimeout so the menu has time
295 // to be positioned and fixed
296 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.onMouseDown = function ( e
) {
348 if ( !this.isDisabled() && e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
358 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
360 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
362 this.emptyFilterMessage
.toggle( this.isEmpty() );
366 * Respond to model initialize event
368 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
369 this.setSavedQueryVisibility();
373 * Respond to model update event
375 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelUpdate = function () {
376 this.updateElementsForView();
380 * Update the elements in the widget to the current view
382 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.updateElementsForView = function () {
383 var view
= this.model
.getCurrentView(),
384 inputValue
= this.input
.getValue().trim(),
385 inputView
= this.model
.getViewByTrigger( inputValue
.substr( 0, 1 ) );
387 if ( inputView
!== 'default' ) {
388 // We have a prefix already, remove it
389 inputValue
= inputValue
.substr( 1 );
392 if ( inputView
!== view
) {
393 // Add the correct prefix
394 inputValue
= this.model
.getViewTrigger( view
) + inputValue
;
398 this.input
.setValue( inputValue
);
400 if ( this.currentView
!== view
) {
401 this.scrollToTop( this.$element
);
402 this.currentView
= view
;
407 * Set the visibility of the saved query button
409 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
410 if ( mw
.user
.isAnon() ) {
414 this.matchingQuery
= this.controller
.findQueryMatchingCurrentState();
416 this.savedQueryTitle
.setLabel(
417 this.matchingQuery
? this.matchingQuery
.getLabel() : ''
419 this.savedQueryTitle
.toggle( !!this.matchingQuery
);
420 this.saveQueryButton
.setDisabled( !!this.matchingQuery
);
421 this.saveQueryButton
.setTitle( !this.matchingQuery
?
422 mw
.msg( 'rcfilters-savedqueries-add-new-title' ) :
423 mw
.msg( 'rcfilters-savedqueries-already-saved' ) );
425 if ( this.matchingQuery
) {
431 * Respond to model itemUpdate event
432 * fixme: when a new state is applied to the model this function is called 60+ times in a row
434 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
436 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
437 if ( !item
.getGroupModel().isHidden() ) {
441 this.model
.isHighlightEnabled() &&
442 item
.getHighlightColor()
445 this.addTag( item
.getName(), item
.getLabel() );
447 this.removeTagByData( item
.getName() );
451 this.setSavedQueryVisibility();
453 // Re-evaluate reset state
454 this.reevaluateResetRestoreState();
460 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
462 this.model
.getItemByName( data
) &&
463 !this.isDuplicateData( data
)
470 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
471 this.controller
.toggleFilterSelect( item
.model
.getName() );
473 // Select the tag if it exists, or reset selection otherwise
474 this.selectTag( this.getItemFromData( item
.model
.getName() ) );
480 * Respond to highlightChange event
482 * @param {boolean} isHighlightEnabled Highlight is enabled
484 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
485 var highlightedItems
= this.model
.getHighlightedItems();
487 if ( isHighlightEnabled
) {
488 // Add capsule widgets
489 highlightedItems
.forEach( function ( filterItem
) {
490 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
493 // Remove capsule widgets if they're not selected
494 highlightedItems
.forEach( function ( filterItem
) {
495 if ( !filterItem
.isSelected() ) {
496 this.removeTagByData( filterItem
.getName() );
501 this.setSavedQueryVisibility();
507 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
509 menuOption
= this.menu
.getItemFromModel( tagItem
.getModel() ),
510 oldInputValue
= this.input
.getValue().trim();
512 this.menu
.setUserSelecting( true );
515 this.input
.setValue( '' );
518 this.controller
.switchView( tagItem
.getView() );
521 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
523 this.menu
.selectItem( menuOption
);
524 this.selectTag( tagItem
);
526 // Scroll to the item
527 if ( this.model
.removeViewTriggers( oldInputValue
) ) {
528 // We're binding a 'once' to the itemVisibilityChange event
529 // so this happens when the menu is ready after the items
530 // are visible again, in case this is done right after the
531 // user filtered the results
533 'itemVisibilityChange',
535 widget
.scrollToTop( menuOption
.$element
);
536 widget
.menu
.setUserSelecting( false );
540 this.scrollToTop( menuOption
.$element
);
541 this.menu
.setUserSelecting( false );
547 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
548 * If no items are given, reset selection from all.
550 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
551 * omit to deselect all
553 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
554 var i
, len
, selected
;
556 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
557 selected
= this.items
[ i
] === item
;
558 if ( this.items
[ i
].isSelected() !== selected
) {
559 this.items
[ i
].toggleSelected( selected
);
566 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
568 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
570 this.controller
.clearFilter( tagItem
.getName() );
576 * Respond to click event on the reset button
578 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
579 if ( this.model
.areCurrentFiltersEmpty() ) {
580 // Reset to default filters
581 this.controller
.resetToDefaults();
583 // Reset to have no filters
584 this.controller
.emptyFilters();
589 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
591 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
592 var defaultsAreEmpty
= this.controller
.areDefaultsEmpty(),
593 currFiltersAreEmpty
= this.model
.areCurrentFiltersEmpty(),
594 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
596 this.resetButton
.setIcon(
597 currFiltersAreEmpty
? 'history' : 'trash'
600 this.resetButton
.setLabel(
601 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
603 this.resetButton
.setTitle(
604 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
607 this.resetButton
.toggle( !hideResetButton
);
608 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
614 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
615 return new mw
.rcfilters
.ui
.MenuSelectWidget(
619 filterFromInput
: true
627 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
628 var filterItem
= this.model
.getItemByName( data
);
631 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
634 this.model
.getInvertModel(),
637 $overlay
: this.$overlay
643 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.emphasize = function () {
645 !this.$handle
.hasClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' )
648 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' )
649 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
651 setTimeout( function () {
653 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' );
655 setTimeout( function () {
657 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
658 }.bind( this ), 1000 );
659 }.bind( this ), 500 );
664 * Scroll the element to top within its container
667 * @param {jQuery} $element Element to position
668 * @param {number} [marginFromTop=0] When scrolling the entire widget to the top, leave this
669 * much space (in pixels) above the widget.
670 * @param {Object} [threshold] Minimum distance from the top of the element to scroll at all
671 * @param {number} [threshold.min] Minimum distance above the element
672 * @param {number} [threshold.max] Minimum distance below the element
674 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
, threshold
) {
675 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
676 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
677 containerScrollTop
= $( container
).scrollTop(),
678 effectiveScrollTop
= $( container
).is( 'body, html' ) ? 0 : containerScrollTop
,
679 newScrollTop
= effectiveScrollTop
+ pos
.top
- ( marginFromTop
|| 0 );
683 threshold
=== undefined ||
686 threshold
.min
=== undefined ||
687 newScrollTop
- containerScrollTop
>= threshold
.min
690 threshold
.max
=== undefined ||
691 newScrollTop
- containerScrollTop
<= threshold
.max
695 $( container
).animate( {
696 scrollTop
: newScrollTop