]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/dialog/dialog.component.ts
LP#1775466 Angular(6) base application
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / share / dialog / dialog.component.ts
1 import {Component, Input, OnInit, ViewChild, TemplateRef, EventEmitter} from '@angular/core';
2 import {NgbModal, NgbModalRef, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
3
4 /**
5  * Dialog base class.  Handles the ngbModal logic.
6  * Sub-classed component templates must have a #dialogContent selector
7  * at the root of the template (see ConfirmDialogComponent).
8  */
9
10 @Component({
11     selector: 'eg-dialog',
12     template: '<ng-template></ng-template>'
13 })
14 export class DialogComponent implements OnInit {
15
16     // Assume all dialogs support a title attribute.
17     @Input() public dialogTitle: string;
18
19     // Pointer to the dialog content template.
20     @ViewChild('dialogContent')
21     private dialogContent: TemplateRef<any>;
22
23     // Emitted after open() is called on the ngbModal.
24     // Note when overriding open(), this will not fire unless also
25     // called in the overridding method.
26     onOpen$ = new EventEmitter<any>();
27
28     // The modalRef allows direct control of the modal instance.
29     private modalRef: NgbModalRef = null;
30
31     constructor(private modalService: NgbModal) {}
32
33     ngOnInit() {
34         this.onOpen$ = new EventEmitter<any>();
35     }
36
37     open(options?: NgbModalOptions): Promise<any> {
38
39         if (this.modalRef !== null) {
40             console.warn('Dismissing existing dialog');
41             this.dismiss();
42         }
43
44         this.modalRef = this.modalService.open(this.dialogContent, options);
45
46         if (this.onOpen$) {
47             // Let the digest cycle complete
48             setTimeout(() => this.onOpen$.emit(true));
49         }
50
51         return new Promise( (resolve, reject) => {
52
53             this.modalRef.result.then(
54                 (result) => {
55                     resolve(result);
56                     this.modalRef = null;
57                 },
58                 (result) => {
59                     console.debug('dialog closed with ' + result);
60                     reject(result);
61                     this.modalRef = null;
62                 }
63             );
64         });
65     }
66
67     close(reason?: any): void {
68         if (this.modalRef) {
69             this.modalRef.close(reason);
70         }
71     }
72
73     dismiss(reason?: any): void {
74         if (this.modalRef) {
75             this.modalRef.dismiss(reason);
76         }
77     }
78 }
79
80