-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
autofill.ts
176 lines (149 loc) · 5.51 KB
/
autofill.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
/**
* @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.dev/license
*/
import {Platform, normalizePassiveListenerOptions} from '@angular/cdk/platform';
import {
Directive,
ElementRef,
EventEmitter,
inject,
Injectable,
NgZone,
OnDestroy,
OnInit,
Output,
} from '@angular/core';
import {_CdkPrivateStyleLoader} from '@angular/cdk/private';
import {coerceElement} from '@angular/cdk/coercion';
import {EMPTY, Observable, Subject} from 'rxjs';
import {_CdkTextFieldStyleLoader} from './text-field-style-loader';
/** An event that is emitted when the autofill state of an input changes. */
export type AutofillEvent = {
/** The element whose autofill state changes. */
target: Element;
/** Whether the element is currently autofilled. */
isAutofilled: boolean;
};
/** Used to track info about currently monitored elements. */
type MonitoredElementInfo = {
readonly subject: Subject<AutofillEvent>;
unlisten: () => void;
};
/** Options to pass to the animationstart listener. */
const listenerOptions = normalizePassiveListenerOptions({passive: true});
/**
* An injectable service that can be used to monitor the autofill state of an input.
* Based on the following blog post:
* https://medium.com/@brunn/detecting-autofilled-fields-in-javascript-aed598d25da7
*/
@Injectable({providedIn: 'root'})
export class AutofillMonitor implements OnDestroy {
private _platform = inject(Platform);
private _ngZone = inject(NgZone);
private _styleLoader = inject(_CdkPrivateStyleLoader);
private _monitoredElements = new Map<Element, MonitoredElementInfo>();
constructor(...args: unknown[]);
constructor() {}
/**
* Monitor for changes in the autofill state of the given input element.
* @param element The element to monitor.
* @return A stream of autofill state changes.
*/
monitor(element: Element): Observable<AutofillEvent>;
/**
* Monitor for changes in the autofill state of the given input element.
* @param element The element to monitor.
* @return A stream of autofill state changes.
*/
monitor(element: ElementRef<Element>): Observable<AutofillEvent>;
monitor(elementOrRef: Element | ElementRef<Element>): Observable<AutofillEvent> {
if (!this._platform.isBrowser) {
return EMPTY;
}
this._styleLoader.load(_CdkTextFieldStyleLoader);
const element = coerceElement(elementOrRef);
const info = this._monitoredElements.get(element);
if (info) {
return info.subject;
}
const result = new Subject<AutofillEvent>();
const cssClass = 'cdk-text-field-autofilled';
const listener = ((event: AnimationEvent) => {
// Animation events fire on initial element render, we check for the presence of the autofill
// CSS class to make sure this is a real change in state, not just the initial render before
// we fire off events.
if (
event.animationName === 'cdk-text-field-autofill-start' &&
!element.classList.contains(cssClass)
) {
element.classList.add(cssClass);
this._ngZone.run(() => result.next({target: event.target as Element, isAutofilled: true}));
} else if (
event.animationName === 'cdk-text-field-autofill-end' &&
element.classList.contains(cssClass)
) {
element.classList.remove(cssClass);
this._ngZone.run(() => result.next({target: event.target as Element, isAutofilled: false}));
}
}) as EventListenerOrEventListenerObject;
this._ngZone.runOutsideAngular(() => {
element.addEventListener('animationstart', listener, listenerOptions);
element.classList.add('cdk-text-field-autofill-monitored');
});
this._monitoredElements.set(element, {
subject: result,
unlisten: () => {
element.removeEventListener('animationstart', listener, listenerOptions);
},
});
return result;
}
/**
* Stop monitoring the autofill state of the given input element.
* @param element The element to stop monitoring.
*/
stopMonitoring(element: Element): void;
/**
* Stop monitoring the autofill state of the given input element.
* @param element The element to stop monitoring.
*/
stopMonitoring(element: ElementRef<Element>): void;
stopMonitoring(elementOrRef: Element | ElementRef<Element>): void {
const element = coerceElement(elementOrRef);
const info = this._monitoredElements.get(element);
if (info) {
info.unlisten();
info.subject.complete();
element.classList.remove('cdk-text-field-autofill-monitored');
element.classList.remove('cdk-text-field-autofilled');
this._monitoredElements.delete(element);
}
}
ngOnDestroy() {
this._monitoredElements.forEach((_info, element) => this.stopMonitoring(element));
}
}
/** A directive that can be used to monitor the autofill state of an input. */
@Directive({
selector: '[cdkAutofill]',
})
export class CdkAutofill implements OnDestroy, OnInit {
private _elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
private _autofillMonitor = inject(AutofillMonitor);
/** Emits when the autofill state of the element changes. */
@Output() readonly cdkAutofill = new EventEmitter<AutofillEvent>();
constructor(...args: unknown[]);
constructor() {}
ngOnInit() {
this._autofillMonitor
.monitor(this._elementRef)
.subscribe(event => this.cdkAutofill.emit(event));
}
ngOnDestroy() {
this._autofillMonitor.stopMonitoring(this._elementRef);
}
}