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 var $popupContent
= $( '<div>' )
17 .addClass( 'mw-rcfilters-ui-capsuleItemWidget-popup-content' ),
18 descLabelWidget
= new OO
.ui
.LabelWidget();
20 // Configuration initialization
21 config
= config
|| {};
23 this.controller
= controller
;
25 this.$overlay
= config
.$overlay
|| this.$element
;
26 this.positioned
= false;
27 this.popupTimeoutShow
= null;
28 this.popupTimeoutHide
= null;
31 mw
.rcfilters
.ui
.CapsuleItemWidget
.parent
.call( this, $.extend( {
32 data
: this.model
.getName(),
33 label
: this.model
.getLabel()
37 OO
.ui
.mixin
.PopupElement
.call( this, $.extend( {
42 $content
: $popupContent
43 .append( descLabelWidget
.$element
),
44 $floatableContainer
: this.$element
,
45 classes
: [ 'mw-rcfilters-ui-capsuleItemWidget-popup' ]
49 // Set initial text for the popup - the description
50 descLabelWidget
.setLabel( this.model
.getDescription() );
52 this.$highlight
= $( '<div>' )
53 .addClass( 'mw-rcfilters-ui-capsuleItemWidget-highlight' );
56 this.model
.connect( this, { update
: 'onModelUpdate' } );
58 this.closeButton
.$element
.on( 'mousedown', this.onCloseButtonMouseDown
.bind( this ) );
61 this.$overlay
.append( this.popup
.$element
);
63 .prepend( this.$highlight
)
64 .attr( 'aria-haspopup', 'true' )
65 .addClass( 'mw-rcfilters-ui-capsuleItemWidget' )
66 .on( 'mouseenter', this.onMouseEnter
.bind( this ) )
67 .on( 'mouseleave', this.onMouseLeave
.bind( this ) );
69 this.setCurrentMuteState();
70 this.setHighlightColor();
73 OO
.inheritClass( mw
.rcfilters
.ui
.CapsuleItemWidget
, OO
.ui
.CapsuleItemWidget
);
74 OO
.mixinClass( mw
.rcfilters
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.PopupElement
);
77 * Respond to model update event
79 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onModelUpdate = function () {
80 this.setCurrentMuteState();
82 this.setHighlightColor();
86 * Override mousedown event to prevent its propagation to the parent,
87 * since the parent (the multiselect widget) focuses the popup when its
88 * mousedown event is fired.
90 * @param {jQuery.Event} e Event
92 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onCloseButtonMouseDown = function ( e
) {
97 * Emit a click event when the capsule is clicked so we can aggregate this
98 * in the parent (the capsule)
100 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onClick = function () {
101 this.emit( 'click' );
105 * Override the event listening to the item close button click
107 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onCloseClick = function () {
108 var element
= this.getElementGroup();
110 if ( element
&& $.isFunction( element
.removeItems
) ) {
111 element
.removeItems( [ this ] );
114 // Respond to user removing the filter
115 this.controller
.clearFilter( this.model
.getName() );
118 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.setHighlightColor = function () {
119 var selectedColor
= this.model
.isHighlightEnabled() ? this.model
.getHighlightColor() : null;
122 .attr( 'data-color', selectedColor
)
124 'mw-rcfilters-ui-capsuleItemWidget-highlight-highlighted',
130 * Set the current mute state for this item
132 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.setCurrentMuteState = function () {
135 'mw-rcfilters-ui-capsuleItemWidget-muted',
136 !this.model
.isSelected() ||
137 this.model
.isIncluded() ||
138 this.model
.isFullyCovered()
141 'mw-rcfilters-ui-capsuleItemWidget-conflicted',
142 this.model
.isConflicted()
147 * Respond to mouse enter event
149 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onMouseEnter = function () {
150 if ( this.model
.getDescription() ) {
151 if ( !this.positioned
) {
152 // Recalculate anchor position to be center of the capsule item
153 this.popup
.$anchor
.css( 'margin-left', ( this.$element
.width() / 2 ) );
154 this.positioned
= true;
157 // Set timeout for the popup to show
158 this.popupTimeoutShow
= setTimeout( function () {
159 this.popup
.toggle( true );
160 }.bind( this ), 500 );
162 // Cancel the hide timeout
163 clearTimeout( this.popupTimeoutHide
);
164 this.popupTimeoutHide
= null;
169 * Respond to mouse leave event
171 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onMouseLeave = function () {
172 this.popupTimeoutHide
= setTimeout( function () {
173 this.popup
.toggle( false );
174 }.bind( this ), 250 );
176 // Clear the show timeout
177 clearTimeout( this.popupTimeoutShow
);
178 this.popupTimeoutShow
= null;
182 * Set selected state on this widget
184 * @param {boolean} [isSelected] Widget is selected
186 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.toggleSelected = function ( isSelected
) {
187 isSelected
= isSelected
!== undefined ? isSelected
: !this.selected
;
189 if ( this.selected
!== isSelected
) {
190 this.selected
= isSelected
;
192 this.$element
.toggleClass( 'mw-rcfilters-ui-capsuleItemWidget-selected', this.selected
);
197 * Remove and destroy external elements of this widget
199 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.destroy = function () {
201 this.popup
.$element
.detach();
204 this.model
.disconnect( this );
205 this.closeButton
.disconnect( this );
207 }( mediaWiki
, jQuery
) );