1 | import * as i1 from '@angular/cdk/platform';
|
2 | import { normalizePassiveListenerOptions } from '@angular/cdk/platform';
|
3 | import * as i0 from '@angular/core';
|
4 | import { Injectable, EventEmitter, Directive, Output, Optional, Inject, Input, NgModule } from '@angular/core';
|
5 | import { coerceElement, coerceNumberProperty, coerceBooleanProperty } from '@angular/cdk/coercion';
|
6 | import { EMPTY, Subject, fromEvent } from 'rxjs';
|
7 | import { auditTime, takeUntil } from 'rxjs/operators';
|
8 | import { DOCUMENT } from '@angular/common';
|
9 |
|
10 |
|
11 | const listenerOptions = normalizePassiveListenerOptions({ passive: true });
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | class AutofillMonitor {
|
18 | constructor(_platform, _ngZone) {
|
19 | this._platform = _platform;
|
20 | this._ngZone = _ngZone;
|
21 | this._monitoredElements = new Map();
|
22 | }
|
23 | monitor(elementOrRef) {
|
24 | if (!this._platform.isBrowser) {
|
25 | return EMPTY;
|
26 | }
|
27 | const element = coerceElement(elementOrRef);
|
28 | const info = this._monitoredElements.get(element);
|
29 | if (info) {
|
30 | return info.subject;
|
31 | }
|
32 | const result = new Subject();
|
33 | const cssClass = 'cdk-text-field-autofilled';
|
34 | const listener = ((event) => {
|
35 |
|
36 |
|
37 |
|
38 | if (event.animationName === 'cdk-text-field-autofill-start' &&
|
39 | !element.classList.contains(cssClass)) {
|
40 | element.classList.add(cssClass);
|
41 | this._ngZone.run(() => result.next({ target: event.target, isAutofilled: true }));
|
42 | }
|
43 | else if (event.animationName === 'cdk-text-field-autofill-end' &&
|
44 | element.classList.contains(cssClass)) {
|
45 | element.classList.remove(cssClass);
|
46 | this._ngZone.run(() => result.next({ target: event.target, isAutofilled: false }));
|
47 | }
|
48 | });
|
49 | this._ngZone.runOutsideAngular(() => {
|
50 | element.addEventListener('animationstart', listener, listenerOptions);
|
51 | element.classList.add('cdk-text-field-autofill-monitored');
|
52 | });
|
53 | this._monitoredElements.set(element, {
|
54 | subject: result,
|
55 | unlisten: () => {
|
56 | element.removeEventListener('animationstart', listener, listenerOptions);
|
57 | },
|
58 | });
|
59 | return result;
|
60 | }
|
61 | stopMonitoring(elementOrRef) {
|
62 | const element = coerceElement(elementOrRef);
|
63 | const info = this._monitoredElements.get(element);
|
64 | if (info) {
|
65 | info.unlisten();
|
66 | info.subject.complete();
|
67 | element.classList.remove('cdk-text-field-autofill-monitored');
|
68 | element.classList.remove('cdk-text-field-autofilled');
|
69 | this._monitoredElements.delete(element);
|
70 | }
|
71 | }
|
72 | ngOnDestroy() {
|
73 | this._monitoredElements.forEach((_info, element) => this.stopMonitoring(element));
|
74 | }
|
75 | static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: AutofillMonitor, deps: [{ token: i1.Platform }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Injectable }); }
|
76 | static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: AutofillMonitor, providedIn: 'root' }); }
|
77 | }
|
78 | i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: AutofillMonitor, decorators: [{
|
79 | type: Injectable,
|
80 | args: [{ providedIn: 'root' }]
|
81 | }], ctorParameters: function () { return [{ type: i1.Platform }, { type: i0.NgZone }]; } });
|
82 |
|
83 | class CdkAutofill {
|
84 | constructor(_elementRef, _autofillMonitor) {
|
85 | this._elementRef = _elementRef;
|
86 | this._autofillMonitor = _autofillMonitor;
|
87 |
|
88 | this.cdkAutofill = new EventEmitter();
|
89 | }
|
90 | ngOnInit() {
|
91 | this._autofillMonitor
|
92 | .monitor(this._elementRef)
|
93 | .subscribe(event => this.cdkAutofill.emit(event));
|
94 | }
|
95 | ngOnDestroy() {
|
96 | this._autofillMonitor.stopMonitoring(this._elementRef);
|
97 | }
|
98 | static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: CdkAutofill, deps: [{ token: i0.ElementRef }, { token: AutofillMonitor }], target: i0.ɵɵFactoryTarget.Directive }); }
|
99 | static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.0.0", type: CdkAutofill, selector: "[cdkAutofill]", outputs: { cdkAutofill: "cdkAutofill" }, ngImport: i0 }); }
|
100 | }
|
101 | i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: CdkAutofill, decorators: [{
|
102 | type: Directive,
|
103 | args: [{
|
104 | selector: '[cdkAutofill]',
|
105 | }]
|
106 | }], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: AutofillMonitor }]; }, propDecorators: { cdkAutofill: [{
|
107 | type: Output
|
108 | }] } });
|
109 |
|
110 |
|
111 | class CdkTextareaAutosize {
|
112 |
|
113 | get minRows() {
|
114 | return this._minRows;
|
115 | }
|
116 | set minRows(value) {
|
117 | this._minRows = coerceNumberProperty(value);
|
118 | this._setMinHeight();
|
119 | }
|
120 |
|
121 | get maxRows() {
|
122 | return this._maxRows;
|
123 | }
|
124 | set maxRows(value) {
|
125 | this._maxRows = coerceNumberProperty(value);
|
126 | this._setMaxHeight();
|
127 | }
|
128 |
|
129 | get enabled() {
|
130 | return this._enabled;
|
131 | }
|
132 | set enabled(value) {
|
133 | value = coerceBooleanProperty(value);
|
134 |
|
135 |
|
136 | if (this._enabled !== value) {
|
137 | (this._enabled = value) ? this.resizeToFitContent(true) : this.reset();
|
138 | }
|
139 | }
|
140 | get placeholder() {
|
141 | return this._textareaElement.placeholder;
|
142 | }
|
143 | set placeholder(value) {
|
144 | this._cachedPlaceholderHeight = undefined;
|
145 | if (value) {
|
146 | this._textareaElement.setAttribute('placeholder', value);
|
147 | }
|
148 | else {
|
149 | this._textareaElement.removeAttribute('placeholder');
|
150 | }
|
151 | this._cacheTextareaPlaceholderHeight();
|
152 | }
|
153 | constructor(_elementRef, _platform, _ngZone,
|
154 | /** @breaking-change 11.0.0 make document required */
|
155 | document) {
|
156 | this._elementRef = _elementRef;
|
157 | this._platform = _platform;
|
158 | this._ngZone = _ngZone;
|
159 | this._destroyed = new Subject();
|
160 | this._enabled = true;
|
161 | |
162 |
|
163 |
|
164 |
|
165 |
|
166 | this._previousMinRows = -1;
|
167 | this._isViewInited = false;
|
168 |
|
169 | this._handleFocusEvent = (event) => {
|
170 | this._hasFocus = event.type === 'focus';
|
171 | };
|
172 | this._document = document;
|
173 | this._textareaElement = this._elementRef.nativeElement;
|
174 | }
|
175 |
|
176 | _setMinHeight() {
|
177 | const minHeight = this.minRows && this._cachedLineHeight ? `${this.minRows * this._cachedLineHeight}px` : null;
|
178 | if (minHeight) {
|
179 | this._textareaElement.style.minHeight = minHeight;
|
180 | }
|
181 | }
|
182 |
|
183 | _setMaxHeight() {
|
184 | const maxHeight = this.maxRows && this._cachedLineHeight ? `${this.maxRows * this._cachedLineHeight}px` : null;
|
185 | if (maxHeight) {
|
186 | this._textareaElement.style.maxHeight = maxHeight;
|
187 | }
|
188 | }
|
189 | ngAfterViewInit() {
|
190 | if (this._platform.isBrowser) {
|
191 |
|
192 | this._initialHeight = this._textareaElement.style.height;
|
193 | this.resizeToFitContent();
|
194 | this._ngZone.runOutsideAngular(() => {
|
195 | const window = this._getWindow();
|
196 | fromEvent(window, 'resize')
|
197 | .pipe(auditTime(16), takeUntil(this._destroyed))
|
198 | .subscribe(() => this.resizeToFitContent(true));
|
199 | this._textareaElement.addEventListener('focus', this._handleFocusEvent);
|
200 | this._textareaElement.addEventListener('blur', this._handleFocusEvent);
|
201 | });
|
202 | this._isViewInited = true;
|
203 | this.resizeToFitContent(true);
|
204 | }
|
205 | }
|
206 | ngOnDestroy() {
|
207 | this._textareaElement.removeEventListener('focus', this._handleFocusEvent);
|
208 | this._textareaElement.removeEventListener('blur', this._handleFocusEvent);
|
209 | this._destroyed.next();
|
210 | this._destroyed.complete();
|
211 | }
|
212 | |
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | _cacheTextareaLineHeight() {
|
220 | if (this._cachedLineHeight) {
|
221 | return;
|
222 | }
|
223 |
|
224 | let textareaClone = this._textareaElement.cloneNode(false);
|
225 | textareaClone.rows = 1;
|
226 |
|
227 |
|
228 |
|
229 | textareaClone.style.position = 'absolute';
|
230 | textareaClone.style.visibility = 'hidden';
|
231 | textareaClone.style.border = 'none';
|
232 | textareaClone.style.padding = '0';
|
233 | textareaClone.style.height = '';
|
234 | textareaClone.style.minHeight = '';
|
235 | textareaClone.style.maxHeight = '';
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | textareaClone.style.overflow = 'hidden';
|
242 | this._textareaElement.parentNode.appendChild(textareaClone);
|
243 | this._cachedLineHeight = textareaClone.clientHeight;
|
244 | textareaClone.remove();
|
245 |
|
246 | this._setMinHeight();
|
247 | this._setMaxHeight();
|
248 | }
|
249 | _measureScrollHeight() {
|
250 | const element = this._textareaElement;
|
251 | const previousMargin = element.style.marginBottom || '';
|
252 | const isFirefox = this._platform.FIREFOX;
|
253 | const needsMarginFiller = isFirefox && this._hasFocus;
|
254 | const measuringClass = isFirefox
|
255 | ? 'cdk-textarea-autosize-measuring-firefox'
|
256 | : 'cdk-textarea-autosize-measuring';
|
257 |
|
258 |
|
259 |
|
260 | if (needsMarginFiller) {
|
261 | element.style.marginBottom = `${element.clientHeight}px`;
|
262 | }
|
263 |
|
264 |
|
265 | element.classList.add(measuringClass);
|
266 |
|
267 |
|
268 | const scrollHeight = element.scrollHeight - 4;
|
269 | element.classList.remove(measuringClass);
|
270 | if (needsMarginFiller) {
|
271 | element.style.marginBottom = previousMargin;
|
272 | }
|
273 | return scrollHeight;
|
274 | }
|
275 | _cacheTextareaPlaceholderHeight() {
|
276 | if (!this._isViewInited || this._cachedPlaceholderHeight != undefined) {
|
277 | return;
|
278 | }
|
279 | if (!this.placeholder) {
|
280 | this._cachedPlaceholderHeight = 0;
|
281 | return;
|
282 | }
|
283 | const value = this._textareaElement.value;
|
284 | this._textareaElement.value = this._textareaElement.placeholder;
|
285 | this._cachedPlaceholderHeight = this._measureScrollHeight();
|
286 | this._textareaElement.value = value;
|
287 | }
|
288 | ngDoCheck() {
|
289 | if (this._platform.isBrowser) {
|
290 | this.resizeToFitContent();
|
291 | }
|
292 | }
|
293 | |
294 |
|
295 |
|
296 |
|
297 |
|
298 | resizeToFitContent(force = false) {
|
299 |
|
300 | if (!this._enabled) {
|
301 | return;
|
302 | }
|
303 | this._cacheTextareaLineHeight();
|
304 | this._cacheTextareaPlaceholderHeight();
|
305 |
|
306 |
|
307 | if (!this._cachedLineHeight) {
|
308 | return;
|
309 | }
|
310 | const textarea = this._elementRef.nativeElement;
|
311 | const value = textarea.value;
|
312 |
|
313 | if (!force && this._minRows === this._previousMinRows && value === this._previousValue) {
|
314 | return;
|
315 | }
|
316 | const scrollHeight = this._measureScrollHeight();
|
317 | const height = Math.max(scrollHeight, this._cachedPlaceholderHeight || 0);
|
318 |
|
319 | textarea.style.height = `${height}px`;
|
320 | this._ngZone.runOutsideAngular(() => {
|
321 | if (typeof requestAnimationFrame !== 'undefined') {
|
322 | requestAnimationFrame(() => this._scrollToCaretPosition(textarea));
|
323 | }
|
324 | else {
|
325 | setTimeout(() => this._scrollToCaretPosition(textarea));
|
326 | }
|
327 | });
|
328 | this._previousValue = value;
|
329 | this._previousMinRows = this._minRows;
|
330 | }
|
331 | |
332 |
|
333 |
|
334 | reset() {
|
335 |
|
336 |
|
337 | if (this._initialHeight !== undefined) {
|
338 | this._textareaElement.style.height = this._initialHeight;
|
339 | }
|
340 | }
|
341 | _noopInputHandler() {
|
342 |
|
343 | }
|
344 |
|
345 | _getDocument() {
|
346 | return this._document || document;
|
347 | }
|
348 |
|
349 | _getWindow() {
|
350 | const doc = this._getDocument();
|
351 | return doc.defaultView || window;
|
352 | }
|
353 | |
354 |
|
355 |
|
356 |
|
357 |
|
358 | _scrollToCaretPosition(textarea) {
|
359 | const { selectionStart, selectionEnd } = textarea;
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 | if (!this._destroyed.isStopped && this._hasFocus) {
|
367 | textarea.setSelectionRange(selectionStart, selectionEnd);
|
368 | }
|
369 | }
|
370 | static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: CdkTextareaAutosize, deps: [{ token: i0.ElementRef }, { token: i1.Platform }, { token: i0.NgZone }, { token: DOCUMENT, optional: true }], target: i0.ɵɵFactoryTarget.Directive }); }
|
371 | static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.0.0", type: CdkTextareaAutosize, selector: "textarea[cdkTextareaAutosize]", inputs: { minRows: ["cdkAutosizeMinRows", "minRows"], maxRows: ["cdkAutosizeMaxRows", "maxRows"], enabled: ["cdkTextareaAutosize", "enabled"], placeholder: "placeholder" }, host: { attributes: { "rows": "1" }, listeners: { "input": "_noopInputHandler()" }, classAttribute: "cdk-textarea-autosize" }, exportAs: ["cdkTextareaAutosize"], ngImport: i0 }); }
|
372 | }
|
373 | i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: CdkTextareaAutosize, decorators: [{
|
374 | type: Directive,
|
375 | args: [{
|
376 | selector: 'textarea[cdkTextareaAutosize]',
|
377 | exportAs: 'cdkTextareaAutosize',
|
378 | host: {
|
379 | 'class': 'cdk-textarea-autosize',
|
380 |
|
381 |
|
382 | 'rows': '1',
|
383 | '(input)': '_noopInputHandler()',
|
384 | },
|
385 | }]
|
386 | }], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: i1.Platform }, { type: i0.NgZone }, { type: undefined, decorators: [{
|
387 | type: Optional
|
388 | }, {
|
389 | type: Inject,
|
390 | args: [DOCUMENT]
|
391 | }] }]; }, propDecorators: { minRows: [{
|
392 | type: Input,
|
393 | args: ['cdkAutosizeMinRows']
|
394 | }], maxRows: [{
|
395 | type: Input,
|
396 | args: ['cdkAutosizeMaxRows']
|
397 | }], enabled: [{
|
398 | type: Input,
|
399 | args: ['cdkTextareaAutosize']
|
400 | }], placeholder: [{
|
401 | type: Input
|
402 | }] } });
|
403 |
|
404 | class TextFieldModule {
|
405 | static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: TextFieldModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
|
406 | static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.0.0", ngImport: i0, type: TextFieldModule, declarations: [CdkAutofill, CdkTextareaAutosize], exports: [CdkAutofill, CdkTextareaAutosize] }); }
|
407 | static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: TextFieldModule }); }
|
408 | }
|
409 | i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: TextFieldModule, decorators: [{
|
410 | type: NgModule,
|
411 | args: [{
|
412 | declarations: [CdkAutofill, CdkTextareaAutosize],
|
413 | exports: [CdkAutofill, CdkTextareaAutosize],
|
414 | }]
|
415 | }] });
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 | export { AutofillMonitor, CdkAutofill, CdkTextareaAutosize, TextFieldModule };
|
422 |
|