<commit_before>﻿import {
  Component,
  ComponentRef,
  ComponentFactoryResolver,
  ViewContainerRef,
  ViewChild,
  OnDestroy
} from '@angular/core';
import {
  IModalDialog,
  IModalDialogOptions,
  IModalDialogButton,
  IModalDialogSettings, ModalDialogOnAction
} from './modal-dialog.interface';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromPromise';
import { Subject } from 'rxjs/Subject';

/**
 * Modal dialog component
 * Usage:
 *
 * Model properties are:
 *
 */
@Component({
  selector: 'modal-dialog',
  styles: [`
      @-moz-keyframes shake {
        from, to                { transform: translate3d(0, -50%, 0); }
        10%, 30%, 50%, 70%, 90% { transform: translate3d(-2rem, -50%, 0); }
        20%, 40%, 60%, 80%      { transform: translate3d(2rem, -50%, 0); }
      }
      @-webkit-keyframes shake {
        from, to                { transform: translate3d(0, -50%, 0); }
        10%, 30%, 50%, 70%, 90% { transform: translate3d(-2rem, -50%, 0); }
        20%, 40%, 60%, 80%      { transform: translate3d(2rem, -50%, 0); }
      }
      @keyframes shake {
        from, to                { transform: translate3d(0, -50%, 0); }
        10%, 30%, 50%, 70%, 90% { transform: translate3d(-2rem, -50%, 0); }
        20%, 40%, 60%, 80%      { transform: translate3d(2rem, -50%, 0); }
      }

      .modal {
        display: block;
        top: 50%;
        left: 50%;
        right: auto;
        bottom: auto;
        backface-visibility: hidden;
        overflow: visible;
      }
      .modal-content {
        left: -50%;
        transform: translateY(-50%);
      }
      .modal-content.shake {
        backface-visibility: hidden;
        -webkit-animation-duration: 0.5s;
        -moz-animation-duration: 0.5s;
        animation-duration: 0.5s;
        -webkit-animation-fill-mode: both;
        -moz-animation-fill-mode: both;
        animation-fill-mode: both;
        -webkit-animation-iteration-count: infinite;
        -moz-animation-iteration-count: infinite;
        animation-iteration-count: infinite;
        -webkit-animation-name: shake;
        -moz-animation-name: shake;
        animation-name: shake;
      }
  `],
  template: `
    <div [ngClass]="settings.overlayClass" (click)="(!actionButtons || !actionButtons.length) && close()"></div>
    <div [ngClass]="settings.modalClass">
      <div [ngClass]="[ showAlert ? settings.alertClass : '', settings.contentClass]">
        <div [ngClass]="settings.headerClass">
          <h4 [ngClass]="settings.headerTitleClass">{{title}}</h4>
          <button (click)="close()" *ngIf="!actionButtons || !actionButtons.length" type="button"
            [title]="settings.closeButtonTitle"
            [ngClass]="settings.closeButtonClass">
          </button>
        </div>
        <div [ngClass]="settings.bodyClass">
          <i #modalDialogBody></i>
        </div>
        <div [ngClass]="settings.footerClass" *ngIf="actionButtons && actionButtons.length">
          <button *ngFor="let button of actionButtons" (click)="doAction(button.onAction)"
            [ngClass]="button.buttonClass || settings.buttonClass">{{button.text}}</button>
        </div>
      </div>
    </div>
    `
})
export class ModalDialogComponent implements IModalDialog, OnDestroy {
  @ViewChild('modalDialogBody', { read: ViewContainerRef })
  public dynamicComponentTarget: ViewContainerRef;
  public reference: ComponentRef<IModalDialog>;

  /** Modal dialog style settings */
  public settings: IModalDialogSettings = {
    overlayClass: 'modal-backdrop fade show',
    modalClass: 'fade show modal',
    contentClass: 'modal-content',
    headerClass: 'modal-header',
    headerTitleClass: 'modal-title',
    closeButtonClass: 'close glyphicon glyphicon-remove',
    closeButtonTitle: 'CLOSE',
    bodyClass: 'modal-body',
    footerClass: 'modal-footer',
    alertClass: 'shake',
    alertDuration: 250,
    notifyWithAlert: true,
    buttonClass: 'btn btn-primary'
  };
  public actionButtons: IModalDialogButton[];
  public title: string;
  public onClose: () => Promise<any> | Observable<any> | boolean;
  public showAlert: boolean = false;

