]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
bdd233cefc1ce905ed234e1827966939166ef8d0
[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   bodyData?: object;
23   onCancel?: Function;
24   bodyContext?: object;
25
26   // Component only
27   boundCancel = this.cancel.bind(this);
28   confirmationForm: FormGroup;
29   private canceled = false;
30
31   constructor(public activeModal: NgbActiveModal) {
32     this.confirmationForm = new FormGroup({});
33   }
34
35   ngOnInit() {
36     this.bodyContext = this.bodyContext || {};
37     this.bodyContext['$implicit'] = this.bodyData;
38     if (!this.onSubmit) {
39       throw new Error('No submit action defined');
40     } else if (!this.buttonText) {
41       throw new Error('No action name defined');
42     } else if (!this.titleText) {
43       throw new Error('No title defined');
44     } else if (!this.bodyTpl && !this.description) {
45       throw new Error('No description defined');
46     }
47   }
48
49   ngOnDestroy() {
50     if (this.onCancel && this.canceled) {
51       this.onCancel();
52     }
53   }
54
55   cancel() {
56     this.canceled = true;
57     this.activeModal.close();
58   }
59
60   stopLoadingSpinner() {
61     this.confirmationForm.setErrors({ cdSubmitButton: true });
62   }
63 }