<commit_before>/* eslint-disable ember/closure-actions, ember/no-attrs-in-components, ember/no-on-calls-in-components */
import { assert } from '@ember/debug';
import { assign } from '@ember/polyfills';
import Component from '@ember/component';
import diffAttrs from 'ember-diff-attrs';
import { on } from '@ember/object/evented';
import { run } from '@ember/runloop';
import { getOwner } from '@ember/application';

export default Component.extend({
  tagName: 'input',
  type: 'text',
  attributeBindings: ['placeholder', 'type'],
  date: null,
  flatpickrRef: null,

  setupComponent: on('init', function() {
    // Require that users pass a date
    assert('{{ember-flatpickr}} requires a `date` to be passed as the value for flatpickr.', this.get('date') !== undefined);

    // Require that users pass an onChange
    assert('{{ember-flatpickr}} requires an `onChange` action or null for no action.', this.get('onChange') !== undefined);

    // Pass all values and setup flatpickr
    run.scheduleOnce('afterRender', this, function() {
      const fastboot = getOwner(this).lookup('service:fastboot');
      if(fastboot && fastboot.isFastBoot){ return; }
      const options = this.getProperties(Object.keys(this.attrs));

      // Add defaultDate, change and close handlers
      assign(options, {
        defaultDate: this.get('date'),
        onChange: this._onChange.bind(this),
        onClose: this._onClose.bind(this),
        onOpen: this._onOpen.bind(this),
        onReady: this._onReady.bind(this)
      });

      const flatpickrRef = flatpickr(this.element, options);

      if (this.get('appendDataInput')) {
        this.element.setAttribute('data-input', '');
      }

      this._setDisabled(this.get('disabled'));

      this.set('flatpickrRef', flatpickrRef);
    });
  }),

  didReceiveAttrs: diffAttrs('date', 'disabled', 'locale', 'maxDate', 'minDate', function(changedAttrs, ...args) {
    this._super(...args);

    if (changedAttrs && changedAttrs.date) {
      const [oldDate, newDate] = changedAttrs.date;
      if (typeof newDate !== 'undefined' && oldDate !== newDate) {
        this.element._flatpickr.setDate(newDate);
      }
    }

    if (changedAttrs && changedAttrs.disabled) {
      const [oldDisabled, newDisabled] = changedAttrs.disabled;
      if (typeof newDisabled !== 'undefined' && oldDisabled !== newDisabled) {
        this._setDisabled(newDisabled);
      }
    }

    if (changedAttrs && changedAttrs.locale) {
      const [oldLocale, newLocale] = changedAttrs.locale;

      if (oldLocale !== newLocale) {
        this.element._flatpickr.destroy();
        this.setupComponent();
      }
    }

    if (changedAttrs && changedAttrs.maxDate) {
      const [oldMaxDate, newMaxDate] = changedAttrs.maxDate;

      if (oldMaxDate !== newMaxDate) {
        this.element._flatpickr.set('maxDate', newMaxDate);
      }
    }

    if (changedAttrs && changedAttrs.minDate) {
      const [oldMinDate, newMinDate] = changedAttrs.minDate;

      if (oldMinDate !== newMinDate) {
        this.element._flatpickr.set('minDate', newMinDate);
      }
    }
  }),

  willDestroyElement() {
    this.element._flatpickr.destroy();
  },

  /**
   * When the date is changed, update the value and send 'onChange' action
   * @param selectedDates The array of selected dates
   * @param dateStr The string representation of the date, formatted by dateFormat
   * @param instance The flatpickr instance
   * @private
   */
  _onChange(selectedDates, dateStr, instance) {
    this.sendAction('onChange', selectedDates, dateStr, instance);
  },

  /**
   * When the flatpickr is closed, fire the 'onClose' action
   * @param selectedDates The array of selected dates<commit_msg>Refactor didReceiveAttrs logic to remove duplication<commit_after>/* eslint-disable ember/closure-actions, ember/no-attrs-in-components, ember/no-on-calls-in-components */
import { assert } from '@ember/debug';
import { assign } from '@ember/polyfills';
import Component from '@ember/component';
import diffAttrs from 'ember-diff-attrs';
import { on } from '@ember/object/evented';
import { run } from '@ember/runloop';
import { getOwner } from '@ember/application';

export default Component.extend({
  tagName: 'input',
  type: 'text',
  attributeBindings: ['placeholder', 'type'],
  date: null,
  flatpickrRef: null,

  setupComponent: on('init', function() {
    // Require that users pass a date
    assert('{{ember-flatpickr}} requires a `date` to be passed as the value for flatpickr.', this.get('date') !== undefined);

    // Require that users pass an onChange
    assert('{{ember-flatpickr}} requires an `onChange` action or null for no action.', this.get('onChange') !== undefined);

    // Pass all values and setup flatpickr
    run.scheduleOnce('afterRender', this, function() {
      const fastboot = getOwner(this).lookup('service:fastboot');
      if (fastboot && fastboot.isFastBoot) {
        return;
      }
      const options = this.getProperties(Object.keys(this.attrs));

      // Add defaultDate, change and close handlers
      assign(options, {
        defaultDate: this.get('date'),
        onChange: this._onChange.bind(this),
        onClose: this._onClose.bind(this),
        onOpen: this._onOpen.bind(this),
        onReady: this._onReady.bind(this)
      });

      const flatpickrRef = flatpickr(this.element, options);

      if (this.get('appendDataInput')) {
        this.element.setAttribute('data-input', '');
      }

      this._setDisabled(this.get('disabled'));

      this.set('flatpickrRef', flatpickrRef);
    });
  }),

  didReceiveAttrs: diffAttrs('date', 'disabled', 'locale', 'maxDate', 'minDate', function(changedAttrs, ...args) {
    this._super(...args);

    this._attributeHasChanged(changedAttrs, 'date', (newDate) => {
      if (typeof newDate !== 'undefined') {
        this.element._flatpickr.setDate(newDate);
      }
    });

    this._attributeHasChanged(changedAttrs, 'disabled', (newDisabled) => {
      if (typeof newDisabled !== 'undefined') {
        this._setDisabled(newDisabled);
      }
    });

    this._attributeHasChanged(changedAttrs, 'locale', () => {
      this.element._flatpickr.destroy();
      this.setupComponent();
    });

    this._attributeHasChanged(changedAttrs, 'maxDate', (newMaxDate) => {
      this.element._flatpickr.set('maxDate', newMaxDate);
    });

    this._attributeHasChanged(changedAttrs, 'minDate', (newMinDate) => {
      this.element._flatpickr.set('minDate', newMinDate);
    });
  }),

  willDestroyElement() {
    this.element._flatpickr.destroy();
  },

  /**
   * Check if the attr has changed, and if so call the callback with the new value
   * @param {object} changedAttrs The object with keys denoting attrs that have changed
   * @param {string} attr The string of which attr to check for changes
   * @param {function} callback A function to call with the newAttr value
   * @private
   */
  _attributeHasChanged(changedAttrs, attr, callback) {
    if (changedAttrs && changedAttrs[attr]) {
      const [oldAttr, newAttr] = changedAttrs[attr];
      if (oldAttr !== newAttr) {
        callback(newAttr);
      }
    }
  },

  /**
   * When the date is changed, update the value and send 'onChange' action
   * @param selectedDates The array of selected dates
   * @param dateStr The string representation of the date, formatted by dateFormat
   * @param instance The flatpickr instance
   * @private
   */
  _onChange(selectedDates, dateStr, instance) {
    this.sendAction('onChange', selectedDates, dateStr, instance);
  },

  /**
   * When the flatpickr is closed, fire the 'onClose' action
   * @param selectedDates The array of selected dates
{'fraction_non_alphanumeric': 0.13693811074918566, 'fraction_numerical': 0.0, 'mean_word_length': 3.391304347826087, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '1347114', 'n_tokens_mistral': 2302, 'n_tokens_neox': 2258, 'n_words': 571}