]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
56543ba6224c9de195c5ab1dd81aafca0f56376e
[ceph.git] /
1 import {
2   Component, OnInit, TemplateRef, ViewChild
3 } from '@angular/core';
4 import { FormControl, FormGroup, Validators } from '@angular/forms';
5
6 import { BsModalRef } from 'ngx-bootstrap';
7 import { Observable } from 'rxjs/Observable';
8
9 import { SubmitButtonComponent } from '../submit-button/submit-button.component';
10
11 @Component({
12   selector: 'cd-deletion-modal',
13   templateUrl: './deletion-modal.component.html',
14   styleUrls: ['./deletion-modal.component.scss']
15 })
16 export class DeletionModalComponent implements OnInit {
17   @ViewChild(SubmitButtonComponent) 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: FormGroup;
26   confirmation: FormControl;
27
28   // Parameters are destructed here than assigned to specific types and marked as optional
29   setUp({modalRef, metaType, deletionMethod, pattern, deletionObserver, description}:
30           { modalRef: BsModalRef, metaType: string, deletionMethod?: Function, pattern?: string,
31             deletionObserver?: () => Observable<any>, description?: TemplateRef<any>}) {
32     if (!modalRef) {
33       throw new Error('No modal reference');
34     } else if (!metaType) {
35       throw new Error('No meta type');
36     } else if (!(deletionMethod || deletionObserver)) {
37       throw new Error('No deletion method');
38     }
39     this.metaType = metaType;
40     this.modalRef = modalRef;
41     this.deletionMethod = deletionMethod;
42     this.pattern = pattern || this.pattern;
43     this.deletionObserver = deletionObserver;
44     this.description = description;
45   }
46
47   ngOnInit() {
48     this.confirmation = new FormControl('', {
49       validators: [
50         Validators.required
51       ],
52       updateOn: 'blur'
53     });
54     this.deletionForm = new FormGroup({
55       confirmation: this.confirmation
56     });
57   }
58
59   invalidControl(submitted: boolean, error?: string): boolean {
60     const control = this.confirmation;
61     return !!(
62       (submitted || control.dirty) &&
63       control.invalid &&
64       (error ? control.errors[error] : true)
65     );
66   }
67
68   updateConfirmation($e) {
69     if ($e.key !== 'Enter') {
70       return;
71     }
72     this.confirmation.setValue($e.target.value);
73     this.confirmation.markAsDirty();
74     this.confirmation.updateValueAndValidity();
75   }
76
77   delete () {
78     this.submitButton.submit();
79   }
80
81   deletionCall() {
82     if (this.deletionObserver) {
83       this.deletionObserver().subscribe(
84         undefined,
85         this.stopLoadingSpinner.bind(this),
86         this.hideModal.bind(this)
87       );
88     } else {
89       this.deletionMethod();
90     }
91   }
92
93   hideModal() {
94     this.modalRef.hide();
95   }
96
97   stopLoadingSpinner() {
98     this.deletionForm.setErrors({'cdSubmitButton': true});
99   }
100 }