import { Component , Injectable , Input , OnInit } from '@angular/core' ; import { CmdbType } from '../../models/cmdb-type' ; import { TypeService } from '../../services/type.service' ; import { ActivatedRoute , Router } from '@angular/router' ; import { ObjectService } from '../../services/object.service' ; import { NgbActiveModal , NgbModal } from '@ng-bootstrap/ng-bootstrap' ; import { PreviousRouteService } from '../../../services/previous-route.service' ; import { AbstractControl , FormControl , FormGroup , ValidatorFn , Validators } from '@angular/forms' ; import { ToastService } from '../../../layout/toast/toast.service' ; @ Component ( { selector : 'cmdb-type-delete-confirm-modal' , styleUrls : [ './type-delete.component.scss' ] , template : `
    <div class="modal-header">
      <h4 class="modal-title" id="modal-title">Type deletion</h4>
      <button type="button" class="close" aria-describedby="modal-title" (click)="modal.dismiss('Cross click')">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="modal-body">
      <strong>Are you sure you want to delete <span class="text-primary">{{typeLabel}}</span> type?</strong>
      <p>
        All objects associated to this type will permanently deleted. <span class="text-danger">This operation can not be undone!</span>
      </p>
      <form id="deleteTypeModalForm" [formGroup]="deleteTypeModalForm" class="needs-validation" novalidate autocomplete="off">
        <div class="form-group">
          <label for="typeNameInput">Type the name: {{typeName}} <span class="required">*</span></label>
          <input type="text" formControlName="name" class="form-control"
                 [ngClass]="{ 'is-valid': name.valid && (name.dirty || name.touched),
                 'is-invalid': name.invalid && (name.dirty || name.touched)}"
                 id="typeNameInput" required>
          <small id="typeNameInputHelp" class="form-text text-muted">Type in the name of the type to confirm the deletion.</small>
          <div *ngIf="name.invalid && (name.dirty || name.touched)"
               class="invalid-feedback">
            <div class="float-right" *ngIf="name.errors.required">
              Name is required
            </div>
            <div class="float-right" *ngIf="name.errors.notequal">
              Your answer is not equal!
            </div>
          </div>
          <div class="clearfix"></div>
        </div>
      </form>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-outline-secondary" (click)="modal.dismiss('cancel')">Cancel</button>
      <button type="button" class="btn btn-danger" [disabled]="deleteTypeModalForm.invalid" (click)="modal.close('delete')">Delete</button>
    </div>
  ` } ) export class TypeDeleteConfirmModalComponent { @ Input ( ) typeID : number = 0 ; @ Input ( ) typeName : string = '' ; @ Input ( ) typeLabel : string = '' ; public deleteTypeModalForm : FormGroup ; constructor ( public modal ) { this . deleteTypeModalForm = new FormGroup ( { name : new FormControl ( '' , [ Validators . required , this . equalName ( ) ] ) , } ) ; } public equalName ( ) { return ( control ) : { [ key ] : boolean ; } | null => { if ( control . value !== this . typeName ) { return { notequal : true } ; } else { return null ; } } ; } public get name ( ) { return this . deleteTypeModalForm . get ( 'name' ) ; } } @ Component ( { selector : 'cmdb-type-delete' , templateUrl : './type-delete.component.html' , styleUrls : [ './type-delete.component.scss' ] } ) export class TypeDeleteComponent implements OnInit { public typeID : number ; public typeInstance : CmdbType ; public numberOfObjects : number ; constructor ( private typeService , private objectService , private router , private route , public prevRoute , private modalService , private toast ) { this . route . params . subscribe ( ( id ) => { this . typeID = id . publicID ; } ) ; } public open ( ) { const deleteModal = this . modalService . open ( TypeDeleteConfirmModalComponent ) ; deleteModal . componentInstance . typeID = this . typeID ; deleteModal . componentInstance . typeName = this . typeInstance . name ; deleteModal . componentInstance . typeLabel = this . typeInstance . label ; deleteModal . result . then ( ( result ) => { if ( result === 'delete' ) { this . typeService . deleteType ( this . typeID ) . subscribe ( ( deletedType ) => { this . router . navigate ( [ '/framework/type/' ] ) ; this . toast . success ( `Type was successfully Deleted: TypeID: ${ this . typeID }` ) ; } ) ; } } , ( reason ) => { console . log ( reason ) ; } ) ; } public ngOnInit ( ) { this . typeService . getType ( this . typeID ) . subscribe ( ( typeInstanceResp ) => { this . typeInstance = typeInstanceResp ; } ) ; this . objectService . countObjectsByType ( this . typeID ) . subscribe ( ( count ) => { this . numberOfObjects = count ; } ) ; } }