Merge "RCFilters: Allow empty saved query"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.FilterTagMultiselectWidget.js
1 ( function ( mw ) {
2 /**
3 * List displaying all filter groups
4 *
5 * @class
6 * @extends OO.ui.MenuTagMultiselectWidget
7 * @mixins OO.ui.mixin.PendingElement
8 *
9 * @constructor
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
15 */
16 mw.rcfilters.ui.FilterTagMultiselectWidget = function MwRcfiltersUiFilterTagMultiselectWidget( controller, model, savedQueriesModel, config ) {
17 var rcFiltersRow,
18 title = new OO.ui.LabelWidget( {
19 label: mw.msg( 'rcfilters-activefilters' ),
20 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-title' ]
21 } ),
22 $contentWrapper = $( '<div>' )
23 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper' );
24
25 config = config || {};
26
27 this.controller = controller;
28 this.model = model;
29 this.queriesModel = savedQueriesModel;
30 this.$overlay = config.$overlay || this.$element;
31 this.matchingQuery = null;
32 this.currentView = this.model.getCurrentView();
33
34 // Parent
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,
43 menu: {
44 hideWhenOutOfView: false,
45 hideOnChoose: false,
46 width: 650,
47 footers: [
48 {
49 name: 'viewSelect',
50 sticky: 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 ),
54 views: [ 'default' ]
55 },
56 {
57 name: 'feedback',
58 // Feedback footer, appears on all views
59 $element: $( '<div>' )
60 .append(
61 new OO.ui.ButtonWidget( {
62 framed: false,
63 icon: 'feedback',
64 flags: [ 'progressive' ],
65 label: mw.msg( 'rcfilters-filterlist-feedbacklink' ),
66 href: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
67 } ).$element
68 )
69 }
70 ]
71 },
72 input: {
73 icon: 'menu',
74 placeholder: mw.msg( 'rcfilters-search-placeholder' )
75 }
76 }, config ) );
77
78 this.savedQueryTitle = new OO.ui.LabelWidget( {
79 label: '',
80 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-savedQueryTitle' ]
81 } );
82
83 this.resetButton = new OO.ui.ButtonWidget( {
84 framed: false,
85 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-resetButton' ]
86 } );
87
88 if ( !mw.user.isAnon() ) {
89 this.saveQueryButton = new mw.rcfilters.ui.SaveFiltersPopupButtonWidget(
90 this.controller,
91 this.queriesModel
92 );
93
94 this.saveQueryButton.$element.on( 'mousedown', function ( e ) { e.stopPropagation(); } );
95
96 this.saveQueryButton.connect( this, {
97 click: 'onSaveQueryButtonClick',
98 saveCurrent: 'setSavedQueryVisibility'
99 } );
100 this.queriesModel.connect( this, {
101 itemUpdate: 'onSavedQueriesItemUpdate',
102 initialize: 'onSavedQueriesInitialize'
103 } );
104 }
105
106 this.emptyFilterMessage = new OO.ui.LabelWidget( {
107 label: mw.msg( 'rcfilters-empty-filter' ),
108 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
109 } );
110 this.$content.append( this.emptyFilterMessage.$element );
111
112 // Events
113 this.resetButton.connect( this, { click: 'onResetButtonClick' } );
114 // Stop propagation for mousedown, so that the widget doesn't
115 // trigger the focus on the input and scrolls up when we click the reset button
116 this.resetButton.$element.on( 'mousedown', function ( e ) { e.stopPropagation(); } );
117 this.model.connect( this, {
118 initialize: 'onModelInitialize',
119 update: 'onModelUpdate',
120 itemUpdate: 'onModelItemUpdate',
121 highlightChange: 'onModelHighlightChange'
122 } );
123 this.input.connect( this, { change: 'onInputChange' } );
124
125 // The filter list and button should appear side by side regardless of how
126 // wide the button is; the button also changes its width depending
127 // on language and its state, so the safest way to present both side
128 // by side is with a table layout
129 rcFiltersRow = $( '<div>' )
130 .addClass( 'mw-rcfilters-ui-row' )
131 .append(
132 this.$content
133 .addClass( 'mw-rcfilters-ui-cell' )
134 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
135 );
136
137 if ( !mw.user.isAnon() ) {
138 rcFiltersRow.append(
139 $( '<div>' )
140 .addClass( 'mw-rcfilters-ui-cell' )
141 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
142 .append( this.saveQueryButton.$element )
143 );
144 }
145
146 // Add a selector at the right of the input
147 this.viewsSelectWidget = new OO.ui.ButtonSelectWidget( {
148 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
149 items: [
150 new OO.ui.ButtonOptionWidget( {
151 framed: false,
152 data: '',
153 disabled: true,
154 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget-label' ],
155 label: mw.msg( 'rcfilters-view-advanced-filters-label' )
156 } ),
157 new OO.ui.ButtonOptionWidget( {
158 framed: false,
159 data: 'namespaces',
160 icon: 'article',
161 title: mw.msg( 'rcfilters-view-namespaces-tooltip' )
162 } ),
163 new OO.ui.ButtonOptionWidget( {
164 framed: false,
165 data: 'tags',
166 icon: 'tag',
167 title: mw.msg( 'rcfilters-view-tags-tooltip' )
168 } )
169 ]
170 } );
171
172 // Rearrange the UI so the select widget is at the right of the input
173 this.$element.append(
174 $( '<div>' )
175 .addClass( 'mw-rcfilters-ui-table' )
176 .append(
177 $( '<div>' )
178 .addClass( 'mw-rcfilters-ui-row' )
179 .append(
180 $( '<div>' )
181 .addClass( 'mw-rcfilters-ui-cell' )
182 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
183 .append( this.input.$element ),
184 $( '<div>' )
185 .addClass( 'mw-rcfilters-ui-cell' )
186 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
187 .append( this.viewsSelectWidget.$element )
188 )
189 )
190 );
191
192 // Event
193 this.viewsSelectWidget.connect( this, { choose: 'onViewsSelectWidgetChoose' } );
194
195 rcFiltersRow.append(
196 $( '<div>' )
197 .addClass( 'mw-rcfilters-ui-cell' )
198 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
199 .append( this.resetButton.$element )
200 );
201
202 // Build the content
203 $contentWrapper.append(
204 title.$element,
205 this.savedQueryTitle.$element,
206 $( '<div>' )
207 .addClass( 'mw-rcfilters-ui-table' )
208 .append(
209 rcFiltersRow
210 )
211 );
212
213 // Initialize
214 this.$handle.append( $contentWrapper );
215 this.emptyFilterMessage.toggle( this.isEmpty() );
216 this.savedQueryTitle.toggle( false );
217
218 this.$element
219 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
220
221 this.reevaluateResetRestoreState();
222 };
223
224 /* Initialization */
225
226 OO.inheritClass( mw.rcfilters.ui.FilterTagMultiselectWidget, OO.ui.MenuTagMultiselectWidget );
227
228 /* Methods */
229
230 /**
231 * Respond to view select widget choose event
232 *
233 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
234 */
235 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget ) {
236 this.controller.switchView( buttonOptionWidget.getData() );
237 this.viewsSelectWidget.selectItem( null );
238 this.focus();
239 };
240
241 /**
242 * Respond to input change event
243 *
244 * @param {string} value Value of the input
245 */
246 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onInputChange = function ( value ) {
247 var view;
248
249 value = value.trim();
250
251 view = this.model.getViewByTrigger( value.substr( 0, 1 ) );
252
253 this.controller.switchView( view );
254 };
255 /**
256 * Respond to query button click
257 */
258 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onSaveQueryButtonClick = function () {
259 this.getMenu().toggle( false );
260 };
261
262 /**
263 * Respond to save query model initialization
264 */
265 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onSavedQueriesInitialize = function () {
266 this.setSavedQueryVisibility();
267 };
268
269 /**
270 * Respond to save query item change. Mainly this is done to update the label in case
271 * a query item has been edited
272 *
273 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
274 */
275 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onSavedQueriesItemUpdate = function ( item ) {
276 if ( this.matchingQuery === item ) {
277 // This means we just edited the item that is currently matched
278 this.savedQueryTitle.setLabel( item.getLabel() );
279 }
280 };
281
282 /**
283 * Respond to menu toggle
284 *
285 * @param {boolean} isVisible Menu is visible
286 */
287 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onMenuToggle = function ( isVisible ) {
288 // Parent
289 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onMenuToggle.call( this );
290
291 if ( isVisible ) {
292 mw.hook( 'RcFilters.popup.open' ).fire();
293
294 if ( !this.getMenu().getSelectedItem() ) {
295 // If there are no selected items, scroll menu to top
296 // This has to be in a setTimeout so the menu has time
297 // to be positioned and fixed
298 setTimeout( function () { this.getMenu().scrollToTop(); }.bind( this ), 0 );
299 }
300 } else {
301 // Clear selection
302 this.selectTag( null );
303
304 // Clear input if the only thing in the input is the prefix
305 if (
306 this.input.getValue().trim() === this.model.getViewTrigger( this.model.getCurrentView() )
307 ) {
308 // Clear the input
309 this.input.setValue( '' );
310 }
311
312 // Log filter grouping
313 this.controller.trackFilterGroupings( 'filtermenu' );
314 }
315
316 this.input.setIcon( isVisible ? 'search' : 'menu' );
317 };
318
319 /**
320 * @inheritdoc
321 */
322 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onInputFocus = function () {
323 // Parent
324 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onInputFocus.call( this );
325
326 // Scroll to top
327 this.scrollToTop( this.$element );
328 };
329
330 /**
331 * @inheritdoc
332 */
333 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.doInputEscape = function () {
334 // Parent
335 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.doInputEscape.call( this );
336
337 // Blur the input
338 this.input.$input.blur();
339 };
340
341 /**
342 * @inheritdoc
343 */
344 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onChangeTags = function () {
345 // Parent method
346 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onChangeTags.call( this );
347
348 this.emptyFilterMessage.toggle( this.isEmpty() );
349 };
350
351 /**
352 * Respond to model initialize event
353 */
354 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelInitialize = function () {
355 this.setSavedQueryVisibility();
356 };
357
358 /**
359 * Respond to model update event
360 */
361 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelUpdate = function () {
362 this.updateElementsForView();
363 };
364
365 /**
366 * Update the elements in the widget to the current view
367 */
368 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.updateElementsForView = function () {
369 var view = this.model.getCurrentView(),
370 inputValue = this.input.getValue().trim(),
371 inputView = this.model.getViewByTrigger( inputValue.substr( 0, 1 ) );
372
373 if ( inputView !== 'default' ) {
374 // We have a prefix already, remove it
375 inputValue = inputValue.substr( 1 );
376 }
377
378 if ( inputView !== view ) {
379 // Add the correct prefix
380 inputValue = this.model.getViewTrigger( view ) + inputValue;
381 }
382
383 // Update input
384 this.input.setValue( inputValue );
385
386 if ( this.currentView !== view ) {
387 this.scrollToTop( this.$element );
388 this.currentView = view;
389 }
390 };
391
392 /**
393 * Set the visibility of the saved query button
394 */
395 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.setSavedQueryVisibility = function () {
396 if ( mw.user.isAnon() ) {
397 return;
398 }
399
400 this.matchingQuery = this.controller.findQueryMatchingCurrentState();
401
402 this.savedQueryTitle.setLabel(
403 this.matchingQuery ? this.matchingQuery.getLabel() : ''
404 );
405 this.savedQueryTitle.toggle( !!this.matchingQuery );
406 this.saveQueryButton.toggle( !this.matchingQuery );
407
408 if ( this.matchingQuery ) {
409 this.emphasize();
410 }
411 };
412
413 /**
414 * Respond to model itemUpdate event
415 *
416 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
417 */
418 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelItemUpdate = function ( item ) {
419 if ( item.getGroupModel().isHidden() ) {
420 return;
421 }
422
423 if (
424 item.isSelected() ||
425 (
426 this.model.isHighlightEnabled() &&
427 item.isHighlightSupported() &&
428 item.getHighlightColor()
429 )
430 ) {
431 this.addTag( item.getName(), item.getLabel() );
432 } else {
433 this.removeTagByData( item.getName() );
434 }
435
436 this.setSavedQueryVisibility();
437
438 // Re-evaluate reset state
439 this.reevaluateResetRestoreState();
440 };
441
442 /**
443 * @inheritdoc
444 */
445 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.isAllowedData = function ( data ) {
446 return (
447 this.model.getItemByName( data ) &&
448 !this.isDuplicateData( data )
449 );
450 };
451
452 /**
453 * @inheritdoc
454 */
455 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onMenuChoose = function ( item ) {
456 this.controller.toggleFilterSelect( item.model.getName() );
457
458 // Select the tag if it exists, or reset selection otherwise
459 this.selectTag( this.getItemFromData( item.model.getName() ) );
460
461 this.focus();
462 };
463
464 /**
465 * Respond to highlightChange event
466 *
467 * @param {boolean} isHighlightEnabled Highlight is enabled
468 */
469 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelHighlightChange = function ( isHighlightEnabled ) {
470 var highlightedItems = this.model.getHighlightedItems();
471
472 if ( isHighlightEnabled ) {
473 // Add capsule widgets
474 highlightedItems.forEach( function ( filterItem ) {
475 this.addTag( filterItem.getName(), filterItem.getLabel() );
476 }.bind( this ) );
477 } else {
478 // Remove capsule widgets if they're not selected
479 highlightedItems.forEach( function ( filterItem ) {
480 if ( !filterItem.isSelected() ) {
481 this.removeTagByData( filterItem.getName() );
482 }
483 }.bind( this ) );
484 }
485 };
486
487 /**
488 * @inheritdoc
489 */
490 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagSelect = function ( tagItem ) {
491 var widget = this,
492 menuOption = this.menu.getItemFromModel( tagItem.getModel() ),
493 oldInputValue = this.input.getValue().trim();
494
495 this.menu.setUserSelecting( true );
496
497 // Reset input
498 this.input.setValue( '' );
499
500 // Switch view
501 this.controller.switchView( tagItem.getView() );
502
503 // Parent method
504 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagSelect.call( this, tagItem );
505
506 this.menu.selectItem( menuOption );
507 this.selectTag( tagItem );
508
509 // Scroll to the item
510 if ( this.model.removeViewTriggers( oldInputValue ) ) {
511 // We're binding a 'once' to the itemVisibilityChange event
512 // so this happens when the menu is ready after the items
513 // are visible again, in case this is done right after the
514 // user filtered the results
515 this.getMenu().once(
516 'itemVisibilityChange',
517 function () {
518 widget.scrollToTop( menuOption.$element );
519 widget.menu.setUserSelecting( false );
520 }
521 );
522 } else {
523 this.scrollToTop( menuOption.$element );
524 this.menu.setUserSelecting( false );
525 }
526
527 };
528
529 /**
530 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
531 * If no items are given, reset selection from all.
532 *
533 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
534 * omit to deselect all
535 */
536 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.selectTag = function ( item ) {
537 var i, len, selected;
538
539 for ( i = 0, len = this.items.length; i < len; i++ ) {
540 selected = this.items[ i ] === item;
541 if ( this.items[ i ].isSelected() !== selected ) {
542 this.items[ i ].toggleSelected( selected );
543 }
544 }
545 };
546 /**
547 * @inheritdoc
548 */
549 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagRemove = function ( tagItem ) {
550 // Parent method
551 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagRemove.call( this, tagItem );
552
553 this.controller.clearFilter( tagItem.getName() );
554
555 tagItem.destroy();
556 };
557
558 /**
559 * Respond to click event on the reset button
560 */
561 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onResetButtonClick = function () {
562 if ( this.model.areCurrentFiltersEmpty() ) {
563 // Reset to default filters
564 this.controller.resetToDefaults();
565 } else {
566 // Reset to have no filters
567 this.controller.emptyFilters();
568 }
569 };
570
571 /**
572 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
573 */
574 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.reevaluateResetRestoreState = function () {
575 var defaultsAreEmpty = this.model.areDefaultFiltersEmpty(),
576 currFiltersAreEmpty = this.model.areCurrentFiltersEmpty(),
577 hideResetButton = currFiltersAreEmpty && defaultsAreEmpty;
578
579 this.resetButton.setIcon(
580 currFiltersAreEmpty ? 'history' : 'trash'
581 );
582
583 this.resetButton.setLabel(
584 currFiltersAreEmpty ? mw.msg( 'rcfilters-restore-default-filters' ) : ''
585 );
586 this.resetButton.setTitle(
587 currFiltersAreEmpty ? null : mw.msg( 'rcfilters-clear-all-filters' )
588 );
589
590 this.resetButton.toggle( !hideResetButton );
591 this.emptyFilterMessage.toggle( currFiltersAreEmpty );
592 };
593
594 /**
595 * @inheritdoc
596 */
597 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createMenuWidget = function ( menuConfig ) {
598 return new mw.rcfilters.ui.MenuSelectWidget(
599 this.controller,
600 this.model,
601 $.extend( {
602 filterFromInput: true
603 }, menuConfig )
604 );
605 };
606
607 /**
608 * @inheritdoc
609 */
610 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createTagItemWidget = function ( data ) {
611 var filterItem = this.model.getItemByName( data );
612
613 if ( filterItem ) {
614 return new mw.rcfilters.ui.FilterTagItemWidget(
615 this.controller,
616 filterItem,
617 {
618 $overlay: this.$overlay
619 }
620 );
621 }
622 };
623
624 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.emphasize = function () {
625 if (
626 !this.$handle.hasClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' )
627 ) {
628 this.$handle
629 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' )
630 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
631
632 setTimeout( function () {
633 this.$handle
634 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' );
635
636 setTimeout( function () {
637 this.$handle
638 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
639 }.bind( this ), 1000 );
640 }.bind( this ), 500 );
641
642 }
643 };
644 /**
645 * Scroll the element to top within its container
646 *
647 * @private
648 * @param {jQuery} $element Element to position
649 * @param {number} [marginFromTop] When scrolling the entire widget to the top, leave this
650 * much space (in pixels) above the widget.
651 */
652 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.scrollToTop = function ( $element, marginFromTop ) {
653 var container = OO.ui.Element.static.getClosestScrollableContainer( $element[ 0 ], 'y' ),
654 pos = OO.ui.Element.static.getRelativePosition( $element, $( container ) ),
655 containerScrollTop = $( container ).is( 'body, html' ) ? 0 : $( container ).scrollTop();
656
657 // Scroll to item
658 $( container ).animate( {
659 scrollTop: containerScrollTop + pos.top - ( marginFromTop || 0 )
660 } );
661 };
662 }( mediaWiki ) );