# all files / src/toolgroups/ PopupToolGroup.js

19.54% Statements 17/87
0% Branches 0/58
0% Functions 0/9
19.54% Lines 17/87

    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
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248

    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     /**
     * PopupToolGroup is an abstract base class used by both {@link OO.ui.MenuToolGroup MenuToolGroup}
     * and {@link OO.ui.ListToolGroup ListToolGroup} to provide a popup--an overlaid menu or list of tools with an
     * optional icon and label. This class can be used for other base classes that also use this functionality.
     *
     * @abstract
     * @class
     * @extends OO.ui.ToolGroup
     * @mixins OO.ui.mixin.IconElement
     * @mixins OO.ui.mixin.IndicatorElement
     * @mixins OO.ui.mixin.LabelElement
     * @mixins OO.ui.mixin.TitledElement
     * @mixins OO.ui.mixin.FlaggedElement
     * @mixins OO.ui.mixin.ClippableElement
     * @mixins OO.ui.mixin.TabIndexedElement
     *
     * @constructor
     * @param {OO.ui.Toolbar} toolbar
     * @param {Object} [config] Configuration options
     * @cfg {string} [header] Text to display at the top of the popup
     */
    OO.ui.PopupToolGroup = function OoUiPopupToolGroup( toolbar, config ) {
    	// Allow passing positional parameters inside the config object
    	if ( OO.isPlainObject( toolbar ) && config === undefined ) {
    		config = toolbar;
    		toolbar = config.toolbar;
    	}

    	// Configuration initialization
    	config = $.extend( {
    		indicator: config.indicator === undefined ? ( toolbar.position === 'bottom' ? 'up' : 'down' ) : config.indicator
    	}, config );

    	// Parent constructor
    	OO.ui.PopupToolGroup.parent.call( this, toolbar, config );

    	// Properties
    	this.active = false;
    	this.dragging = false;
    	this.onBlurHandler = this.onBlur.bind( this );
    	this.$handle = $( '<span>' );

    	// Mixin constructors
    	OO.ui.mixin.IconElement.call( this, config );
    	OO.ui.mixin.IndicatorElement.call( this, config );
    	OO.ui.mixin.LabelElement.call( this, config );
    	OO.ui.mixin.TitledElement.call( this, config );
    	OO.ui.mixin.FlaggedElement.call( this, config );
    	OO.ui.mixin.ClippableElement.call( this, $.extend( {}, config, { $clippable: this.$group } ) );
    	OO.ui.mixin.TabIndexedElement.call( this, $.extend( {}, config, { $tabIndexed: this.$handle } ) );

    	// Events
    	this.$handle.on( {
    		keydown: this.onHandleMouseKeyDown.bind( this ),
    		keyup: this.onHandleMouseKeyUp.bind( this ),
    		mousedown: this.onHandleMouseKeyDown.bind( this ),
    		mouseup: this.onHandleMouseKeyUp.bind( this )
    	} );

    	// Initialization
    	this.$handle
    		.addClass( 'oo-ui-popupToolGroup-handle' )
    		.attr( 'role', 'button' )
    		.append( this.$icon, this.$label, this.$indicator );
    	// If the pop-up should have a header, add it to the top of the toolGroup.
    	// Note: If this feature is useful for other widgets, we could abstract it into an
    	// OO.ui.HeaderedElement mixin constructor.
    	if ( config.header !== undefined ) {
    		this.$group
    			.prepend( $( '<span>' )
    				.addClass( 'oo-ui-popupToolGroup-header' )
    				.text( config.header )
    			);
    	}
    	this.$element
    		.addClass( 'oo-ui-popupToolGroup' )
    		.prepend( this.$handle );
    };

    /* Setup */

    OO.inheritClass( OO.ui.PopupToolGroup, OO.ui.ToolGroup );
    OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.IconElement );
    OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.IndicatorElement );
    OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.LabelElement );
    OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.TitledElement );
    OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.FlaggedElement );
    OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.ClippableElement );
    OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.TabIndexedElement );

    /* Methods */

    /**
     * @inheritdoc OO.ui.mixin.ClippableElement
     */
    OO.ui.PopupToolGroup.prototype.getHorizontalAnchorEdge = function () {
    	var out;
    	if ( this.$element.hasClass( 'oo-ui-popupToolGroup-right' ) ) {
    		out = 'right';
    	} else {
    		out = 'left';
    	}
    	// Flip for RTL
    	if ( this.$element.css( 'direction' ) === 'rtl' ) {
    		out = ( out === 'left' ) ? 'right' : 'left';
    	}
    	return out;
    };

    /**
     * @inheritdoc OO.ui.mixin.ClippableElement
     */
    OO.ui.PopupToolGroup.prototype.getVerticalAnchorEdge = function () {
    	if ( this.toolbar.position === 'bottom' ) {
    		return 'bottom';
    	}
    	return 'top';
    };

    /**
     * @inheritdoc
     */
    OO.ui.PopupToolGroup.prototype.setDisabled = function () {
    	// Parent method
    	OO.ui.PopupToolGroup.parent.prototype.setDisabled.apply( this, arguments );

    	if ( this.isDisabled() && this.isElementAttached() ) {
    		this.setActive( false );
    	}
    };

    /**
     * Handle focus being lost.
     *
     * The event is actually generated from a mouseup/keyup, so it is not a normal blur event object.
     *
     * @protected
     * @param {MouseEvent|KeyboardEvent} e Mouse up or key up event
     */
    OO.ui.PopupToolGroup.prototype.onBlur = function ( e ) {
    	// Only deactivate when clicking outside the dropdown element
    	if ( $( e.target ).closest( '.oo-ui-popupToolGroup' )[ 0 ] !== this.$element[ 0 ] ) {
    		this.setActive( false );
    	}
    };

    /**
     * @inheritdoc
     */
    OO.ui.PopupToolGroup.prototype.onMouseKeyUp = function ( e ) {
    	// Only close toolgroup when a tool was actually selected
    	if (
    		!this.isDisabled() && this.pressed && this.pressed === this.findTargetTool( e ) &&
    		( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
    	) {
    		this.setActive( false );
    	}
    	return OO.ui.PopupToolGroup.parent.prototype.onMouseKeyUp.call( this, e );
    };

    /**
     * Handle mouse up and key up events.
     *
     * @protected
     * @param {jQuery.Event} e Mouse up or key up event
     */
    OO.ui.PopupToolGroup.prototype.onHandleMouseKeyUp = function ( e ) {
    	if (
    		!this.isDisabled() &&
    		( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
    	) {
    		return false;
    	}
    };

    /**
     * Handle mouse down and key down events.
     *
     * @protected
     * @param {jQuery.Event} e Mouse down or key down event
     */
    OO.ui.PopupToolGroup.prototype.onHandleMouseKeyDown = function ( e ) {
    	if (
    		!this.isDisabled() &&
    		( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
    	) {
    		this.setActive( !this.active );
    		return false;
    	}
    };

    /**
     * Switch into 'active' mode.
     *
     * When active, the popup is visible. A mouseup event anywhere in the document will trigger
     * deactivation.
     *
     * @param {boolean} value The active state to set
     */
    OO.ui.PopupToolGroup.prototype.setActive = function ( value ) {
    	var containerWidth, containerLeft;
    	value = !!value;
    	if ( this.active !== value ) {
    		this.active = value;
    		if ( value ) {
    			this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler, true );
    			this.getElementDocument().addEventListener( 'keyup', this.onBlurHandler, true );

    			this.$clippable.css( 'left', '' );
    			// Try anchoring the popup to the left first
    			this.$element.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
    			this.setFlags( { progressive: true } );
    			this.toggleClipping( true );
    			if ( this.isClippedHorizontally() || this.isFloatableOutOfView() ) {
    				// Anchoring to the left caused the popup to clip, so anchor it to the right instead
    				this.toggleClipping( false );
    				this.$element
    					.removeClass( 'oo-ui-popupToolGroup-left' )
    					.addClass( 'oo-ui-popupToolGroup-right' );
    				this.toggleClipping( true );
    			}
    			if ( this.isClippedHorizontally() || this.isFloatableOutOfView() ) {
    				// Anchoring to the right also caused the popup to clip, so just make it fill the container
    				containerWidth = this.$clippableScrollableContainer.width();
    				containerLeft = this.$clippableScrollableContainer[ 0 ] === document.documentElement ?
    					0 :
    					this.$clippableScrollableContainer.offset().left;

    				this.toggleClipping( false );
    				this.$element.removeClass( 'oo-ui-popupToolGroup-right' );

    				this.$clippable.css( {
    					left: -( this.$element.offset().left - containerLeft ),
    					width: containerWidth
    				} );
    			}
    		} else {
    			this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler, true );
    			this.getElementDocument().removeEventListener( 'keyup', this.onBlurHandler, true );
    			this.$element.removeClass(
    				'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left  oo-ui-popupToolGroup-right'
    			);
    			this.setFlags( { progressive: false } );
    			this.toggleClipping( false );
    		}
    	}
    };