6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { Directive , ElementRef , EventEmitter , Injectable , OnDestroy , Output } from '@angular/core' ;
9
+ import { Platform , supportsPassiveEventListeners } from '@angular/cdk/platform' ;
10
+ import {
11
+ Directive ,
12
+ ElementRef ,
13
+ EventEmitter ,
14
+ Injectable ,
15
+ NgZone ,
16
+ OnDestroy ,
17
+ Output
18
+ } from '@angular/core' ;
10
19
import { Observable } from 'rxjs/Observable' ;
20
+ import { empty as observableEmpty } from 'rxjs/observable/empty' ;
11
21
import { Subject } from 'rxjs/Subject' ;
12
22
13
23
@@ -25,6 +35,10 @@ type MonitoredElementInfo = {
25
35
} ;
26
36
27
37
38
+ /** Options to pass to the animationstart listener. */
39
+ const listenerOptions : any = supportsPassiveEventListeners ( ) ? { passive : true } : false ;
40
+
41
+
28
42
/**
29
43
* An injectable service that can be used to monitor the autofill state of an input.
30
44
* Based on the following blog post:
@@ -34,7 +48,13 @@ type MonitoredElementInfo = {
34
48
export class AutofillMonitor implements OnDestroy {
35
49
private _monitoredElements = new Map < Element , MonitoredElementInfo > ( ) ;
36
50
51
+ constructor ( private _platform : Platform , private _ngZone : NgZone ) { }
52
+
37
53
monitor ( element : Element ) : Observable < AutofillEvent > {
54
+ if ( ! this . _platform . isBrowser ) {
55
+ return observableEmpty ( ) ;
56
+ }
57
+
38
58
const info = this . _monitoredElements . get ( element ) ;
39
59
if ( info ) {
40
60
return info . subject . asObservable ( ) ;
@@ -51,13 +71,15 @@ export class AutofillMonitor implements OnDestroy {
51
71
}
52
72
} ;
53
73
54
- element . addEventListener ( 'animationstart' , listener ) ;
74
+ this . _ngZone . runOutsideAngular ( ( ) => {
75
+ element . addEventListener ( 'animationstart' , listener , listenerOptions ) ;
76
+ } ) ;
55
77
element . classList . add ( 'mat-input-autofill-monitored' ) ;
56
78
57
79
this . _monitoredElements . set ( element , {
58
80
subject : result ,
59
81
unlisten : ( ) => {
60
- element . removeEventListener ( 'animationstart' , listener ) ;
82
+ element . removeEventListener ( 'animationstart' , listener , listenerOptions ) ;
61
83
}
62
84
} ) ;
63
85
@@ -90,9 +112,10 @@ export class AutofillMonitor implements OnDestroy {
90
112
export class MatAutofill implements OnDestroy {
91
113
@Output ( ) matAutofill = new EventEmitter < AutofillEvent > ( ) ;
92
114
93
- constructor ( private _elementRef : ElementRef , private _autofillMonitor : AutofillMonitor ) {
115
+ constructor ( private _elementRef : ElementRef , private _autofillMonitor : AutofillMonitor ,
116
+ ngZone : NgZone ) {
94
117
this . _autofillMonitor . monitor ( this . _elementRef . nativeElement )
95
- . subscribe ( event => this . matAutofill . emit ( event ) ) ;
118
+ . subscribe ( event => ngZone . run ( ( ) => this . matAutofill . emit ( event ) ) ) ;
96
119
}
97
120
98
121
ngOnDestroy ( ) {
0 commit comments