# all files / src/widgets/ LabelWidget.js

82.35% Statements 14/17
83.33% Branches 5/6
50% Functions 1/2
82.35% Lines 14/17

    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

    1×

    2002×

    2002×

    2002×
    2002×

    2002×

    2002×
    80×
    80×

    2002×

    1×
    1×
    1×

    1×
     /**
     * LabelWidgets help identify the function of interface elements. Each LabelWidget can
     * be configured with a `label` option that is set to a string, a label node, or a function:
     *
     * - String: a plaintext string
     * - jQuery selection: a jQuery selection, used for anything other than a plaintext label, e.g., a
     *   label that includes a link or special styling, such as a gray color or additional graphical elements.
     * - Function: a function that will produce a string in the future. Functions are used
     *   in cases where the value of the label is not currently defined.
     *
     * In addition, the LabelWidget can be associated with an {@link OO.ui.InputWidget input widget}, which
     * will come into focus when the label is clicked.
     *
     *     @example
     *     // Examples of LabelWidgets
     *     var label1 = new OO.ui.LabelWidget( {
     *         label: 'plaintext label'
     *     } );
     *     var label2 = new OO.ui.LabelWidget( {
     *         label: $( '<a href="default.html">jQuery label</a>' )
     *     } );
     *     // Create a fieldset layout with fields for each example
     *     var fieldset = new OO.ui.FieldsetLayout();
     *     fieldset.addItems( [
     *         new OO.ui.FieldLayout( label1 ),
     *         new OO.ui.FieldLayout( label2 )
     *     ] );
     *     $( 'body' ).append( fieldset.$element );
     *
     * @class
     * @extends OO.ui.Widget
     * @mixins OO.ui.mixin.LabelElement
     * @mixins OO.ui.mixin.TitledElement
     *
     * @constructor
     * @param {Object} [config] Configuration options
     * @cfg {OO.ui.InputWidget} [input] {@link OO.ui.InputWidget Input widget} that uses the label.
     *  Clicking the label will focus the specified input field.
     */
    OO.ui.LabelWidget = function OoUiLabelWidget( config ) {
    	// Configuration initialization
    	config = config || {};

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

    	// Mixin constructors
    	OO.ui.mixin.LabelElement.call( this, $.extend( {}, config, { $label: this.$element } ) );
    	OO.ui.mixin.TitledElement.call( this, config );

    	// Properties
    	this.input = config.input;

    	// Initialization
    	if ( this.input ) {
    		Eif ( this.input.getInputId() ) {
    			this.$element.attr( 'for', this.input.getInputId() );
    		} else {
    			this.$label.on( 'click', function () {
    				this.input.simulateLabelClick();
    				return false;
    			}.bind( this ) );
    		}
    	}
    	this.$element.addClass( 'oo-ui-labelWidget' );
    };

    /* Setup */

    OO.inheritClass( OO.ui.LabelWidget, OO.ui.Widget );
    OO.mixinClass( OO.ui.LabelWidget, OO.ui.mixin.LabelElement );
    OO.mixinClass( OO.ui.LabelWidget, OO.ui.mixin.TitledElement );

    /* Static Properties */

    /**
     * @static
     * @inheritdoc
     */
    OO.ui.LabelWidget.static.tagName = 'label';