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