1 import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
4 import { BsModalRef } from 'ngx-bootstrap';
5 import { Observable } from 'rxjs';
7 import { CdFormGroup } from '../../forms/cd-form-group';
8 import { SubmitButtonComponent } from '../submit-button/submit-button.component';
11 selector: 'cd-deletion-modal',
12 templateUrl: './deletion-modal.component.html',
13 styleUrls: ['./deletion-modal.component.scss']
15 export class DeletionModalComponent implements OnInit {
16 @ViewChild(SubmitButtonComponent)
17 submitButton: SubmitButtonComponent;
18 description: TemplateRef<any>;
21 deletionObserver: () => Observable<any>;
22 deletionMethod: Function;
25 deletionForm: CdFormGroup;
26 confirmation: FormControl;
28 // Parameters are destructed here than assigned to specific types and marked as optional
39 deletionMethod?: Function;
41 deletionObserver?: () => Observable<any>;
42 description?: TemplateRef<any>;
45 throw new Error('No modal reference');
46 } else if (!metaType) {
47 throw new Error('No meta type');
48 } else if (!(deletionMethod || deletionObserver)) {
49 throw new Error('No deletion method');
51 this.metaType = metaType;
52 this.modalRef = modalRef;
53 this.deletionMethod = deletionMethod;
54 this.pattern = pattern || this.pattern;
55 this.deletionObserver = deletionObserver;
56 this.description = description;
60 this.confirmation = new FormControl('', {
61 validators: [Validators.required],
64 this.deletionForm = new CdFormGroup({
65 confirmation: this.confirmation
69 updateConfirmation($e) {
70 if ($e.key !== 'Enter') {
73 this.confirmation.setValue($e.target.value);
74 this.confirmation.markAsDirty();
75 this.confirmation.updateValueAndValidity();
79 if (this.deletionObserver) {
80 this.deletionObserver().subscribe(
82 this.stopLoadingSpinner.bind(this),
83 this.hideModal.bind(this)
86 this.deletionMethod();
94 stopLoadingSpinner() {
95 this.deletionForm.setErrors({ cdSubmitButton: true });
99 return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');