Merge "Allow editors to exclude navigation items from search indices"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / ui / mw.rcfilters.ui.FilterTagItemWidget.js
1 ( function ( mw, $ ) {
2 /**
3 * Extend OOUI's FilterTagItemWidget to also display a popup on hover.
4 *
5 * @class
6 * @extends OO.ui.FilterTagItemWidget
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.FilterTagItemWidget = function MwRcfiltersUiFilterTagItemWidget( 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.FilterTagItemWidget.parent.call( this, $.extend( {
24 data: this.model.getName(),
25 label: this.model.getLabel()
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-filterTagItemWidget-popup-content' )
39 .append( this.popupLabel.$element ),
40 $floatableContainer: this.$element,
41 classes: [ 'mw-rcfilters-ui-filterTagItemWidget-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-filterTagItemWidget-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 .prepend( this.$highlight )
59 .attr( 'aria-haspopup', 'true' )
60 .on( 'mouseenter', this.onMouseEnter.bind( this ) )
61 .on( 'mouseleave', this.onMouseLeave.bind( this ) );
62
63 this.setCurrentMuteState();
64 this.setHighlightColor();
65 };
66
67 /* Initialization */
68
69 OO.inheritClass( mw.rcfilters.ui.FilterTagItemWidget, OO.ui.TagItemWidget );
70 OO.mixinClass( mw.rcfilters.ui.FilterTagItemWidget, OO.ui.mixin.PopupElement );
71
72 /* Methods */
73
74 /**
75 * Respond to model update event
76 */
77 mw.rcfilters.ui.FilterTagItemWidget.prototype.onModelUpdate = function () {
78 this.setCurrentMuteState();
79
80 this.setHighlightColor();
81 };
82
83 mw.rcfilters.ui.FilterTagItemWidget.prototype.setHighlightColor = function () {
84 var selectedColor = this.model.isHighlightEnabled() ? this.model.getHighlightColor() : null;
85
86 this.$highlight
87 .attr( 'data-color', selectedColor )
88 .toggleClass(
89 'mw-rcfilters-ui-filterTagItemWidget-highlight-highlighted',
90 !!selectedColor
91 );
92 };
93
94 /**
95 * Set the current mute state for this item
96 */
97 mw.rcfilters.ui.FilterTagItemWidget.prototype.setCurrentMuteState = function () {
98 this.$element
99 .toggleClass(
100 'mw-rcfilters-ui-filterTagItemWidget-muted',
101 !this.model.isSelected() ||
102 this.model.isIncluded() ||
103 this.model.isFullyCovered()
104 )
105 .toggleClass(
106 'mw-rcfilters-ui-filterTagItemWidget-conflicted',
107 this.model.isSelected() && this.model.isConflicted()
108 );
109 };
110
111 /**
112 * Respond to mouse enter event
113 */
114 mw.rcfilters.ui.FilterTagItemWidget.prototype.onMouseEnter = function () {
115 var labelText = this.model.getStateMessage();
116
117 if ( labelText ) {
118 this.popupLabel.setLabel( labelText );
119
120 if ( !this.positioned ) {
121 // Recalculate anchor position to be center of the capsule item
122 this.popup.$anchor.css( 'margin-left', ( this.$element.width() / 2 ) );
123 this.positioned = true;
124 }
125
126 // Set timeout for the popup to show
127 this.popupTimeoutShow = setTimeout( function () {
128 this.popup.toggle( true );
129 }.bind( this ), 500 );
130
131 // Cancel the hide timeout
132 clearTimeout( this.popupTimeoutHide );
133 this.popupTimeoutHide = null;
134 }
135 };
136
137 /**
138 * Respond to mouse leave event
139 */
140 mw.rcfilters.ui.FilterTagItemWidget.prototype.onMouseLeave = function () {
141 this.popupTimeoutHide = setTimeout( function () {
142 this.popup.toggle( false );
143 }.bind( this ), 250 );
144
145 // Clear the show timeout
146 clearTimeout( this.popupTimeoutShow );
147 this.popupTimeoutShow = null;
148 };
149
150 /**
151 * Set selected state on this widget
152 *
153 * @param {boolean} [isSelected] Widget is selected
154 */
155 mw.rcfilters.ui.FilterTagItemWidget.prototype.toggleSelected = function ( isSelected ) {
156 isSelected = isSelected !== undefined ? isSelected : !this.selected;
157
158 if ( this.selected !== isSelected ) {
159 this.selected = isSelected;
160
161 this.$element.toggleClass( 'mw-rcfilters-ui-filterTagItemWidget-selected', this.selected );
162 }
163 };
164
165 /**
166 * Get the selected state of this widget
167 *
168 * @return {boolean} Tag is selected
169 */
170 mw.rcfilters.ui.FilterTagItemWidget.prototype.isSelected = function () {
171 return this.selected;
172 };
173
174 /**
175 * Get item name
176 *
177 * @return {string} Filter name
178 */
179 mw.rcfilters.ui.FilterTagItemWidget.prototype.getName = function () {
180 return this.model.getName();
181 };
182
183 /**
184 * Remove and destroy external elements of this widget
185 */
186 mw.rcfilters.ui.FilterTagItemWidget.prototype.destroy = function () {
187 // Destroy the popup
188 this.popup.$element.detach();
189
190 // Disconnect events
191 this.model.disconnect( this );
192 this.closeButton.disconnect( this );
193 };
194 }( mediaWiki, jQuery ) );