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' ),
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;
29 mw
.rcfilters
.ui
.CapsuleItemWidget
.parent
.call( this, $.extend( {
30 data
: this.model
.getName(),
31 label
: this.model
.getLabel()
35 OO
.ui
.mixin
.PopupElement
.call( this, $.extend( {
39 $content
: $popupContent
40 .append( descLabelWidget
.$element
),
41 $floatableContainer
: this.$element
45 // Set initial text for the popup - the description
46 descLabelWidget
.setLabel( this.model
.getDescription() );
49 this.model
.connect( this, { update
: 'onModelUpdate' } );
51 this.closeButton
.connect( this, { click
: 'onCapsuleRemovedByUser' } );
54 this.$overlay
.append( this.popup
.$element
);
56 .attr( 'aria-haspopup', 'true' )
57 .addClass( 'mw-rcfilters-ui-capsuleItemWidget' )
58 .on( 'mouseover', this.onHover
.bind( this, true ) )
59 .on( 'mouseout', this.onHover
.bind( this, false ) );
61 this.setCurrentMuteState();
64 OO
.inheritClass( mw
.rcfilters
.ui
.CapsuleItemWidget
, OO
.ui
.CapsuleItemWidget
);
65 OO
.mixinClass( mw
.rcfilters
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.PopupElement
);
68 * Respond to model update event
70 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onModelUpdate = function () {
71 this.setCurrentMuteState();
75 * Set the current mute state for this item
77 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.setCurrentMuteState = function () {
80 'mw-rcfilters-ui-capsuleItemWidget-muted',
81 this.model
.isIncluded() ||
82 this.model
.isConflicted() ||
83 this.model
.isFullyCovered()
88 * Respond to hover event on the capsule item.
90 * @param {boolean} isHovering Mouse is hovering on the item
92 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onHover = function ( isHovering
) {
93 if ( this.model
.getDescription() ) {
94 this.popup
.toggle( isHovering
);
96 if ( isHovering
&& !this.positioned
) {
97 // Recalculate position to be center of the capsule item
98 this.popup
.$element
.css( 'margin-left', ( this.$element
.width() / 2 ) );
99 this.positioned
= true;
105 * Respond to the user removing the capsule with the close button
107 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onCapsuleRemovedByUser = function () {
108 this.controller
.updateFilter( this.model
.getName(), false );
112 * Remove and destroy external elements of this widget
114 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.destroy = function () {
116 this.popup
.$element
.detach();
119 this.model
.disconnect( this );
120 this.closeButton
.disconnect( this );
122 }( mediaWiki
, jQuery
) );