/
overlay-container.ts
100 lines (85 loc) · 3.83 KB
/
overlay-container.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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {DOCUMENT} from '@angular/common';
import {Inject, Injectable, OnDestroy} from '@angular/core';
import {Platform} from '@angular/cdk/platform';
declare const __karma__: unknown;
declare const jasmine: unknown;
declare const jest: unknown;
declare const Mocha: unknown;
/**
* Whether we're in a testing environment.
* TODO(crisbeto): remove this once we have an overlay testing module or Angular starts tearing
* down the testing `NgModule` (see https://github.com/angular/angular/issues/18831).
*/
const isTestEnvironment = (typeof __karma__ !== 'undefined' && !!__karma__) ||
(typeof jasmine !== 'undefined' && !!jasmine) ||
(typeof jest !== 'undefined' && !!jest) ||
(typeof Mocha !== 'undefined' && !!Mocha);
/** Container inside which all overlays will render. */
@Injectable({providedIn: 'root'})
export class OverlayContainer implements OnDestroy {
protected _containerElement: HTMLElement;
protected _document: Document;
constructor(@Inject(DOCUMENT) document: any, protected _platform: Platform) {
this._document = document;
}
ngOnDestroy() {
const container = this._containerElement;
if (container && container.parentNode) {
container.parentNode.removeChild(container);
}
}
/**
* This method returns the overlay container element. It will lazily
* create the element the first time it is called to facilitate using
* the container in non-browser environments.
* @returns the container element
*/
getContainerElement(): HTMLElement {
if (!this._containerElement) {
this._createContainer();
}
return this._containerElement;
}
/**
* Create the overlay container element, which is simply a div
* with the 'cdk-overlay-container' class on the document body.
*/
protected _createContainer(): void {
const containerClass = 'cdk-overlay-container';
if (this._platform.isBrowser || isTestEnvironment) {
const oppositePlatformContainers =
this._document.querySelectorAll(`.${containerClass}[platform="server"], ` +
`.${containerClass}[platform="test"]`);
// Remove any old containers from the opposite platform.
// This can happen when transitioning from the server to the client.
for (let i = 0; i < oppositePlatformContainers.length; i++) {
oppositePlatformContainers[i].parentNode!.removeChild(oppositePlatformContainers[i]);
}
}
const container = this._document.createElement('div');
container.classList.add(containerClass);
// A long time ago we kept adding new overlay containers whenever a new app was instantiated,
// but at some point we added logic which clears the duplicate ones in order to avoid leaks.
// The new logic was a little too aggressive since it was breaking some legitimate use cases.
// To mitigate the problem we made it so that only containers from a different platform are
// cleared, but the side-effect was that people started depending on the overly-aggressive
// logic to clean up their tests for them. Until we can introduce an overlay-specific testing
// module which does the cleanup, we try to detect that we're in a test environment and we
// always clear the container. See #17006.
// TODO(crisbeto): remove the test environment check once we have an overlay testing module.
if (isTestEnvironment) {
container.setAttribute('platform', 'test');
} else if (!this._platform.isBrowser) {
container.setAttribute('platform', 'server');
}
this._document.body.appendChild(container);
this._containerElement = container;
}
}