RC filters: update the state of the app on popstate.
[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 this.popupTimeoutShow = null;
28 this.popupTimeoutHide = null;
29
30 // Parent constructor
31 mw.rcfilters.ui.CapsuleItemWidget.parent.call( this, $.extend( {
32 data: this.model.getName(),
33 label: this.model.getLabel()
34 }, config ) );
35
36 // Mixin constructors
37 OO.ui.mixin.PopupElement.call( this, $.extend( {
38 popup: {
39 padded: false,
40 align: 'center',
41 $content: $popupContent
42 .append( descLabelWidget.$element ),
43 $floatableContainer: this.$element,
44 classes: [ 'mw-rcfilters-ui-capsuleItemWidget-popup' ]
45 }
46 }, config ) );
47
48 // Set initial text for the popup - the description
49 descLabelWidget.setLabel( this.model.getDescription() );
50
51 this.$highlight = $( '<div>' )
52 .addClass( 'mw-rcfilters-ui-capsuleItemWidget-highlight' );
53
54 // Events
55 this.model.connect( this, { update: 'onModelUpdate' } );
56
57 this.closeButton.$element.on( 'mousedown', this.onCloseButtonMouseDown.bind( this ) );
58
59 // Initialization
60 this.$overlay.append( this.popup.$element );
61 this.$element
62 .prepend( this.$highlight )
63 .attr( 'aria-haspopup', 'true' )
64 .addClass( 'mw-rcfilters-ui-capsuleItemWidget' )
65 .on( 'mouseenter', this.onMouseEnter.bind( this ) )
66 .on( 'mouseleave', this.onMouseLeave.bind( this ) );
67
68 this.setCurrentMuteState();
69 this.setHighlightColor();
70 };
71
72 OO.inheritClass( mw.rcfilters.ui.CapsuleItemWidget, OO.ui.CapsuleItemWidget );
73 OO.mixinClass( mw.rcfilters.ui.CapsuleItemWidget, OO.ui.mixin.PopupElement );
74
75 /**
76 * Respond to model update event
77 */
78 mw.rcfilters.ui.CapsuleItemWidget.prototype.onModelUpdate = function () {
79 this.setCurrentMuteState();
80
81 this.setHighlightColor();
82 };
83
84 /**
85 * Override mousedown event to prevent its propagation to the parent,
86 * since the parent (the multiselect widget) focuses the popup when its
87 * mousedown event is fired.
88 *
89 * @param {jQuery.Event} e Event
90 */
91 mw.rcfilters.ui.CapsuleItemWidget.prototype.onCloseButtonMouseDown = function ( e ) {
92 e.stopPropagation();
93 };
94
95 /**
96 * Emit a click event when the capsule is clicked so we can aggregate this
97 * in the parent (the capsule)
98 */
99 mw.rcfilters.ui.CapsuleItemWidget.prototype.onClick = function () {
100 this.emit( 'click' );
101 };
102
103 /**
104 * Override the event listening to the item close button click
105 */
106 mw.rcfilters.ui.CapsuleItemWidget.prototype.onCloseClick = function () {
107 var element = this.getElementGroup();
108
109 if ( element && $.isFunction( element.removeItems ) ) {
110 element.removeItems( [ this ] );
111 }
112
113 // Respond to user removing the filter
114 this.controller.clearFilter( this.model.getName() );
115 };
116
117 mw.rcfilters.ui.CapsuleItemWidget.prototype.setHighlightColor = function () {
118 var selectedColor = this.model.isHighlightEnabled() ? this.model.getHighlightColor() : null;
119
120 this.$highlight
121 .attr( 'data-color', selectedColor )
122 .toggleClass(
123 'mw-rcfilters-ui-capsuleItemWidget-highlight-highlighted',
124 !!selectedColor
125 );
126 };
127
128 /**
129 * Set the current mute state for this item
130 */
131 mw.rcfilters.ui.CapsuleItemWidget.prototype.setCurrentMuteState = function () {
132 this.$element
133 .toggleClass(
134 'mw-rcfilters-ui-capsuleItemWidget-muted',
135 !this.model.isSelected() ||
136 this.model.isIncluded() ||
137 this.model.isFullyCovered()
138 )
139 .toggleClass(
140 'mw-rcfilters-ui-capsuleItemWidget-conflicted',
141 this.model.isConflicted()
142 );
143 };
144
145 /**
146 * Respond to mouse enter event
147 */
148 mw.rcfilters.ui.CapsuleItemWidget.prototype.onMouseEnter = function () {
149 if ( this.model.getDescription() ) {
150 if ( !this.positioned ) {
151 // Recalculate position to be center of the capsule item
152 this.popup.$element.css( 'margin-left', ( this.$element.width() / 2 ) );
153 this.positioned = true;
154 }
155
156 // Set timeout for the popup to show
157 this.popupTimeoutShow = setTimeout( function () {
158 this.popup.toggle( true );
159 }.bind( this ), 500 );
160
161 // Cancel the hide timeout
162 clearTimeout( this.popupTimeoutHide );
163 this.popupTimeoutHide = null;
164 }
165 };
166
167 /**
168 * Respond to mouse leave event
169 */
170 mw.rcfilters.ui.CapsuleItemWidget.prototype.onMouseLeave = function () {
171 this.popupTimeoutHide = setTimeout( function () {
172 this.popup.toggle( false );
173 }.bind( this ), 250 );
174
175 // Clear the show timeout
176 clearTimeout( this.popupTimeoutShow );
177 this.popupTimeoutShow = null;
178 };
179
180 /**
181 * Set selected state on this widget
182 *
183 * @param {boolean} [isSelected] Widget is selected
184 */
185 mw.rcfilters.ui.CapsuleItemWidget.prototype.toggleSelected = function ( isSelected ) {
186 isSelected = isSelected !== undefined ? isSelected : !this.selected;
187
188 if ( this.selected !== isSelected ) {
189 this.selected = isSelected;
190
191 this.$element.toggleClass( 'mw-rcfilters-ui-capsuleItemWidget-selected', this.selected );
192 }
193 };
194
195 /**
196 * Remove and destroy external elements of this widget
197 */
198 mw.rcfilters.ui.CapsuleItemWidget.prototype.destroy = function () {
199 // Destroy the popup
200 this.popup.$element.detach();
201
202 // Disconnect events
203 this.model.disconnect( this );
204 this.closeButton.disconnect( this );
205 };
206 }( mediaWiki, jQuery ) );