<commit_before>import Ember from 'ember';

const { Mixin, computed, run, inject: { service } } = Ember;

export default Mixin.create({
  eventDispatcher: service('lf-event-dispatcher'),

  classNames: ['form-group'],
  classNameBindings: ['validationState'],
  name: null, //passed in
  property: null, //passed in

  id: computed('inputId', function() {
    return this.get('inputId') || `ember${Ember.uuid()}`;
  }),

  /**
   * This property determines whether errors should be displayed
   *
   * @param validationStateVisible
   * @type {boolean}
   */
  validationStateVisible: false,

  /**
   * This property determines whether input is valid. It's used in computing validationState
   *
   * @param valid
   * @type {boolean}
   */
  valid: null,

  /**
   * Tracks the value of the input field
   *
   * We have to keep the input value to validate it after each change - we can't
   * observe property since we're not using two-way data binding

   * @param _value
   * @type {String}
   */
  _value: null,

  /**
   * This property determines whether field has been edited. It should be set
   * in child classes eg. when focusOut/blur event is triggered
   *
   * @param _edited
   * @type {boolean}
   */
  _edited: false,

  /**
   * This property is used to compute which class should the wrapping form-group element use.
   * Standard bootstrap classes are used: has-success or has-error
   *
   * @param validationState
   * @type {String}
   */
  validationState: computed('valid', 'validationStateVisible', function() {
    if (!this.get('validationStateVisible')) { return ''; }
    if (!this.get('valid')) { return 'has-error'; }
    return 'has-success';
  }),

  focusOut() {
    this._super(...arguments);
    this.executeValidate();
  },

  init() {
    this._super(...arguments);
    this.get('eventDispatcher').on('lf-forceValidate', this, this.onForceValidate);
  },

  /**
   * Trigger validation on didInsertElement so that the fields property gets
   * populated and the isValid property gets set properly
   */
  didInsertElement() {
    this._super(...arguments);
    run.schedule("afterRender", () => {
      if (!this.get('validate')) {
        return this.set('valid', true);
      }

      this.get('validate')(this.get('name'), this.get('property'));
    });
  },

  willDestroyElement() {
    this._super(...arguments);
    this.get('eventDispatcher').off('lf-forceValidate', this, this.onForceValidate);
  },

  executeValidate() {
    this.set('_edited', true);
    this.validateField(this.get('property'));
    this.showValidationState();
  },

  /**
   * Convenience function for validating fields. It always marks fields as valid
   * when no rules are found or no validation function is passed. Otherwise it runs
   * the function passed in as "validate" attribute and sets valid and errorMessages appropriately.
   *
   * @param {String} value
   */
  validateField(value) {
    // no validations - field always valid
    if (!this.get('validate')) { return this.set('valid', true); }
    let { isValid, messages, noRules } = this.get('validate')(this.get('name'), value);
    if (noRules) { return this.set('valid', true); }

    this.setProperties({
      'valid': isValid,
      'errorMessages': messages
    });
  },

  /**
   * Call update hook if it was passed.
   *
   * @param {String} value
   */
  callUpdateHook(value) {
    if (this.get('on-update')) {
      this.get('on-update')(value);
    }
  },

  /**
   * Clear all validations. It removes _edited state and clears all errorMessages.
   */
  clearValidations() {
    this.validateField(null);
    this.setProperties({
      '_edited': false,
      'errorMessages': [],
      validationStateVisible: false
    });
  },

  /**
   * Setter for validationStateVisible
   */
  showValidationState() {
    this.set('validationStateVisible', true);
  },

  hideValidationState() {
    this.set('validationStateVisible', false);
  },

  /**
   * 'lf-forceValidate' Event handler
   */
  onForceValidate() {
    this.executeValidate();
  },
});<commit_msg>LFInput mixin: executeValidate() method accepting boolean specifying whether to show validation state of all fields, + onForceValidate() event handler passing it.<commit_after>import Ember from 'ember';

