summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki.rcfilters/ui/mw.rcfilters.ui.TagItemWidget.js
blob: 188650a9b7c4f04a002e13a873677aaf5df9acb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
( function ( mw, $ ) {
	/**
	 * Extend OOUI's TagItemWidget to also display a popup on hover.
	 *
	 * @class
	 * @extends OO.ui.TagItemWidget
	 * @mixins OO.ui.mixin.PopupElement
	 *
	 * @constructor
	 * @param {mw.rcfilters.Controller} controller
	 * @param {mw.rcfilters.dm.FiltersViewModel} filtersViewModel
	 * @param {mw.rcfilters.dm.FilterItem} invertModel
	 * @param {mw.rcfilters.dm.FilterItem} itemModel Item model
	 * @param {Object} config Configuration object
	 * @cfg {jQuery} [$overlay] A jQuery object serving as overlay for popups
	 */
	mw.rcfilters.ui.TagItemWidget = function MwRcfiltersUiTagItemWidget(
		controller, filtersViewModel, invertModel, itemModel, config
	) {
		// Configuration initialization
		config = config || {};

		this.controller = controller;
		this.invertModel = invertModel;
		this.filtersViewModel = filtersViewModel;
		this.itemModel = itemModel;
		this.selected = false;

		mw.rcfilters.ui.TagItemWidget.parent.call( this, $.extend( {
			data: this.itemModel.getName()
		}, config ) );

		this.$overlay = config.$overlay || this.$element;
		this.popupLabel = new OO.ui.LabelWidget();

		// Mixin constructors
		OO.ui.mixin.PopupElement.call( this, $.extend( {
			popup: {
				padded: false,
				align: 'center',
				position: 'above',
				$content: $( '<div>' )
					.addClass( 'mw-rcfilters-ui-tagItemWidget-popup-content' )
					.append( this.popupLabel.$element ),
				$floatableContainer: this.$element,
				classes: [ 'mw-rcfilters-ui-tagItemWidget-popup' ]
			}
		}, config ) );

		this.popupTimeoutShow = null;
		this.popupTimeoutHide = null;

		this.$highlight = $( '<div>' )
			.addClass( 'mw-rcfilters-ui-tagItemWidget-highlight' );

		// Add title attribute with the item label to 'x' button
		this.closeButton.setTitle( mw.msg( 'rcfilters-tag-remove', this.itemModel.getLabel() ) );

		// Events
		this.filtersViewModel.connect( this, { highlightChange: 'updateUiBasedOnState' } );
		this.invertModel.connect( this, { update: 'updateUiBasedOnState' } );
		this.itemModel.connect( this, { update: 'updateUiBasedOnState' } );

		// Initialization
		this.$overlay.append( this.popup.$element );
		this.$element
			.addClass( 'mw-rcfilters-ui-tagItemWidget' )
			.prepend( this.$highlight )
			.attr( 'aria-haspopup', 'true' )
			.on( 'mouseenter', this.onMouseEnter.bind( this ) )
			.on( 'mouseleave', this.onMouseLeave.bind( this ) );

		this.updateUiBasedOnState();
	};

	/* Initialization */

	OO.inheritClass( mw.rcfilters.ui.TagItemWidget, OO.ui.TagItemWidget );
	OO.mixinClass( mw.rcfilters.ui.TagItemWidget, OO.ui.mixin.PopupElement );

	/* Methods */

	/**
	 * Respond to model update event
	 */
	mw.rcfilters.ui.TagItemWidget.prototype.updateUiBasedOnState = function () {
		// Update label if needed
		var labelMsg = this.itemModel.getLabelMessageKey( this.invertModel.isSelected() );
		if ( labelMsg ) {
			this.setLabel( $( '<div>' ).append(
				$( '<bdi>' ).html(
					mw.message( labelMsg, mw.html.escape( this.itemModel.getLabel() ) ).parse()
				)
			).contents() );
		} else {
			this.setLabel(
				$( '<bdi>' ).append(
					this.itemModel.getLabel()
				)
			);
		}

		this.setCurrentMuteState();
		this.setHighlightColor();
	};

	/**
	 * Set the current highlight color for this item
	 */
	mw.rcfilters.ui.TagItemWidget.prototype.setHighlightColor = function () {
		var selectedColor = this.filtersViewModel.isHighlightEnabled() && this.itemModel.isHighlighted ?
			this.itemModel.getHighlightColor() :
			null;

		this.$highlight
			.attr( 'data-color', selectedColor )
			.toggleClass(
				'mw-rcfilters-ui-tagItemWidget-highlight-highlighted',
				!!selectedColor
			);
	};

	/**
	 * Set the current mute state for this item
	 */
	mw.rcfilters.ui.TagItemWidget.prototype.setCurrentMuteState = function () {};

	/**
	 * Respond to mouse enter event
	 */
	mw.rcfilters.ui.TagItemWidget.prototype.onMouseEnter = function () {
		var labelText = this.itemModel.getStateMessage();

		if ( labelText ) {
			this.popupLabel.setLabel( labelText );

			// Set timeout for the popup to show
			this.popupTimeoutShow = setTimeout( function () {
				this.popup.toggle( true );
			}.bind( this ), 500 );

			// Cancel the hide timeout
			clearTimeout( this.popupTimeoutHide );
			this.popupTimeoutHide = null;
		}
	};

	/**
	 * Respond to mouse leave event
	 */
	mw.rcfilters.ui.TagItemWidget.prototype.onMouseLeave = function () {
		this.popupTimeoutHide = setTimeout( function () {
			this.popup.toggle( false );
		}.bind( this ), 250 );

		// Clear the show timeout
		clearTimeout( this.popupTimeoutShow );
		this.popupTimeoutShow = null;
	};

	/**
	 * Set selected state on this widget
	 *
	 * @param {boolean} [isSelected] Widget is selected
	 */
	mw.rcfilters.ui.TagItemWidget.prototype.toggleSelected = function ( isSelected ) {
		isSelected = isSelected !== undefined ? isSelected : !this.selected;

		if ( this.selected !== isSelected ) {
			this.selected = isSelected;

			this.$element.toggleClass( 'mw-rcfilters-ui-tagItemWidget-selected', this.selected );
		}
	};

	/**
	 * Get the selected state of this widget
	 *
	 * @return {boolean} Tag is selected
	 */
	mw.rcfilters.ui.TagItemWidget.prototype.isSelected = function () {
		return this.selected;
	};

	/**
	 * Get item name
	 *
	 * @return {string} Filter name
	 */
	mw.rcfilters.ui.TagItemWidget.prototype.getName = function () {
		return this.itemModel.getName();
	};

	/**
	 * Get item model
	 *
	 * @return {string} Filter model
	 */
	mw.rcfilters.ui.TagItemWidget.prototype.getModel = function () {
		return this.itemModel;
	};

	/**
	 * Get item view
	 *
	 * @return {string} Filter view
	 */
	mw.rcfilters.ui.TagItemWidget.prototype.getView = function () {
		return this.itemModel.getGroupModel().getView();
	};

	/**
	 * Remove and destroy external elements of this widget
	 */
	mw.rcfilters.ui.TagItemWidget.prototype.destroy = function () {
		// Destroy the popup
		this.popup.$element.detach();

		// Disconnect events
		this.itemModel.disconnect( this );
		this.closeButton.disconnect( this );
	};
}( mediaWiki, jQuery ) );