]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
e819fd1a6880407fb30fd2720817d6afa2014f85
[ceph.git] /
1 import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3
4 import { BsModalRef } from 'ngx-bootstrap';
5 import { Observable } from 'rxjs';
6
7 import { CdFormGroup } from '../../forms/cd-form-group';
8 import { SubmitButtonComponent } from '../submit-button/submit-button.component';
9
10 @Component({
11   selector: 'cd-deletion-modal',
12   templateUrl: './deletion-modal.component.html',
13   styleUrls: ['./deletion-modal.component.scss']
14 })
15 export class DeletionModalComponent implements OnInit {
16   @ViewChild(SubmitButtonComponent)
17   submitButton: SubmitButtonComponent;
18   description: TemplateRef<any>;
19   metaType: string;
20   pattern = 'yes';
21   deletionObserver: () => Observable<any>;
22   deletionMethod: Function;
23   modalRef: BsModalRef;
24
25   deletionForm: CdFormGroup;
26   confirmation: FormControl;
27
28   // Parameters are destructed here than assigned to specific types and marked as optional
29   setUp({
30     modalRef,
31     metaType,
32     deletionMethod,
33     pattern,
34     deletionObserver,
35     description
36   }: {
37     modalRef: BsModalRef;
38     metaType: string;
39     deletionMethod?: Function;
40     pattern?: string;
41     deletionObserver?: () => Observable<any>;
42     description?: TemplateRef<any>;
43   }) {
44     if (!modalRef) {
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');
50     }
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;
57   }
58
59   ngOnInit() {
60     this.confirmation = new FormControl('', {
61       validators: [Validators.required],
62       updateOn: 'blur'
63     });
64     this.deletionForm = new CdFormGroup({
65       confirmation: this.confirmation
66     });
67   }
68
69   updateConfirmation($e) {
70     if ($e.key !== 'Enter') {
71       return;
72     }
73     this.confirmation.setValue($e.target.value);
74     this.confirmation.markAsDirty();
75     this.confirmation.updateValueAndValidity();
76   }
77
78   deletionCall() {
79     if (this.deletionObserver) {
80       this.deletionObserver().subscribe(
81         undefined,
82         this.stopLoadingSpinner.bind(this),
83         this.hideModal.bind(this)
84       );
85     } else {
86       this.deletionMethod();
87     }
88   }
89
90   hideModal() {
91     this.modalRef.hide();
92   }
93
94   stopLoadingSpinner() {
95     this.deletionForm.setErrors({ cdSubmitButton: true });
96   }
97
98   escapeRegExp(text) {
99     return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
100   }
101 }