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' ),
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: true,
38 align: 'center',
39 $content: $popupContent
40 .append( descLabelWidget.$element ),
41 $floatableContainer: this.$element
42 }
43 }, config ) );
44
45 // Set initial text for the popup - the description
46 descLabelWidget.setLabel( this.model.getDescription() );
47
48 this.$highlight = $( '<div>' )
49 .addClass( 'mw-rcfilters-ui-capsuleItemWidget-highlight' );
50
51 // Events
52 this.model.connect( this, { update: 'onModelUpdate' } );
53
54 this.closeButton.$element.on( 'mousedown', this.onCloseButtonMouseDown.bind( this ) );
55
56 // Initialization
57 this.$overlay.append( this.popup.$element );
58 this.$element
59 .prepend( this.$highlight )
60 .attr( 'aria-haspopup', 'true' )
61 .addClass( 'mw-rcfilters-ui-capsuleItemWidget' )
62 .on( 'mouseover', this.onHover.bind( this, true ) )
63 .on( 'mouseout', this.onHover.bind( this, false ) );
64
65 this.setCurrentMuteState();
66 this.setHighlightColor();
67 };
68
69 OO.inheritClass( mw.rcfilters.ui.CapsuleItemWidget, OO.ui.CapsuleItemWidget );
70 OO.mixinClass( mw.rcfilters.ui.CapsuleItemWidget, OO.ui.mixin.PopupElement );
71
72 /**
73 * Respond to model update event
74 */
75 mw.rcfilters.ui.CapsuleItemWidget.prototype.onModelUpdate = function () {
76 this.setCurrentMuteState();
77
78 this.setHighlightColor();
79 };
80
81 /**
82 * Override mousedown event to prevent its propagation to the parent,
83 * since the parent (the multiselect widget) focuses the popup when its
84 * mousedown event is fired.
85 *
86 * @param {jQuery.Event} e Event
87 */
88 mw.rcfilters.ui.CapsuleItemWidget.prototype.onCloseButtonMouseDown = function ( e ) {
89 e.stopPropagation();
90 };
91
92 /**
93 * Override the event listening to the item close button click
94 */
95 mw.rcfilters.ui.CapsuleItemWidget.prototype.onCloseClick = function () {
96 var element = this.getElementGroup();
97
98 if ( element && $.isFunction( element.removeItems ) ) {
99 element.removeItems( [ this ] );
100 }
101
102 // Respond to user removing the filter
103 this.controller.updateFilter( this.model.getName(), false );
104 this.controller.clearHighlightColor( this.model.getName() );
105 };
106
107 mw.rcfilters.ui.CapsuleItemWidget.prototype.setHighlightColor = function () {
108 var selectedColor = this.model.isHighlightEnabled() ? this.model.getHighlightColor() : null;
109
110 this.$highlight
111 .attr( 'data-color', selectedColor )
112 .toggleClass(
113 'mw-rcfilters-ui-capsuleItemWidget-highlight-highlighted',
114 !!selectedColor
115 );
116 };
117
118 /**
119 * Set the current mute state for this item
120 */
121 mw.rcfilters.ui.CapsuleItemWidget.prototype.setCurrentMuteState = function () {
122 this.$element
123 .toggleClass(
124 'mw-rcfilters-ui-capsuleItemWidget-muted',
125 !this.model.isSelected() ||
126 this.model.isIncluded() ||
127 this.model.isConflicted() ||
128 this.model.isFullyCovered()
129 );
130 };
131
132 /**
133 * Respond to hover event on the capsule item.
134 *
135 * @param {boolean} isHovering Mouse is hovering on the item
136 */
137 mw.rcfilters.ui.CapsuleItemWidget.prototype.onHover = function ( isHovering ) {
138 if ( this.model.getDescription() ) {
139 this.popup.toggle( isHovering );
140
141 if ( isHovering && !this.positioned ) {
142 // Recalculate position to be center of the capsule item
143 this.popup.$element.css( 'margin-left', ( this.$element.width() / 2 ) );
144 this.positioned = true;
145 }
146 }
147 };
148
149 /**
150 * Remove and destroy external elements of this widget
151 */
152 mw.rcfilters.ui.CapsuleItemWidget.prototype.destroy = function () {
153 // Destroy the popup
154 this.popup.$element.detach();
155
156 // Disconnect events
157 this.model.disconnect( this );
158 this.closeButton.disconnect( this );
159 };
160 }( mediaWiki, jQuery ) );