f79e013228050537db07d32f00e4164a2f549f5f
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
;
21 this.selected
= false;
23 mw
.rcfilters
.ui
.FilterTagItemWidget
.parent
.call( this, $.extend( {
24 data
: this.model
.getName(),
25 label
: this.model
.getLabel()
28 this.$overlay
= config
.$overlay
|| this.$element
;
29 this.popupLabel
= new OO
.ui
.LabelWidget();
32 OO
.ui
.mixin
.PopupElement
.call( this, $.extend( {
37 $content
: $( '<div>' )
38 .addClass( 'mw-rcfilters-ui-filterTagItemWidget-popup-content' )
39 .append( this.popupLabel
.$element
),
40 $floatableContainer
: this.$element
,
41 classes
: [ 'mw-rcfilters-ui-filterTagItemWidget-popup' ]
45 this.positioned
= false;
46 this.popupTimeoutShow
= null;
47 this.popupTimeoutHide
= null;
49 this.$highlight
= $( '<div>' )
50 .addClass( 'mw-rcfilters-ui-filterTagItemWidget-highlight' );
53 this.model
.connect( this, { update
: 'onModelUpdate' } );
56 this.$overlay
.append( this.popup
.$element
);
58 .prepend( this.$highlight
)
59 .attr( 'aria-haspopup', 'true' )
60 .on( 'mouseenter', this.onMouseEnter
.bind( this ) )
61 .on( 'mouseleave', this.onMouseLeave
.bind( this ) );
63 this.setCurrentMuteState();
64 this.setHighlightColor();
69 OO
.inheritClass( mw
.rcfilters
.ui
.FilterTagItemWidget
, OO
.ui
.TagItemWidget
);
70 OO
.mixinClass( mw
.rcfilters
.ui
.FilterTagItemWidget
, OO
.ui
.mixin
.PopupElement
);
75 * Respond to model update event
77 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.onModelUpdate = function () {
78 this.setCurrentMuteState();
80 this.setHighlightColor();
83 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.setHighlightColor = function () {
84 var selectedColor
= this.model
.isHighlightEnabled() ? this.model
.getHighlightColor() : null;
87 .attr( 'data-color', selectedColor
)
89 'mw-rcfilters-ui-filterTagItemWidget-highlight-highlighted',
95 * Set the current mute state for this item
97 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.setCurrentMuteState = function () {
100 'mw-rcfilters-ui-filterTagItemWidget-muted',
101 !this.model
.isSelected() ||
102 this.model
.isIncluded() ||
103 this.model
.isFullyCovered()
106 'mw-rcfilters-ui-filterTagItemWidget-conflicted',
107 this.model
.isSelected() && this.model
.isConflicted()
112 * Respond to mouse enter event
114 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.onMouseEnter = function () {
115 var labelText
= this.model
.getStateMessage();
118 this.popupLabel
.setLabel( labelText
);
120 if ( !this.positioned
) {
121 // Recalculate anchor position to be center of the capsule item
122 this.popup
.$anchor
.css( 'margin-left', ( this.$element
.width() / 2 ) );
123 this.positioned
= true;
126 // Set timeout for the popup to show
127 this.popupTimeoutShow
= setTimeout( function () {
128 this.popup
.toggle( true );
129 }.bind( this ), 500 );
131 // Cancel the hide timeout
132 clearTimeout( this.popupTimeoutHide
);
133 this.popupTimeoutHide
= null;
138 * Respond to mouse leave event
140 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.onMouseLeave = function () {
141 this.popupTimeoutHide
= setTimeout( function () {
142 this.popup
.toggle( false );
143 }.bind( this ), 250 );
145 // Clear the show timeout
146 clearTimeout( this.popupTimeoutShow
);
147 this.popupTimeoutShow
= null;
151 * Set selected state on this widget
153 * @param {boolean} [isSelected] Widget is selected
155 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.toggleSelected = function ( isSelected
) {
156 isSelected
= isSelected
!== undefined ? isSelected
: !this.selected
;
158 if ( this.selected
!== isSelected
) {
159 this.selected
= isSelected
;
161 this.$element
.toggleClass( 'mw-rcfilters-ui-filterTagItemWidget-selected', this.selected
);
166 * Get the selected state of this widget
168 * @return {boolean} Tag is selected
170 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.isSelected = function () {
171 return this.selected
;
177 * @return {string} Filter name
179 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.getName = function () {
180 return this.model
.getName();
184 * Remove and destroy external elements of this widget
186 mw
.rcfilters
.ui
.FilterTagItemWidget
.prototype.destroy = function () {
188 this.popup
.$element
.detach();
191 this.model
.disconnect( this );
192 this.closeButton
.disconnect( this );
194 }( mediaWiki
, jQuery
) );