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 // Track instances so we can refer to them later in closeAll()
37 // NOTE this could also be done by importing router and subscribing
38 // to route events here, but that would require all subclassed
39 // components to import and pass the router via the constructor.
41 static instances: {[ident: number]: any} = {};
43 // Assume all dialogs support a title attribute.
44 @Input() public dialogTitle: string;
46 // Pointer to the dialog content template.
47 @ViewChild('dialogContent', {static: false}) dialogContent: TemplateRef<any>;
49 identifier: number = DialogComponent.counter++;
51 // Emitted after open() is called on the ngbModal.
52 // Note when overriding open(), this will not fire unless also
53 // called in the overridding method.
54 onOpen$ = new EventEmitter<any>();
56 // How we relay responses to the caller.
57 observer: Observer<any>;
59 // The modalRef allows direct control of the modal instance.
60 protected modalRef: NgbModalRef = null;
62 constructor(private modalService: NgbModal) {}
64 // Close all active dialogs
66 Object.keys(DialogComponent.instances).forEach(id => {
67 if (DialogComponent.instances[id]) {
68 DialogComponent.instances[id].close();
69 delete DialogComponent.instances[id];
75 this.onOpen$ = new EventEmitter<any>();
78 open(options: NgbModalOptions = { backdrop: 'static' }): Observable<any> {
80 if (this.modalRef !== null) {
81 this.error('Dialog was replaced!');
85 // force backdrop to static if caller passed in any options
86 options.backdrop = 'static';
88 this.modalRef = this.modalService.open(this.dialogContent, options);
89 DialogComponent.instances[this.identifier] = this;
92 // Let the digest cycle complete
93 setTimeout(() => this.onOpen$.emit(true));
96 return new Observable(observer => {
97 this.observer = observer;
99 this.modalRef.result.then(
100 // Results are relayed to the caller via our observer.
101 // Our Observer is marked complete via this.close().
102 // Nothing to do here.
105 // Modal was dismissed via UI control which
106 // bypasses call to this.close()
107 dismissed => this.finalize()
112 // Send a response to the caller without closing the dialog.
113 respond(value: any) {
114 if (this.observer && value !== undefined) {
115 this.observer.next(value);
119 // Sends error event to the caller and closes the dialog.
120 // Once an error is sent, our observable is complete and
121 // cannot be used again to send any messages.
122 error(value: any, close?: boolean) {
124 console.error('Dialog produced error', value);
125 this.observer.error(value);
126 this.observer = null;
128 if (this.modalRef) { this.modalRef.close(); }
132 // Close the dialog, optionally with a value to relay to the caller.
133 // Calling close() with no value simply dismisses the dialog.
136 if (this.modalRef) { this.modalRef.close(); }
141 console.warn('Dialog.dismiss() is deprecated. Use close() instead');
145 // Clean up after closing the dialog.
147 if (this.observer) { // null if this.error() called
148 this.observer.complete();
149 this.observer = null;
151 this.modalRef = null;
152 delete DialogComponent.instances[this.identifier];