Merge "Temporary hax to hide cawiki's hacked in search sidebar"
[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' ),
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
28 // Parent constructor
29 mw.rcfilters.ui.CapsuleItemWidget.parent.call( this, $.extend( {
30 data: this.model.getName(),
31 label: this.model.getLabel()
32 }, config ) );
33
34 // Mixin constructors
35 OO.ui.mixin.PopupElement.call( this, $.extend( {
36 popup: {
37 padded: true,
38 align: 'center',
39 $content: $popupContent
40 .append( descLabelWidget.$element ),
41 $floatableContainer: this.$element
42 }
43 }, config ) );
44
45 // Set initial text for the popup - the description
46 descLabelWidget.setLabel( this.model.getDescription() );
47
48 // Events
49 this.model.connect( this, { update: 'onModelUpdate' } );
50
51 this.closeButton.connect( this, { click: 'onCapsuleRemovedByUser' } );
52
53 // Initialization
54 this.$overlay.append( this.popup.$element );
55 this.$element
56 .attr( 'aria-haspopup', 'true' )
57 .addClass( 'mw-rcfilters-ui-capsuleItemWidget' )
58 .on( 'mouseover', this.onHover.bind( this, true ) )
59 .on( 'mouseout', this.onHover.bind( this, false ) );
60 };
61
62 OO.inheritClass( mw.rcfilters.ui.CapsuleItemWidget, OO.ui.CapsuleItemWidget );
63 OO.mixinClass( mw.rcfilters.ui.CapsuleItemWidget, OO.ui.mixin.PopupElement );
64
65 /**
66 * Respond to model update event
67 */
68 mw.rcfilters.ui.CapsuleItemWidget.prototype.onModelUpdate = function () {
69 // Deal with active/inactive capsule filter items
70 this.$element
71 .toggleClass(
72 'mw-rcfilters-ui-filterCapsuleMultiselectWidget-item-inactive',
73 !this.model.isActive()
74 );
75 };
76
77 /**
78 * Respond to hover event on the capsule item.
79 *
80 * @param {boolean} isHovering Mouse is hovering on the item
81 */
82 mw.rcfilters.ui.CapsuleItemWidget.prototype.onHover = function ( isHovering ) {
83 if ( this.model.getDescription() ) {
84 this.popup.toggle( isHovering );
85
86 if ( isHovering && !this.positioned ) {
87 // Recalculate position to be center of the capsule item
88 this.popup.$element.css( 'margin-left', ( this.$element.width() / 2 ) );
89 this.positioned = true;
90 }
91 }
92 };
93
94 /**
95 * Respond to the user removing the capsule with the close button
96 */
97 mw.rcfilters.ui.CapsuleItemWidget.prototype.onCapsuleRemovedByUser = function () {
98 this.controller.updateFilter( this.model.getName(), false );
99 };
100 }( mediaWiki, jQuery ) );