Merge "Ability to create tests with nested modules"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.TagItemWidget.js
1 ( function ( mw, $ ) {
2 /**
3 * Extend OOUI's TagItemWidget to also display a popup on hover.
4 *
5 * @class
6 * @extends OO.ui.TagItemWidget
7 * @mixins OO.ui.mixin.PopupElement
8 *
9 * @constructor
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
14 */
15 mw.rcfilters.ui.TagItemWidget = function MwRcfiltersUiTagItemWidget( controller, model, config ) {
16 // Configuration initialization
17 config = config || {};
18
19 this.controller = controller;
20 this.model = model;
21 this.selected = false;
22
23 mw.rcfilters.ui.TagItemWidget.parent.call( this, $.extend( {
24 data: this.model.getName(),
25 label: $( '<div>' ).html( this.model.getPrefixedLabel() ).contents()
26 }, config ) );
27
28 this.$overlay = config.$overlay || this.$element;
29 this.popupLabel = new OO.ui.LabelWidget();
30
31 // Mixin constructors
32 OO.ui.mixin.PopupElement.call( this, $.extend( {
33 popup: {
34 padded: false,
35 align: 'center',
36 position: 'above',
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' ]
42 }
43 }, config ) );
44
45 this.positioned = false;
46 this.popupTimeoutShow = null;
47 this.popupTimeoutHide = null;
48
49 this.$highlight = $( '<div>' )
50 .addClass( 'mw-rcfilters-ui-tagItemWidget-highlight' );
51
52 // Events
53 this.model.connect( this, { update: 'onModelUpdate' } );
54
55 // Initialization
56 this.$overlay.append( this.popup.$element );
57 this.$element
58 .addClass( 'mw-rcfilters-ui-tagItemWidget' )
59 .prepend( this.$highlight )
60 .attr( 'aria-haspopup', 'true' )
61 .on( 'mouseenter', this.onMouseEnter.bind( this ) )
62 .on( 'mouseleave', this.onMouseLeave.bind( this ) );
63
64 this.setCurrentMuteState();
65 this.setHighlightColor();
66 };
67
68 /* Initialization */
69
70 OO.inheritClass( mw.rcfilters.ui.TagItemWidget, OO.ui.TagItemWidget );
71 OO.mixinClass( mw.rcfilters.ui.TagItemWidget, OO.ui.mixin.PopupElement );
72
73 /* Methods */
74
75 /**
76 * Respond to model update event
77 */
78 mw.rcfilters.ui.TagItemWidget.prototype.onModelUpdate = function () {
79 this.setCurrentMuteState();
80
81 // Update label if needed
82 this.setLabel( $( '<div>' ).html( this.model.getPrefixedLabel() ).contents() );
83
84 this.setHighlightColor();
85 };
86
87 mw.rcfilters.ui.TagItemWidget.prototype.setHighlightColor = function () {
88 var selectedColor = this.model.isHighlightEnabled() ? this.model.getHighlightColor() : null;
89
90 this.$highlight
91 .attr( 'data-color', selectedColor )
92 .toggleClass(
93 'mw-rcfilters-ui-tagItemWidget-highlight-highlighted',
94 !!selectedColor
95 );
96 };
97
98 /**
99 * Set the current mute state for this item
100 */
101 mw.rcfilters.ui.TagItemWidget.prototype.setCurrentMuteState = function () {};
102
103 /**
104 * Respond to mouse enter event
105 */
106 mw.rcfilters.ui.TagItemWidget.prototype.onMouseEnter = function () {
107 var labelText = this.model.getStateMessage();
108
109 if ( labelText ) {
110 this.popupLabel.setLabel( labelText );
111
112 if ( !this.positioned ) {
113 // Recalculate anchor position to be center of the capsule item
114 this.popup.$anchor.css( 'margin-left', ( this.$element.width() / 2 ) );
115 this.positioned = true;
116 }
117
118 // Set timeout for the popup to show
119 this.popupTimeoutShow = setTimeout( function () {
120 this.popup.toggle( true );
121 }.bind( this ), 500 );
122
123 // Cancel the hide timeout
124 clearTimeout( this.popupTimeoutHide );
125 this.popupTimeoutHide = null;
126 }
127 };
128
129 /**
130 * Respond to mouse leave event
131 */
132 mw.rcfilters.ui.TagItemWidget.prototype.onMouseLeave = function () {
133 this.popupTimeoutHide = setTimeout( function () {
134 this.popup.toggle( false );
135 }.bind( this ), 250 );
136
137 // Clear the show timeout
138 clearTimeout( this.popupTimeoutShow );
139 this.popupTimeoutShow = null;
140 };
141
142 /**
143 * Set selected state on this widget
144 *
145 * @param {boolean} [isSelected] Widget is selected
146 */
147 mw.rcfilters.ui.TagItemWidget.prototype.toggleSelected = function ( isSelected ) {
148 isSelected = isSelected !== undefined ? isSelected : !this.selected;
149
150 if ( this.selected !== isSelected ) {
151 this.selected = isSelected;
152
153 this.$element.toggleClass( 'mw-rcfilters-ui-tagItemWidget-selected', this.selected );
154 }
155 };
156
157 /**
158 * Get the selected state of this widget
159 *
160 * @return {boolean} Tag is selected
161 */
162 mw.rcfilters.ui.TagItemWidget.prototype.isSelected = function () {
163 return this.selected;
164 };
165
166 /**
167 * Get item name
168 *
169 * @return {string} Filter name
170 */
171 mw.rcfilters.ui.TagItemWidget.prototype.getName = function () {
172 return this.model.getName();
173 };
174
175 /**
176 * Get item model
177 *
178 * @return {string} Filter model
179 */
180 mw.rcfilters.ui.TagItemWidget.prototype.getModel = function () {
181 return this.model;
182 };
183
184 /**
185 * Get item view
186 *
187 * @return {string} Filter view
188 */
189 mw.rcfilters.ui.TagItemWidget.prototype.getView = function () {
190 return this.model.getGroupModel().getView();
191 };
192
193 /**
194 * Remove and destroy external elements of this widget
195 */
196 mw.rcfilters.ui.TagItemWidget.prototype.destroy = function () {
197 // Destroy the popup
198 this.popup.$element.detach();
199
200 // Disconnect events
201 this.model.disconnect( this );
202 this.closeButton.disconnect( this );
203 };
204 }( mediaWiki, jQuery ) );