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