# all files / src/widgets/ TagItemWidget.js

67.92% Statements 36/53
25% Branches 7/28
50% Functions 4/8
67.92% Lines 36/53

    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

    1×
    48×

    48×

    48×
    48×
    48×
    48×
    48×

    48×

    48×

    48×

    48×

    48×

    48×

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

    1×

    102×

    102×
    54×

    102×

    1×

    1×

    1×

    1×
    48×

    48×
    2×

    2×

    2×

    1×
    92×

     /**
     * TagItemWidgets are used within a {@link OO.ui.TagMultiselectWidget
     * TagMultiselectWidget} 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
     * @mixins OO.ui.mixin.DraggableElement
     *
     * @constructor
     * @param {Object} [config] Configuration object
     * @cfg {boolean} [valid=true] Item is valid
     */
    OO.ui.TagItemWidget = function OoUiTagItemWidget( config ) {
    	config = config || {};

    	// Parent constructor
    	OO.ui.TagItemWidget.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 );
    	OO.ui.mixin.DraggableElement.call( this, config );

    	this.valid = config.valid === undefined ? true : !!config.valid;

    	this.closeButton = new OO.ui.ButtonWidget( {
    		framed: false,
    		icon: 'close',
    		tabIndex: -1,
    		title: OO.ui.msg( 'ooui-item-remove' )
    	} );
    	this.closeButton.setDisabled( this.isDisabled() );

    	// Events
    	this.closeButton
    		.connect( this, { click: 'remove' } );
    	this.$element
    		.on( 'click', this.select.bind( this ) )
    		.on( 'keydown', this.onKeyDown.bind( this ) )
    		// Prevent propagation of mousedown; the tag item "lives" in the
    		// clickable area of the TagMultiselectWidget, which listens to
    		// mousedown to open the menu or popup. We want to prevent that
    		// for clicks specifically on the tag itself, so the actions taken
    		// are more deliberate. When the tag is clicked, it will emit the
    		// selection event (similar to how #OO.ui.MultioptionWidget emits 'change')
    		// and can be handled separately.
    		.on( 'mousedown', function ( e ) { e.stopPropagation(); } );

    	// Initialization
    	this.$element
    		.addClass( 'oo-ui-tagItemWidget' )
    		.append( this.$label, this.closeButton.$element );
    };

    /* Initialization */

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

    /* Events */

    /**
     * @event remove
     *
     * A remove action was performed on the item
     */

    /**
     * @event navigate
     * @param {string} direction Direction of the movement, forward or backwards
     *
     * A navigate action was performed on the item
     */

    /**
     * @event select
     *
     * The tag widget was selected. This can occur when the widget
     * is either clicked or enter was pressed on it.
     */

    /**
     * @event valid
     * @param {boolean} isValid Item is valid
     *
     * Item validity has changed
     */

    /* Methods */

    /**
     * @inheritdoc
     */
    OO.ui.TagItemWidget.prototype.setDisabled = function ( state ) {
    	// Parent method
    	OO.ui.TagItemWidget.parent.prototype.setDisabled.call( this, state );

    	if ( this.closeButton ) {
    		this.closeButton.setDisabled( state );
    	}
    	return this;
    };

    /**
     * Handle removal of the item
     *
     * This is mainly for extensibility concerns, so other children
     * of this class can change the behavior if they need to. This
     * is called by both clicking the 'remove' button but also
     * on keypress, which is harder to override if needed.
     *
     * @fires remove
     */
    OO.ui.TagItemWidget.prototype.remove = function () {
    	if ( !this.isDisabled() ) {
    		this.emit( 'remove' );
    	}
    };

    /**
     * Handle a keydown event on the widget
     *
     * @fires navigate
     * @fires remove
     * @param {jQuery.Event} e Key down event
     * @return {boolean|undefined} false to stop the operation
     */
    OO.ui.TagItemWidget.prototype.onKeyDown = function ( e ) {
    	var movement;

    	if ( e.keyCode === OO.ui.Keys.BACKSPACE || e.keyCode === OO.ui.Keys.DELETE ) {
    		this.remove();
    		return false;
    	} else if ( e.keyCode === OO.ui.Keys.ENTER ) {
    		this.select();
    		return false;
    	} else if (
    		e.keyCode === OO.ui.Keys.LEFT ||
    		e.keyCode === OO.ui.Keys.RIGHT
    	) {
    		if ( OO.ui.Element.static.getDir( this.$element ) === 'rtl' ) {
    			movement = {
    				left: 'forwards',
    				right: 'backwards'
    			};
    		} else {
    			movement = {
    				left: 'backwards',
    				right: 'forwards'
    			};
    		}

    		this.emit(
    			'navigate',
    			e.keyCode === OO.ui.Keys.LEFT ?
    				movement.left : movement.right
    		);
    	}
    };

    /**
     * Select this item
     *
     * @fires select
     */
    OO.ui.TagItemWidget.prototype.select = function () {
    	if ( !this.isDisabled() ) {
    		this.emit( 'select' );
    	}
    };

    /**
     * Set the valid state of this item
     *
     * @param {boolean} [valid] Item is valid
     * @fires valid
     */
    OO.ui.TagItemWidget.prototype.toggleValid = function ( valid ) {
    	valid = valid === undefined ? !this.valid : !!valid;

    	if ( this.valid !== valid ) {
    		this.valid = valid;

    		this.setFlags( { invalid: !this.valid } );

    		this.emit( 'valid', this.valid );
    	}
    };

    /**
     * Check whether the item is valid
     *
     * @return {boolean} Item is valid
     */
    OO.ui.TagItemWidget.prototype.isValid = function () {
    	return this.valid;
    };