forked from ng-bootstrap/ng-bootstrap
/
modal-ref.ts
127 lines (111 loc) · 3.65 KB
/
modal-ref.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import {ComponentRef} from '@angular/core';
import {NgbModalBackdrop} from './modal-backdrop';
import {NgbModalWindow} from './modal-window';
import {ContentRef} from '../util/popup';
/**
* A reference to the currently opened (active) modal.
*
* Instances of this class can be injected into your component passed as modal content.
* So you can `.close()` or `.dismiss()` the modal window from your component.
*/
export class NgbActiveModal {
/**
* Closes the modal with an optional `result` value.
*
* The `NgbMobalRef.result` promise will be resolved with the provided value.
*/
close(result?: any): void {}
/**
* Dismisses the modal with an optional `reason` value.
*
* The `NgbModalRef.result` promise will be rejected with the provided value.
*/
dismiss(reason?: any): void {}
}
/**
* A reference to the newly opened modal returned by the `NgbModal.open()` method.
*/
export class NgbModalRef<T = any> {
private _resolve: (result?: any) => void;
private _reject: (reason?: any) => void;
/**
* The instance of a component used for the modal content.
*
* When a `TemplateRef` is used as the content or when the modal is closed, will return `undefined`.
*/
get componentInstance(): T extends new (...args: any[]) => any? InstanceType<T>: undefined {
if (this._contentRef && this._contentRef.componentRef) {
return this._contentRef.componentRef.instance;
}
}
/**
* The promise that is resolved when the modal is closed and rejected when the modal is dismissed.
*/
result: Promise<any>;
constructor(
private _windowCmptRef: ComponentRef<NgbModalWindow>, private _contentRef: ContentRef,
private _backdropCmptRef?: ComponentRef<NgbModalBackdrop>, private _beforeDismiss?: Function) {
_windowCmptRef.instance.dismissEvent.subscribe((reason: any) => { this.dismiss(reason); });
this.result = new Promise((resolve, reject) => {
this._resolve = resolve;
this._reject = reject;
});
this.result.then(null, () => {});
}
/**
* Closes the modal with an optional `result` value.
*
* The `NgbMobalRef.result` promise will be resolved with the provided value.
*/
close(result?: any): void {
if (this._windowCmptRef) {
this._resolve(result);
this._removeModalElements();
}
}
private _dismiss(reason?: any) {
this._reject(reason);
this._removeModalElements();
}
/**
* Dismisses the modal with an optional `reason` value.
*
* The `NgbModalRef.result` promise will be rejected with the provided value.
*/
dismiss(reason?: any): void {
if (this._windowCmptRef) {
if (!this._beforeDismiss) {
this._dismiss(reason);
} else {
const dismiss = this._beforeDismiss();
if (dismiss && dismiss.then) {
dismiss.then(
result => {
if (result !== false) {
this._dismiss(reason);
}
},
() => {});
} else if (dismiss !== false) {
this._dismiss(reason);
}
}
}
}
private _removeModalElements() {
const windowNativeEl = this._windowCmptRef.location.nativeElement;
windowNativeEl.parentNode.removeChild(windowNativeEl);
this._windowCmptRef.destroy();
if (this._backdropCmptRef) {
const backdropNativeEl = this._backdropCmptRef.location.nativeElement;
backdropNativeEl.parentNode.removeChild(backdropNativeEl);
this._backdropCmptRef.destroy();
}
if (this._contentRef && this._contentRef.viewRef) {
this._contentRef.viewRef.destroy();
}
this._windowCmptRef = null;
this._backdropCmptRef = null;
this._contentRef = null;
}
}