Merge "jquery.suggestions: Correctly place dropdown for inputs with 'position: fixed'"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / FilterTagMultiselectWidget.js
1 var ViewSwitchWidget = require( './ViewSwitchWidget.js' ),
2 SaveFiltersPopupButtonWidget = require( './SaveFiltersPopupButtonWidget.js' ),
3 MenuSelectWidget = require( './MenuSelectWidget.js' ),
4 FilterTagItemWidget = require( './FilterTagItemWidget.js' ),
5 FilterTagMultiselectWidget;
6
7 /**
8 * List displaying all filter groups
9 *
10 * @class mw.rcfilters.ui.FilterTagMultiselectWidget
11 * @extends OO.ui.MenuTagMultiselectWidget
12 * @mixins OO.ui.mixin.PendingElement
13 *
14 * @constructor
15 * @param {mw.rcfilters.Controller} controller Controller
16 * @param {mw.rcfilters.dm.FiltersViewModel} model View model
17 * @param {mw.rcfilters.dm.SavedQueriesModel} savedQueriesModel Saved queries model
18 * @param {Object} config Configuration object
19 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
20 * @cfg {jQuery} [$wrapper] A jQuery object for the wrapper of the general
21 * system. If not given, falls back to this widget's $element
22 * @cfg {boolean} [collapsed] Filter area is collapsed
23 */
24 FilterTagMultiselectWidget = function MwRcfiltersUiFilterTagMultiselectWidget( controller, model, savedQueriesModel, config ) {
25 var rcFiltersRow,
26 title = new OO.ui.LabelWidget( {
27 label: mw.msg( 'rcfilters-activefilters' ),
28 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-title' ]
29 } ),
30 $contentWrapper = $( '<div>' )
31 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper' );
32
33 config = config || {};
34
35 this.controller = controller;
36 this.model = model;
37 this.queriesModel = savedQueriesModel;
38 this.$overlay = config.$overlay || this.$element;
39 this.$wrapper = config.$wrapper || this.$element;
40 this.matchingQuery = null;
41 this.currentView = this.model.getCurrentView();
42 this.collapsed = false;
43
44 // Parent
45 FilterTagMultiselectWidget.parent.call( this, $.extend( true, {
46 label: mw.msg( 'rcfilters-filterlist-title' ),
47 placeholder: mw.msg( 'rcfilters-empty-filter' ),
48 inputPosition: 'outline',
49 allowArbitrary: false,
50 allowDisplayInvalidTags: false,
51 allowReordering: false,
52 $overlay: this.$overlay,
53 menu: {
54 // Our filtering is done through the model
55 filterFromInput: false,
56 hideWhenOutOfView: false,
57 hideOnChoose: false,
58 width: 650,
59 footers: [
60 {
61 name: 'viewSelect',
62 sticky: false,
63 // View select menu, appears on default view only
64 $element: $( '<div>' )
65 .append( new ViewSwitchWidget( this.controller, this.model ).$element ),
66 views: [ 'default' ]
67 },
68 {
69 name: 'feedback',
70 // Feedback footer, appears on all views
71 $element: $( '<div>' )
72 .append(
73 new OO.ui.ButtonWidget( {
74 framed: false,
75 icon: 'feedback',
76 flags: [ 'progressive' ],
77 label: mw.msg( 'rcfilters-filterlist-feedbacklink' ),
78 href: 'https://www.mediawiki.org/wiki/Help_talk:New_filters_for_edit_review'
79 } ).$element
80 )
81 }
82 ]
83 },
84 input: {
85 icon: 'menu',
86 placeholder: mw.msg( 'rcfilters-search-placeholder' )
87 }
88 }, config ) );
89
90 this.savedQueryTitle = new OO.ui.LabelWidget( {
91 label: '',
92 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-content-savedQueryTitle' ]
93 } );
94
95 this.resetButton = new OO.ui.ButtonWidget( {
96 framed: false,
97 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-resetButton' ]
98 } );
99
100 this.hideShowButton = new OO.ui.ButtonWidget( {
101 framed: false,
102 flags: [ 'progressive' ],
103 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-hideshowButton' ]
104 } );
105 this.toggleCollapsed( !!config.collapsed );
106
107 if ( !mw.user.isAnon() ) {
108 this.saveQueryButton = new SaveFiltersPopupButtonWidget(
109 this.controller,
110 this.queriesModel,
111 {
112 $overlay: this.$overlay
113 }
114 );
115
116 this.saveQueryButton.$element.on( 'mousedown', function ( e ) {
117 e.stopPropagation();
118 } );
119
120 this.saveQueryButton.connect( this, {
121 click: 'onSaveQueryButtonClick',
122 saveCurrent: 'setSavedQueryVisibility'
123 } );
124 this.queriesModel.connect( this, {
125 itemUpdate: 'onSavedQueriesItemUpdate',
126 initialize: 'onSavedQueriesInitialize',
127 default: 'reevaluateResetRestoreState'
128 } );
129 }
130
131 this.emptyFilterMessage = new OO.ui.LabelWidget( {
132 label: mw.msg( 'rcfilters-empty-filter' ),
133 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-emptyFilters' ]
134 } );
135 this.$content.append( this.emptyFilterMessage.$element );
136
137 // Events
138 this.resetButton.connect( this, { click: 'onResetButtonClick' } );
139 this.hideShowButton.connect( this, { click: 'onHideShowButtonClick' } );
140 // Stop propagation for mousedown, so that the widget doesn't
141 // trigger the focus on the input and scrolls up when we click the reset button
142 this.resetButton.$element.on( 'mousedown', function ( e ) {
143 e.stopPropagation();
144 } );
145 this.hideShowButton.$element.on( 'mousedown', function ( e ) {
146 e.stopPropagation();
147 } );
148 this.model.connect( this, {
149 initialize: 'onModelInitialize',
150 update: 'onModelUpdate',
151 searchChange: 'onModelSearchChange',
152 itemUpdate: 'onModelItemUpdate',
153 highlightChange: 'onModelHighlightChange'
154 } );
155 this.input.connect( this, { change: 'onInputChange' } );
156
157 // The filter list and button should appear side by side regardless of how
158 // wide the button is; the button also changes its width depending
159 // on language and its state, so the safest way to present both side
160 // by side is with a table layout
161 rcFiltersRow = $( '<div>' )
162 .addClass( 'mw-rcfilters-ui-row' )
163 .append(
164 this.$content
165 .addClass( 'mw-rcfilters-ui-cell' )
166 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-filters' )
167 );
168
169 if ( !mw.user.isAnon() ) {
170 rcFiltersRow.append(
171 $( '<div>' )
172 .addClass( 'mw-rcfilters-ui-cell' )
173 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-save' )
174 .append( this.saveQueryButton.$element )
175 );
176 }
177
178 // Add a selector at the right of the input
179 this.viewsSelectWidget = new OO.ui.ButtonSelectWidget( {
180 classes: [ 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select-widget' ],
181 items: [
182 new OO.ui.ButtonOptionWidget( {
183 framed: false,
184 data: 'namespaces',
185 icon: 'article',
186 label: mw.msg( 'namespaces' ),
187 title: mw.msg( 'rcfilters-view-namespaces-tooltip' )
188 } ),
189 new OO.ui.ButtonOptionWidget( {
190 framed: false,
191 data: 'tags',
192 icon: 'tag',
193 label: mw.msg( 'tags-title' ),
194 title: mw.msg( 'rcfilters-view-tags-tooltip' )
195 } )
196 ]
197 } );
198
199 // Rearrange the UI so the select widget is at the right of the input
200 this.$element.append(
201 $( '<div>' )
202 .addClass( 'mw-rcfilters-ui-table' )
203 .append(
204 $( '<div>' )
205 .addClass( 'mw-rcfilters-ui-row' )
206 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views' )
207 .append(
208 $( '<div>' )
209 .addClass( 'mw-rcfilters-ui-cell' )
210 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-input' )
211 .append( this.input.$element ),
212 $( '<div>' )
213 .addClass( 'mw-rcfilters-ui-cell' )
214 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-views-select' )
215 .append( this.viewsSelectWidget.$element )
216 )
217 )
218 );
219
220 // Event
221 this.viewsSelectWidget.connect( this, { choose: 'onViewsSelectWidgetChoose' } );
222
223 rcFiltersRow.append(
224 $( '<div>' )
225 .addClass( 'mw-rcfilters-ui-cell' )
226 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-cell-reset' )
227 .append( this.resetButton.$element )
228 );
229
230 // Build the content
231 $contentWrapper.append(
232 $( '<div>' )
233 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-top' )
234 .append(
235 $( '<div>' )
236 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-top-title' )
237 .append( title.$element ),
238 $( '<div>' )
239 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-top-queryName' )
240 .append( this.savedQueryTitle.$element ),
241 $( '<div>' )
242 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-top-hideshow' )
243 .append(
244 this.hideShowButton.$element
245 )
246 ),
247 $( '<div>' )
248 .addClass( 'mw-rcfilters-ui-table' )
249 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-wrapper-filters' )
250 .append( rcFiltersRow )
251 );
252
253 // Initialize
254 this.$handle.append( $contentWrapper );
255 this.emptyFilterMessage.toggle( this.isEmpty() );
256 this.savedQueryTitle.toggle( false );
257
258 this.$element
259 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget' );
260
261 this.reevaluateResetRestoreState();
262 };
263
264 /* Initialization */
265
266 OO.inheritClass( FilterTagMultiselectWidget, OO.ui.MenuTagMultiselectWidget );
267
268 /* Methods */
269
270 /**
271 * Override parent method to avoid unnecessary resize events.
272 */
273 FilterTagMultiselectWidget.prototype.updateIfHeightChanged = function () { };
274
275 /**
276 * Respond to view select widget choose event
277 *
278 * @param {OO.ui.ButtonOptionWidget} buttonOptionWidget Chosen widget
279 */
280 FilterTagMultiselectWidget.prototype.onViewsSelectWidgetChoose = function ( buttonOptionWidget ) {
281 this.controller.switchView( buttonOptionWidget.getData() );
282 this.viewsSelectWidget.selectItem( null );
283 this.focus();
284 };
285
286 /**
287 * Respond to model search change event
288 *
289 * @param {string} value Search value
290 */
291 FilterTagMultiselectWidget.prototype.onModelSearchChange = function ( value ) {
292 this.input.setValue( value );
293 };
294
295 /**
296 * Respond to input change event
297 *
298 * @param {string} value Value of the input
299 */
300 FilterTagMultiselectWidget.prototype.onInputChange = function ( value ) {
301 this.controller.setSearch( value );
302 };
303
304 /**
305 * Respond to query button click
306 */
307 FilterTagMultiselectWidget.prototype.onSaveQueryButtonClick = function () {
308 this.getMenu().toggle( false );
309 };
310
311 /**
312 * Respond to save query model initialization
313 */
314 FilterTagMultiselectWidget.prototype.onSavedQueriesInitialize = function () {
315 this.setSavedQueryVisibility();
316 };
317
318 /**
319 * Respond to save query item change. Mainly this is done to update the label in case
320 * a query item has been edited
321 *
322 * @param {mw.rcfilters.dm.SavedQueryItemModel} item Saved query item
323 */
324 FilterTagMultiselectWidget.prototype.onSavedQueriesItemUpdate = function ( item ) {
325 if ( this.matchingQuery === item ) {
326 // This means we just edited the item that is currently matched
327 this.savedQueryTitle.setLabel( item.getLabel() );
328 }
329 };
330
331 /**
332 * Respond to menu toggle
333 *
334 * @param {boolean} isVisible Menu is visible
335 */
336 FilterTagMultiselectWidget.prototype.onMenuToggle = function ( isVisible ) {
337 // Parent
338 FilterTagMultiselectWidget.parent.prototype.onMenuToggle.call( this );
339
340 if ( isVisible ) {
341 this.focus();
342
343 mw.hook( 'RcFilters.popup.open' ).fire();
344
345 if ( !this.getMenu().findSelectedItem() ) {
346 // If there are no selected items, scroll menu to top
347 // This has to be in a setTimeout so the menu has time
348 // to be positioned and fixed
349 setTimeout(
350 function () {
351 this.getMenu().scrollToTop();
352 }.bind( this )
353 );
354 }
355 } else {
356 // Clear selection
357 this.selectTag( null );
358
359 // Clear the search
360 this.controller.setSearch( '' );
361
362 // Log filter grouping
363 this.controller.trackFilterGroupings( 'filtermenu' );
364
365 this.blur();
366 }
367
368 this.input.setIcon( isVisible ? 'search' : 'menu' );
369 };
370
371 /**
372 * @inheritdoc
373 */
374 FilterTagMultiselectWidget.prototype.onInputFocus = function () {
375 // Parent
376 FilterTagMultiselectWidget.parent.prototype.onInputFocus.call( this );
377
378 // Only scroll to top of the viewport if:
379 // - The widget is more than 20px from the top
380 // - The widget is not above the top of the viewport (do not scroll downwards)
381 // (This isn't represented because >20 is, anyways and always, bigger than 0)
382 this.scrollToTop( this.$element, 0, { min: 20, max: Infinity } );
383 };
384
385 /**
386 * @inheritdoc
387 */
388 FilterTagMultiselectWidget.prototype.doInputEscape = function () {
389 // Parent
390 FilterTagMultiselectWidget.parent.prototype.doInputEscape.call( this );
391
392 // Blur the input
393 this.input.$input.trigger( 'blur' );
394 };
395
396 /**
397 * @inheritdoc
398 */
399 FilterTagMultiselectWidget.prototype.onMouseDown = function ( e ) {
400 if ( !this.collapsed && !this.isDisabled() && e.which === OO.ui.MouseButtons.LEFT ) {
401 this.menu.toggle();
402
403 return false;
404 }
405 };
406
407 /**
408 * @inheritdoc
409 */
410 FilterTagMultiselectWidget.prototype.onChangeTags = function () {
411 // If initialized, call parent method.
412 if ( this.controller.isInitialized() ) {
413 FilterTagMultiselectWidget.parent.prototype.onChangeTags.call( this );
414 }
415
416 this.emptyFilterMessage.toggle( this.isEmpty() );
417 };
418
419 /**
420 * Respond to model initialize event
421 */
422 FilterTagMultiselectWidget.prototype.onModelInitialize = function () {
423 this.setSavedQueryVisibility();
424 };
425
426 /**
427 * Respond to model update event
428 */
429 FilterTagMultiselectWidget.prototype.onModelUpdate = function () {
430 this.updateElementsForView();
431 };
432
433 /**
434 * Update the elements in the widget to the current view
435 */
436 FilterTagMultiselectWidget.prototype.updateElementsForView = function () {
437 var view = this.model.getCurrentView(),
438 inputValue = this.input.getValue().trim(),
439 inputView = this.model.getViewByTrigger( inputValue.substr( 0, 1 ) );
440
441 if ( inputView !== 'default' ) {
442 // We have a prefix already, remove it
443 inputValue = inputValue.substr( 1 );
444 }
445
446 if ( inputView !== view ) {
447 // Add the correct prefix
448 inputValue = this.model.getViewTrigger( view ) + inputValue;
449 }
450
451 // Update input
452 this.input.setValue( inputValue );
453
454 if ( this.currentView !== view ) {
455 this.scrollToTop( this.$element );
456 this.currentView = view;
457 }
458 };
459
460 /**
461 * Set the visibility of the saved query button
462 */
463 FilterTagMultiselectWidget.prototype.setSavedQueryVisibility = function () {
464 if ( mw.user.isAnon() ) {
465 return;
466 }
467
468 this.matchingQuery = this.controller.findQueryMatchingCurrentState();
469
470 this.savedQueryTitle.setLabel(
471 this.matchingQuery ? this.matchingQuery.getLabel() : ''
472 );
473 this.savedQueryTitle.toggle( !!this.matchingQuery );
474 this.saveQueryButton.setDisabled( !!this.matchingQuery );
475 this.saveQueryButton.setTitle( !this.matchingQuery ?
476 mw.msg( 'rcfilters-savedqueries-add-new-title' ) :
477 mw.msg( 'rcfilters-savedqueries-already-saved' ) );
478
479 if ( this.matchingQuery ) {
480 this.emphasize();
481 }
482 };
483
484 /**
485 * Respond to model itemUpdate event
486 * fixme: when a new state is applied to the model this function is called 60+ times in a row
487 *
488 * @param {mw.rcfilters.dm.FilterItem} item Filter item model
489 */
490 FilterTagMultiselectWidget.prototype.onModelItemUpdate = function ( item ) {
491 if ( !item.getGroupModel().isHidden() ) {
492 if (
493 item.isSelected() ||
494 (
495 this.model.isHighlightEnabled() &&
496 item.getHighlightColor()
497 )
498 ) {
499 this.addTag( item.getName(), item.getLabel() );
500 } else {
501 // Only attempt to remove the tag if we can find an item for it (T198140, T198231)
502 if ( this.findItemFromData( item.getName() ) !== null ) {
503 this.removeTagByData( item.getName() );
504 }
505 }
506 }
507
508 this.setSavedQueryVisibility();
509
510 // Re-evaluate reset state
511 this.reevaluateResetRestoreState();
512 };
513
514 /**
515 * @inheritdoc
516 */
517 FilterTagMultiselectWidget.prototype.isAllowedData = function ( data ) {
518 return (
519 this.model.getItemByName( data ) &&
520 !this.isDuplicateData( data )
521 );
522 };
523
524 /**
525 * @inheritdoc
526 */
527 FilterTagMultiselectWidget.prototype.onMenuChoose = function ( item ) {
528 this.controller.toggleFilterSelect( item.model.getName() );
529
530 // Select the tag if it exists, or reset selection otherwise
531 this.selectTag( this.findItemFromData( item.model.getName() ) );
532
533 this.focus();
534 };
535
536 /**
537 * Respond to highlightChange event
538 *
539 * @param {boolean} isHighlightEnabled Highlight is enabled
540 */
541 FilterTagMultiselectWidget.prototype.onModelHighlightChange = function ( isHighlightEnabled ) {
542 var highlightedItems = this.model.getHighlightedItems();
543
544 if ( isHighlightEnabled ) {
545 // Add capsule widgets
546 highlightedItems.forEach( function ( filterItem ) {
547 this.addTag( filterItem.getName(), filterItem.getLabel() );
548 }.bind( this ) );
549 } else {
550 // Remove capsule widgets if they're not selected
551 highlightedItems.forEach( function ( filterItem ) {
552 if ( !filterItem.isSelected() ) {
553 // Only attempt to remove the tag if we can find an item for it (T198140, T198231)
554 if ( this.findItemFromData( filterItem.getName() ) !== null ) {
555 this.removeTagByData( filterItem.getName() );
556 }
557 }
558 }.bind( this ) );
559 }
560
561 this.setSavedQueryVisibility();
562 };
563
564 /**
565 * @inheritdoc
566 */
567 FilterTagMultiselectWidget.prototype.onTagSelect = function ( tagItem ) {
568 var menuOption = this.menu.getItemFromModel( tagItem.getModel() );
569
570 this.menu.setUserSelecting( true );
571 // Parent method
572 FilterTagMultiselectWidget.parent.prototype.onTagSelect.call( this, tagItem );
573
574 // Switch view
575 this.controller.resetSearchForView( tagItem.getView() );
576
577 this.selectTag( tagItem );
578 this.scrollToTop( menuOption.$element );
579
580 this.menu.setUserSelecting( false );
581 };
582
583 /**
584 * Select a tag by reference. This is what OO.ui.SelectWidget is doing.
585 * If no items are given, reset selection from all.
586 *
587 * @param {mw.rcfilters.ui.FilterTagItemWidget} [item] Tag to select,
588 * omit to deselect all
589 */
590 FilterTagMultiselectWidget.prototype.selectTag = function ( item ) {
591 var i, len, selected;
592
593 for ( i = 0, len = this.items.length; i < len; i++ ) {
594 selected = this.items[ i ] === item;
595 if ( this.items[ i ].isSelected() !== selected ) {
596 this.items[ i ].toggleSelected( selected );
597 }
598 }
599 };
600 /**
601 * @inheritdoc
602 */
603 FilterTagMultiselectWidget.prototype.onTagRemove = function ( tagItem ) {
604 // Parent method
605 FilterTagMultiselectWidget.parent.prototype.onTagRemove.call( this, tagItem );
606
607 this.controller.clearFilter( tagItem.getName() );
608
609 tagItem.destroy();
610 };
611
612 /**
613 * Respond to click event on the reset button
614 */
615 FilterTagMultiselectWidget.prototype.onResetButtonClick = function () {
616 if ( this.model.areVisibleFiltersEmpty() ) {
617 // Reset to default filters
618 this.controller.resetToDefaults();
619 } else {
620 // Reset to have no filters
621 this.controller.emptyFilters();
622 }
623 };
624
625 /**
626 * Respond to hide/show button click
627 */
628 FilterTagMultiselectWidget.prototype.onHideShowButtonClick = function () {
629 this.toggleCollapsed();
630 };
631
632 /**
633 * Toggle the collapsed state of the filters widget
634 *
635 * @param {boolean} isCollapsed Widget is collapsed
636 */
637 FilterTagMultiselectWidget.prototype.toggleCollapsed = function ( isCollapsed ) {
638 isCollapsed = isCollapsed === undefined ? !this.collapsed : !!isCollapsed;
639
640 this.collapsed = isCollapsed;
641
642 if ( isCollapsed ) {
643 // If we are collapsing, close the menu, in case it was open
644 // We should make sure the menu closes before the rest of the elements
645 // are hidden, otherwise there is an unknown error in jQuery as ooui
646 // sets and unsets properties on the input (which is hidden at that point)
647 this.menu.toggle( false );
648 }
649 this.input.setDisabled( isCollapsed );
650 this.hideShowButton.setLabel( mw.msg(
651 isCollapsed ? 'rcfilters-activefilters-show' : 'rcfilters-activefilters-hide'
652 ) );
653 this.hideShowButton.setTitle( mw.msg(
654 isCollapsed ? 'rcfilters-activefilters-show-tooltip' : 'rcfilters-activefilters-hide-tooltip'
655 ) );
656
657 // Toggle the wrapper class, so we have min height values correctly throughout
658 this.$wrapper.toggleClass( 'mw-rcfilters-collapsed', isCollapsed );
659
660 // Save the state
661 this.controller.updateCollapsedState( isCollapsed );
662 };
663
664 /**
665 * Reevaluate the restore state for the widget between setting to defaults and clearing all filters
666 */
667 FilterTagMultiselectWidget.prototype.reevaluateResetRestoreState = function () {
668 var defaultsAreEmpty = this.controller.areDefaultsEmpty(),
669 currFiltersAreEmpty = this.model.areVisibleFiltersEmpty(),
670 hideResetButton = currFiltersAreEmpty && defaultsAreEmpty;
671
672 this.resetButton.setIcon(
673 currFiltersAreEmpty ? 'history' : 'trash'
674 );
675
676 this.resetButton.setLabel(
677 currFiltersAreEmpty ? mw.msg( 'rcfilters-restore-default-filters' ) : ''
678 );
679 this.resetButton.setTitle(
680 currFiltersAreEmpty ? null : mw.msg( 'rcfilters-clear-all-filters' )
681 );
682
683 this.resetButton.toggle( !hideResetButton );
684 this.emptyFilterMessage.toggle( currFiltersAreEmpty );
685 };
686
687 /**
688 * @inheritdoc
689 */
690 FilterTagMultiselectWidget.prototype.createMenuWidget = function ( menuConfig ) {
691 return new MenuSelectWidget(
692 this.controller,
693 this.model,
694 menuConfig
695 );
696 };
697
698 /**
699 * @inheritdoc
700 */
701 FilterTagMultiselectWidget.prototype.createTagItemWidget = function ( data ) {
702 var filterItem = this.model.getItemByName( data );
703
704 if ( filterItem ) {
705 return new FilterTagItemWidget(
706 this.controller,
707 this.model,
708 this.model.getInvertModel(),
709 filterItem,
710 {
711 $overlay: this.$overlay
712 }
713 );
714 }
715 };
716
717 FilterTagMultiselectWidget.prototype.emphasize = function () {
718 if (
719 !this.$handle.hasClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' )
720 ) {
721 this.$handle
722 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' )
723 .addClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
724
725 setTimeout( function () {
726 this.$handle
727 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-emphasize' );
728
729 setTimeout( function () {
730 this.$handle
731 .removeClass( 'mw-rcfilters-ui-filterTagMultiselectWidget-animate' );
732 }.bind( this ), 1000 );
733 }.bind( this ), 500 );
734
735 }
736 };
737 /**
738 * Scroll the element to top within its container
739 *
740 * @private
741 * @param {jQuery} $element Element to position
742 * @param {number} [marginFromTop=0] When scrolling the entire widget to the top, leave this
743 * much space (in pixels) above the widget.
744 * @param {Object} [threshold] Minimum distance from the top of the element to scroll at all
745 * @param {number} [threshold.min] Minimum distance above the element
746 * @param {number} [threshold.max] Minimum distance below the element
747 */
748 FilterTagMultiselectWidget.prototype.scrollToTop = function ( $element, marginFromTop, threshold ) {
749 var container = OO.ui.Element.static.getClosestScrollableContainer( $element[ 0 ], 'y' ),
750 pos = OO.ui.Element.static.getRelativePosition( $element, $( container ) ),
751 containerScrollTop = $( container ).scrollTop(),
752 effectiveScrollTop = $( container ).is( 'body, html' ) ? 0 : containerScrollTop,
753 newScrollTop = effectiveScrollTop + pos.top - ( marginFromTop || 0 );
754
755 // Scroll to item
756 if (
757 threshold === undefined ||
758 (
759 (
760 threshold.min === undefined ||
761 newScrollTop - containerScrollTop >= threshold.min
762 ) &&
763 (
764 threshold.max === undefined ||
765 newScrollTop - containerScrollTop <= threshold.max
766 )
767 )
768 ) {
769 $( container ).animate( {
770 scrollTop: newScrollTop
771 } );
772 }
773 };
774
775 module.exports = FilterTagMultiselectWidget;