# all files / src/widgets/ ToggleSwitchWidget.js

23.33% Statements 7/30
0% Branches 0/11
0% Functions 0/5
23.33% Lines 7/30

    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

    1×

    1×
    1×

    1×

    1×

    1×

    1×

     /**
     * ToggleSwitches are switches that slide on and off. Their state is represented by a Boolean
     * value (`true` for ‘on’, and `false` otherwise, the default). The ‘off’ state is represented
     * visually by a slider in the leftmost position.
     *
     *     @example
     *     // Toggle switches in the 'off' and 'on' position.
     *     var toggleSwitch1 = new OO.ui.ToggleSwitchWidget();
     *     var toggleSwitch2 = new OO.ui.ToggleSwitchWidget( {
     *         value: true
     *     } );
     *
     *     // Create a FieldsetLayout to layout and label switches
     *     var fieldset = new OO.ui.FieldsetLayout( {
     *        label: 'Toggle switches'
     *     } );
     *     fieldset.addItems( [
     *         new OO.ui.FieldLayout( toggleSwitch1, { label: 'Off', align: 'top' } ),
     *         new OO.ui.FieldLayout( toggleSwitch2, { label: 'On', align: 'top' } )
     *     ] );
     *     $( 'body' ).append( fieldset.$element );
     *
     * @class
     * @extends OO.ui.ToggleWidget
     * @mixins OO.ui.mixin.TabIndexedElement
     *
     * @constructor
     * @param {Object} [config] Configuration options
     * @cfg {boolean} [value=false] The toggle switch’s initial on/off state.
     *  By default, the toggle switch is in the 'off' position.
     */
    OO.ui.ToggleSwitchWidget = function OoUiToggleSwitchWidget( config ) {
    	// Parent constructor
    	OO.ui.ToggleSwitchWidget.parent.call( this, config );

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

    	// Properties
    	this.dragging = false;
    	this.dragStart = null;
    	this.sliding = false;
    	this.$glow = $( '<span>' );
    	this.$grip = $( '<span>' );

    	// Events
    	this.$element.on( {
    		click: this.onClick.bind( this ),
    		keypress: this.onKeyPress.bind( this )
    	} );

    	// Initialization
    	this.$glow.addClass( 'oo-ui-toggleSwitchWidget-glow' );
    	this.$grip.addClass( 'oo-ui-toggleSwitchWidget-grip' );
    	this.$element
    		.addClass( 'oo-ui-toggleSwitchWidget' )
    		.attr( 'role', 'checkbox' )
    		.append( this.$glow, this.$grip );
    };

    /* Setup */

    OO.inheritClass( OO.ui.ToggleSwitchWidget, OO.ui.ToggleWidget );
    OO.mixinClass( OO.ui.ToggleSwitchWidget, OO.ui.mixin.TabIndexedElement );

    /* Methods */

    /**
     * Handle mouse click events.
     *
     * @private
     * @param {jQuery.Event} e Mouse click event
     */
    OO.ui.ToggleSwitchWidget.prototype.onClick = function ( e ) {
    	if ( !this.isDisabled() && e.which === OO.ui.MouseButtons.LEFT ) {
    		this.setValue( !this.value );
    	}
    	return false;
    };

    /**
     * Handle key press events.
     *
     * @private
     * @param {jQuery.Event} e Key press event
     */
    OO.ui.ToggleSwitchWidget.prototype.onKeyPress = function ( e ) {
    	if ( !this.isDisabled() && ( e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER ) ) {
    		this.setValue( !this.value );
    		return false;
    	}
    };

    /**
     * @inheritdoc
     */
    OO.ui.ToggleSwitchWidget.prototype.setValue = function ( value ) {
    	OO.ui.ToggleSwitchWidget.parent.prototype.setValue.call( this, value );
    	this.$element.attr( 'aria-checked', this.value.toString() );
    	return this;
    };

    /**
     * @inheritdoc
     */
    OO.ui.ToggleSwitchWidget.prototype.simulateLabelClick = function () {
    	if ( !this.isDisabled() ) {
    		this.setValue( !this.value );
    	}
    	this.focus();
    };