3 * Extend OOUI's TagItemWidget to also display a popup on hover.
6 * @extends OO.ui.TagItemWidget
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
.TagItemWidget
= function MwRcfiltersUiTagItemWidget( controller
, model
, config
) {
16 // Configuration initialization
17 config
= config
|| {};
19 this.controller
= controller
;
21 this.selected
= false;
23 mw
.rcfilters
.ui
.TagItemWidget
.parent
.call( this, $.extend( {
24 data
: this.model
.getName(),
25 label
: $( '<div>' ).html( this.model
.getPrefixedLabel() ).contents()
28 this.$overlay
= config
.$overlay
|| this.$element
;
29 this.popupLabel
= new OO
.ui
.LabelWidget();
32 OO
.ui
.mixin
.PopupElement
.call( this, $.extend( {
37 $content
: $( '<div>' )
38 .addClass( 'mw-rcfilters-ui-tagItemWidget-popup-content' )
39 .append( this.popupLabel
.$element
),
40 $floatableContainer
: this.$element
,
41 classes
: [ 'mw-rcfilters-ui-tagItemWidget-popup' ]
45 this.popupTimeoutShow
= null;
46 this.popupTimeoutHide
= null;
48 this.$highlight
= $( '<div>' )
49 .addClass( 'mw-rcfilters-ui-tagItemWidget-highlight' );
52 this.model
.connect( this, { update
: 'onModelUpdate' } );
55 this.$overlay
.append( this.popup
.$element
);
57 .addClass( 'mw-rcfilters-ui-tagItemWidget' )
58 .prepend( this.$highlight
)
59 .attr( 'aria-haspopup', 'true' )
60 .on( 'mouseenter', this.onMouseEnter
.bind( this ) )
61 .on( 'mouseleave', this.onMouseLeave
.bind( this ) );
63 this.setCurrentMuteState();
64 this.setHighlightColor();
69 OO
.inheritClass( mw
.rcfilters
.ui
.TagItemWidget
, OO
.ui
.TagItemWidget
);
70 OO
.mixinClass( mw
.rcfilters
.ui
.TagItemWidget
, OO
.ui
.mixin
.PopupElement
);
75 * Respond to model update event
77 mw
.rcfilters
.ui
.TagItemWidget
.prototype.onModelUpdate = function () {
78 this.setCurrentMuteState();
80 // Update label if needed
81 this.setLabel( $( '<div>' ).html( this.model
.getPrefixedLabel() ).contents() );
83 this.setHighlightColor();
86 mw
.rcfilters
.ui
.TagItemWidget
.prototype.setHighlightColor = function () {
87 var selectedColor
= this.model
.isHighlightEnabled() ? this.model
.getHighlightColor() : null;
90 .attr( 'data-color', selectedColor
)
92 'mw-rcfilters-ui-tagItemWidget-highlight-highlighted',
98 * Set the current mute state for this item
100 mw
.rcfilters
.ui
.TagItemWidget
.prototype.setCurrentMuteState = function () {};
103 * Respond to mouse enter event
105 mw
.rcfilters
.ui
.TagItemWidget
.prototype.onMouseEnter = function () {
106 var labelText
= this.model
.getStateMessage();
109 this.popupLabel
.setLabel( labelText
);
111 // Set timeout for the popup to show
112 this.popupTimeoutShow
= setTimeout( function () {
113 this.popup
.toggle( true );
114 }.bind( this ), 500 );
116 // Cancel the hide timeout
117 clearTimeout( this.popupTimeoutHide
);
118 this.popupTimeoutHide
= null;
123 * Respond to mouse leave event
125 mw
.rcfilters
.ui
.TagItemWidget
.prototype.onMouseLeave = function () {
126 this.popupTimeoutHide
= setTimeout( function () {
127 this.popup
.toggle( false );
128 }.bind( this ), 250 );
130 // Clear the show timeout
131 clearTimeout( this.popupTimeoutShow
);
132 this.popupTimeoutShow
= null;
136 * Set selected state on this widget
138 * @param {boolean} [isSelected] Widget is selected
140 mw
.rcfilters
.ui
.TagItemWidget
.prototype.toggleSelected = function ( isSelected
) {
141 isSelected
= isSelected
!== undefined ? isSelected
: !this.selected
;
143 if ( this.selected
!== isSelected
) {
144 this.selected
= isSelected
;
146 this.$element
.toggleClass( 'mw-rcfilters-ui-tagItemWidget-selected', this.selected
);
151 * Get the selected state of this widget
153 * @return {boolean} Tag is selected
155 mw
.rcfilters
.ui
.TagItemWidget
.prototype.isSelected = function () {
156 return this.selected
;
162 * @return {string} Filter name
164 mw
.rcfilters
.ui
.TagItemWidget
.prototype.getName = function () {
165 return this.model
.getName();
171 * @return {string} Filter model
173 mw
.rcfilters
.ui
.TagItemWidget
.prototype.getModel = function () {
180 * @return {string} Filter view
182 mw
.rcfilters
.ui
.TagItemWidget
.prototype.getView = function () {
183 return this.model
.getGroupModel().getView();
187 * Remove and destroy external elements of this widget
189 mw
.rcfilters
.ui
.TagItemWidget
.prototype.destroy = function () {
191 this.popup
.$element
.detach();
194 this.model
.disconnect( this );
195 this.closeButton
.disconnect( this );
197 }( mediaWiki
, jQuery
) );