1 import {Component, Input, OnInit, ViewChild, TemplateRef, EventEmitter} from '@angular/core';
2 import {Observable, Observer} from 'rxjs';
3 import {NgbModal, NgbModalRef, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
6 * Dialog base class. Handles the ngbModal logic.
7 * Sub-classed component templates must have a #dialogContent selector
8 * at the root of the template (see ConfirmDialogComponent).
10 * Dialogs interact with the caller via Observable.
12 * dialog.open().subscribe(
13 * value => handleValue(value),
14 * error => handleError(error),
15 * () => console.debug('dialog closed')
18 * It is up to the dialog implementer to decide what values to
19 * pass to the caller via the dialog.respond(data) and/or
20 * dialog.close(data) methods.
22 * dialog.close(...) closes the modal window and completes the
23 * observable, unless an error was previously passed, in which
24 * case the observable is already complete.
26 * dialog.close() with no data closes the dialog without passing
27 * any values to the caller.
31 selector: 'eg-dialog',
32 template: '<ng-template></ng-template>'
34 export class DialogComponent implements OnInit {
36 // Assume all dialogs support a title attribute.
37 @Input() public dialogTitle: string;
39 // Pointer to the dialog content template.
40 @ViewChild('dialogContent')
41 private dialogContent: TemplateRef<any>;
43 // Emitted after open() is called on the ngbModal.
44 // Note when overriding open(), this will not fire unless also
45 // called in the overridding method.
46 onOpen$ = new EventEmitter<any>();
48 // How we relay responses to the caller.
49 observer: Observer<any>;
51 // The modalRef allows direct control of the modal instance.
52 private modalRef: NgbModalRef = null;
54 constructor(private modalService: NgbModal) {}
57 this.onOpen$ = new EventEmitter<any>();
60 open(options?: NgbModalOptions): Observable<any> {
62 if (this.modalRef !== null) {
63 this.error('Dialog was replaced!');
67 this.modalRef = this.modalService.open(this.dialogContent, options);
70 // Let the digest cycle complete
71 setTimeout(() => this.onOpen$.emit(true));
74 return new Observable(observer => {
75 this.observer = observer;
77 this.modalRef.result.then(
78 // Results are relayed to the caller via our observer.
79 // Our Observer is marked complete via this.close().
80 // Nothing to do here.
83 // Modal was dismissed via UI control which
84 // bypasses call to this.close()
85 dismissed => this.finalize()
90 // Send a response to the caller without closing the dialog.
92 if (this.observer && value !== undefined) {
93 this.observer.next(value);
97 // Sends error event to the caller and closes the dialog.
98 // Once an error is sent, our observable is complete and
99 // cannot be used again to send any messages.
100 error(value: any, close?: boolean) {
102 console.error('Dialog produced error', value);
103 this.observer.error(value);
104 this.observer = null;
106 if (this.modalRef) { this.modalRef.close(); }
110 // Close the dialog, optionally with a value to relay to the caller.
111 // Calling close() with no value simply dismisses the dialog.
114 if (this.modalRef) { this.modalRef.close(); }
119 console.warn('Dialog.dismiss() is deprecated. Use close() instead');
123 // Clean up after closing the dialog.
125 if (this.observer) { // null if this.error() called
126 this.observer.complete();
127 this.observer = null;
129 this.modalRef = null;