Delete maintenance/deleteRevision.php
[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: $( '<div>' ).html( this.model.getPrefixedLabel() ).contents()
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.popupTimeoutShow = null;
46 this.popupTimeoutHide = null;
47
48 this.$highlight = $( '<div>' )
49 .addClass( 'mw-rcfilters-ui-tagItemWidget-highlight' );
50
51 // Events
52 this.model.connect( this, { update: 'onModelUpdate' } );
53
54 // Initialization
55 this.$overlay.append( this.popup.$element );
56 this.$element
57 .addClass( 'mw-rcfilters-ui-tagItemWidget' )
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.TagItemWidget, OO.ui.TagItemWidget );
70 OO.mixinClass( mw.rcfilters.ui.TagItemWidget, OO.ui.mixin.PopupElement );
71
72 /* Methods */
73
74 /**
75 * Respond to model update event
76 */
77 mw.rcfilters.ui.TagItemWidget.prototype.onModelUpdate = function () {
78 this.setCurrentMuteState();
79
80 // Update label if needed
81 this.setLabel( $( '<div>' ).html( this.model.getPrefixedLabel() ).contents() );
82
83 this.setHighlightColor();
84 };
85
86 mw.rcfilters.ui.TagItemWidget.prototype.setHighlightColor = function () {
87 var selectedColor = this.model.isHighlightEnabled() ? this.model.getHighlightColor() : null;
88
89 this.$highlight
90 .attr( 'data-color', selectedColor )
91 .toggleClass(
92 'mw-rcfilters-ui-tagItemWidget-highlight-highlighted',
93 !!selectedColor
94 );
95 };
96
97 /**
98 * Set the current mute state for this item
99 */
100 mw.rcfilters.ui.TagItemWidget.prototype.setCurrentMuteState = function () {};
101
102 /**
103 * Respond to mouse enter event
104 */
105 mw.rcfilters.ui.TagItemWidget.prototype.onMouseEnter = function () {
106 var labelText = this.model.getStateMessage();
107
108 if ( labelText ) {
109 this.popupLabel.setLabel( labelText );
110
111 // Set timeout for the popup to show
112 this.popupTimeoutShow = setTimeout( function () {
113 this.popup.toggle( true );
114 }.bind( this ), 500 );
115
116 // Cancel the hide timeout
117 clearTimeout( this.popupTimeoutHide );
118 this.popupTimeoutHide = null;
119 }
120 };
121
122 /**
123 * Respond to mouse leave event
124 */
125 mw.rcfilters.ui.TagItemWidget.prototype.onMouseLeave = function () {
126 this.popupTimeoutHide = setTimeout( function () {
127 this.popup.toggle( false );
128 }.bind( this ), 250 );
129
130 // Clear the show timeout
131 clearTimeout( this.popupTimeoutShow );
132 this.popupTimeoutShow = null;
133 };
134
135 /**
136 * Set selected state on this widget
137 *
138 * @param {boolean} [isSelected] Widget is selected
139 */
140 mw.rcfilters.ui.TagItemWidget.prototype.toggleSelected = function ( isSelected ) {
141 isSelected = isSelected !== undefined ? isSelected : !this.selected;
142
143 if ( this.selected !== isSelected ) {
144 this.selected = isSelected;
145
146 this.$element.toggleClass( 'mw-rcfilters-ui-tagItemWidget-selected', this.selected );
147 }
148 };
149
150 /**
151 * Get the selected state of this widget
152 *
153 * @return {boolean} Tag is selected
154 */
155 mw.rcfilters.ui.TagItemWidget.prototype.isSelected = function () {
156 return this.selected;
157 };
158
159 /**
160 * Get item name
161 *
162 * @return {string} Filter name
163 */
164 mw.rcfilters.ui.TagItemWidget.prototype.getName = function () {
165 return this.model.getName();
166 };
167
168 /**
169 * Get item model
170 *
171 * @return {string} Filter model
172 */
173 mw.rcfilters.ui.TagItemWidget.prototype.getModel = function () {
174 return this.model;
175 };
176
177 /**
178 * Get item view
179 *
180 * @return {string} Filter view
181 */
182 mw.rcfilters.ui.TagItemWidget.prototype.getView = function () {
183 return this.model.getGroupModel().getView();
184 };
185
186 /**
187 * Remove and destroy external elements of this widget
188 */
189 mw.rcfilters.ui.TagItemWidget.prototype.destroy = function () {
190 // Destroy the popup
191 this.popup.$element.detach();
192
193 // Disconnect events
194 this.model.disconnect( this );
195 this.closeButton.disconnect( this );
196 };
197 }( mediaWiki, jQuery ) );