forked from ng-bootstrap/ng-bootstrap
/
modal-stack.ts
224 lines (197 loc) · 8.83 KB
/
modal-stack.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import {DOCUMENT} from '@angular/common';
import {
ApplicationRef,
ComponentFactoryResolver,
ComponentRef,
Inject,
Injectable,
Injector,
NgZone,
RendererFactory2,
TemplateRef
} from '@angular/core';
import {Subject} from 'rxjs';
import {ngbFocusTrap} from '../util/focus-trap';
import {ContentRef} from '../util/popup';
import {ScrollBar} from '../util/scrollbar';
import {isDefined, isString} from '../util/util';
import {NgbModalBackdrop} from './modal-backdrop';
import {NgbModalOptions} from './modal-config';
import {NgbActiveModal, NgbModalRef} from './modal-ref';
import {NgbModalWindow} from './modal-window';
@Injectable({providedIn: 'root'})
export class NgbModalStack {
private _activeWindowCmptHasChanged = new Subject();
private _ariaHiddenValues: Map<Element, string> = new Map();
private _backdropAttributes = ['backdropClass'];
private _modalRefs: NgbModalRef[] = [];
private _windowAttributes =
['ariaLabelledBy', 'backdrop', 'centered', 'keyboard', 'scrollable', 'size', 'windowClass'];
private _windowCmpts: ComponentRef<NgbModalWindow>[] = [];
constructor(
private _applicationRef: ApplicationRef, private _injector: Injector, @Inject(DOCUMENT) private _document: any,
private _scrollBar: ScrollBar, private _rendererFactory: RendererFactory2, private _ngZone: NgZone) {
// Trap focus on active WindowCmpt
this._activeWindowCmptHasChanged.subscribe(() => {
if (this._windowCmpts.length) {
const activeWindowCmpt = this._windowCmpts[this._windowCmpts.length - 1];
ngbFocusTrap(this._ngZone, activeWindowCmpt.location.nativeElement, this._activeWindowCmptHasChanged);
this._revertAriaHidden();
this._setAriaHidden(activeWindowCmpt.location.nativeElement);
}
});
}
open(moduleCFR: ComponentFactoryResolver, contentInjector: Injector, content: any, options): NgbModalRef {
const containerEl =
isDefined(options.container) ? this._document.querySelector(options.container) : this._document.body;
const renderer = this._rendererFactory.createRenderer(null, null);
const revertPaddingForScrollBar = this._scrollBar.compensate();
const removeBodyClass = () => {
if (!this._modalRefs.length) {
renderer.removeClass(this._document.body, 'modal-open');
this._revertAriaHidden();
}
};
if (!containerEl) {
throw new Error(`The specified modal container "${options.container || 'body'}" was not found in the DOM.`);
}
const activeModal = new NgbActiveModal();
const contentRef =
this._getContentRef(moduleCFR, options.injector || contentInjector, content, activeModal, options);
let backdropCmptRef: ComponentRef<NgbModalBackdrop> =
options.backdrop !== false ? this._attachBackdrop(moduleCFR, containerEl) : null;
let windowCmptRef: ComponentRef<NgbModalWindow> = this._attachWindowComponent(moduleCFR, containerEl, contentRef);
let ngbModalRef: NgbModalRef = new NgbModalRef(windowCmptRef, contentRef, backdropCmptRef, options.beforeDismiss);
this._registerModalRef(ngbModalRef);
this._registerWindowCmpt(windowCmptRef);
ngbModalRef.result.then(revertPaddingForScrollBar, revertPaddingForScrollBar);
ngbModalRef.result.then(removeBodyClass, removeBodyClass);
activeModal.close = (result: any) => { ngbModalRef.close(result); };
activeModal.dismiss = (reason: any) => { ngbModalRef.dismiss(reason); };
this._applyWindowOptions(windowCmptRef.instance, options);
if (this._modalRefs.length === 1) {
renderer.addClass(this._document.body, 'modal-open');
}
if (backdropCmptRef && backdropCmptRef.instance) {
this._applyBackdropOptions(backdropCmptRef.instance, options);
}
return ngbModalRef;
}
dismissAll(reason?: any) { this._modalRefs.forEach(ngbModalRef => ngbModalRef.dismiss(reason)); }
hasOpenModals(): boolean { return this._modalRefs.length > 0; }
private _attachBackdrop(moduleCFR: ComponentFactoryResolver, containerEl: any): ComponentRef<NgbModalBackdrop> {
let backdropFactory = moduleCFR.resolveComponentFactory(NgbModalBackdrop);
let backdropCmptRef = backdropFactory.create(this._injector);
this._applicationRef.attachView(backdropCmptRef.hostView);
containerEl.appendChild(backdropCmptRef.location.nativeElement);
return backdropCmptRef;
}
private _attachWindowComponent(moduleCFR: ComponentFactoryResolver, containerEl: any, contentRef: any):
ComponentRef<NgbModalWindow> {
let windowFactory = moduleCFR.resolveComponentFactory(NgbModalWindow);
let windowCmptRef = windowFactory.create(this._injector, contentRef.nodes);
this._applicationRef.attachView(windowCmptRef.hostView);
containerEl.appendChild(windowCmptRef.location.nativeElement);
return windowCmptRef;
}
private _applyWindowOptions(windowInstance: NgbModalWindow, options: Object): void {
this._windowAttributes.forEach((optionName: string) => {
if (isDefined(options[optionName])) {
windowInstance[optionName] = options[optionName];
}
});
}
private _applyBackdropOptions(backdropInstance: NgbModalBackdrop, options: Object): void {
this._backdropAttributes.forEach((optionName: string) => {
if (isDefined(options[optionName])) {
backdropInstance[optionName] = options[optionName];
}
});
}
private _getContentRef(
moduleCFR: ComponentFactoryResolver, contentInjector: Injector, content: any, activeModal: NgbActiveModal,
options: NgbModalOptions): ContentRef {
if (!content) {
return new ContentRef([]);
} else if (content instanceof TemplateRef) {
return this._createFromTemplateRef(content, activeModal);
} else if (isString(content)) {
return this._createFromString(content);
} else {
return this._createFromComponent(moduleCFR, contentInjector, content, activeModal, options);
}
}
private _createFromTemplateRef(content: TemplateRef<any>, activeModal: NgbActiveModal): ContentRef {
const context = {
$implicit: activeModal,
close(result) { activeModal.close(result); },
dismiss(reason) { activeModal.dismiss(reason); }
};
const viewRef = content.createEmbeddedView(context);
this._applicationRef.attachView(viewRef);
return new ContentRef([viewRef.rootNodes], viewRef);
}
private _createFromString(content: string): ContentRef {
const component = this._document.createTextNode(`${content}`);
return new ContentRef([[component]]);
}
private _createFromComponent(
moduleCFR: ComponentFactoryResolver, contentInjector: Injector, content: any, context: NgbActiveModal,
options: NgbModalOptions): ContentRef {
const contentCmptFactory = moduleCFR.resolveComponentFactory(content);
const modalContentInjector =
Injector.create({providers: [{provide: NgbActiveModal, useValue: context}], parent: contentInjector});
const componentRef = contentCmptFactory.create(modalContentInjector);
const componentNativeEl = componentRef.location.nativeElement;
if (options.scrollable) {
(componentNativeEl as HTMLElement).classList.add('component-host-scrollable');
}
this._applicationRef.attachView(componentRef.hostView);
// FIXME: we should here get rid of the component nativeElement
// and use `[Array.from(componentNativeEl.childNodes)]` instead and remove the above CSS class.
return new ContentRef([[componentNativeEl]], componentRef.hostView, componentRef);
}
private _setAriaHidden(element: Element) {
const parent = element.parentElement;
if (parent && element !== this._document.body) {
Array.from(parent.children).forEach(sibling => {
if (sibling !== element && sibling.nodeName !== 'SCRIPT') {
this._ariaHiddenValues.set(sibling, sibling.getAttribute('aria-hidden'));
sibling.setAttribute('aria-hidden', 'true');
}
});
this._setAriaHidden(parent);
}
}
private _revertAriaHidden() {
this._ariaHiddenValues.forEach((value, element) => {
if (value) {
element.setAttribute('aria-hidden', value);
} else {
element.removeAttribute('aria-hidden');
}
});
this._ariaHiddenValues.clear();
}
private _registerModalRef(ngbModalRef: NgbModalRef) {
const unregisterModalRef = () => {
const index = this._modalRefs.indexOf(ngbModalRef);
if (index > -1) {
this._modalRefs.splice(index, 1);
}
};
this._modalRefs.push(ngbModalRef);
ngbModalRef.result.then(unregisterModalRef, unregisterModalRef);
}
private _registerWindowCmpt(ngbWindowCmpt: ComponentRef<NgbModalWindow>) {
this._windowCmpts.push(ngbWindowCmpt);
this._activeWindowCmptHasChanged.next();
ngbWindowCmpt.onDestroy(() => {
const index = this._windowCmpts.indexOf(ngbWindowCmpt);
if (index > -1) {
this._windowCmpts.splice(index, 1);
this._activeWindowCmptHasChanged.next();
}
});
}
}