<commit_before>import {
  Directive,
  Input,
  Output,
  OnInit,
  OnChanges,
  ComponentRef,
  ViewContainerRef,
  EventEmitter,
  ComponentFactoryResolver,
  Optional,
  SimpleChanges,
  SkipSelf,
  Host
} from '@angular/core';
import {AbstractControl, ControlContainer, FormGroup} from '@angular/forms';
import {DateTimePickerComponent} from './datetime-picker.component';
import {DateTime} from './datetime';

declare var moment: any;

/**
 * If the given string is not a valid date, it defaults back to today
 */
@Directive({
  selector : '[datetime-picker], [ng2-datetime-picker]',
  providers: [DateTime],
  host     : {
    '(click)': 'showDatetimePicker()',
    '(focus)': 'showDatetimePicker()'
  }
})
export class DateTimePickerDirective implements OnInit, OnChanges {
  @Input('date-format')     dateFormat: string;
  @Input('date-only')       dateOnly: boolean;
  @Input('time-only')       timeOnly: boolean;
  @Input('close-on-select') closeOnSelect: string;
  @Input('first-day-of-week') firstDayOfWeek: string;
  @Input('default-value')   defaultValue: Date;
  @Input('minute-step')     minuteStep: number;
  @Input('min-date')        minDate: Date;
  @Input('max-date')        maxDate: Date;
  @Input('min-hour')        minHour: Date;
  @Input('max-hour')        maxHour: Date;
  @Input('disabled-dates')  disabledDates: Date[];
  @Input() formControlName: string;

  @Input('ngModel')        ngModel: any;
  @Output('ngModelChange') ngModelChange = new EventEmitter();

  private el: HTMLInputElement;                               /* input element */
  private datetimePickerEl: HTMLElement;                      /* dropdown element */
  private componentRef:ComponentRef<DateTimePickerComponent>; /* dropdown component reference */
  private ctrl: AbstractControl;
  private sub: any;
  private justShown: boolean;

  constructor (
    private resolver:ComponentFactoryResolver,
    private viewContainerRef:ViewContainerRef,
    @Optional() @Host() @SkipSelf() private parent: ControlContainer
  ) {
    this.el = this.viewContainerRef.element.nativeElement;
  }

  ngOnInit ():void {
    if(this.parent && this.parent["form"] && this.formControlName) {
      this.ctrl = (<FormGroup>this.parent["form"]).get(this.formControlName);
      this.sub = this.ctrl.valueChanges.subscribe((date) => {
        this.setElement(date)
        this.updateDatepicker();
      })
    }

    //wrap this element with a <div> tag, so that we can position dynamic elememnt correctly
    let wrapper            = document.createElement("div");
    wrapper.className      = 'ng2-datetime-picker';
    this.el.parentElement.insertBefore(wrapper, this.el.nextSibling);
    wrapper.appendChild(this.el);

    // add a click listener to document, so that it can hide when others clicked
    document.body.addEventListener('click', this.hideDatetimePicker);
    this.el.addEventListener('keyup', this.keyEventListener);<commit_msg>fixbug when working with nested formGroup<commit_after>import {
  Directive,
  Input,
  Output,
  OnInit,
  OnChanges,
  ComponentRef,
  ViewContainerRef,
  EventEmitter,
  ComponentFactoryResolver,
  Optional,
  SimpleChanges,
  SkipSelf,
  Host
} from '@angular/core';
import {AbstractControl, ControlContainer, FormGroup, FormGroupDirective} from '@angular/forms';
import {DateTimePickerComponent} from './datetime-picker.component';
import {DateTime} from './datetime';

declare var moment: any;

/**
 * If the given string is not a valid date, it defaults back to today
 */
@Directive({
  selector : '[datetime-picker], [ng2-datetime-picker]',
  providers: [DateTime],
  host     : {
    '(click)': 'showDatetimePicker()',
    '(focus)': 'showDatetimePicker()'
  }
})
export class DateTimePickerDirective implements OnInit, OnChanges {
  @Input('date-format')     dateFormat: string;
  @Input('date-only')       dateOnly: boolean;
  @Input('time-only')       timeOnly: boolean;
  @Input('close-on-select') closeOnSelect: string;
  @Input('first-day-of-week') firstDayOfWeek: string;
  @Input('default-value')   defaultValue: Date;
  @Input('minute-step')     minuteStep: number;
  @Input('min-date')        minDate: Date;
  @Input('max-date')        maxDate: Date;
  @Input('min-hour')        minHour: Date;
  @Input('max-hour')        maxHour: Date;
  @Input('disabled-dates')  disabledDates: Date[];
  @Input() formControlName: string;

  @Input('ngModel')        ngModel: any;
  @Output('ngModelChange') ngModelChange = new EventEmitter();

  private el: HTMLInputElement;                               /* input element */
  private datetimePickerEl: HTMLElement;                      /* dropdown element */
  private componentRef:ComponentRef<DateTimePickerComponent>; /* dropdown component reference */
  private ctrl: AbstractControl;
  private sub: any;
  private justShown: boolean;

  constructor (
    private resolver:ComponentFactoryResolver,
    private viewContainerRef:ViewContainerRef,
    @Optional() @Host() @SkipSelf() private parent: ControlContainer
  ) {
    this.el = this.viewContainerRef.element.nativeElement;
  }

  ngOnInit ():void {
    if(this.parent && this.formControlName) {
      if (this.parent["form"]) {
        this.ctrl = (<FormGroup>this.parent["form"]).get(this.formControlName);
      } else if (this.parent["name"]) {
        let formDir = this.parent.formDirective;
        if (formDir instanceof FormGroupDirective && formDir.form.get(this.parent["name"])) {
          this.ctrl = formDir.form.get(this.parent["name"]).get(this.formControlName);
        }
      }
      if (this.ctrl) {
        this.sub = this.ctrl.valueChanges.subscribe((date) => {
          this.setElement(date)
          this.updateDatepicker();
        });
      }
    }

    //wrap this element with a <div> tag, so that we can position dynamic elememnt correctly
    let wrapper            = document.createElement("div");
    wrapper.className      = 'ng2-datetime-picker';
    this.el.parentElement.insertBefore(wrapper, this.el.nextSibling);
    wrapper.appendChild(this.el);

    // add a click listener to document, so that it can hide when others clicked
    document.body.addEventListener('click', this.hideDatetimePicker);
    this.el.addEventListener('keyup', this.keyEventListener);
{'fraction_non_alphanumeric': 0.1308037143772014, 'fraction_numerical': 0.0006404098623118795, 'mean_word_length': 3.539970930232558, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3846271', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1798, 'n_words': 421}