13d6b68f3c20498f8ddbfdfe071bffbc0ba97895
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.FiltersViewModel} filtersViewModel
12 * @param {mw.rcfilters.dm.FilterItem} invertModel
13 * @param {mw.rcfilters.dm.FilterItem} itemModel Item model
14 * @param {Object} config Configuration object
15 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
17 mw
.rcfilters
.ui
.TagItemWidget
= function MwRcfiltersUiTagItemWidget(
18 controller
, filtersViewModel
, invertModel
, itemModel
, config
20 // Configuration initialization
21 config
= config
|| {};
23 this.controller
= controller
;
24 this.invertModel
= invertModel
;
25 this.filtersViewModel
= filtersViewModel
;
26 this.itemModel
= itemModel
;
27 this.selected
= false;
29 mw
.rcfilters
.ui
.TagItemWidget
.parent
.call( this, $.extend( {
30 data
: this.itemModel
.getName()
33 this.$overlay
= config
.$overlay
|| this.$element
;
34 this.popupLabel
= new OO
.ui
.LabelWidget();
37 OO
.ui
.mixin
.PopupElement
.call( this, $.extend( {
42 $content
: $( '<div>' )
43 .addClass( 'mw-rcfilters-ui-tagItemWidget-popup-content' )
44 .append( this.popupLabel
.$element
),
45 $floatableContainer
: this.$element
,
46 classes
: [ 'mw-rcfilters-ui-tagItemWidget-popup' ]
50 this.popupTimeoutShow
= null;
51 this.popupTimeoutHide
= null;
53 this.$highlight
= $( '<div>' )
54 .addClass( 'mw-rcfilters-ui-tagItemWidget-highlight' );
56 // Add title attribute with the item label to 'x' button
57 this.closeButton
.setTitle( mw
.msg( 'rcfilters-tag-remove', this.itemModel
.getLabel() ) );
60 this.filtersViewModel
.connect( this, { highlightChange
: 'updateUiBasedOnState' } );
61 this.invertModel
.connect( this, { update
: 'updateUiBasedOnState' } );
62 this.itemModel
.connect( this, { update
: 'updateUiBasedOnState' } );
65 this.$overlay
.append( this.popup
.$element
);
67 .addClass( 'mw-rcfilters-ui-tagItemWidget' )
68 .prepend( this.$highlight
)
69 .attr( 'aria-haspopup', 'true' )
70 .on( 'mouseenter', this.onMouseEnter
.bind( this ) )
71 .on( 'mouseleave', this.onMouseLeave
.bind( this ) );
73 this.updateUiBasedOnState();
78 OO
.inheritClass( mw
.rcfilters
.ui
.TagItemWidget
, OO
.ui
.TagItemWidget
);
79 OO
.mixinClass( mw
.rcfilters
.ui
.TagItemWidget
, OO
.ui
.mixin
.PopupElement
);
84 * Respond to model update event
86 mw
.rcfilters
.ui
.TagItemWidget
.prototype.updateUiBasedOnState = function () {
87 // Update label if needed
88 var labelMsg
= this.itemModel
.getLabelMessageKey( this.invertModel
.isSelected() );
90 this.setLabel( $( '<div>' ).html(
91 mw
.message( labelMsg
, mw
.html
.escape( this.itemModel
.getLabel() ) ).parse()
94 this.setLabel( this.itemModel
.getLabel() );
97 this.setCurrentMuteState();
98 this.setHighlightColor();
102 * Set the current highlight color for this item
104 mw
.rcfilters
.ui
.TagItemWidget
.prototype.setHighlightColor = function () {
105 var selectedColor
= this.filtersViewModel
.isHighlightEnabled() && this.itemModel
.isHighlighted
?
106 this.itemModel
.getHighlightColor() :
110 .attr( 'data-color', selectedColor
)
112 'mw-rcfilters-ui-tagItemWidget-highlight-highlighted',
118 * Set the current mute state for this item
120 mw
.rcfilters
.ui
.TagItemWidget
.prototype.setCurrentMuteState = function () {};
123 * Respond to mouse enter event
125 mw
.rcfilters
.ui
.TagItemWidget
.prototype.onMouseEnter = function () {
126 var labelText
= this.itemModel
.getStateMessage();
129 this.popupLabel
.setLabel( labelText
);
131 // Set timeout for the popup to show
132 this.popupTimeoutShow
= setTimeout( function () {
133 this.popup
.toggle( true );
134 }.bind( this ), 500 );
136 // Cancel the hide timeout
137 clearTimeout( this.popupTimeoutHide
);
138 this.popupTimeoutHide
= null;
143 * Respond to mouse leave event
145 mw
.rcfilters
.ui
.TagItemWidget
.prototype.onMouseLeave = function () {
146 this.popupTimeoutHide
= setTimeout( function () {
147 this.popup
.toggle( false );
148 }.bind( this ), 250 );
150 // Clear the show timeout
151 clearTimeout( this.popupTimeoutShow
);
152 this.popupTimeoutShow
= null;
156 * Set selected state on this widget
158 * @param {boolean} [isSelected] Widget is selected
160 mw
.rcfilters
.ui
.TagItemWidget
.prototype.toggleSelected = function ( isSelected
) {
161 isSelected
= isSelected
!== undefined ? isSelected
: !this.selected
;
163 if ( this.selected
!== isSelected
) {
164 this.selected
= isSelected
;
166 this.$element
.toggleClass( 'mw-rcfilters-ui-tagItemWidget-selected', this.selected
);
171 * Get the selected state of this widget
173 * @return {boolean} Tag is selected
175 mw
.rcfilters
.ui
.TagItemWidget
.prototype.isSelected = function () {
176 return this.selected
;
182 * @return {string} Filter name
184 mw
.rcfilters
.ui
.TagItemWidget
.prototype.getName = function () {
185 return this.itemModel
.getName();
191 * @return {string} Filter model
193 mw
.rcfilters
.ui
.TagItemWidget
.prototype.getModel = function () {
194 return this.itemModel
;
200 * @return {string} Filter view
202 mw
.rcfilters
.ui
.TagItemWidget
.prototype.getView = function () {
203 return this.itemModel
.getGroupModel().getView();
207 * Remove and destroy external elements of this widget
209 mw
.rcfilters
.ui
.TagItemWidget
.prototype.destroy = function () {
211 this.popup
.$element
.detach();
214 this.itemModel
.disconnect( this );
215 this.closeButton
.disconnect( this );
217 }( mediaWiki
, jQuery
) );