8
8
Renderer ,
9
9
ElementRef ,
10
10
EventEmitter ,
11
+ Inject ,
12
+ Optional ,
11
13
} from '@angular/core' ;
12
14
import {
13
15
animate ,
@@ -17,6 +19,7 @@ import {
17
19
transition ,
18
20
AnimationEvent ,
19
21
} from '@angular/animations' ;
22
+ import { DOCUMENT } from '@angular/platform-browser' ;
20
23
import { BasePortalHost , ComponentPortal , PortalHostDirective , TemplatePortal } from '../core' ;
21
24
import { MdDialogConfig } from './dialog-config' ;
22
25
import { MdDialogContentAlreadyAttachedError } from './dialog-errors' ;
@@ -64,6 +67,9 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
64
67
/** Element that was focused before the dialog was opened. Save this to restore upon close. */
65
68
private _elementFocusedBeforeDialogWasOpened : HTMLElement = null ;
66
69
70
+ /** Reference to the global document object. */
71
+ private _document : Document | Document ;
72
+
67
73
/** The dialog configuration. */
68
74
dialogConfig : MdDialogConfig ;
69
75
@@ -77,9 +83,11 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
77
83
private _ngZone : NgZone ,
78
84
private _renderer : Renderer ,
79
85
private _elementRef : ElementRef ,
80
- private _focusTrapFactory : FocusTrapFactory ) {
86
+ private _focusTrapFactory : FocusTrapFactory ,
87
+ @Optional ( ) @Inject ( DOCUMENT ) _document : any ) {
81
88
82
89
super ( ) ;
90
+ this . _document = _document ;
83
91
}
84
92
85
93
/**
@@ -91,6 +99,7 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
91
99
throw new MdDialogContentAlreadyAttachedError ( ) ;
92
100
}
93
101
102
+ this . _savePreviouslyFocusedElement ( ) ;
94
103
return this . _portalHost . attachComponentPortal ( portal ) ;
95
104
}
96
105
@@ -103,12 +112,12 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
103
112
throw new MdDialogContentAlreadyAttachedError ( ) ;
104
113
}
105
114
115
+ this . _savePreviouslyFocusedElement ( ) ;
106
116
return this . _portalHost . attachTemplatePortal ( portal ) ;
107
117
}
108
118
109
119
/**
110
120
* Moves the focus inside the focus trap.
111
- * @private
112
121
*/
113
122
private _trapFocus ( ) {
114
123
if ( ! this . _focusTrap ) {
@@ -118,10 +127,18 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
118
127
// If were to attempt to focus immediately, then the content of the dialog would not yet be
119
128
// ready in instances where change detection has to run first. To deal with this, we simply
120
129
// wait for the microtask queue to be empty.
121
- this . _elementFocusedBeforeDialogWasOpened = document . activeElement as HTMLElement ;
122
130
this . _focusTrap . focusFirstTabbableElementWhenReady ( ) ;
123
131
}
124
132
133
+ /**
134
+ * Saves a reference to the element that was focused before the dialog was opened.
135
+ */
136
+ private _savePreviouslyFocusedElement ( ) {
137
+ if ( this . _document ) {
138
+ this . _elementFocusedBeforeDialogWasOpened = this . _document . activeElement as HTMLElement ;
139
+ }
140
+ }
141
+
125
142
/**
126
143
* Kicks off the leave animation.
127
144
* @docs -private
0 commit comments