const { Mixin, computed, run, inject: { service } } = Ember;

export default Mixin.create({
  eventDispatcher: service('lf-event-dispatcher'),

  classNames: ['form-group'],
  classNameBindings: ['validationState'],
  name: null, //passed in
  property: null, //passed in

  id: computed('inputId', function() {
    return this.get('inputId') || `ember${Ember.uuid()}`;
  }),

  /**
   * This property determines whether errors should be displayed
   *
   * @param validationStateVisible
   * @type {boolean}
   */
  validationStateVisible: false,

  /**
   * This property determines whether input is valid. It's used in computing validationState
   *
   * @param valid
   * @type {boolean}
   */
  valid: null,

  /**
   * Tracks the value of the input field
   *
   * We have to keep the input value to validate it after each change - we can't
   * observe property since we're not using two-way data binding

   * @param _value
   * @type {String}
   */
  _value: null,

  /**
   * This property determines whether field has been edited. It should be set
   * in child classes eg. when focusOut/blur event is triggered
   *
   * @param _edited
   * @type {boolean}
   */
  _edited: false,

  /**
   * This property is used to compute which class should the wrapping form-group element use.
   * Standard bootstrap classes are used: has-success or has-error
   *
   * @param validationState
   * @type {String}
   */
  validationState: computed('valid', 'validationStateVisible', function() {
    if (!this.get('validationStateVisible')) { return ''; }
    if (!this.get('valid')) { return 'has-error'; }
    return 'has-success';
  }),

  focusOut() {
    this._super(...arguments);
    this.executeValidate();
  },

  init() {
    this._super(...arguments);
    this.get('eventDispatcher').on('lf-forceValidate', this, this.onForceValidate);
  },

  /**
   * Trigger validation on didInsertElement so that the fields property gets
   * populated and the isValid property gets set properly
   */
  didInsertElement() {
    this._super(...arguments);
    run.schedule("afterRender", () => {
      if (!this.get('validate')) {
        return this.set('valid', true);
      }

      this.get('validate')(this.get('name'), this.get('property'));
    });
  },

  willDestroyElement() {
    this._super(...arguments);
    this.get('eventDispatcher').off('lf-forceValidate', this, this.onForceValidate);
  },

  executeValidate(showValidationState = true) {
    this.set('_edited', true);
    this.validateField(this.get('property'));
    if (showValidationState) {
      this.showValidationState();
    }
  },

  /**
   * Convenience function for validating fields. It always marks fields as valid
   * when no rules are found or no validation function is passed. Otherwise it runs
   * the function passed in as "validate" attribute and sets valid and errorMessages appropriately.
   *
   * @param {String} value
   */
  validateField(value) {
    // no validations - field always valid
    if (!this.get('validate')) { return this.set('valid', true); }
    let { isValid, messages, noRules } = this.get('validate')(this.get('name'), value);
    if (noRules) { return this.set('valid', true); }

    this.setProperties({
      'valid': isValid,
      'errorMessages': messages
    });
  },

  /**
   * Call update hook if it was passed.
   *
   * @param {String} value
   */
  callUpdateHook(value) {
    if (this.get('on-update')) {
      this.get('on-update')(value);
    }
  },

  /**
   * Clear all validations. It removes _edited state and clears all errorMessages.
   */
  clearValidations() {
    this.validateField(null);
    this.setProperties({
      '_edited': false,
      'errorMessages': [],
      validationStateVisible: false
    });
  },

  /**
   * Setter for validationStateVisible
   */
  showValidationState() {
    this.set('validationStateVisible', true);
  },

  hideValidationState() {
    this.set('validationStateVisible', false);
  },

  /**
   * 'lf-forceValidate' Event handler
   */
  onForceValidate(showValidationState) {
    this.executeValidate(showValidationState);
  },
});
{'fraction_non_alphanumeric': 0.14031526152376403, 'fraction_numerical': 0.0, 'mean_word_length': 3.133761105626851, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2204722', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2433, 'n_words': 682}