# all files / src/widgets/ ToggleButtonWidget.js

35.14% Statements 13/37
0% Branches 0/8
0% Functions 0/4
35.14% Lines 13/37

    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

    1×

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

    1×

    1×

    1×

    1×

     /**
     * ToggleButtons are buttons that have a state (‘on’ or ‘off’) that is represented by a
     * Boolean value. Like other {@link OO.ui.ButtonWidget buttons}, toggle buttons can be
     * configured with {@link OO.ui.mixin.IconElement icons}, {@link OO.ui.mixin.IndicatorElement indicators},
     * {@link OO.ui.mixin.TitledElement titles}, {@link OO.ui.mixin.FlaggedElement styling flags},
     * and {@link OO.ui.mixin.LabelElement labels}. Please see
     * the [OOjs UI documentation][1] on MediaWiki for more information.
     *
     *     @example
     *     // Toggle buttons in the 'off' and 'on' state.
     *     var toggleButton1 = new OO.ui.ToggleButtonWidget( {
     *         label: 'Toggle Button off'
     *     } );
     *     var toggleButton2 = new OO.ui.ToggleButtonWidget( {
     *         label: 'Toggle Button on',
     *         value: true
     *     } );
     *     // Append the buttons to the DOM.
     *     $( 'body' ).append( toggleButton1.$element, toggleButton2.$element );
     *
     * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Buttons_and_Switches#Toggle_buttons
     *
     * @class
     * @extends OO.ui.ToggleWidget
     * @mixins OO.ui.mixin.ButtonElement
     * @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.TabIndexedElement
     *
     * @constructor
     * @param {Object} [config] Configuration options
     * @cfg {boolean} [value=false] The toggle button’s initial on/off
     *  state. By default, the button is in the 'off' state.
     */
    OO.ui.ToggleButtonWidget = function OoUiToggleButtonWidget( config ) {
    	// Configuration initialization
    	config = config || {};

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

    	// Mixin constructors
    	OO.ui.mixin.ButtonElement.call( this, $.extend( {}, config, { active: this.active } ) );
    	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, $.extend( {}, config, { $titled: this.$button } ) );
    	OO.ui.mixin.FlaggedElement.call( this, config );
    	OO.ui.mixin.TabIndexedElement.call( this, $.extend( {}, config, { $tabIndexed: this.$button } ) );

    	// Events
    	this.connect( this, { click: 'onAction' } );

    	// Initialization
    	this.$button.append( this.$icon, this.$label, this.$indicator );
    	this.$element
    		.addClass( 'oo-ui-toggleButtonWidget' )
    		.append( this.$button );
    };

    /* Setup */

    OO.inheritClass( OO.ui.ToggleButtonWidget, OO.ui.ToggleWidget );
    OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.mixin.ButtonElement );
    OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.mixin.IconElement );
    OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.mixin.IndicatorElement );
    OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.mixin.LabelElement );
    OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.mixin.TitledElement );
    OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.mixin.FlaggedElement );
    OO.mixinClass( OO.ui.ToggleButtonWidget, OO.ui.mixin.TabIndexedElement );

    /* Static Properties */

    /**
     * @static
     * @inheritdoc
     */
    OO.ui.ToggleButtonWidget.static.tagName = 'span';

    /* Methods */

    /**
     * Handle the button action being triggered.
     *
     * @private
     */
    OO.ui.ToggleButtonWidget.prototype.onAction = function () {
    	this.setValue( !this.value );
    };

    /**
     * @inheritdoc
     */
    OO.ui.ToggleButtonWidget.prototype.setValue = function ( value ) {
    	value = !!value;
    	if ( value !== this.value ) {
    		// Might be called from parent constructor before ButtonElement constructor
    		if ( this.$button ) {
    			this.$button.attr( 'aria-pressed', value.toString() );
    		}
    		this.setActive( value );
    	}

    	// Parent method
    	OO.ui.ToggleButtonWidget.parent.prototype.setValue.call( this, value );

    	return this;
    };

    /**
     * @inheritdoc
     */
    OO.ui.ToggleButtonWidget.prototype.setButtonElement = function ( $button ) {
    	if ( this.$button ) {
    		this.$button.removeAttr( 'aria-pressed' );
    	}
    	OO.ui.mixin.ButtonElement.prototype.setButtonElement.call( this, $button );
    	this.$button.attr( 'aria-pressed', this.value.toString() );
    };