<commit_before>import { Component, ViewChild, OnInit, Output, EventEmitter, Input, OnChanges, OnDestroy } from '@angular/core';

import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';

import { cloneDeep } from 'lodash';
import * as moment from 'moment';
import { IMyOptions, IMyDateModel } from 'mydatepicker';
import { Broadcaster } from 'ngx-base';
import { Space, Spaces } from 'ngx-fabric8-wit';

import { IterationService } from '../iteration.service';
import { IterationModel } from '../../models/iteration.model';


@Component({
  selector: 'fab-planner-iteration-modal',
  templateUrl: './iteration-modal.component.html',
  styleUrls: ['./iteration-modal.component.scss']
})
export class FabPlannerIterationModalComponent implements OnInit, OnDestroy, OnChanges {

  private spaceSubscription: Subscription = null;

  @Output()
  public onSubmit = new EventEmitter();

  @ViewChild('createUpdateIterationDialog') createUpdateIterationDialog: any;
  @ViewChild('iterationSearch') iterationSearch: any;
  @ViewChild('iterationList') iterationList: any;

  public iteration: IterationModel;
  private validationError = false;
  private modalType: string = 'create';
  private submitBtnTxt: string = 'Create';
  private modalTitle: string = 'Create Iteration';
  private startDate: any;
  private endDate: any;
  private spaceError: Boolean = false;
  private spaceName: string = 'FIXME';
  private iterationName: string;
  iterations: IterationModel[] = [];
  filteredIterations: IterationModel[] = [];
  selectedParentIteration: IterationModel;
  selectedParentIterationName:string = '';
  iterationSearchDisable: Boolean = false;
  showIterationDropdown: Boolean = false;

  private startDatePickerOptions: IMyOptions = {
    dateFormat: 'dd mmm yyyy',
    selectionTxtFontSize: '14px',
    openSelectorOnInputClick: true,
    editableDateField: false,
    showClearDateBtn: false,
    componentDisabled: false
  };

  private endDatePickerOptions: IMyOptions = {
    dateFormat: 'dd mmm yyyy',
    selectionTxtFontSize: '14px',
    openSelectorOnInputClick: true,
    editableDateField: false,
    showClearDateBtn: false,
    componentDisabled: false
  };

  constructor(
    private iterationService: IterationService,
    private spaces: Spaces,
    private broadcaster: Broadcaster) {}


  ngOnInit() {
    this.resetValues();
    this.spaceSubscription = this.spaces.current.subscribe(space =>  {
      if (space) {
        console.log('[FabPlannerIterationModalComponent] New Space selected: ' + space.attributes.name);
      } else {
        console.log('[FabPlannerIterationModalComponent] Space deselected.');
      }
    });
  }

  resetValues() {

    this.iteration  = {
      // id: '',
      attributes: {
        name: '',
        description: '',
        state: 'new',
        parent_path: '',
        resolved_parent_path: ''
      },
      relationships: {
        parent: {
          data: {
            id: "",
            type: "iterations"
          },
          links: {
            self: ""
          }
        },
        space: {
          data: {
            id: '',
            type: 'space'
          },
          links: {
            self: ''
          }
        }
      },
      type: 'iterations'
    } as IterationModel;

    let today = moment();
    this.startDate = { date: { year: today.format('YYYY'), month: today.format('M'), day: today.format('D') } };
    let inaweek = moment().add(7, 'd');
    this.endDate = { date: { year: inaweek.format('YYYY'), month: inaweek.format('M'), day: inaweek.format('D') } };
    this.validationError = false;
    this.spaceError = false;
    let startDatePickerComponentCopy = Object.assign({}, this.startDatePickerOptions);
    startDatePickerComponentCopy.componentDisabled = false;
    this.startDatePickerOptions = startDatePickerComponentCopy;
    this.selectedParentIterationName = '';
    this.filteredIterations = [];
    this.selectedParentIteration = null;
    this.iterationSearchDisable = false;
    this.showIterationDropdown = false;
  }

  ngOnChanges() {
    console.log(this.modalType);
  }

  ngOnDestroy() {
    // prevent memory leak when component is destroyed
    this.spaceSubscription.unsubscribe();
  }

