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).
11 selector: 'eg-dialog',
12 template: '<ng-template></ng-template>'
14 export class DialogComponent implements OnInit {
16 // Assume all dialogs support a title attribute.
17 @Input() public dialogTitle: string;
19 // Pointer to the dialog content template.
20 @ViewChild('dialogContent')
21 private dialogContent: TemplateRef<any>;
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>();
28 // The modalRef allows direct control of the modal instance.
29 private modalRef: NgbModalRef = null;
31 constructor(private modalService: NgbModal) {}
34 this.onOpen$ = new EventEmitter<any>();
37 open(options?: NgbModalOptions): Promise<any> {
39 if (this.modalRef !== null) {
40 console.warn('Dismissing existing dialog');
44 this.modalRef = this.modalService.open(this.dialogContent, options);
47 // Let the digest cycle complete
48 setTimeout(() => this.onOpen$.emit(true));
51 return new Promise( (resolve, reject) => {
53 this.modalRef.result.then(
59 console.debug('dialog closed with ' + result);
67 close(reason?: any): void {
69 this.modalRef.close(reason);
73 dismiss(reason?: any): void {
75 this.modalRef.dismiss(reason);