<commit_before>/**
 * @module components
 *
 */
import Ember from 'ember';
import layout from '../templates/components/torpid-validate';

/**
 * `Torpid/Component/TorpidValidate`
 *
 */
export default Ember.Component.extend(
{
	layout: layout,

	classNames: ['torpid-validate'],
	classNameBindings: ['invalid:invalid', 'isValid:valid'],

	type: 'text',
	placeholder: '',
	label: '',
	hint: '',
	error: '',
	value: '',
	invalidError: 'The input is invalid',
	requiredError: 'This is a required field',

	autocomplete: null,
	tabindex: 0,
	maxlength: "",

	required: false,

	disabled: false,

	isInvalid: false,
	isValid: false,
	isRequired: false,

	autofocus : false,
	showLabel: true,

	validateExpression: /.*/,

	labelString: function()
	{
		var label = this.get('label');
		if(Ember.isNone(label) || Ember.isEmpty(label))
		{
			label = this.get('placeholder');
		}

		if(this.get('required'))
		{
			label = label + '*';
		}

		return label;
	}.property('placeholder', 'label', 'required'),

	init: function()
	{
		this._super();

		Ember.assert('error is a private variable use invalidError or requiredError for error messages - ' + this.get('error'), Ember.isEmpty(this.get('error')));
	},

	invalid: function()
	{
		var invalid = false;
		if(this.get('isInvalid'))
		{
			invalid = true;
			this.set('error', this.get('invalidError'));
		}
		else if(this.get('isRequired'))
		{
			invalid = true;
			this.set('error', this.get('requiredError'));
		}
		else
		{
			this.set('error', '');
		}

		return invalid;
	}.property('isInvalid', 'isRequired'),

	validate: function(value) //jshint ignore:line
	{
		if(Ember.isEmpty(value) && this.get('required'))
		{
			this.set('isRequired', true);
		}
		else
		{
			this.set('isRequired', false);
		}

		var exp = this.get('validateExpression');
			exp = typeof exp === 'string' ? new RegExp(exp) : exp;

		return exp.test(value);
	},

	checkIfValid: function(value)
	{
		var valid = this.validate(value);

		this.set('isValid', valid);
		this.set('isInvalid', !valid);
	},

	reset: function()
	{<commit_msg>fixes to torpid-validate error<commit_after>/**
 * @module components
 *
 */
import Ember from 'ember';
import layout from '../templates/components/torpid-validate';

/**
 * `Torpid/Component/TorpidValidate`
 *
 */
export default Ember.Component.extend(
{
	layout: layout,

	classNames: ['torpid-validate'],
	classNameBindings: ['invalid:invalid', 'isValid:valid'],

	type: 'text',
	placeholder: '',
	label: '',
	hint: '',
	error: '',
	value: '',
	invalidError: 'The input is invalid',
	requiredError: 'This is a required field',

	autocomplete: null,
	tabindex: 0,
	maxlength: "",

	required: false,

	disabled: false,

	isInvalid: false,
	isValid: false,
	isRequired: false,

	autofocus : false,
	showLabel: true,

	validateExpression: /.*/,

	labelString: function()
	{
		var label = this.get('label');
		if(Ember.isNone(label) || Ember.isEmpty(label))
		{
			label = this.get('placeholder');
		}

		if(this.get('required'))
		{
			label = label + '*';
		}

		return label;
	}.property('placeholder', 'label', 'required'),

	init: function()
	{
		this._super();

		Ember.assert('error is a private variable use invalidError or requiredError for error messages - ' + this.get('error'), Ember.isEmpty(this.get('error')));
	},

	invalid: function()
	{
		var invalid = false;
		if(this.get('isInvalid'))
		{
			invalid = true;
			this.set('error', this.get('invalidError'));
		}
		else if(this.get('isRequired'))
		{
			invalid = true;
			this.set('error', this.get('requiredError'));
		}
		else
		{
			this.set('error', '');
		}

		return invalid;
	}.property('isInvalid', 'isRequired', 'invalidError', 'requiredError'),

	validate: function(value) //jshint ignore:line
	{
		if(Ember.isEmpty(value) && this.get('required'))
		{
			this.set('isRequired', true);
		}
		else
		{
			this.set('isRequired', false);
		}

		var exp = this.get('validateExpression');
			exp = typeof exp === 'string' ? new RegExp(exp) : exp;

		return exp.test(value);
	},

	checkIfValid: function(value)
	{
		var valid = this.validate(value);

		this.set('isValid', valid);
		this.set('isInvalid', !valid);
	},

	reset: function()
	{
{'fraction_non_alphanumeric': 0.18114203454894434, 'fraction_numerical': 0.0004798464491362764, 'mean_word_length': 3.945432977461447, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4133367', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1417, 'n_words': 305}