df3263a2064a3f60f03c6e190f580614b2d30370
[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 // 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 } );
331 };
332
333 /**
334 * @inheritdoc
335 */
336 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.doInputEscape = function () {
337 // Parent
338 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.doInputEscape.call( this );
339
340 // Blur the input
341 this.input.$input.blur();
342 };
343
344 /**
345 * @inheritdoc
346 */
347 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onChangeTags = function () {
348 // Parent method
349 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onChangeTags.call( this );
350
351 this.emptyFilterMessage.toggle( this.isEmpty() );
352 };
353
354 /**
355 * Respond to model initialize event
356 */
357 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelInitialize = function () {
358 this.setSavedQueryVisibility();
359 };
360
361 /**
362 * Respond to model update event
363 */
364 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelUpdate = function () {
365 this.updateElementsForView();
366 };
367
368 /**
369 * Update the elements in the widget to the current view
370 */
371 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.updateElementsForView = function () {
372 var view = this.model.getCurrentView(),
373 inputValue = this.input.getValue().trim(),
374 inputView = this.model.getViewByTrigger( inputValue.substr( 0, 1 ) );
375
376 if ( inputView !== 'default' ) {
377 // We have a prefix already, remove it
378 inputValue = inputValue.substr( 1 );
379 }
380
381 if ( inputView !== view ) {
382 // Add the correct prefix
383 inputValue = this.model.getViewTrigger( view ) + inputValue;
384 }
385
386 // Update input
387 this.input.setValue( inputValue );
388
389 if ( this.currentView !== view ) {
390 this.scrollToTop( this.$element );
391 this.currentView = view;
392 }
393 };
394
395 /**
396 * Set the visibility of the saved query button
397 */
398 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.setSavedQueryVisibility = function () {
399 if ( mw.user.isAnon() ) {
400 return;
401 }
402
403 this.matchingQuery = this.controller.findQueryMatchingCurrentState();
404
405 this.savedQueryTitle.setLabel(
406 this.matchingQuery ? this.matchingQuery.getLabel() : ''
407 );
408 this.savedQueryTitle.toggle( !!this.matchingQuery );
409 this.saveQueryButton.toggle( !this.matchingQuery );
410
411 if ( this.matchingQuery ) {
412 this.emphasize();
413 }
414 };
415
416 /**
417 * Respond to model itemUpdate event
418 *
419 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
420 */
421 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelItemUpdate = function ( item ) {
422 if ( item.getGroupModel().isHidden() ) {
423 return;
424 }
425
426 if (
427 item.isSelected() ||
428 (
429 this.model.isHighlightEnabled() &&
430 item.isHighlightSupported() &&
431 item.getHighlightColor()
432 )
433 ) {
434 this.addTag( item.getName(), item.getLabel() );
435 } else {
436 this.removeTagByData( item.getName() );
437 }
438
439 this.setSavedQueryVisibility();
440
441 // Re-evaluate reset state
442 this.reevaluateResetRestoreState();
443 };
444
445 /**
446 * @inheritdoc
447 */
448 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.isAllowedData = function ( data ) {
449 return (
450 this.model.getItemByName( data ) &&
451 !this.isDuplicateData( data )
452 );
453 };
454
455 /**
456 * @inheritdoc
457 */
458 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onMenuChoose = function ( item ) {
459 this.controller.toggleFilterSelect( item.model.getName() );
460
461 // Select the tag if it exists, or reset selection otherwise
462 this.selectTag( this.getItemFromData( item.model.getName() ) );
463
464 this.focus();
465 };
466
467 /**
468 * Respond to highlightChange event
469 *
470 * @param {boolean} isHighlightEnabled Highlight is enabled
471 */
472 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onModelHighlightChange = function ( isHighlightEnabled ) {
473 var highlightedItems = this.model.getHighlightedItems();
474
475 if ( isHighlightEnabled ) {
476 // Add capsule widgets
477 highlightedItems.forEach( function ( filterItem ) {
478 this.addTag( filterItem.getName(), filterItem.getLabel() );
479 }.bind( this ) );
480 } else {
481 // Remove capsule widgets if they're not selected
482 highlightedItems.forEach( function ( filterItem ) {
483 if ( !filterItem.isSelected() ) {
484 this.removeTagByData( filterItem.getName() );
485 }
486 }.bind( this ) );
487 }
488 };
489
490 /**
491 * @inheritdoc
492 */
493 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagSelect = function ( tagItem ) {
494 var widget = this,
495 menuOption = this.menu.getItemFromModel( tagItem.getModel() ),
496 oldInputValue = this.input.getValue().trim();
497
498 this.menu.setUserSelecting( true );
499
500 // Reset input
501 this.input.setValue( '' );
502
503 // Switch view
504 this.controller.switchView( tagItem.getView() );
505
506 // Parent method
507 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagSelect.call( this, tagItem );
508
509 this.menu.selectItem( menuOption );
510 this.selectTag( tagItem );
511
512 // Scroll to the item
513 if ( this.model.removeViewTriggers( oldInputValue ) ) {
514 // We're binding a 'once' to the itemVisibilityChange event
515 // so this happens when the menu is ready after the items
516 // are visible again, in case this is done right after the
517 // user filtered the results
518 this.getMenu().once(
519 'itemVisibilityChange',
520 function () {
521 widget.scrollToTop( menuOption.$element );
522 widget.menu.setUserSelecting( false );
523 }
524 );
525 } else {
526 this.scrollToTop( menuOption.$element );
527 this.menu.setUserSelecting( false );
528 }
529
530 };
531
532 /**
533 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
534 * If no items are given, reset selection from all.
535 *
536 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
537 * omit to deselect all
538 */
539 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.selectTag = function ( item ) {
540 var i, len, selected;
541
542 for ( i = 0, len = this.items.length; i < len; i++ ) {
543 selected = this.items[ i ] === item;
544 if ( this.items[ i ].isSelected() !== selected ) {
545 this.items[ i ].toggleSelected( selected );
546 }
547 }
548 };
549 /**
550 * @inheritdoc
551 */
552 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onTagRemove = function ( tagItem ) {
553 // Parent method
554 mw.rcfilters.ui.FilterTagMultiselectWidget.parent.prototype.onTagRemove.call( this, tagItem );
555
556 this.controller.clearFilter( tagItem.getName() );
557
558 tagItem.destroy();
559 };
560
561 /**
562 * Respond to click event on the reset button
563 */
564 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.onResetButtonClick = function () {
565 if ( this.model.areCurrentFiltersEmpty() ) {
566 // Reset to default filters
567 this.controller.resetToDefaults();
568 } else {
569 // Reset to have no filters
570 this.controller.emptyFilters();
571 }
572 };
573
574 /**
575 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
576 */
577 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.reevaluateResetRestoreState = function () {
578 var defaultsAreEmpty = this.model.areDefaultFiltersEmpty(),
579 currFiltersAreEmpty = this.model.areCurrentFiltersEmpty(),
580 hideResetButton = currFiltersAreEmpty && defaultsAreEmpty;
581
582 this.resetButton.setIcon(
583 currFiltersAreEmpty ? 'history' : 'trash'
584 );
585
586 this.resetButton.setLabel(
587 currFiltersAreEmpty ? mw.msg( 'rcfilters-restore-default-filters' ) : ''
588 );
589 this.resetButton.setTitle(
590 currFiltersAreEmpty ? null : mw.msg( 'rcfilters-clear-all-filters' )
591 );
592
593 this.resetButton.toggle( !hideResetButton );
594 this.emptyFilterMessage.toggle( currFiltersAreEmpty );
595 };
596
597 /**
598 * @inheritdoc
599 */
600 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createMenuWidget = function ( menuConfig ) {
601 return new mw.rcfilters.ui.MenuSelectWidget(
602 this.controller,
603 this.model,
604 $.extend( {
605 filterFromInput: true
606 }, menuConfig )
607 );
608 };
609
610 /**
611 * @inheritdoc
612 */
613 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.createTagItemWidget = function ( data ) {
614 var filterItem = this.model.getItemByName( data );
615
616 if ( filterItem ) {
617 return new mw.rcfilters.ui.FilterTagItemWidget(
618 this.controller,
619 filterItem,
620 {
621 $overlay: this.$overlay
622 }
623 );
624 }
625 };
626
627 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.emphasize = function () {
628 if (
629 !this.$handle.hasClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' )
630 ) {
631 this.$handle
632 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' )
633 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
634
635 setTimeout( function () {
636 this.$handle
637 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' );
638
639 setTimeout( function () {
640 this.$handle
641 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
642 }.bind( this ), 1000 );
643 }.bind( this ), 500 );
644
645 }
646 };
647 /**
648 * Scroll the element to top within its container
649 *
650 * @private
651 * @param {jQuery} $element Element to position
652 * @param {number} [marginFromTop=0] When scrolling the entire widget to the top, leave this
653 * much space (in pixels) above the widget.
654 * @param {Object} [threshold] Minimum distance from the top of the element to scroll at all
655 * @param {number} [threshold.min] Minimum distance above the element
656 * @param {number} [threshold.max] Minimum distance below the element
657 */
658 mw.rcfilters.ui.FilterTagMultiselectWidget.prototype.scrollToTop = function ( $element, marginFromTop, threshold ) {
659 var container = OO.ui.Element.static.getClosestScrollableContainer( $element[ 0 ], 'y' ),
660 pos = OO.ui.Element.static.getRelativePosition( $element, $( container ) ),
661 containerScrollTop = $( container ).scrollTop(),
662 effectiveScrollTop = $( container ).is( 'body, html' ) ? 0 : containerScrollTop,
663 newScrollTop = effectiveScrollTop + pos.top - ( marginFromTop || 0 );
664
665 // Scroll to item
666 if (
667 threshold === undefined ||
668 (
669 (
670 threshold.min === undefined ||
671 newScrollTop - containerScrollTop >= threshold.min
672 ) &&
673 (
674 threshold.max === undefined ||
675 newScrollTop - containerScrollTop <= threshold.max
676 )
677 )
678 ) {
679 $( container ).animate( {
680 scrollTop: newScrollTop
681 } );
682 }
683 };
684 }( mediaWiki ) );