3 * List displaying all filter groups
5 * @extends OO.ui.MenuTagMultiselectWidget
6 * @mixins OO.ui.mixin.PendingElement
9 * @param {mw.rcfilters.Controller} controller Controller
10 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
11 * @param {mw.rcfilters.dm.SavedQueriesModel} savedQueriesModel Saved queries model
12 * @param {Object} config Configuration object
13 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
15 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
= function MwRcfiltersUiFilterTagMultiselectWidget( controller
, model
, savedQueriesModel
, config
) {
17 areSavedQueriesEnabled
= mw
.config
.get( 'wgStructuredChangeFiltersEnableSaving' ),
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.areSavedQueriesEnabled
= areSavedQueriesEnabled
;
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,
49 new OO
.ui
.ButtonWidget( {
52 flags
: [ 'progressive' ],
53 label
: mw
.msg( 'rcfilters-filterlist-feedbacklink' ),
54 href
: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
60 placeholder
: mw
.msg( 'rcfilters-search-placeholder' )
64 this.savedQueryTitle
= new OO
.ui
.LabelWidget( {
66 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-savedQueryTitle' ]
69 this.resetButton
= new OO
.ui
.ButtonWidget( {
71 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-resetButton' ]
74 if ( areSavedQueriesEnabled
) {
75 this.saveQueryButton
= new mw
.rcfilters
.ui
.SaveFiltersPopupButtonWidget(
80 this.saveQueryButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
82 this.saveQueryButton
.connect( this, {
83 click
: 'onSaveQueryButtonClick',
84 saveCurrent
: 'setSavedQueryVisibility'
88 this.emptyFilterMessage
= new OO
.ui
.LabelWidget( {
89 label
: mw
.msg( 'rcfilters-empty-filter' ),
90 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
92 this.$content
.append( this.emptyFilterMessage
.$element
);
95 this.resetButton
.connect( this, { click
: 'onResetButtonClick' } );
96 // Stop propagation for mousedown, so that the widget doesn't
97 // trigger the focus on the input and scrolls up when we click the reset button
98 this.resetButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
99 this.model
.connect( this, {
100 initialize
: 'onModelInitialize',
101 update
: 'onModelUpdate',
102 itemUpdate
: 'onModelItemUpdate',
103 highlightChange
: 'onModelHighlightChange'
105 this.input
.connect( this, { change
: 'onInputChange' } );
106 this.queriesModel
.connect( this, { itemUpdate
: 'onSavedQueriesItemUpdate' } );
108 // The filter list and button should appear side by side regardless of how
109 // wide the button is; the button also changes its width depending
110 // on language and its state, so the safest way to present both side
111 // by side is with a table layout
112 rcFiltersRow
= $( '<div>' )
113 .addClass( 'mw-rcfilters-ui-row' )
116 .addClass( 'mw-rcfilters-ui-cell' )
117 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
120 if ( areSavedQueriesEnabled
) {
123 .addClass( 'mw-rcfilters-ui-cell' )
124 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
125 .append( this.saveQueryButton
.$element
)
131 .addClass( 'mw-rcfilters-ui-cell' )
132 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
133 .append( this.resetButton
.$element
)
137 $contentWrapper
.append(
139 this.savedQueryTitle
.$element
,
141 .addClass( 'mw-rcfilters-ui-table' )
148 this.$handle
.append( $contentWrapper
);
149 this.emptyFilterMessage
.toggle( this.isEmpty() );
150 this.savedQueryTitle
.toggle( false );
153 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
155 this.reevaluateResetRestoreState();
160 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagMultiselectWidget
, OO
.ui
.MenuTagMultiselectWidget
);
165 * Respond to input change event
167 * @param {string} value Value of the input
169 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputChange = function ( value
) {
170 var view
= this.model
.getViewByTrigger( value
.substr( 0, 1 ) );
172 this.controller
.switchView( view
);
175 * Respond to query button click
177 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSaveQueryButtonClick = function () {
178 this.getMenu().toggle( false );
182 * Respond to save query item change. Mainly this is done to update the label in case
183 * a query item has been edited
185 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
187 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesItemUpdate = function ( item
) {
188 if ( this.matchingQuery
=== item
) {
189 // This means we just edited the item that is currently matched
190 this.savedQueryTitle
.setLabel( item
.getLabel() );
195 * Respond to menu toggle
197 * @param {boolean} isVisible Menu is visible
199 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
201 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onMenuToggle
.call( this );
204 mw
.hook( 'RcFilters.popup.open' ).fire();
206 if ( !this.getMenu().getSelectedItem() ) {
207 // If there are no selected items, scroll menu to top
208 // This has to be in a setTimeout so the menu has time
209 // to be positioned and fixed
210 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
214 this.selectTag( null );
216 // Clear input if the only thing in the input is the prefix
218 this.input
.getValue() === this.model
.getViewTrigger( this.model
.getCurrentView() )
221 this.input
.setValue( '' );
229 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputFocus = function () {
231 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
234 this.scrollToTop( this.$element
);
240 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.doInputEscape = function () {
242 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.doInputEscape
.call( this );
245 this.input
.$input
.blur();
251 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
253 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
255 this.emptyFilterMessage
.toggle( this.isEmpty() );
259 * Respond to model initialize event
261 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
262 this.setSavedQueryVisibility();
266 * Respond to model update event
268 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelUpdate = function () {
269 this.updateElementsForView();
273 * Update the elements in the widget to the current view
275 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.updateElementsForView = function () {
276 var view
= this.model
.getCurrentView(),
277 inputValue
= this.input
.getValue(),
278 inputView
= this.model
.getViewByTrigger( inputValue
.substr( 0, 1 ) );
280 if ( inputView
!== 'default' ) {
281 // We have a prefix already, remove it
282 inputValue
= inputValue
.substr( 1 );
285 if ( inputView
!== view
) {
286 // Add the correct prefix
287 inputValue
= this.model
.getViewTrigger( view
) + inputValue
;
291 this.input
.setValue( inputValue
);
295 * Set the visibility of the saved query button
297 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
298 if ( this.areSavedQueriesEnabled
) {
299 this.matchingQuery
= this.controller
.findQueryMatchingCurrentState();
301 this.savedQueryTitle
.setLabel(
302 this.matchingQuery
? this.matchingQuery
.getLabel() : ''
304 this.savedQueryTitle
.toggle( !!this.matchingQuery
);
305 this.saveQueryButton
.toggle(
313 * Respond to model itemUpdate event
315 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
317 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
321 this.model
.isHighlightEnabled() &&
322 item
.isHighlightSupported() &&
323 item
.getHighlightColor()
326 this.addTag( item
.getName(), item
.getLabel() );
328 this.removeTagByData( item
.getName() );
331 this.setSavedQueryVisibility();
333 // Re-evaluate reset state
334 this.reevaluateResetRestoreState();
340 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
342 this.model
.getItemByName( data
) &&
343 !this.isDuplicateData( data
)
350 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
351 this.controller
.toggleFilterSelect( item
.model
.getName() );
353 // Select the tag if it exists, or reset selection otherwise
354 this.selectTag( this.getItemFromData( item
.model
.getName() ) );
360 * Respond to highlightChange event
362 * @param {boolean} isHighlightEnabled Highlight is enabled
364 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
365 var highlightedItems
= this.model
.getHighlightedItems();
367 if ( isHighlightEnabled
) {
368 // Add capsule widgets
369 highlightedItems
.forEach( function ( filterItem
) {
370 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
373 // Remove capsule widgets if they're not selected
374 highlightedItems
.forEach( function ( filterItem
) {
375 if ( !filterItem
.isSelected() ) {
376 this.removeTagByData( filterItem
.getName() );
385 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
387 menuOption
= this.menu
.getItemFromModel( tagItem
.getModel() ),
388 oldInputValue
= this.input
.getValue();
391 this.input
.setValue( '' );
394 this.controller
.switchView( tagItem
.getView() );
397 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
399 this.menu
.selectItem( menuOption
);
400 this.selectTag( tagItem
);
402 // Scroll to the item
403 if ( oldInputValue
) {
404 // We're binding a 'once' to the itemVisibilityChange event
405 // so this happens when the menu is ready after the items
406 // are visible again, in case this is done right after the
407 // user filtered the results
409 'itemVisibilityChange',
410 function () { widget
.scrollToTop( menuOption
.$element
); }
413 this.scrollToTop( menuOption
.$element
);
418 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
419 * If no items are given, reset selection from all.
421 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
422 * omit to deselect all
424 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
425 var i
, len
, selected
;
427 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
428 selected
= this.items
[ i
] === item
;
429 if ( this.items
[ i
].isSelected() !== selected
) {
430 this.items
[ i
].toggleSelected( selected
);
437 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
439 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
441 this.controller
.clearFilter( tagItem
.getName() );
447 * Respond to click event on the reset button
449 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
450 if ( this.model
.areCurrentFiltersEmpty() ) {
451 // Reset to default filters
452 this.controller
.resetToDefaults();
454 // Reset to have no filters
455 this.controller
.emptyFilters();
460 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
462 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
463 var defaultsAreEmpty
= this.model
.areDefaultFiltersEmpty(),
464 currFiltersAreEmpty
= this.model
.areCurrentFiltersEmpty(),
465 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
467 this.resetButton
.setIcon(
468 currFiltersAreEmpty
? 'history' : 'trash'
471 this.resetButton
.setLabel(
472 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
474 this.resetButton
.setTitle(
475 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
478 this.resetButton
.toggle( !hideResetButton
);
479 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
485 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
486 return new mw
.rcfilters
.ui
.MenuSelectWidget(
490 filterFromInput
: true
498 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
499 var filterItem
= this.model
.getItemByName( data
);
502 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
506 $overlay
: this.$overlay
513 * Scroll the element to top within its container
516 * @param {jQuery} $element Element to position
517 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
518 * much space (in pixels) above the widget.
520 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
) {
521 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
522 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
523 containerScrollTop
= $( container
).is( 'body, html' ) ? 0 : $( container
).scrollTop();
526 $( container
).animate( {
527 scrollTop
: containerScrollTop
+ pos
.top
- ( marginFromTop
|| 0 )