RCFilters UI: Fix CapsuleItemWidget popup styling
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.CapsuleItemWidget.js
1 ( function ( mw, $ ) {
2 /**
3 * Extend OOUI's CapsuleItemWidget to also display a popup on hover.
4 *
5 * @class
6 * @extends OO.ui.CapsuleItemWidget
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.CapsuleItemWidget = function MwRcfiltersUiCapsuleItemWidget( controller, model, config ) {
16 var $popupContent = $( '<div>' )
17 .addClass( 'mw-rcfilters-ui-capsuleItemWidget-popup-content' ),
18 descLabelWidget = new OO.ui.LabelWidget();
19
20 // Configuration initialization
21 config = config || {};
22
23 this.controller = controller;
24 this.model = model;
25 this.$overlay = config.$overlay || this.$element;
26 this.positioned = false;
27
28 // Parent constructor
29 mw.rcfilters.ui.CapsuleItemWidget.parent.call( this, $.extend( {
30 data: this.model.getName(),
31 label: this.model.getLabel()
32 }, config ) );
33
34 // Mixin constructors
35 OO.ui.mixin.PopupElement.call( this, $.extend( {
36 popup: {
37 padded: false,
38 align: 'center',
39 $content: $popupContent
40 .append( descLabelWidget.$element ),
41 $floatableContainer: this.$element,
42 classes: [ 'mw-rcfilters-ui-capsuleItemWidget-popup' ]
43 }
44 }, config ) );
45
46 // Set initial text for the popup - the description
47 descLabelWidget.setLabel( this.model.getDescription() );
48
49 this.$highlight = $( '<div>' )
50 .addClass( 'mw-rcfilters-ui-capsuleItemWidget-highlight' );
51
52 // Events
53 this.model.connect( this, { update: 'onModelUpdate' } );
54
55 this.closeButton.connect( this, { click: 'onCapsuleRemovedByUser' } );
56
57 // Initialization
58 this.$overlay.append( this.popup.$element );
59 this.$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 ) );
65
66 this.setCurrentMuteState();
67 this.setHighlightColor();
68 };
69
70 OO.inheritClass( mw.rcfilters.ui.CapsuleItemWidget, OO.ui.CapsuleItemWidget );
71 OO.mixinClass( mw.rcfilters.ui.CapsuleItemWidget, OO.ui.mixin.PopupElement );
72
73 /**
74 * Respond to model update event
75 */
76 mw.rcfilters.ui.CapsuleItemWidget.prototype.onModelUpdate = function () {
77 this.setCurrentMuteState();
78
79 this.setHighlightColor();
80 };
81
82 mw.rcfilters.ui.CapsuleItemWidget.prototype.setHighlightColor = function () {
83 var selectedColor = this.model.isHighlightEnabled() ? this.model.getHighlightColor() : null;
84
85 this.$highlight
86 .attr( 'data-color', selectedColor )
87 .toggleClass(
88 'mw-rcfilters-ui-capsuleItemWidget-highlight-highlighted',
89 !!selectedColor
90 );
91 };
92
93 /**
94 * Set the current mute state for this item
95 */
96 mw.rcfilters.ui.CapsuleItemWidget.prototype.setCurrentMuteState = function () {
97 this.$element
98 .toggleClass(
99 'mw-rcfilters-ui-capsuleItemWidget-muted',
100 !this.model.isSelected() ||
101 this.model.isIncluded() ||
102 this.model.isConflicted() ||
103 this.model.isFullyCovered()
104 );
105 };
106
107 /**
108 * Respond to hover event on the capsule item.
109 *
110 * @param {boolean} isHovering Mouse is hovering on the item
111 */
112 mw.rcfilters.ui.CapsuleItemWidget.prototype.onHover = function ( isHovering ) {
113 if ( this.model.getDescription() ) {
114 this.popup.toggle( isHovering );
115
116 if ( isHovering && !this.positioned ) {
117 // Recalculate position to be center of the capsule item
118 this.popup.$element.css( 'margin-left', ( this.$element.width() / 2 ) );
119 this.positioned = true;
120 }
121 }
122 };
123
124 /**
125 * Respond to the user removing the capsule with the close button
126 */
127 mw.rcfilters.ui.CapsuleItemWidget.prototype.onCapsuleRemovedByUser = function () {
128 this.controller.updateFilter( this.model.getName(), false );
129 this.controller.clearHighlightColor( this.model.getName() );
130 };
131
132 /**
133 * Remove and destroy external elements of this widget
134 */
135 mw.rcfilters.ui.CapsuleItemWidget.prototype.destroy = function () {
136 // Destroy the popup
137 this.popup.$element.detach();
138
139 // Disconnect events
140 this.model.disconnect( this );
141 this.closeButton.disconnect( this );
142 };
143 }( mediaWiki, jQuery ) );