# all files / src/widgets/ CapsuleItemWidget.js

23.68% Statements 9/38
0% Branches 0/21
0% Functions 0/5
23.68% Lines 9/38

    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

    1×

    1×
    1×
    1×
    1×
    1×

    1×

    1×

    1×

     /**
     * CapsuleItemWidgets are used within a {@link OO.ui.CapsuleMultiselectWidget
     * CapsuleMultiselectWidget} to display the selected items.
     *
     * @class
     * @extends OO.ui.Widget
     * @mixins OO.ui.mixin.ItemWidget
     * @mixins OO.ui.mixin.LabelElement
     * @mixins OO.ui.mixin.FlaggedElement
     * @mixins OO.ui.mixin.TabIndexedElement
     *
     * @constructor
     * @param {Object} [config] Configuration options
     */
    OO.ui.CapsuleItemWidget = function OoUiCapsuleItemWidget( config ) {
    	// Configuration initialization
    	config = config || {};

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

    	// Mixin constructors
    	OO.ui.mixin.ItemWidget.call( this );
    	OO.ui.mixin.LabelElement.call( this, config );
    	OO.ui.mixin.FlaggedElement.call( this, config );
    	OO.ui.mixin.TabIndexedElement.call( this, config );

    	// Events
    	this.closeButton = new OO.ui.ButtonWidget( {
    		framed: false,
    		icon: 'close',
    		tabIndex: -1,
    		title: OO.ui.msg( 'ooui-item-remove' )
    	} ).on( 'click', this.onCloseClick.bind( this ) );

    	this.on( 'disable', function ( disabled ) {
    		this.closeButton.setDisabled( disabled );
    	}.bind( this ) );

    	// Initialization
    	this.$element
    		.on( {
    			click: this.onClick.bind( this ),
    			keydown: this.onKeyDown.bind( this )
    		} )
    		.addClass( 'oo-ui-capsuleItemWidget' )
    		.append( this.$label, this.closeButton.$element );
    };

    /* Setup */

    OO.inheritClass( OO.ui.CapsuleItemWidget, OO.ui.Widget );
    OO.mixinClass( OO.ui.CapsuleItemWidget, OO.ui.mixin.ItemWidget );
    OO.mixinClass( OO.ui.CapsuleItemWidget, OO.ui.mixin.LabelElement );
    OO.mixinClass( OO.ui.CapsuleItemWidget, OO.ui.mixin.FlaggedElement );
    OO.mixinClass( OO.ui.CapsuleItemWidget, OO.ui.mixin.TabIndexedElement );

    /* Methods */

    /**
     * Handle close icon clicks
     */
    OO.ui.CapsuleItemWidget.prototype.onCloseClick = function () {
    	var element = this.getElementGroup();

    	if ( element && $.isFunction( element.removeItems ) ) {
    		element.removeItems( [ this ] );
    		element.focus();
    	}
    };

    /**
     * Handle click event for the entire capsule
     */
    OO.ui.CapsuleItemWidget.prototype.onClick = function () {
    	var element = this.getElementGroup();

    	if ( !this.isDisabled() && element && $.isFunction( element.editItem ) ) {
    		element.editItem( this );
    	}
    };

    /**
     * Handle keyDown event for the entire capsule
     *
     * @param {jQuery.Event} e Key down event
     */
    OO.ui.CapsuleItemWidget.prototype.onKeyDown = function ( e ) {
    	var element = this.getElementGroup();

    	if ( e.keyCode === OO.ui.Keys.BACKSPACE || e.keyCode === OO.ui.Keys.DELETE ) {
    		element.removeItems( [ this ] );
    		element.focus();
    		return false;
    	} else if ( e.keyCode === OO.ui.Keys.ENTER ) {
    		element.editItem( this );
    		return false;
    	} else if ( e.keyCode === OO.ui.Keys.LEFT ) {
    		element.getPreviousItem( this ).focus();
    	} else if ( e.keyCode === OO.ui.Keys.RIGHT ) {
    		element.getNextItem( this ).focus();
    	}
    };