  openCreateUpdateModal(
    type: string = 'create',
    iteration: IterationModel | null = null
  ) {
    event.stopPropagation();
    this.modalType = type;
    if (this.modalType == 'create') {
      this.getIterations();
      this.submitBtnTxt = 'Create';
      this.modalTitle = 'Create Iteration';
      this.iterationSearch.nativeElement.setAttribute('placeholder', 'None');
      let endDatePickerComponentCopy = Object.assign({}, this.endDatePickerOptions);
      endDatePickerComponentCopy['disableUntil'] = this.startDate.date;
      this.endDatePickerOptions = endDatePickerComponentCopy;
    }
    if (this.modalType == 'start') {
      this.submitBtnTxt = 'Start';
      this.modalTitle = 'Start Iteration';
    }
    if (this.modalType == 'update') {
      this.getIterations();
      this.submitBtnTxt = 'Update';
      this.modalTitle = 'Update Iteration';
      this.iterationSearchDisable = true;
      this.selectedParentIterationName = iteration.attributes.resolved_parent_path;

      if (typeof iteration.attributes.startAt !== 'undefined') {
        let startAt = moment(iteration.attributes.startAt);
        let date = { year: startAt.format('YYYY'), month: startAt.format('M'), day: startAt.format('D') } as any;
        let endDatePickerComponentCopy = Object.assign({}, this.endDatePickerOptions);
        endDatePickerComponentCopy['disableUntil'] = date;
        this.endDatePickerOptions = endDatePickerComponentCopy;
      }
      else {
        let endDatePickerComponentCopy = Object.assign({}, this.endDatePickerOptions);
        endDatePickerComponentCopy['disableUntil'] = this.startDate.date;
        this.endDatePickerOptions = endDatePickerComponentCopy;
      }
      if (iteration.attributes.state === 'start') {
        let startDatePickerComponentCopy = Object.assign({}, this.startDatePickerOptions);
        startDatePickerComponentCopy.componentDisabled = true;<commit_msg>fix(iteration): date picker field empty while creating new iteration<commit_after>import { Component, ViewChild, OnInit, Output, EventEmitter, Input, OnChanges, OnDestroy } from '@angular/core';

import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';

import { cloneDeep } from 'lodash';
import * as moment from 'moment';
import { IMyOptions, IMyDateModel } from 'mydatepicker';
import { Broadcaster } from 'ngx-base';
import { Space, Spaces } from 'ngx-fabric8-wit';

import { IterationService } from '../iteration.service';
import { IterationModel } from '../../models/iteration.model';


@Component({
  selector: 'fab-planner-iteration-modal',
  templateUrl: './iteration-modal.component.html',
  styleUrls: ['./iteration-modal.component.scss']
})
export class FabPlannerIterationModalComponent implements OnInit, OnDestroy, OnChanges {

  private spaceSubscription: Subscription = null;

  @Output()
  public onSubmit = new EventEmitter();

  @ViewChild('createUpdateIterationDialog') createUpdateIterationDialog: any;
  @ViewChild('iterationSearch') iterationSearch: any;
  @ViewChild('iterationList') iterationList: any;

  public iteration: IterationModel;
  private validationError = false;
  private modalType: string = 'create';
  private submitBtnTxt: string = 'Create';
  private modalTitle: string = 'Create Iteration';
  private startDate: any;
  private endDate: any;
  private spaceError: Boolean = false;
  private spaceName: string = 'FIXME';
  private iterationName: string;
  iterations: IterationModel[] = [];
  filteredIterations: IterationModel[] = [];
  selectedParentIteration: IterationModel;
  selectedParentIterationName:string = '';
  iterationSearchDisable: Boolean = false;
  showIterationDropdown: Boolean = false;

  private startDatePickerOptions: IMyOptions = {
    dateFormat: 'dd mmm yyyy',
    selectionTxtFontSize: '14px',
    openSelectorOnInputClick: true,
    editableDateField: false,
    showClearDateBtn: false,
    componentDisabled: false
  };

  private endDatePickerOptions: IMyOptions = {
    dateFormat: 'dd mmm yyyy',
    selectionTxtFontSize: '14px',
    openSelectorOnInputClick: true,
    editableDateField: false,
    showClearDateBtn: false,
    componentDisabled: false
  };

  constructor(
    private iterationService: IterationService,
    private spaces: Spaces,
    private broadcaster: Broadcaster) {}


  ngOnInit() {
    this.resetValues();
    this.spaceSubscription = this.spaces.current.subscribe(space =>  {
      if (space) {
        console.log('[FabPlannerIterationModalComponent] New Space selected: ' + space.attributes.name);
      } else {
        console.log('[FabPlannerIterationModalComponent] Space deselected.');
      }
    });
  }

  resetValues() {

    this.iteration  = {
      // id: '',
      attributes: {
        name: '',
        description: '',
        state: 'new',
        parent_path: '',
        resolved_parent_path: ''
      },
      relationships: {
        parent: {
          data: {
            id: "",
            type: "iterations"
          },
          links: {
            self: ""
          }
        },
        space: {
          data: {
            id: '',
            type: 'space'
          },
          links: {
            self: ''
          }
        }
      },
      type: 'iterations'
    } as IterationModel;

    let today = moment();
    this.startDate = { date: { year: today.format('YYYY'), month: today.format('M'), day: today.format('D') } };
    let inaweek = moment().add(7, 'd');
    this.endDate = { date: { year: inaweek.format('YYYY'), month: inaweek.format('M'), day: inaweek.format('D') } };
    this.validationError = false;
    this.spaceError = false;
    let startDatePickerComponentCopy = Object.assign({}, this.startDatePickerOptions);
    startDatePickerComponentCopy.componentDisabled = false;
    this.startDatePickerOptions = startDatePickerComponentCopy;
    this.selectedParentIterationName = '';
    this.filteredIterations = [];
    this.selectedParentIteration = null;
    this.iterationSearchDisable = false;
    this.showIterationDropdown = false;
  }

  ngOnChanges() {
    console.log(this.modalType);
  }

  ngOnDestroy() {
    // prevent memory leak when component is destroyed
    this.spaceSubscription.unsubscribe();
  }

  openCreateUpdateModal(
    type: string = 'create',
    iteration: IterationModel | null = null
  ) {
    event.stopPropagation();
    this.modalType = type;
    if (this.modalType == 'create') {
      this.getIterations();
      this.submitBtnTxt = 'Create';
      this.modalTitle = 'Create Iteration';
      this.iterationSearch.nativeElement.setAttribute('placeholder', 'None');
      let endDatePickerComponentCopy = Object.assign({}, this.endDatePickerOptions);
      let startDatePickerComponentCopy = Object.assign({}, this.startDatePickerOptions);
      let aDayBefore = moment().subtract(1, 'days');
      this.startDate = { date: { year: aDayBefore.format('YYYY'), month: aDayBefore.format('M'), day: aDayBefore.format('D') } };
      startDatePickerComponentCopy['disableUntil'] = this.startDate.date;
      endDatePickerComponentCopy['disableUntil'] = this.startDate.date;
      this.startDatePickerOptions = startDatePickerComponentCopy;
      this.endDatePickerOptions = endDatePickerComponentCopy;
      this.startDate = '';
      this.endDate = '';
    }
    if (this.modalType == 'start') {
      this.submitBtnTxt = 'Start';
      this.modalTitle = 'Start Iteration';
    }
    if (this.modalType == 'update') {
      this.getIterations();
      this.submitBtnTxt = 'Update';
      this.modalTitle = 'Update Iteration';
      this.iterationSearchDisable = true;
      this.selectedParentIterationName = iteration.attributes.resolved_parent_path;

      if (typeof iteration.attributes.startAt !== 'undefined') {
        let startAt = moment(iteration.attributes.startAt);
        let date = { year: startAt.format('YYYY'), month: startAt.format('M'), day: startAt.format('D') } as any;
        let endDatePickerComponentCopy = Object.assign({}, this.endDatePickerOptions);
        endDatePickerComponentCopy['disableUntil'] = date;
        this.endDatePickerOptions = endDatePickerComponentCopy;
      }
      else {
        let endDatePickerComponentCopy = Object.assign({}, this.endDatePickerOptions);
        endDatePickerComponentCopy['disableUntil'] = this.startDate.date;
        this.endDatePickerOptions = endDatePickerComponentCopy;
      }
      if (iteration.attributes.state === 'start') {
        let startDatePickerComponentCopy = Object.assign({}, this.startDatePickerOptions);
        startDatePickerComponentCopy.componentDisabled = true;
{'fraction_non_alphanumeric': 0.12064009943292162, 'fraction_numerical': 0.0010098656101918744, 'mean_word_length': 3.3938566552901026, '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': '14481', 'n_tokens_mistral': 3672, 'n_tokens_neox': 3638, 'n_words': 763}