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 * Override the event listening to the item close button click
96 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onCloseClick = function () {
97 var element
= this.getElementGroup();
99 if ( element
&& $.isFunction( element
.removeItems
) ) {
100 element
.removeItems( [ this ] );
103 // Respond to user removing the filter
104 this.controller
.updateFilter( this.model
.getName(), false );
105 this.controller
.clearHighlightColor( this.model
.getName() );
108 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.setHighlightColor = function () {
109 var selectedColor
= this.model
.isHighlightEnabled() ? this.model
.getHighlightColor() : null;
112 .attr( 'data-color', selectedColor
)
114 'mw-rcfilters-ui-capsuleItemWidget-highlight-highlighted',
120 * Set the current mute state for this item
122 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.setCurrentMuteState = function () {
125 'mw-rcfilters-ui-capsuleItemWidget-muted',
126 !this.model
.isSelected() ||
127 this.model
.isIncluded() ||
128 this.model
.isConflicted() ||
129 this.model
.isFullyCovered()
134 * Respond to hover event on the capsule item.
136 * @param {boolean} isHovering Mouse is hovering on the item
138 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.onHover = function ( isHovering
) {
139 if ( this.model
.getDescription() ) {
140 this.popup
.toggle( isHovering
);
142 if ( isHovering
&& !this.positioned
) {
143 // Recalculate position to be center of the capsule item
144 this.popup
.$element
.css( 'margin-left', ( this.$element
.width() / 2 ) );
145 this.positioned
= true;
151 * Remove and destroy external elements of this widget
153 mw
.rcfilters
.ui
.CapsuleItemWidget
.prototype.destroy = function () {
155 this.popup
.$element
.detach();
158 this.model
.disconnect( this );
159 this.closeButton
.disconnect( this );
161 }( mediaWiki
, jQuery
) );