Merge "RCFilters UI: Stop mousedown propagation when capsule item 'x' button is clicked"
[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.$element.on( 'mousedown', this.onCloseButtonMouseDown.bind( this ) );
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 /**
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.
86 *
87 * @param {jQuery.Event} e Event
88 */
89 mw.rcfilters.ui.CapsuleItemWidget.prototype.onCloseButtonMouseDown = function ( e ) {
90 e.stopPropagation();
91 };
92
93 /**
94 * Override the event listening to the item close button click
95 */
96 mw.rcfilters.ui.CapsuleItemWidget.prototype.onCloseClick = function () {
97 var element = this.getElementGroup();
98
99 if ( element && $.isFunction( element.removeItems ) ) {
100 element.removeItems( [ this ] );
101 }
102
103 // Respond to user removing the filter
104 this.controller.updateFilter( this.model.getName(), false );
105 this.controller.clearHighlightColor( this.model.getName() );
106 };
107
108 mw.rcfilters.ui.CapsuleItemWidget.prototype.setHighlightColor = function () {
109 var selectedColor = this.model.isHighlightEnabled() ? this.model.getHighlightColor() : null;
110
111 this.$highlight
112 .attr( 'data-color', selectedColor )
113 .toggleClass(
114 'mw-rcfilters-ui-capsuleItemWidget-highlight-highlighted',
115 !!selectedColor
116 );
117 };
118
119 /**
120 * Set the current mute state for this item
121 */
122 mw.rcfilters.ui.CapsuleItemWidget.prototype.setCurrentMuteState = function () {
123 this.$element
124 .toggleClass(
125 'mw-rcfilters-ui-capsuleItemWidget-muted',
126 !this.model.isSelected() ||
127 this.model.isIncluded() ||
128 this.model.isConflicted() ||
129 this.model.isFullyCovered()
130 );
131 };
132
133 /**
134 * Respond to hover event on the capsule item.
135 *
136 * @param {boolean} isHovering Mouse is hovering on the item
137 */
138 mw.rcfilters.ui.CapsuleItemWidget.prototype.onHover = function ( isHovering ) {
139 if ( this.model.getDescription() ) {
140 this.popup.toggle( isHovering );
141
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;
146 }
147 }
148 };
149
150 /**
151 * Remove and destroy external elements of this widget
152 */
153 mw.rcfilters.ui.CapsuleItemWidget.prototype.destroy = function () {
154 // Destroy the popup
155 this.popup.$element.detach();
156
157 // Disconnect events
158 this.model.disconnect( this );
159 this.closeButton.disconnect( this );
160 };
161 }( mediaWiki, jQuery ) );