]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
fe56249816a01fb794725badaf8612f026fb3876
[ceph.git] /
1 import { Component, OnDestroy, OnInit, TemplateRef } from '@angular/core';
2 import { FormGroup } from '@angular/forms';
3
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5
6 @Component({
7   selector: 'cd-confirmation-modal',
8   templateUrl: './confirmation-modal.component.html',
9   styleUrls: ['./confirmation-modal.component.scss']
10 })
11 export class ConfirmationModalComponent implements OnInit, OnDestroy {
12   // Needed
13   buttonText: string;
14   titleText: string;
15   onSubmit: Function;
16
17   // One of them is needed
18   bodyTpl?: TemplateRef<any>;
19   description?: TemplateRef<any>;
20
21   // Optional
22   warning = false;
23   bodyData?: object;
24   onCancel?: Function;
25   bodyContext?: object;
26   showSubmit = true;
27
28   // Component only
29   boundCancel = this.cancel.bind(this);
30   confirmationForm: FormGroup;
31   private canceled = false;
32
33   constructor(public activeModal: NgbActiveModal) {
34     this.confirmationForm = new FormGroup({});
35   }
36
37   ngOnInit() {
38     this.bodyContext = this.bodyContext || {};
39     this.bodyContext['$implicit'] = this.bodyData;
40     if (!this.onSubmit) {
41       throw new Error('No submit action defined');
42     } else if (!this.buttonText) {
43       throw new Error('No action name defined');
44     } else if (!this.titleText) {
45       throw new Error('No title defined');
46     } else if (!this.bodyTpl && !this.description) {
47       throw new Error('No description defined');
48     }
49   }
50
51   ngOnDestroy() {
52     if (this.onCancel && this.canceled) {
53       this.onCancel();
54     }
55   }
56
57   cancel() {
58     this.canceled = true;
59     this.activeModal.close();
60   }
61
62   stopLoadingSpinner() {
63     this.confirmationForm.setErrors({ cdSubmitButton: true });
64   }
65 }