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