1 import {Component, Input, OnInit, ViewChild, TemplateRef, EventEmitter} from '@angular/core';
2 import {NgbModal, NgbModalRef, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
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).
10 export interface DialogRejectionResponse {
11 // Did the user simply close the dialog without performing an action.
13 // Relays error, etc. messages from the dialog handler to the caller.
18 selector: 'eg-dialog',
19 template: '<ng-template></ng-template>'
21 export class DialogComponent implements OnInit {
23 // Assume all dialogs support a title attribute.
24 @Input() public dialogTitle: string;
26 // Pointer to the dialog content template.
27 @ViewChild('dialogContent')
28 private dialogContent: TemplateRef<any>;
30 // Emitted after open() is called on the ngbModal.
31 // Note when overriding open(), this will not fire unless also
32 // called in the overridding method.
33 onOpen$ = new EventEmitter<any>();
35 // The modalRef allows direct control of the modal instance.
36 private modalRef: NgbModalRef = null;
38 constructor(private modalService: NgbModal) {}
41 this.onOpen$ = new EventEmitter<any>();
44 async open(options?: NgbModalOptions): Promise<any> {
46 if (this.modalRef !== null) {
47 console.warn('Dismissing existing dialog');
51 this.modalRef = this.modalService.open(this.dialogContent, options);
54 // Let the digest cycle complete
55 setTimeout(() => this.onOpen$.emit(true));
58 return new Promise( (resolve, reject) => {
60 this.modalRef.result.then(
67 // NgbModal creates some result values for us, which
68 // are outside of our control. Other dismissal
69 // reasons are agreed upon by implementing subclasses.
70 console.debug('dialog closed with ' + result);
73 result === 0 // body click
74 || result === 1 // Esc key
75 || result === 'canceled' // Cancel button
76 || result === 'cross_click' // modal top-right X
79 const rejection: DialogRejectionResponse = {
91 close(reason?: any): void {
93 this.modalRef.close(reason);
97 dismiss(reason?: any): void {
99 this.modalRef.dismiss(reason);