<commit_before>import {AfterContentInit, Directive, ElementRef, Input, OnDestroy} from '@angular/core';
import {Inject} from '@angular/core';
import {DEFAULT_CFG} from './default-promise-btn-config';
import {PromiseBtnConfig} from './promise-btn-config';
import {userCfg} from './user-cfg';

@Directive({
  selector: '[promiseBtn]'
})

export class PromiseBtnDirective implements OnDestroy, AfterContentInit {
  cfg: PromiseBtnConfig;
  // later initialized via initPromiseWatcher()
  promiseWatcher: any;
  // the timeout used for min duration display
  minDurationTimeout: number;
  // boolean to determine minDurationTimeout state
  isMinDurationTimeoutDone: boolean;
  // boolean to determine if promise was resolved
  isPromiseDone: boolean;
  // the promise button button element
  btnEl: HTMLElement;
  // a pointer to the old value of promise
  lastPromise: Promise<any>;
  // the promise itself or a function expression
  promise: Promise<any>;

  constructor(el: ElementRef, @Inject(userCfg) userCfg: {}) {
    // provide configuration
    this.cfg = Object.assign({}, DEFAULT_CFG, userCfg);

    // save element
    this.btnEl = el.nativeElement;
  }

  @Input()
  set promiseBtn(promise: Promise<any>) {
    this.promise = promise;
    this.checkAndInitPromiseWatcher(this.btnEl);
  }

  ngAfterContentInit() {
    this.prepareBtnEl(this.btnEl);
    // trigger changes once
    this.checkAndInitPromiseWatcher(this.btnEl);
  }

  ngOnDestroy() {
    // cleanup
    if (this.minDurationTimeout) {
      clearTimeout(this.minDurationTimeout);
    }
  }

  /**
   * Initializes all html and event handlers
   * @param {Object}btnEl
   */
  prepareBtnEl(btnEl: HTMLElement) {
    // handle promises passed via promiseBtn attribute
    this.appendSpinnerTpl(btnEl);
    this.addHandlersForCurrentBtnOnlyIfSet(btnEl);
  }

  /**
   * Checks if all required parameters are there and inits the promise handler
   * @param {Object}btnEl
   */
  checkAndInitPromiseWatcher(btnEl: HTMLElement) {
    if (btnEl && this.promise) {
      if (!this.promiseWatcher) {
        this.initPromiseHandler(this.promise, btnEl);
      }
      this.lastPromise = this.promise;
    }
  }

  /**
   * Helper FN to add class
   * @param {Object}el
   */
  addLoadingClass(el: any) {
    el.className += ' ' + this.cfg.btnLoadingClass;
    el.className = el.className.trim();
  }

  /**
   * Helper FN to remove classes
   * @param {Object}el
   */
  removeLoadingClass(el: any) {
    const classNameToRemove = this.cfg.btnLoadingClass;
    let newElClass = ' ' + el.className + ' ';
    while (newElClass.indexOf(' ' + classNameToRemove + ' ') !== -1) {
      newElClass = newElClass.replace(' ' + classNameToRemove + ' ', '');<commit_msg>cleanup promise btn directive<commit_after>import {AfterContentInit, Directive, ElementRef, Input, OnDestroy} from '@angular/core';
import {Inject} from '@angular/core';
import {DEFAULT_CFG} from './default-promise-btn-config';
import {PromiseBtnConfig} from './promise-btn-config';
import {userCfg} from './user-cfg';

@Directive({
  selector: '[promiseBtn]'
})

export class PromiseBtnDirective implements OnDestroy, AfterContentInit {
  cfg: PromiseBtnConfig;
  // later initialized via initPromiseWatcher()
  promiseWatcher: any;
  // the timeout used for min duration display
  minDurationTimeout: number;
  // boolean to determine minDurationTimeout state
  isMinDurationTimeoutDone: boolean;
  // boolean to determine if promise was resolved
  isPromiseDone: boolean;
  // the promise button button element
  btnEl: HTMLElement;
  // the promise itself or a function expression
  promise: Promise<any>;

  constructor(el: ElementRef, @Inject(userCfg) userCfg: {}) {
    // provide configuration
    this.cfg = Object.assign({}, DEFAULT_CFG, userCfg);

    // save element
    this.btnEl = el.nativeElement;
  }

  @Input()
  set promiseBtn(promise: Promise<any>) {
    this.promise = promise;
    this.checkAndInitPromiseHandler(this.btnEl);
  }

  ngAfterContentInit() {
    this.prepareBtnEl(this.btnEl);
    // trigger changes once to handle initial promises
    this.checkAndInitPromiseHandler(this.btnEl);
  }

  ngOnDestroy() {
    // cleanup
    if (this.minDurationTimeout) {
      clearTimeout(this.minDurationTimeout);
    }
  }

  /**
   * Initializes all html and event handlers
   * @param {Object}btnEl
   */
  prepareBtnEl(btnEl: HTMLElement) {
    // handle promises passed via promiseBtn attribute
    this.appendSpinnerTpl(btnEl);
    this.addHandlersForCurrentBtnOnlyIfSet(btnEl);
  }

  /**
   * Checks if all required parameters are there and inits the promise handler
   * @param {Object}btnEl
   */
  checkAndInitPromiseHandler(btnEl: HTMLElement) {
    if (btnEl && this.promise) {
      if (!this.promiseWatcher) {
        this.initPromiseHandler(this.promise, btnEl);
      }
    }
  }

  /**
   * Helper FN to add class
   * @param {Object}el
   */
  addLoadingClass(el: any) {
    el.className += ' ' + this.cfg.btnLoadingClass;
    el.className = el.className.trim();
  }

  /**
   * Helper FN to remove classes
   * @param {Object}el
   */
  removeLoadingClass(el: any) {
    const classNameToRemove = this.cfg.btnLoadingClass;
    let newElClass = ' ' + el.className + ' ';
    while (newElClass.indexOf(' ' + classNameToRemove + ' ') !== -1) {
      newElClass = newElClass.replace(' ' + classNameToRemove + ' ', '');
{'fraction_non_alphanumeric': 0.11933085501858737, 'fraction_numerical': 0.0003717472118959108, 'mean_word_length': 3.491652754590985, 'pattern_counts': {'":': 0, '<': 8, '<?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': '1854010', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1644, 'n_words': 399}