3 * Extend OOUI's FilterTagItemWidget to also display a popup on hover.
6 * @extends OO.ui.FilterTagItemWidget
7 * @mixins OO.ui.mixin.PopupElement
10 * @param {mw.rcfilters.Controller} controller
11 * @param {mw.rcfilters.dm.FilterItem} model Item model
12 * @param {Object} config Configuration object
13 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
15 mw
.rcfilters
.ui
.FilterTagItemWidget
= function MwRcfiltersUiFilterTagItemWidget( controller
, model
, config
) {
16 // Configuration initialization
17 config
= config
|| {};
19 this.controller
= controller
;
22 mw
.rcfilters
.ui
.FilterTagItemWidget
.parent
.call( this, $.extend( {
23 data
: this.model
.getName(),
24 label
: this.model
.getLabel()
27 this.$overlay
= config
.$overlay
|| this.$element
;
28 this.popupLabel
= new OO
.ui
.LabelWidget();
31 OO
.ui
.mixin
.PopupElement
.call( this, $.extend( {
36 $content
: $( '<div>' )
37 .addClass( 'mw-rcfilters-ui-filterTagItemWidget-popup-content' )
38 .append( this.popupLabel
.$element
),
39 $floatableContainer
: this.$element
,
40 classes
: [ 'mw-rcfilters-ui-filterTagItemWidget-popup' ]
44 this.positioned
= false;
45 this.popupTimeoutShow
= null;
46 this.popupTimeoutHide
= null;
48 this.$highlight
= $( '<div>' )
49 .addClass( 'mw-rcfilters-ui-filterTagItemWidget-highlight' );
52 this.model
.connect( this, { update
: 'onModelUpdate' } );
55 this.$overlay
.append( this.popup
.$element
);
57 .prepend( this.$highlight
)
58 .attr( 'aria-haspopup', 'true' )
59 .on( 'mouseenter', this.onMouseEnter
.bind( this ) )
60 .on( 'mouseleave', this.onMouseLeave
.bind( this ) );
62 this.setCurrentMuteState();
63 this.setHighlightColor();
68 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagItemWidget
, OO
.ui
.TagItemWidget
);
69 OO
.mixinClass( mw
.rcfilters
.ui
.FilterTagItemWidget
, OO
.ui
.mixin
.PopupElement
);
74 * Respond to model update event
76 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.onModelUpdate = function () {
77 this.setCurrentMuteState();
79 this.setHighlightColor();
82 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.setHighlightColor = function () {
83 var selectedColor
= this.model
.isHighlightEnabled() ? this.model
.getHighlightColor() : null;
86 .attr( 'data-color', selectedColor
)
88 'mw-rcfilters-ui-filterTagItemWidget-highlight-highlighted',
94 * Set the current mute state for this item
96 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.setCurrentMuteState = function () {
99 'mw-rcfilters-ui-filterTagItemWidget-muted',
100 !this.model
.isSelected() ||
101 this.model
.isIncluded() ||
102 this.model
.isFullyCovered()
105 'mw-rcfilters-ui-filterTagItemWidget-conflicted',
106 this.model
.isSelected() && this.model
.isConflicted()
111 * Respond to mouse enter event
113 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.onMouseEnter = function () {
114 var labelText
= this.model
.getStateMessage();
117 this.popupLabel
.setLabel( labelText
);
119 if ( !this.positioned
) {
120 // Recalculate anchor position to be center of the capsule item
121 this.popup
.$anchor
.css( 'margin-left', ( this.$element
.width() / 2 ) );
122 this.positioned
= true;
125 // Set timeout for the popup to show
126 this.popupTimeoutShow
= setTimeout( function () {
127 this.popup
.toggle( true );
128 }.bind( this ), 500 );
130 // Cancel the hide timeout
131 clearTimeout( this.popupTimeoutHide
);
132 this.popupTimeoutHide
= null;
137 * Respond to mouse leave event
139 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.onMouseLeave = function () {
140 this.popupTimeoutHide
= setTimeout( function () {
141 this.popup
.toggle( false );
142 }.bind( this ), 250 );
144 // Clear the show timeout
145 clearTimeout( this.popupTimeoutShow
);
146 this.popupTimeoutShow
= null;
150 * Set selected state on this widget
152 * @param {boolean} [isSelected] Widget is selected
154 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.toggleSelected = function ( isSelected
) {
155 isSelected
= isSelected
!== undefined ? isSelected
: !this.selected
;
157 if ( this.selected
!== isSelected
) {
158 this.selected
= isSelected
;
160 this.$element
.toggleClass( 'mw-rcfilters-ui-filterTagItemWidget-selected', this.selected
);
167 * @return {string} Filter name
169 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.getName = function () {
170 return this.model
.getName();
174 * Remove and destroy external elements of this widget
176 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.destroy = function () {
178 this.popup
.$element
.detach();
181 this.model
.disconnect( this );
182 this.closeButton
.disconnect( this );
184 }( mediaWiki
, jQuery
) );