]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
608f9b76245960521c2eb46ca3f43f905115290f
[ceph-ci.git] /
1 import { Component, OnDestroy, OnInit, TemplateRef } from '@angular/core';
2 import { UntypedFormGroup } 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   showCancel = true;
28
29   // Component only
30   boundCancel = this.cancel.bind(this);
31   confirmationForm: UntypedFormGroup;
32   private canceled = false;
33
34   constructor(public activeModal: NgbActiveModal) {
35     this.confirmationForm = new UntypedFormGroup({});
36   }
37
38   ngOnInit() {
39     this.bodyContext = this.bodyContext || {};
40     this.bodyContext['$implicit'] = this.bodyData;
41     if (!this.onSubmit) {
42       throw new Error('No submit action defined');
43     } else if (!this.buttonText) {
44       throw new Error('No action name defined');
45     } else if (!this.titleText) {
46       throw new Error('No title defined');
47     } else if (!this.bodyTpl && !this.description) {
48       throw new Error('No description defined');
49     }
50   }
51
52   ngOnDestroy() {
53     if (this.onCancel && this.canceled) {
54       this.onCancel();
55     }
56   }
57
58   cancel() {
59     this.canceled = true;
60     this.activeModal.close();
61   }
62
63   stopLoadingSpinner() {
64     this.confirmationForm.setErrors({ cdSubmitButton: true });
65   }
66 }