# all files / src/widgets/ CheckboxInputWidget.js

78.05% Statements 32/41
66.67% Branches 12/18
66.67% Functions 6/9
78.05% Lines 32/41

    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

    1×

    1944×

    1944×

    1944×

    1944×

    1×

    1×

    1×
    712×
    712×
    712×

    1×
    1232×

    1×

    1×
    4024×
    4024×
    1944×
    1944×
    1944×

    4024×
    1944×
    1944×

    4024×

    1×

    1232×
    1232×

    1232×

    1×

    1×
    712×
    712×

     /**
     * CheckboxInputWidgets, like HTML checkboxes, can be selected and/or configured with a value.
     * Note that these {@link OO.ui.InputWidget input widgets} are best laid out
     * in {@link OO.ui.FieldLayout field layouts} that use the {@link OO.ui.FieldLayout#align inline}
     * alignment. For more information, please see the [OOUI documentation on MediaWiki][1].
     *
     * This widget can be used inside an HTML form, such as a OO.ui.FormLayout.
     *
     *     @example
     *     // An example of selected, unselected, and disabled checkbox inputs
     *     var checkbox1=new OO.ui.CheckboxInputWidget( {
     *          value: 'a',
     *          selected: true
     *     } );
     *     var checkbox2=new OO.ui.CheckboxInputWidget( {
     *         value: 'b'
     *     } );
     *     var checkbox3=new OO.ui.CheckboxInputWidget( {
     *         value:'c',
     *         disabled: true
     *     } );
     *     // Create a fieldset layout with fields for each checkbox.
     *     var fieldset = new OO.ui.FieldsetLayout( {
     *         label: 'Checkboxes'
     *     } );
     *     fieldset.addItems( [
     *         new OO.ui.FieldLayout( checkbox1, { label: 'Selected checkbox', align: 'inline' } ),
     *         new OO.ui.FieldLayout( checkbox2, { label: 'Unselected checkbox', align: 'inline' } ),
     *         new OO.ui.FieldLayout( checkbox3, { label: 'Disabled checkbox', align: 'inline' } ),
     *     ] );
     *     $( 'body' ).append( fieldset.$element );
     *
     * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets/Inputs
     *
     * @class
     * @extends OO.ui.InputWidget
     *
     * @constructor
     * @param {Object} [config] Configuration options
     * @cfg {boolean} [selected=false] Select the checkbox initially. By default, the checkbox is not selected.
     */
    OO.ui.CheckboxInputWidget = function OoUiCheckboxInputWidget( config ) {
    	// Configuration initialization
    	config = config || {};

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

    	// Initialization
    	this.$element
    		.addClass( 'oo-ui-checkboxInputWidget' )
    		// Required for pretty styling in WikimediaUI theme
    		.append( $( '<span>' ) );
    	this.setSelected( config.selected !== undefined ? config.selected : false );
    };

    /* Setup */

    OO.inheritClass( OO.ui.CheckboxInputWidget, OO.ui.InputWidget );

    /* Static Properties */

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

    /* Static Methods */

    /**
     * @inheritdoc
     */
    OO.ui.CheckboxInputWidget.static.gatherPreInfuseState = function ( node, config ) {
    	var state = OO.ui.CheckboxInputWidget.parent.static.gatherPreInfuseState( node, config );
    	state.checked = config.$input.prop( 'checked' );
    	return state;
    };

    /* Methods */

    /**
     * @inheritdoc
     * @protected
     */
    OO.ui.CheckboxInputWidget.prototype.getInputElement = function () {
    	return $( '<input>' ).attr( 'type', 'checkbox' );
    };

    /**
     * @inheritdoc
     */
    OO.ui.CheckboxInputWidget.prototype.onEdit = function () {
    	var widget = this;
    	if ( !this.isDisabled() ) {
    		// Allow the stack to clear so the value will be updated
    		setTimeout( function () {
    			widget.setSelected( widget.$input.prop( 'checked' ) );
    		} );
    	}
    };

    /**
     * Set selection state of this checkbox.
     *
     * @param {boolean} state `true` for selected
     * @chainable
     */
    OO.ui.CheckboxInputWidget.prototype.setSelected = function ( state ) {
    	state = !!state;
    	if ( this.selected !== state ) {
    		this.selected = state;
    		this.$input.prop( 'checked', this.selected );
    		this.emit( 'change', this.selected );
    	}
    	// The first time that the selection state is set (probably while constructing the widget),
    	// remember it in defaultSelected. This property can be later used to check whether
    	// the selection state of the input has been changed since it was created.
    	if ( this.defaultSelected === undefined ) {
    		this.defaultSelected = this.selected;
    		this.$input[ 0 ].defaultChecked = this.defaultSelected;
    	}
    	return this;
    };

    /**
     * Check if this checkbox is selected.
     *
     * @return {boolean} Checkbox is selected
     */
    OO.ui.CheckboxInputWidget.prototype.isSelected = function () {
    	// Resynchronize our internal data with DOM data. Other scripts executing on the page can modify
    	// it, and we won't know unless they're kind enough to trigger a 'change' event.
    	var selected = this.$input.prop( 'checked' );
    	Iif ( this.selected !== selected ) {
    		this.setSelected( selected );
    	}
    	return this.selected;
    };

    /**
     * @inheritdoc
     */
    OO.ui.CheckboxInputWidget.prototype.simulateLabelClick = function () {
    	if ( !this.isDisabled() ) {
    		this.$input.click();
    	}
    	this.focus();
    };

    /**
     * @inheritdoc
     */
    OO.ui.CheckboxInputWidget.prototype.restorePreInfuseState = function ( state ) {
    	OO.ui.CheckboxInputWidget.parent.prototype.restorePreInfuseState.call( this, state );
    	Iif ( state.checked !== undefined && state.checked !== this.isSelected() ) {
    		this.setSelected( state.checked );
    	}
    };