]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/dialog/dialog.component.ts
cafbbcfe60cfd46989b85fbbe398a329039e56bb
[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 {Observable, Observer} from 'rxjs';
3 import {NgbModal, NgbModalRef, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
4
5 /**
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).
9  *
10  * Dialogs interact with the caller via Observable.
11  *
12  * dialog.open().subscribe(
13  *   value => handleValue(value),
14  *   error => handleError(error),
15  *   ()    => console.debug('dialog closed')
16  * );
17  *
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.
21  *
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.
25  *
26  * dialog.close() with no data closes the dialog without passing
27  * any values to the caller.
28  */
29
30 @Component({
31     selector: 'eg-dialog',
32     template: '<ng-template></ng-template>'
33 })
34 export class DialogComponent implements OnInit {
35
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.
40     static counter = 0;
41     static instances: {[ident: number]: any} = {};
42
43     // Assume all dialogs support a title attribute.
44     @Input() public dialogTitle: string;
45
46     // Pointer to the dialog content template.
47     @ViewChild('dialogContent', {static: false})
48     private dialogContent: TemplateRef<any>;
49
50     identifier: number = DialogComponent.counter++;
51
52     // Emitted after open() is called on the ngbModal.
53     // Note when overriding open(), this will not fire unless also
54     // called in the overridding method.
55     onOpen$ = new EventEmitter<any>();
56
57     // How we relay responses to the caller.
58     observer: Observer<any>;
59
60     // The modalRef allows direct control of the modal instance.
61     private modalRef: NgbModalRef = null;
62
63     constructor(private modalService: NgbModal) {}
64
65     // Close all active dialogs
66     static closeAll() {
67         Object.keys(DialogComponent.instances).forEach(id => {
68             if (DialogComponent.instances[id]) {
69                 DialogComponent.instances[id].close();
70                 delete DialogComponent.instances[id];
71             }
72         });
73     }
74
75     ngOnInit() {
76         this.onOpen$ = new EventEmitter<any>();
77     }
78
79     open(options?: NgbModalOptions): Observable<any> {
80
81         if (this.modalRef !== null) {
82             this.error('Dialog was replaced!');
83             this.finalize();
84         }
85
86         this.modalRef = this.modalService.open(this.dialogContent, options);
87         DialogComponent.instances[this.identifier] = this;
88
89         if (this.onOpen$) {
90             // Let the digest cycle complete
91             setTimeout(() => this.onOpen$.emit(true));
92         }
93
94         return new Observable(observer => {
95             this.observer = observer;
96
97             this.modalRef.result.then(
98                 // Results are relayed to the caller via our observer.
99                 // Our Observer is marked complete via this.close().
100                 // Nothing to do here.
101                 result => {},
102
103                 // Modal was dismissed via UI control which
104                 // bypasses call to this.close()
105                 dismissed => this.finalize()
106             );
107         });
108     }
109
110     // Send a response to the caller without closing the dialog.
111     respond(value: any) {
112         if (this.observer && value !== undefined) {
113             this.observer.next(value);
114         }
115     }
116
117     // Sends error event to the caller and closes the dialog.
118     // Once an error is sent, our observable is complete and
119     // cannot be used again to send any messages.
120     error(value: any, close?: boolean) {
121         if (this.observer) {
122             console.error('Dialog produced error', value);
123             this.observer.error(value);
124             this.observer = null;
125         }
126         if (this.modalRef) { this.modalRef.close(); }
127         this.finalize();
128     }
129
130     // Close the dialog, optionally with a value to relay to the caller.
131     // Calling close() with no value simply dismisses the dialog.
132     close(value?: any) {
133         this.respond(value);
134         if (this.modalRef) { this.modalRef.close(); }
135         this.finalize();
136     }
137
138     dismiss() {
139         console.warn('Dialog.dismiss() is deprecated.  Use close() instead');
140         this.close();
141     }
142
143     // Clean up after closing the dialog.
144     finalize() {
145         if (this.observer) { // null if this.error() called
146             this.observer.complete();
147             this.observer = null;
148         }
149         this.modalRef = null;
150         delete DialogComponent.instances[this.identifier];
151     }
152
153 }
154
155