]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/dialog/dialog.component.ts
LP 2061136 follow-up: ng lint --fix
[working/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}) dialogContent: TemplateRef<any>;
48
49     identifier: number = DialogComponent.counter++;
50
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>();
55
56     // How we relay responses to the caller.
57     observer: Observer<any>;
58
59     // The modalRef allows direct control of the modal instance.
60     protected modalRef: NgbModalRef = null;
61
62     constructor(private modalService: NgbModal) {}
63
64     // Close all active dialogs
65     static closeAll() {
66         Object.keys(DialogComponent.instances).forEach(id => {
67             if (DialogComponent.instances[id]) {
68                 DialogComponent.instances[id].close();
69                 delete DialogComponent.instances[id];
70             }
71         });
72     }
73
74     ngOnInit() {
75         this.onOpen$ = new EventEmitter<any>();
76     }
77
78     open(options: NgbModalOptions = { backdrop: 'static' }): Observable<any> {
79
80         if (this.modalRef !== null) {
81             this.error('Dialog was replaced!');
82             this.finalize();
83         }
84
85         // force backdrop to static if caller passed in any options
86         options.backdrop = 'static';
87
88         this.modalRef = this.modalService.open(this.dialogContent, options);
89         DialogComponent.instances[this.identifier] = this;
90
91         if (this.onOpen$) {
92             // Let the digest cycle complete
93             setTimeout(() => this.onOpen$.emit(true));
94         }
95
96         return new Observable(observer => {
97             this.observer = observer;
98
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.
103                 result => {},
104
105                 // Modal was dismissed via UI control which
106                 // bypasses call to this.close()
107                 dismissed => this.finalize()
108             );
109         });
110     }
111
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);
116         }
117     }
118
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) {
123         if (this.observer) {
124             console.error('Dialog produced error', value);
125             this.observer.error(value);
126             this.observer = null;
127         }
128         if (this.modalRef) { this.modalRef.close(); }
129         this.finalize();
130     }
131
132     // Close the dialog, optionally with a value to relay to the caller.
133     // Calling close() with no value simply dismisses the dialog.
134     close(value?: any) {
135         this.respond(value);
136         if (this.modalRef) { this.modalRef.close(); }
137         this.finalize();
138     }
139
140     dismiss() {
141         console.warn('Dialog.dismiss() is deprecated.  Use close() instead');
142         this.close();
143     }
144
145     // Clean up after closing the dialog.
146     finalize() {
147         if (this.observer) { // null if this.error() called
148             this.observer.complete();
149             this.observer = null;
150         }
151         this.modalRef = null;
152         delete DialogComponent.instances[this.identifier];
153     }
154
155 }
156
157