<commit_before>import Ember from 'ember';
import ComponentVaidation from 'ember-validation/mixins/component';
import layout from '../templates/components/form-validation';

const { RSVP, get } = Ember;

/**
 * @module
 * @augments ember/Component
 * @augments ember-validation/mixins/component
 */
export default Ember.Component.extend(ComponentVaidation, {

  validationScheme: {},

  layout: layout,

  /** @type {String} */
  tagName: 'form',

  /** @type {Boolean} */
  isValidating: false,

  /** @type {Boolean} */
  isSubmitting: false,

  /** @type {Boolean} */
  isSubmitted: false,

  /** @type {Boolean} */
  submitError: '',

  /**
   * Focus first invalid input after validaton failed
   * @type {Boolean}
   */
  focusFirstInvalid: true,

  /**
   * Validate all form on submit
   *
   * @function
   * @param {jQuery.Event} event
   * @returns {undefined}
   */
  submit(event) {

    event.preventDefault();

    this.setProperties({
      submitError: '',
      isSubmitted: false,
      isValidating: true
    });

    this.validationStart()
      .then(() => {
        this.validate()
          .then(this.validationPassed.bind(this), this.validationFailed.bind(this));
      })
      .finally(this.validationEnd.bind(this));

  },

  /**
   * Called before validation starts.
   * If returned promise rejected - validation will not starts
   *
   * @function
   * @returns {Ember/RSVP.resolve}
   */
  validationStart: () => RSVP.resolve(),

  /**
   * Called after validation was passed
   * Call given `action`
   *
   * @function
   * @returns {undefined}
   */
  validationPassed() {
    this.hideAllErrors();
    this.submitStart();
    this.attrs.action(this.submitDone.bind(this), this.submitFailed.bind(this));
  },

  /**
   * Called after validation was failed
   *
   * @function
   * @returns {undefined}
   */
  validationFailed(/*errors*/) {
    this.showAllErrors();
    Ember.run.scheduleOnce('afterRender', this, () => {
      if (this.get('focusFirstInvalid')) {
        let mediator = this.get('mediators').find((mediator) => {
          let name = get(mediator, 'errorsName');

          if (name && this.get('errors.' + name)) {
            return true;
          }
        });

        if (mediator) {
          const view = get(mediator, 'view');

          if (typeof view.focus === 'function') {
            view.focus();
          } else {
            view.element.focus();
          }

          this.set('visibleErrors.' + get(mediator, 'errorsName'), true);
        }

      }
    });
  },

  /**
   * Called at the end of validation
   *
   * @function
   * @returns {undefined}
   */
  validationEnd() {
    this.set('isValidating', false);
  },

  submitStart() {
    this.set('isSubmitting', true);
  },

  /**
   * Called when submitting done
   *
   * @function
   * @returns {undefined}
   */
  submitDone() {
    this.set('isSubmitted', true);
    this.submitEnd();
  },

  /**
   * Called when submitting failed
   *
   * @function
   * @returns {undefined}
   */
  submitFailed(error) {
    this.set('submitError', error);
    this.submitEnd();
  },

  /**
   * Called after submitting complete
   *
   * @function
   * @returns {undefined}
   */
  submitEnd() {
    this.set('isSubmitting', false);
  },

  /**
   * Make all errors visible
   *
   * @function
   * @returns {undefined}
   */
  showAllErrors() {
    this.get('mediators').forEach((mediator) => {
<<<<<<< HEAD
      let errorsName = Ember.get(mediator, 'errorsName');
      errorsName && this.set('visibleErrors.' + errorsName, true);
=======
      let name = get(mediator, 'errorsName');
      name && this.set('visibleErrors.' + name, true);
>>>>>>> form-validation.focusFirstInvalid was added
    });
  },

  /**
   * Make all errors hidden
   *
   * @function
   * @returns {undefined}
   */
  hideAllErrors() {
<<<<<<< HEAD
    Object.keys(this.get('visibleErrors')).forEach((errorsName) => {
      this.set('visibleErrors.' + errorsName, true);
=======
    Ember.keys(this.get('visibleErrors')).forEach((name) => {
      this.set('visibleErrors.' + name, true);
>>>>>>> form-validation.focusFirstInvalid was added
    });
  },

  /**
   * Check `action` argument was given
   *
   * @function
   * @returns {undefined}
   */
  didReceiveAttrs() {
    Ember.assert(`You must provide an \`action\` action to \`form-validation\`.`, typeof this.attrs.action === 'function');
  },

  didInsertElement() {
    this._super(...arguments);

    this.$().on('focusin', () => { this.set('submitError', ''); });

    this.$().on('change keyup', 'input, textarea, select', () => { this.set('isSubmitted', false); });<commit_msg>conflict resolved<commit_after>import Ember from 'ember';
import ComponentVaidation from 'ember-validation/mixins/component';
import layout from '../templates/components/form-validation';

const { RSVP, get } = Ember;

/**
 * @module
 * @augments ember/Component
 * @augments ember-validation/mixins/component
 */
export default Ember.Component.extend(ComponentVaidation, {

  validationScheme: {},

  layout: layout,

  /** @type {String} */
  tagName: 'form',

  /** @type {Boolean} */
  isValidating: false,

  /** @type {Boolean} */
  isSubmitting: false,

  /** @type {Boolean} */
  isSubmitted: false,

  /** @type {Boolean} */
  submitError: '',

  /**
   * Focus first invalid input after validaton failed
   * @type {Boolean}
   */
  focusFirstInvalid: true,

  /**
   * Validate all form on submit
   *
   * @function
   * @param {jQuery.Event} event
   * @returns {undefined}
   */
  submit(event) {

    event.preventDefault();

    this.setProperties({
      submitError: '',
      isSubmitted: false,
      isValidating: true
    });

    this.validationStart()
      .then(() => {
        this.validate()
          .then(this.validationPassed.bind(this), this.validationFailed.bind(this));
      })
      .finally(this.validationEnd.bind(this));

  },

  /**
   * Called before validation starts.
   * If returned promise rejected - validation will not starts
   *
   * @function
   * @returns {Ember/RSVP.resolve}
   */
  validationStart: () => RSVP.resolve(),

  /**
   * Called after validation was passed
   * Call given `action`
   *
   * @function
   * @returns {undefined}
   */
  validationPassed() {
    this.hideAllErrors();
    this.submitStart();
    this.attrs.action(this.submitDone.bind(this), this.submitFailed.bind(this));
  },

  /**
   * Called after validation was failed
   *
   * @function
   * @returns {undefined}
   */
  validationFailed(/*errors*/) {
    this.showAllErrors();
    Ember.run.scheduleOnce('afterRender', this, () => {
      if (this.get('focusFirstInvalid')) {
        let mediator = this.get('mediators').find((mediator) => {
          let name = get(mediator, 'errorsName');

          if (name && this.get('errors.' + name)) {
            return true;
          }
        });

        if (mediator) {
          const view = get(mediator, 'view');

          if (typeof view.focus === 'function') {
            view.focus();
          } else {
            view.element.focus();
          }

          this.set('visibleErrors.' + get(mediator, 'errorsName'), true);
        }

      }
    });
  },

  /**
   * Called at the end of validation
   *
   * @function
   * @returns {undefined}
   */
  validationEnd() {
    this.set('isValidating', false);
  },

  submitStart() {
    this.set('isSubmitting', true);
  },

  /**
   * Called when submitting done
   *
   * @function
   * @returns {undefined}
   */
  submitDone() {
    this.set('isSubmitted', true);
    this.submitEnd();
  },

  /**
   * Called when submitting failed
   *
   * @function
   * @returns {undefined}
   */
  submitFailed(error) {
    this.set('submitError', error);
    this.submitEnd();
  },

  /**
   * Called after submitting complete
   *
   * @function
   * @returns {undefined}
   */
  submitEnd() {
    this.set('isSubmitting', false);
  },

  /**
   * Make all errors visible
   *
   * @function
   * @returns {undefined}
   */
  showAllErrors() {
    this.get('mediators').forEach((mediator) => {
      let name = get(mediator, 'errorsName');
      name && this.set('visibleErrors.' + name, true);
    });
  },

  /**
   * Make all errors hidden
   *
   * @function
   * @returns {undefined}
   */
  hideAllErrors() {
    Object.keys(this.get('visibleErrors')).forEach((errorsName) => {
      this.set('visibleErrors.' + errorsName, true);
    });
  },

  /**
   * Check `action` argument was given
   *
   * @function
   * @returns {undefined}
   */
  didReceiveAttrs() {
    Ember.assert(`You must provide an \`action\` action to \`form-validation\`.`, typeof this.attrs.action === 'function');
  },

  didInsertElement() {
    this._super(...arguments);

    this.$().on('focusin', () => { this.set('submitError', ''); });

    this.$().on('change keyup', 'input, textarea, select', () => { this.set('isSubmitted', false); });
{'fraction_non_alphanumeric': 0.17193808882907133, 'fraction_numerical': 0.0, 'mean_word_length': 2.5725160256410255, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 34, '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': '2473000', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2741, 'n_words': 553}