all files / src/src/mixins/ RequiredElement.js

78.13% Statements 25/32
58.62% Branches 17/29
80% Functions 4/5
78.13% Lines 25/32
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                                  21819×     21819× 21819× 21819× 21819×         21819×                                                                   1065× 1065×     1065×                 86×                   21820× 20755×     1065× 1065× 1065×   753× 753× 367×     1065×    
/**
 * RequiredElement is mixed into other classes to provide a `required` attribute.
 *
 * @abstract
 * @class
 *
 * @constructor
 * @param {Object} [config] Configuration options
 * @param {jQuery} [config.$required] The element to which the `required` attribute is applied.
 *  If this config is omitted, the required functionality is applied to $input if it
 *  exists, or $element if it doesn't.
 * @param {boolean} [config.required=false] Mark the field as required with `true`.
 * @param {OO.ui.Element} [config.indicatorElement=this] Element which mixes in OO.ui.mixin.IndicatorElement
 *  Will set the indicator on that element to 'required' when the element is required.
 *  Note that `false` & setting `indicator: 'required'` will result in no indicator shown.
 */
OO.ui.mixin.RequiredElement = function OoUiMixinRequiredElement( config ) {
	// Configuration initialization
	config = config || {};
 
	// Properties
	this.$required = config.$required || this.$input || this.$element;
	this.required = false;
	this.indicatorElement = config.indicatorElement !== undefined ? config.indicatorElement : this;
	Iif ( this.indicatorElement && !this.indicatorElement.getIndicator ) {
		throw new Error( 'config.indicatorElement must mixin OO.ui.mixin.IndicatorElement.' );
	}
 
	// Initialization
	this.setRequired( !!config.required );
};
 
/* Setup */
 
OO.initClass( OO.ui.mixin.RequiredElement );
 
/* Methods */
 
/**
 * Set the element which can take the required attribute.
 *
 * This method is used to retarget a RequiredElement mixin so that its functionality applies to the
 * specified element.
 * If an element is already set, the mixin’s effect on that element is removed before the new
 * element is set up.
 *
 * @param {jQuery} $required Element that should use the 'required' functionality
 */
OO.ui.mixin.RequiredElement.prototype.setRequiredElement = function ( $required ) {
	if ( this.$required === $required ) {
		return;
	}
 
	if ( this.$required && this.required ) {
		this.updateRequiredElement( false );
	}
 
	this.$required = $required;
	this.updateRequiredElement();
};
 
/**
 * @private
 * @param {boolean} [state]
 */
OO.ui.mixin.RequiredElement.prototype.updateRequiredElement = function ( state ) {
	Eif ( state === undefined ) {
		state = this.required;
	}
 
	this.$required
		.prop( 'required', state );
};
 
/**
 * Check if the input is {@link OO.ui.mixin.RequiredElement#required required}.
 *
 * @return {boolean}
 */
OO.ui.mixin.RequiredElement.prototype.isRequired = function () {
	return this.required;
};
 
/**
 * Set the {@link OO.ui.mixin.RequiredElement#required required} state of the input.
 *
 * @param {boolean} state Make input required
 * @chainable
 * @return {OO.ui.Widget} The widget, for chaining
 */
OO.ui.mixin.RequiredElement.prototype.setRequired = function ( state ) {
	if ( this.required === state ) {
		return this;
	}
 
	this.required = !!state;
	this.updateRequiredElement();
	if ( this.indicatorElement ) {
		// Make sure to not destroy other, unrelated indicators
		const expected = state ? null : 'required';
		if ( this.indicatorElement.getIndicator() === expected ) {
			this.indicatorElement.setIndicator( state ? 'required' : null );
		}
	}
	return this;
};