3 * Extend OOUI's TagItemWidget to also display a popup on hover.
6 * @extends OO.ui.TagItemWidget
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
.TagItemWidget
= function MwRcfiltersUiTagItemWidget( controller
, model
, config
) {
16 // Configuration initialization
17 config
= config
|| {};
19 this.controller
= controller
;
21 this.selected
= false;
23 mw
.rcfilters
.ui
.TagItemWidget
.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-tagItemWidget-popup-content' )
39 .append( this.popupLabel
.$element
),
40 $floatableContainer
: this.$element
,
41 classes
: [ 'mw-rcfilters-ui-tagItemWidget-popup' ]
45 this.positioned
= false;
46 this.popupTimeoutShow
= null;
47 this.popupTimeoutHide
= null;
49 this.$highlight
= $( '<div>' )
50 .addClass( 'mw-rcfilters-ui-tagItemWidget-highlight' );
53 this.model
.connect( this, { update
: 'onModelUpdate' } );
56 this.$overlay
.append( this.popup
.$element
);
58 .addClass( 'mw-rcfilters-ui-tagItemWidget' )
59 .prepend( this.$highlight
)
60 .attr( 'aria-haspopup', 'true' )
61 .on( 'mouseenter', this.onMouseEnter
.bind( this ) )
62 .on( 'mouseleave', this.onMouseLeave
.bind( this ) );
64 this.setCurrentMuteState();
65 this.setHighlightColor();
70 OO
.inheritClass( mw
.rcfilters
.ui
.TagItemWidget
, OO
.ui
.TagItemWidget
);
71 OO
.mixinClass( mw
.rcfilters
.ui
.TagItemWidget
, OO
.ui
.mixin
.PopupElement
);
76 * Respond to model update event
78 mw
.rcfilters
.ui
.TagItemWidget
.prototype.onModelUpdate = function () {
79 this.setCurrentMuteState();
81 this.setHighlightColor();
84 mw
.rcfilters
.ui
.TagItemWidget
.prototype.setHighlightColor = function () {
85 var selectedColor
= this.model
.isHighlightEnabled() ? this.model
.getHighlightColor() : null;
88 .attr( 'data-color', selectedColor
)
90 'mw-rcfilters-ui-tagItemWidget-highlight-highlighted',
96 * Set the current mute state for this item
98 mw
.rcfilters
.ui
.TagItemWidget
.prototype.setCurrentMuteState = function () {};
101 * Respond to mouse enter event
103 mw
.rcfilters
.ui
.TagItemWidget
.prototype.onMouseEnter = function () {
104 var labelText
= this.model
.getStateMessage();
107 this.popupLabel
.setLabel( labelText
);
109 if ( !this.positioned
) {
110 // Recalculate anchor position to be center of the capsule item
111 this.popup
.$anchor
.css( 'margin-left', ( this.$element
.width() / 2 ) );
112 this.positioned
= true;
115 // Set timeout for the popup to show
116 this.popupTimeoutShow
= setTimeout( function () {
117 this.popup
.toggle( true );
118 }.bind( this ), 500 );
120 // Cancel the hide timeout
121 clearTimeout( this.popupTimeoutHide
);
122 this.popupTimeoutHide
= null;
127 * Respond to mouse leave event
129 mw
.rcfilters
.ui
.TagItemWidget
.prototype.onMouseLeave = function () {
130 this.popupTimeoutHide
= setTimeout( function () {
131 this.popup
.toggle( false );
132 }.bind( this ), 250 );
134 // Clear the show timeout
135 clearTimeout( this.popupTimeoutShow
);
136 this.popupTimeoutShow
= null;
140 * Set selected state on this widget
142 * @param {boolean} [isSelected] Widget is selected
144 mw
.rcfilters
.ui
.TagItemWidget
.prototype.toggleSelected = function ( isSelected
) {
145 isSelected
= isSelected
!== undefined ? isSelected
: !this.selected
;
147 if ( this.selected
!== isSelected
) {
148 this.selected
= isSelected
;
150 this.$element
.toggleClass( 'mw-rcfilters-ui-tagItemWidget-selected', this.selected
);
155 * Get the selected state of this widget
157 * @return {boolean} Tag is selected
159 mw
.rcfilters
.ui
.TagItemWidget
.prototype.isSelected = function () {
160 return this.selected
;
166 * @return {string} Filter name
168 mw
.rcfilters
.ui
.TagItemWidget
.prototype.getName = function () {
169 return this.model
.getName();
173 * Remove and destroy external elements of this widget
175 mw
.rcfilters
.ui
.TagItemWidget
.prototype.destroy = function () {
177 this.popup
.$element
.detach();
180 this.model
.disconnect( this );
181 this.closeButton
.disconnect( this );
183 }( mediaWiki
, jQuery
) );