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;
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
,
42 classes
: [ 'mw-rcfilters-ui-capsuleItemWidget-popup' ]
46 // Set initial text for the popup - the description
47 descLabelWidget
.setLabel( this.model
.getDescription() );
49 this.$highlight
= $( '<div>' )
50 .addClass( 'mw-rcfilters-ui-capsuleItemWidget-highlight' );
53 this.model
.connect( this, { update
: 'onModelUpdate' } );
55 this.closeButton
.$element
.on( 'mousedown', this.onCloseButtonMouseDown
.bind( this ) );
58 this.$overlay
.append( this.popup
.$element
);
60 .prepend( this.$highlight
)
61 .attr( 'aria-haspopup', 'true' )
62 .addClass( 'mw-rcfilters-ui-capsuleItemWidget' )
63 .on( 'mouseover', this.onHover
.bind( this, true ) )
64 .on( 'mouseout', this.onHover
.bind( this, false ) );
66 this.setCurrentMuteState();
67 this.setHighlightColor();
70 OO
.inheritClass( mw
.rcfilters
.ui
.CapsuleItemWidget
, OO
.ui
.CapsuleItemWidget
);
71 OO
.mixinClass( mw
.rcfilters
.ui
.CapsuleItemWidget
, OO
.ui
.mixin
.PopupElement
);
74 * Respond to model update event
76 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onModelUpdate = function () {
77 this.setCurrentMuteState();
79 this.setHighlightColor();
83 * Override mousedown event to prevent its propagation to the parent,
84 * since the parent (the multiselect widget) focuses the popup when its
85 * mousedown event is fired.
87 * @param {jQuery.Event} e Event
89 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onCloseButtonMouseDown = function ( e
) {
94 * Emit a click event when the capsule is clicked so we can aggregate this
95 * in the parent (the capsule)
97 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onClick = function () {
102 * Override the event listening to the item close button click
104 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onCloseClick = function () {
105 var element
= this.getElementGroup();
107 if ( element
&& $.isFunction( element
.removeItems
) ) {
108 element
.removeItems( [ this ] );
111 // Respond to user removing the filter
112 this.controller
.updateFilter( this.model
.getName(), false );
113 this.controller
.clearHighlightColor( this.model
.getName() );
116 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.setHighlightColor = function () {
117 var selectedColor
= this.model
.isHighlightEnabled() ? this.model
.getHighlightColor() : null;
120 .attr( 'data-color', selectedColor
)
122 'mw-rcfilters-ui-capsuleItemWidget-highlight-highlighted',
128 * Set the current mute state for this item
130 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.setCurrentMuteState = function () {
133 'mw-rcfilters-ui-capsuleItemWidget-muted',
134 !this.model
.isSelected() ||
135 this.model
.isIncluded() ||
136 this.model
.isConflicted() ||
137 this.model
.isFullyCovered()
142 * Respond to hover event on the capsule item.
144 * @param {boolean} isHovering Mouse is hovering on the item
146 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onHover = function ( isHovering
) {
147 if ( this.model
.getDescription() ) {
148 this.popup
.toggle( isHovering
);
150 if ( isHovering
&& !this.positioned
) {
151 // Recalculate position to be center of the capsule item
152 this.popup
.$element
.css( 'margin-left', ( this.$element
.width() / 2 ) );
153 this.positioned
= true;
159 * Remove and destroy external elements of this widget
161 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.destroy = function () {
163 this.popup
.$element
.detach();
166 this.model
.disconnect( this );
167 this.closeButton
.disconnect( this );
169 }( mediaWiki
, jQuery
) );