  private _inProgress = false;
  private _alertTimeout: number;
  private _childInstance: any;

  private _closeDialog$: Subject<void>;

  /**
   * CTOR
   * @param componentFactoryResolver
   */
  constructor(private componentFactoryResolver: ComponentFactoryResolver) {
  }

  /**
   * Initialize dialog with reference to instance and options
   * @param reference
   * @param options
   */
  dialogInit(reference: ComponentRef<IModalDialog>, options?: IModalDialogOptions) {
    this.reference = reference;

    // inject component
    if (options && options.childComponent) {
      let factory = this.componentFactoryResolver.resolveComponentFactory(options.childComponent);
      let componentRef = this.dynamicComponentTarget.createComponent(factory) as ComponentRef<IModalDialog>;
      this._childInstance = componentRef.instance as IModalDialog;

      this._closeDialog$ = new Subject<void>();
      this._closeDialog$.subscribe(_ => {
        this._finalizeAndDestroy();
      });

      options.closeDialogSubject = this._closeDialog$;

      this._childInstance['dialogInit'](componentRef, options);
      (document.activeElement as HTMLElement).blur();
    }
    // set options
    this._setOptions(options);
  }

  /**
   * Run action defined on action button
   * @param action
   */
  doAction(action?: ModalDialogOnAction) {
    // disable multi clicks
    if (this._inProgress) {
      return;
    }
    this._inProgress = true;
    this._closeIfSuccessful(action);
  }

