<commit_before>import { DatePickerModes } from './conts.js';
import { DateTime, Unit } from './datetime';
import { TempusDominus } from './tempus-dominus';
import Collapse from './display/collapse';
import Namespace from './namespace';

/**
 *
 */
export default class Actions {
  private _context: TempusDominus;
  private collapse: Collapse;

  constructor(context: TempusDominus) {
    this._context = context;
    this.collapse = new Collapse();
  }

  /**
   * Performs the selected `action`. See ActionTypes
   * @param e This is normally a click event
   * @param action If not provided, then look for a [data-action]
   */
  do(e: any, action?: ActionTypes) {
    const currentTarget = e.currentTarget;
    if (currentTarget.classList.contains(Namespace.css.disabled)) return false;
    action = action || currentTarget.dataset.action;
    const lastPicked = (
      this._context.dates.lastPicked || this._context._viewDate
    ).clone;

    /**
     * Common function to manipulate {@link lastPicked} by `unit`
     * @param unit
     * @param value Value to change by
     */
    const manipulateAndSet = (unit: Unit, value = 1) => {
      const newDate = lastPicked.manipulate(value, unit);
      if (this._context._validation.isValid(newDate, unit)) {
        this._context.dates._setValue(
          newDate,
          this._context.dates.lastPickedIndex
        );
      }
    };

    switch (action) {
      case ActionTypes.next:
      case ActionTypes.previous:
        const { unit, step } = DatePickerModes[this._context._currentViewMode];
        if (action === ActionTypes.next)
          this._context._viewDate.manipulate(step, unit);
        else this._context._viewDate.manipulate(step * -1, unit);
        this._context._viewUpdate(unit);

        this._context._display._showMode();
        break;
      case ActionTypes.pickerSwitch:
        this._context._display._showMode(1);
        this._context._viewUpdate(
          DatePickerModes[this._context._currentViewMode].unit
        );
        this._context._display._updateCalendarHeader();
        break;
      case ActionTypes.selectMonth:
      case ActionTypes.selectYear:
      case ActionTypes.selectDecade:
        const value = +currentTarget.getAttribute('data-value');
        switch (action) {
          case ActionTypes.selectMonth:
            this._context._viewDate.month = value;
            this._context._viewUpdate(Unit.month);
            break;
          case ActionTypes.selectYear:
            this._context._viewDate.year = value;
            this._context._viewUpdate(Unit.year);
            break;
          case ActionTypes.selectDecade:
            this._context._viewDate.year = value;
            this._context._viewUpdate(Unit.year);
            break;
        }

        if (
          this._context._currentViewMode === this._context._minViewModeNumber
        ) {
          this._context.dates._setValue(
            this._context._viewDate,
            this._context.dates.lastPickedIndex
          );
          if (!this._context._options.display.inline) {
            this._context._display.hide();
          }
        } else {
          this._context._display._showMode(-1);
        }
        break;
      case ActionTypes.selectDay:
        const day = this._context._viewDate.clone;
        if (currentTarget.classList.contains(Namespace.css.old)) {
          day.manipulate(-1, Unit.month);
        }
        if (currentTarget.classList.contains(Namespace.css.new)) {
          day.manipulate(1, Unit.month);
        }

        day.date = +currentTarget.getAttribute('data-day');
        let index = 0;
        if (this._context._options.multipleDates) {
          index = this._context.dates.pickedIndex(day, Unit.date);
          if (index !== -1) {
            this._context.dates._setValue(null, index); //deselect multi-date
          } else {
            this._context.dates._setValue(<commit_msg>Update src/js/actions.ts<commit_after>import { DatePickerModes } from './conts.js';
import { DateTime, Unit } from './datetime';
import { TempusDominus } from './tempus-dominus';
import Collapse from './display/collapse';
import Namespace from './namespace';

/**
 *
 */
export default class Actions {
  private _context: TempusDominus;
  private collapse: Collapse;

  constructor(context: TempusDominus) {
    this._context = context;
    this.collapse = new Collapse();
  }

  /**
   * Performs the selected `action`. See ActionTypes
   * @param e This is normally a click event
   * @param action If not provided, then look for a [data-action]
   */
  do(e: any, action?: ActionTypes) {
    const currentTarget = e.currentTarget;
    if (currentTarget.classList.contains(Namespace.css.disabled)) return false;
    action = action || currentTarget.dataset.action;
    const lastPicked = (
      this._context.dates.lastPicked || this._context._viewDate
    ).clone;

    /**
     * Common function to manipulate {@link lastPicked} by `unit`
     * @param unit
     * @param value Value to change by
     */
    const manipulateAndSet = (unit: Unit, value = 1) => {
      const newDate = lastPicked.manipulate(value, unit);
      if (this._context._validation.isValid(newDate, unit)) {
        this._context.dates._setValue(
          newDate,
          this._context.dates.lastPickedIndex
        );
      }
    };

    switch (action) {
      case ActionTypes.next:
      case ActionTypes.previous:
        const { unit, step } = DatePickerModes[this._context._currentViewMode];
        if (action === ActionTypes.next)
          this._context._viewDate.manipulate(step, unit);
        else this._context._viewDate.manipulate(step * -1, unit);
        this._context._viewUpdate(unit);

        this._context._display._showMode();
        break;
      case ActionTypes.pickerSwitch:
        this._context._display._showMode(1);
        this._context._viewUpdate(
          DatePickerModes[this._context._currentViewMode].unit
        );
        this._context._display._updateCalendarHeader();
        break;
      case ActionTypes.selectMonth:
      case ActionTypes.selectYear:
      case ActionTypes.selectDecade:
        const value = +currentTarget.getAttribute('data-value');
        switch (action) {
          case ActionTypes.selectMonth:
            this._context._viewDate.month = value;
            this._context._viewUpdate(Unit.month);
            break;
          case ActionTypes.selectYear:
            this._context._viewDate.year = value;
            this._context._viewUpdate(Unit.year);
            break;
          case ActionTypes.selectDecade:
            this._context._viewDate.year = value;
            this._context._viewUpdate(Unit.year);
            break;
        }

        if (
          this._context._currentViewMode === this._context._minViewModeNumber
        ) {
          this._context.dates._setValue(
            this._context._viewDate,
            this._context.dates.lastPickedIndex
          );
          if (!this._context._options.display.inline) {
            this._context._display.hide();
          }
        } else {
          this._context._display._showMode(-1);
        }
        break;
      case ActionTypes.selectDay:
        const day = this._context._viewDate.clone;
        if (currentTarget.classList.contains(Namespace.css.old)) {
          day.manipulate(-1, Unit.month);
        }
        if (currentTarget.classList.contains(Namespace.css.new)) {
          day.manipulate(1, Unit.month);
        }

        day.date = +currentTarget.dataset.day;
        let index = 0;
        if (this._context._options.multipleDates) {
          index = this._context.dates.pickedIndex(day, Unit.date);
          if (index !== -1) {
            this._context.dates._setValue(null, index); //deselect multi-date
          } else {
            this._context.dates._setValue(
{'fraction_non_alphanumeric': 0.11248407643312101, 'fraction_numerical': 0.002038216560509554, 'mean_word_length': 2.278079331941545, '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': '6465619', 'n_tokens_mistral': 2162, 'n_tokens_neox': 2161, 'n_words': 430}