6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { Injectable } from '@angular/core' ;
9
+ import { Injectable , Inject , OnDestroy } from '@angular/core' ;
10
10
import { OverlayContainer } from './overlay-container' ;
11
+ import { DOCUMENT } from '@angular/common' ;
12
+
11
13
12
14
/**
13
15
* Alternative to OverlayContainer that supports correct displaying of overlay elements in
@@ -17,7 +19,22 @@ import {OverlayContainer} from './overlay-container';
17
19
* Should be provided in the root component.
18
20
*/
19
21
@Injectable ( )
20
- export class FullscreenOverlayContainer extends OverlayContainer {
22
+ export class FullscreenOverlayContainer extends OverlayContainer implements OnDestroy {
23
+ private _fullScreenEventName : string | undefined ;
24
+ private _fullScreenListener : ( ) => void ;
25
+
26
+ constructor ( @Inject ( DOCUMENT ) _document : any ) {
27
+ super ( _document ) ;
28
+ }
29
+
30
+ ngOnDestroy ( ) {
31
+ super . ngOnDestroy ( ) ;
32
+
33
+ if ( this . _fullScreenEventName && this . _fullScreenListener ) {
34
+ this . _document . removeEventListener ( this . _fullScreenEventName , this . _fullScreenListener ) ;
35
+ }
36
+ }
37
+
21
38
protected _createContainer ( ) : void {
22
39
super . _createContainer ( ) ;
23
40
this . _adjustParentForFullscreenChange ( ) ;
@@ -28,32 +45,50 @@ export class FullscreenOverlayContainer extends OverlayContainer {
28
45
if ( ! this . _containerElement ) {
29
46
return ;
30
47
}
31
- let fullscreenElement = this . getFullscreenElement ( ) ;
32
- let parent = fullscreenElement || document . body ;
48
+
49
+ const fullscreenElement = this . getFullscreenElement ( ) ;
50
+ const parent = fullscreenElement || this . _document . body ;
33
51
parent . appendChild ( this . _containerElement ) ;
34
52
}
35
53
36
54
private _addFullscreenChangeListener ( fn : ( ) => void ) {
37
- if ( document . fullscreenEnabled ) {
38
- document . addEventListener ( 'fullscreenchange' , fn ) ;
39
- } else if ( document . webkitFullscreenEnabled ) {
40
- document . addEventListener ( 'webkitfullscreenchange' , fn ) ;
41
- } else if ( ( document as any ) . mozFullScreenEnabled ) {
42
- document . addEventListener ( 'mozfullscreenchange' , fn ) ;
43
- } else if ( ( document as any ) . msFullscreenEnabled ) {
44
- document . addEventListener ( 'MSFullscreenChange' , fn ) ;
55
+ const eventName = this . _getEventName ( ) ;
56
+
57
+ if ( eventName ) {
58
+ if ( this . _fullScreenListener ) {
59
+ this . _document . removeEventListener ( eventName , this . _fullScreenListener ) ;
60
+ }
61
+
62
+ this . _document . addEventListener ( eventName , fn ) ;
63
+ this . _fullScreenListener = fn ;
45
64
}
46
65
}
47
66
67
+ private _getEventName ( ) : string | undefined {
68
+ if ( ! this . _fullScreenEventName ) {
69
+ if ( this . _document . fullscreenEnabled ) {
70
+ this . _fullScreenEventName = 'fullscreenchange' ;
71
+ } else if ( this . _document . webkitFullscreenEnabled ) {
72
+ this . _fullScreenEventName = 'webkitfullscreenchange' ;
73
+ } else if ( ( this . _document as any ) . mozFullScreenEnabled ) {
74
+ this . _fullScreenEventName = 'mozfullscreenchange' ;
75
+ } else if ( ( this . _document as any ) . msFullscreenEnabled ) {
76
+ this . _fullScreenEventName = 'MSFullscreenChange' ;
77
+ }
78
+ }
79
+
80
+ return this . _fullScreenEventName ;
81
+ }
82
+
48
83
/**
49
84
* When the page is put into fullscreen mode, a specific element is specified.
50
85
* Only that element and its children are visible when in fullscreen mode.
51
86
*/
52
87
getFullscreenElement ( ) : Element {
53
- return document . fullscreenElement ||
54
- document . webkitFullscreenElement ||
55
- ( document as any ) . mozFullScreenElement ||
56
- ( document as any ) . msFullscreenElement ||
57
- null ;
88
+ return this . _document . fullscreenElement ||
89
+ this . _document . webkitFullscreenElement ||
90
+ ( this . _document as any ) . mozFullScreenElement ||
91
+ ( this . _document as any ) . msFullscreenElement ||
92
+ null ;
58
93
}
59
94
}
0 commit comments