  /**
   * Method to run on close
   * if action buttons are defined, it will not close
   */
  close() {
    if (this._inProgress) {
      return;<commit_msg>(fix): fallback mechanic in case of missing active element (IE11 bug)<commit_after>﻿import {
  Component,
  ComponentRef,
  ComponentFactoryResolver,
  ViewContainerRef,
  ViewChild,
  OnDestroy
} from '@angular/core';
import {
  IModalDialog,
  IModalDialogOptions,
  IModalDialogButton,
  IModalDialogSettings, ModalDialogOnAction
} from './modal-dialog.interface';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromPromise';
import { Subject } from 'rxjs/Subject';

/**
 * Modal dialog component
 * Usage:
 *
 * Model properties are:
 *
 */
@Component({
  selector: 'modal-dialog',
  styles: [`
      @-moz-keyframes shake {
        from, to                { transform: translate3d(0, -50%, 0); }
        10%, 30%, 50%, 70%, 90% { transform: translate3d(-2rem, -50%, 0); }
        20%, 40%, 60%, 80%      { transform: translate3d(2rem, -50%, 0); }
      }
      @-webkit-keyframes shake {
        from, to                { transform: translate3d(0, -50%, 0); }
        10%, 30%, 50%, 70%, 90% { transform: translate3d(-2rem, -50%, 0); }
        20%, 40%, 60%, 80%      { transform: translate3d(2rem, -50%, 0); }
      }
      @keyframes shake {
        from, to                { transform: translate3d(0, -50%, 0); }
        10%, 30%, 50%, 70%, 90% { transform: translate3d(-2rem, -50%, 0); }
        20%, 40%, 60%, 80%      { transform: translate3d(2rem, -50%, 0); }
      }

      .modal {
        display: block;
        top: 50%;
        left: 50%;
        right: auto;
        bottom: auto;
        backface-visibility: hidden;
        overflow: visible;
      }
      .modal-content {
        left: -50%;
        transform: translateY(-50%);
      }
      .modal-content.shake {
        backface-visibility: hidden;
        -webkit-animation-duration: 0.5s;
        -moz-animation-duration: 0.5s;
        animation-duration: 0.5s;
        -webkit-animation-fill-mode: both;
        -moz-animation-fill-mode: both;
        animation-fill-mode: both;
        -webkit-animation-iteration-count: infinite;
        -moz-animation-iteration-count: infinite;
        animation-iteration-count: infinite;
        -webkit-animation-name: shake;
        -moz-animation-name: shake;
        animation-name: shake;
      }
  `],
  template: `
    <div [ngClass]="settings.overlayClass" (click)="(!actionButtons || !actionButtons.length) && close()"></div>
    <div [ngClass]="settings.modalClass">
      <div [ngClass]="[ showAlert ? settings.alertClass : '', settings.contentClass]">
        <div [ngClass]="settings.headerClass">
          <h4 [ngClass]="settings.headerTitleClass">{{title}}</h4>
          <button (click)="close()" *ngIf="!actionButtons || !actionButtons.length" type="button"
            [title]="settings.closeButtonTitle"
            [ngClass]="settings.closeButtonClass">
          </button>
        </div>
        <div [ngClass]="settings.bodyClass">
          <i #modalDialogBody></i>
        </div>
        <div [ngClass]="settings.footerClass" *ngIf="actionButtons && actionButtons.length">
          <button *ngFor="let button of actionButtons" (click)="doAction(button.onAction)"
            [ngClass]="button.buttonClass || settings.buttonClass">{{button.text}}</button>
        </div>
      </div>
    </div>
    `
})
export class ModalDialogComponent implements IModalDialog, OnDestroy {
  @ViewChild('modalDialogBody', { read: ViewContainerRef })
  public dynamicComponentTarget: ViewContainerRef;
  public reference: ComponentRef<IModalDialog>;

  /** Modal dialog style settings */
  public settings: IModalDialogSettings = {
    overlayClass: 'modal-backdrop fade show',
    modalClass: 'fade show modal',
    contentClass: 'modal-content',
    headerClass: 'modal-header',
    headerTitleClass: 'modal-title',
    closeButtonClass: 'close glyphicon glyphicon-remove',
    closeButtonTitle: 'CLOSE',
    bodyClass: 'modal-body',
    footerClass: 'modal-footer',
    alertClass: 'shake',
    alertDuration: 250,
    notifyWithAlert: true,
    buttonClass: 'btn btn-primary'
  };
  public actionButtons: IModalDialogButton[];
  public title: string;
  public onClose: () => Promise<any> | Observable<any> | boolean;
  public showAlert: boolean = false;

  private _inProgress = false;
  private _alertTimeout: number;
  private _childInstance: any;

  private _closeDialog$: Subject<void>;

  /**
   * CTOR
   * @param componentFactoryResolver
   */
  constructor(private componentFactoryResolver: ComponentFactoryResolver) {
  }

  /**
   * Initialize dialog with reference to instance and options
   * @param reference
   * @param options
   */
  dialogInit(reference: ComponentRef<IModalDialog>, options?: IModalDialogOptions) {
    this.reference = reference;

    // inject component
    if (options && options.childComponent) {
      let factory = this.componentFactoryResolver.resolveComponentFactory(options.childComponent);
      let componentRef = this.dynamicComponentTarget.createComponent(factory) as ComponentRef<IModalDialog>;
      this._childInstance = componentRef.instance as IModalDialog;

      this._closeDialog$ = new Subject<void>();
      this._closeDialog$.subscribe(_ => {
        this._finalizeAndDestroy();
      });

      options.closeDialogSubject = this._closeDialog$;

      this._childInstance['dialogInit'](componentRef, options);
      document.activeElement != null ? (document.activeElement as HTMLElement).blur(): (document.body as HTMLElement).blur();
    }
    // set options
    this._setOptions(options);
  }

  /**
   * Run action defined on action button
   * @param action
   */
  doAction(action?: ModalDialogOnAction) {
    // disable multi clicks
    if (this._inProgress) {
      return;
    }
    this._inProgress = true;
    this._closeIfSuccessful(action);
  }

  /**
   * Method to run on close
   * if action buttons are defined, it will not close
   */
  close() {
    if (this._inProgress) {
      return;
{'fraction_non_alphanumeric': 0.13705410479604027, 'fraction_numerical': 0.02031063321385902, 'mean_word_length': 2.886898839137645, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 61, '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': '6175540', 'n_tokens_mistral': 3635, 'n_tokens_neox': 3426, 'n_words': 776}