3 * Extend OOUI's CapsuleItemWidget to also display a popup on hover.
6 * @extends OO.ui.CapsuleItemWidget
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
.CapsuleItemWidget
= function MwRcfiltersUiCapsuleItemWidget( controller
, model
, config
) {
16 // Configuration initialization
17 config
= config
|| {};
19 this.controller
= controller
;
21 this.popupLabel
= new OO
.ui
.LabelWidget();
22 this.$overlay
= config
.$overlay
|| this.$element
;
23 this.positioned
= false;
24 this.popupTimeoutShow
= null;
25 this.popupTimeoutHide
= null;
28 mw
.rcfilters
.ui
.CapsuleItemWidget
.parent
.call( this, $.extend( {
29 data
: this.model
.getName(),
30 label
: this.model
.getLabel()
34 OO
.ui
.mixin
.PopupElement
.call( this, $.extend( {
39 $content
: $( '<div>' )
40 .addClass( 'mw-rcfilters-ui-capsuleItemWidget-popup-content' )
41 .append( this.popupLabel
.$element
),
42 $floatableContainer
: this.$element
,
43 classes
: [ 'mw-rcfilters-ui-capsuleItemWidget-popup' ]
47 this.$highlight
= $( '<div>' )
48 .addClass( 'mw-rcfilters-ui-capsuleItemWidget-highlight' );
51 this.model
.connect( this, { update
: 'onModelUpdate' } );
54 this.$overlay
.append( this.popup
.$element
);
56 .prepend( this.$highlight
)
57 .attr( 'aria-haspopup', 'true' )
58 .addClass( 'mw-rcfilters-ui-capsuleItemWidget' )
59 .on( 'mousedown', this.onMouseDown
.bind( this ) )
60 .on( 'mouseenter', this.onMouseEnter
.bind( this ) )
61 .on( 'mouseleave', this.onMouseLeave
.bind( this ) );
63 this.setCurrentMuteState();
64 this.setHighlightColor();
67 OO
.inheritClass( mw
.rcfilters
.ui
.CapsuleItemWidget
, OO
.ui
.CapsuleItemWidget
);
68 OO
.mixinClass( mw
.rcfilters
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.PopupElement
);
71 * Respond to model update event
73 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onModelUpdate = function () {
74 this.setCurrentMuteState();
76 this.setHighlightColor();
80 * Override mousedown event to prevent its propagation to the parent,
81 * since the parent (the multiselect widget) focuses the popup when its
82 * mousedown event is fired.
84 * @param {jQuery.Event} e Event
86 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onMouseDown = function ( e
) {
91 * Emit a click event when the capsule is clicked so we can aggregate this
92 * in the parent (the capsule)
94 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onClick = function () {
99 * Override the event listening to the item close button click
101 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onCloseClick = function () {
102 var element
= this.getElementGroup();
104 if ( element
&& $.isFunction( element
.removeItems
) ) {
105 element
.removeItems( [ this ] );
108 // Respond to user removing the filter
109 this.controller
.clearFilter( this.model
.getName() );
112 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.setHighlightColor = function () {
113 var selectedColor
= this.model
.isHighlightEnabled() ? this.model
.getHighlightColor() : null;
116 .attr( 'data-color', selectedColor
)
118 'mw-rcfilters-ui-capsuleItemWidget-highlight-highlighted',
124 * Set the current mute state for this item
126 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.setCurrentMuteState = function () {
129 'mw-rcfilters-ui-capsuleItemWidget-muted',
130 !this.model
.isSelected() ||
131 this.model
.isIncluded() ||
132 this.model
.isFullyCovered()
135 'mw-rcfilters-ui-capsuleItemWidget-conflicted',
136 this.model
.isSelected() && this.model
.isConflicted()
141 * Respond to mouse enter event
143 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onMouseEnter = function () {
144 var labelText
= this.model
.getStateMessage();
147 this.popupLabel
.setLabel( labelText
);
149 if ( !this.positioned
) {
150 // Recalculate anchor position to be center of the capsule item
151 this.popup
.$anchor
.css( 'margin-left', ( this.$element
.width() / 2 ) );
152 this.positioned
= true;
155 // Set timeout for the popup to show
156 this.popupTimeoutShow
= setTimeout( function () {
157 this.popup
.toggle( true );
158 }.bind( this ), 500 );
160 // Cancel the hide timeout
161 clearTimeout( this.popupTimeoutHide
);
162 this.popupTimeoutHide
= null;
167 * Respond to mouse leave event
169 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onMouseLeave = function () {
170 this.popupTimeoutHide
= setTimeout( function () {
171 this.popup
.toggle( false );
172 }.bind( this ), 250 );
174 // Clear the show timeout
175 clearTimeout( this.popupTimeoutShow
);
176 this.popupTimeoutShow
= null;
180 * Set selected state on this widget
182 * @param {boolean} [isSelected] Widget is selected
184 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.toggleSelected = function ( isSelected
) {
185 isSelected
= isSelected
!== undefined ? isSelected
: !this.selected
;
187 if ( this.selected
!== isSelected
) {
188 this.selected
= isSelected
;
190 this.$element
.toggleClass( 'mw-rcfilters-ui-capsuleItemWidget-selected', this.selected
);
195 * Remove and destroy external elements of this widget
197 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.destroy = function () {
199 this.popup
.$element
.detach();
202 this.model
.disconnect( this );
203 this.closeButton
.disconnect( this );
205 }( mediaWiki
, jQuery
) );