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,
50 disabled
: !mw
.config
.get( 'wgStructuredChangeFiltersEnableExperimentalViews' ),
52 // View select menu, appears on default view only
53 $element
: $( '<div>' )
54 .append( new mw
.rcfilters
.ui
.ViewSwitchWidget( this.controller
, this.model
).$element
),
59 // Feedback footer, appears on all views
60 $element
: $( '<div>' )
62 new OO
.ui
.ButtonWidget( {
65 flags
: [ 'progressive' ],
66 label
: mw
.msg( 'rcfilters-filterlist-feedbacklink' ),
67 href
: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
75 placeholder
: mw
.msg( 'rcfilters-search-placeholder' )
79 this.savedQueryTitle
= new OO
.ui
.LabelWidget( {
81 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-savedQueryTitle' ]
84 this.resetButton
= new OO
.ui
.ButtonWidget( {
86 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-resetButton' ]
89 if ( areSavedQueriesEnabled
) {
90 this.saveQueryButton
= new mw
.rcfilters
.ui
.SaveFiltersPopupButtonWidget(
95 this.saveQueryButton
.$element
.on( 'mousedown', function ( e
) { e
.stopPropagation(); } );
97 this.saveQueryButton
.connect( this, {
98 click
: 'onSaveQueryButtonClick',
99 saveCurrent
: 'setSavedQueryVisibility'
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' } );
121 this.queriesModel
.connect( this, { itemUpdate
: 'onSavedQueriesItemUpdate' } );
123 // The filter list and button should appear side by side regardless of how
124 // wide the button is; the button also changes its width depending
125 // on language and its state, so the safest way to present both side
126 // by side is with a table layout
127 rcFiltersRow
= $( '<div>' )
128 .addClass( 'mw-rcfilters-ui-row' )
131 .addClass( 'mw-rcfilters-ui-cell' )
132 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
135 if ( areSavedQueriesEnabled
) {
138 .addClass( 'mw-rcfilters-ui-cell' )
139 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
140 .append( this.saveQueryButton
.$element
)
144 if ( mw
.config
.get( 'wgStructuredChangeFiltersEnableExperimentalViews' ) ) {
145 // Add a selector at the right of the input
146 this.viewsSelectWidget
= new OO
.ui
.ButtonSelectWidget( {
147 classes
: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
149 new OO
.ui
.ButtonOptionWidget( {
152 title
: mw
.msg( 'namespaces' )
154 new OO
.ui
.ButtonOptionWidget( {
157 title
: mw
.msg( 'rcfilters-view-tags' )
162 // Rearrange the UI so the select widget is at the right of the input
163 this.$element
.append(
165 .addClass( 'mw-rcfilters-ui-table' )
168 .addClass( 'mw-rcfilters-ui-row' )
171 .addClass( 'mw-rcfilters-ui-cell' )
172 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
173 .append( this.input
.$element
),
175 .addClass( 'mw-rcfilters-ui-cell' )
176 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
177 .append( this.viewsSelectWidget
.$element
)
183 this.viewsSelectWidget
.connect( this, { choose
: 'onViewsSelectWidgetChoose' } );
188 .addClass( 'mw-rcfilters-ui-cell' )
189 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
190 .append( this.resetButton
.$element
)
194 $contentWrapper
.append(
196 this.savedQueryTitle
.$element
,
198 .addClass( 'mw-rcfilters-ui-table' )
205 this.$handle
.append( $contentWrapper
);
206 this.emptyFilterMessage
.toggle( this.isEmpty() );
207 this.savedQueryTitle
.toggle( false );
210 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
212 this.reevaluateResetRestoreState();
217 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagMultiselectWidget
, OO
.ui
.MenuTagMultiselectWidget
);
222 * Respond to view select widget choose event
224 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
226 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget
) {
227 this.controller
.switchView( buttonOptionWidget
.getData() );
228 this.viewsSelectWidget
.selectItem( null );
233 * Respond to input change event
235 * @param {string} value Value of the input
237 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputChange = function ( value
) {
238 var view
= this.model
.getViewByTrigger( value
.substr( 0, 1 ) );
240 this.controller
.switchView( view
);
243 * Respond to query button click
245 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSaveQueryButtonClick = function () {
246 this.getMenu().toggle( false );
250 * Respond to save query item change. Mainly this is done to update the label in case
251 * a query item has been edited
253 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
255 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onSavedQueriesItemUpdate = function ( item
) {
256 if ( this.matchingQuery
=== item
) {
257 // This means we just edited the item that is currently matched
258 this.savedQueryTitle
.setLabel( item
.getLabel() );
263 * Respond to menu toggle
265 * @param {boolean} isVisible Menu is visible
267 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuToggle = function ( isVisible
) {
269 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onMenuToggle
.call( this );
272 mw
.hook( 'RcFilters.popup.open' ).fire();
274 if ( !this.getMenu().getSelectedItem() ) {
275 // If there are no selected items, scroll menu to top
276 // This has to be in a setTimeout so the menu has time
277 // to be positioned and fixed
278 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
282 this.selectTag( null );
284 // Clear input if the only thing in the input is the prefix
286 this.input
.getValue() === this.model
.getViewTrigger( this.model
.getCurrentView() )
289 this.input
.setValue( '' );
293 this.input
.setIcon( isVisible
? 'search' : 'menu' );
299 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onInputFocus = function () {
301 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onInputFocus
.call( this );
304 this.scrollToTop( this.$element
);
310 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.doInputEscape = function () {
312 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.doInputEscape
.call( this );
315 this.input
.$input
.blur();
321 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onChangeTags = function () {
323 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onChangeTags
.call( this );
325 this.emptyFilterMessage
.toggle( this.isEmpty() );
329 * Respond to model initialize event
331 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelInitialize = function () {
332 this.setSavedQueryVisibility();
336 * Respond to model update event
338 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelUpdate = function () {
339 this.updateElementsForView();
343 * Update the elements in the widget to the current view
345 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.updateElementsForView = function () {
346 var view
= this.model
.getCurrentView(),
347 inputValue
= this.input
.getValue(),
348 inputView
= this.model
.getViewByTrigger( inputValue
.substr( 0, 1 ) );
350 if ( inputView
!== 'default' ) {
351 // We have a prefix already, remove it
352 inputValue
= inputValue
.substr( 1 );
355 if ( inputView
!== view
) {
356 // Add the correct prefix
357 inputValue
= this.model
.getViewTrigger( view
) + inputValue
;
361 this.input
.setValue( inputValue
);
365 * Set the visibility of the saved query button
367 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.setSavedQueryVisibility = function () {
368 if ( this.areSavedQueriesEnabled
) {
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(
383 * Respond to model itemUpdate event
385 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
387 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelItemUpdate = function ( item
) {
391 this.model
.isHighlightEnabled() &&
392 item
.isHighlightSupported() &&
393 item
.getHighlightColor()
396 this.addTag( item
.getName(), item
.getLabel() );
398 this.removeTagByData( item
.getName() );
401 this.setSavedQueryVisibility();
403 // Re-evaluate reset state
404 this.reevaluateResetRestoreState();
410 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.isAllowedData = function ( data
) {
412 this.model
.getItemByName( data
) &&
413 !this.isDuplicateData( data
)
420 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onMenuChoose = function ( item
) {
421 this.controller
.toggleFilterSelect( item
.model
.getName() );
423 // Select the tag if it exists, or reset selection otherwise
424 this.selectTag( this.getItemFromData( item
.model
.getName() ) );
430 * Respond to highlightChange event
432 * @param {boolean} isHighlightEnabled Highlight is enabled
434 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onModelHighlightChange = function ( isHighlightEnabled
) {
435 var highlightedItems
= this.model
.getHighlightedItems();
437 if ( isHighlightEnabled
) {
438 // Add capsule widgets
439 highlightedItems
.forEach( function ( filterItem
) {
440 this.addTag( filterItem
.getName(), filterItem
.getLabel() );
443 // Remove capsule widgets if they're not selected
444 highlightedItems
.forEach( function ( filterItem
) {
445 if ( !filterItem
.isSelected() ) {
446 this.removeTagByData( filterItem
.getName() );
455 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagSelect = function ( tagItem
) {
457 menuOption
= this.menu
.getItemFromModel( tagItem
.getModel() ),
458 oldInputValue
= this.input
.getValue();
460 this.menu
.setUserSelecting( true );
463 this.input
.setValue( '' );
466 this.controller
.switchView( tagItem
.getView() );
469 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagSelect
.call( this, tagItem
);
471 this.menu
.selectItem( menuOption
);
472 this.selectTag( tagItem
);
474 // Scroll to the item
475 if ( this.model
.removeViewTriggers( oldInputValue
) ) {
476 // We're binding a 'once' to the itemVisibilityChange event
477 // so this happens when the menu is ready after the items
478 // are visible again, in case this is done right after the
479 // user filtered the results
481 'itemVisibilityChange',
483 widget
.scrollToTop( menuOption
.$element
);
484 widget
.menu
.setUserSelecting( false );
488 this.scrollToTop( menuOption
.$element
);
489 this.menu
.setUserSelecting( false );
495 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
496 * If no items are given, reset selection from all.
498 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
499 * omit to deselect all
501 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.selectTag = function ( item
) {
502 var i
, len
, selected
;
504 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
505 selected
= this.items
[ i
] === item
;
506 if ( this.items
[ i
].isSelected() !== selected
) {
507 this.items
[ i
].toggleSelected( selected
);
514 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onTagRemove = function ( tagItem
) {
516 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.parent
.prototype.onTagRemove
.call( this, tagItem
);
518 this.controller
.clearFilter( tagItem
.getName() );
524 * Respond to click event on the reset button
526 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.onResetButtonClick = function () {
527 if ( this.model
.areCurrentFiltersEmpty() ) {
528 // Reset to default filters
529 this.controller
.resetToDefaults();
531 // Reset to have no filters
532 this.controller
.emptyFilters();
537 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
539 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.reevaluateResetRestoreState = function () {
540 var defaultsAreEmpty
= this.model
.areDefaultFiltersEmpty(),
541 currFiltersAreEmpty
= this.model
.areCurrentFiltersEmpty(),
542 hideResetButton
= currFiltersAreEmpty
&& defaultsAreEmpty
;
544 this.resetButton
.setIcon(
545 currFiltersAreEmpty
? 'history' : 'trash'
548 this.resetButton
.setLabel(
549 currFiltersAreEmpty
? mw
.msg( 'rcfilters-restore-default-filters' ) : ''
551 this.resetButton
.setTitle(
552 currFiltersAreEmpty
? null : mw
.msg( 'rcfilters-clear-all-filters' )
555 this.resetButton
.toggle( !hideResetButton
);
556 this.emptyFilterMessage
.toggle( currFiltersAreEmpty
);
562 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createMenuWidget = function ( menuConfig
) {
563 return new mw
.rcfilters
.ui
.MenuSelectWidget(
567 filterFromInput
: true
575 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.createTagItemWidget = function ( data
) {
576 var filterItem
= this.model
.getItemByName( data
);
579 return new mw
.rcfilters
.ui
.FilterTagItemWidget(
583 $overlay
: this.$overlay
590 * Scroll the element to top within its container
593 * @param {jQuery} $element Element to position
594 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
595 * much space (in pixels) above the widget.
597 mw
.rcfilters
.ui
.FilterTagMultiselectWidget
.prototype.scrollToTop = function ( $element
, marginFromTop
) {
598 var container
= OO
.ui
.Element
.static.getClosestScrollableContainer( $element
[ 0 ], 'y' ),
599 pos
= OO
.ui
.Element
.static.getRelativePosition( $element
, $( container
) ),
600 containerScrollTop
= $( container
).is( 'body, html' ) ? 0 : $( container
).scrollTop();
603 $( container
).animate( {
604 scrollTop
: containerScrollTop
+ pos
.top
- ( marginFromTop
|| 0 )