| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- /**
- * @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.io/license
- */
- import { Platform, normalizePassiveListenerOptions, PlatformModule } from '@angular/cdk/platform';
- import { Directive, ElementRef, EventEmitter, Injectable, NgZone, Output, Input, NgModule, ɵɵdefineInjectable, ɵɵinject } from '@angular/core';
- import { coerceElement, coerceBooleanProperty } from '@angular/cdk/coercion';
- import { EMPTY, Subject, fromEvent } from 'rxjs';
- import { auditTime, takeUntil } from 'rxjs/operators';
- /**
- * @fileoverview added by tsickle
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
- */
- /**
- * Options to pass to the animationstart listener.
- * @type {?}
- */
- 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
- */
- class AutofillMonitor {
- /**
- * @param {?} _platform
- * @param {?} _ngZone
- */
- constructor(_platform, _ngZone) {
- this._platform = _platform;
- this._ngZone = _ngZone;
- this._monitoredElements = new Map();
- }
- /**
- * @param {?} elementOrRef
- * @return {?}
- */
- monitor(elementOrRef) {
- if (!this._platform.isBrowser) {
- return EMPTY;
- }
- /** @type {?} */
- const element = coerceElement(elementOrRef);
- /** @type {?} */
- const info = this._monitoredElements.get(element);
- if (info) {
- return info.subject.asObservable();
- }
- /** @type {?} */
- const result = new Subject();
- /** @type {?} */
- const cssClass = 'cdk-text-field-autofilled';
- /** @type {?} */
- const listener = (/** @type {?} */ (((/**
- * @param {?} event
- * @return {?}
- */
- (event) => {
- // 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((/**
- * @return {?}
- */
- () => result.next({ target: (/** @type {?} */ (event.target)), isAutofilled: true })));
- }
- else if (event.animationName === 'cdk-text-field-autofill-end' &&
- element.classList.contains(cssClass)) {
- element.classList.remove(cssClass);
- this._ngZone.run((/**
- * @return {?}
- */
- () => result.next({ target: (/** @type {?} */ (event.target)), isAutofilled: false })));
- }
- }))));
- this._ngZone.runOutsideAngular((/**
- * @return {?}
- */
- () => {
- element.addEventListener('animationstart', listener, listenerOptions);
- element.classList.add('cdk-text-field-autofill-monitored');
- }));
- this._monitoredElements.set(element, {
- subject: result,
- unlisten: (/**
- * @return {?}
- */
- () => {
- element.removeEventListener('animationstart', listener, listenerOptions);
- })
- });
- return result.asObservable();
- }
- /**
- * @param {?} elementOrRef
- * @return {?}
- */
- stopMonitoring(elementOrRef) {
- /** @type {?} */
- const element = coerceElement(elementOrRef);
- /** @type {?} */
- 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);
- }
- }
- /**
- * @return {?}
- */
- ngOnDestroy() {
- this._monitoredElements.forEach((/**
- * @param {?} _info
- * @param {?} element
- * @return {?}
- */
- (_info, element) => this.stopMonitoring(element)));
- }
- }
- AutofillMonitor.decorators = [
- { type: Injectable, args: [{ providedIn: 'root' },] },
- ];
- /** @nocollapse */
- AutofillMonitor.ctorParameters = () => [
- { type: Platform },
- { type: NgZone }
- ];
- /** @nocollapse */ AutofillMonitor.ngInjectableDef = ɵɵdefineInjectable({ factory: function AutofillMonitor_Factory() { return new AutofillMonitor(ɵɵinject(Platform), ɵɵinject(NgZone)); }, token: AutofillMonitor, providedIn: "root" });
- /**
- * A directive that can be used to monitor the autofill state of an input.
- */
- class CdkAutofill {
- /**
- * @param {?} _elementRef
- * @param {?} _autofillMonitor
- */
- constructor(_elementRef, _autofillMonitor) {
- this._elementRef = _elementRef;
- this._autofillMonitor = _autofillMonitor;
- /**
- * Emits when the autofill state of the element changes.
- */
- this.cdkAutofill = new EventEmitter();
- }
- /**
- * @return {?}
- */
- ngOnInit() {
- this._autofillMonitor
- .monitor(this._elementRef)
- .subscribe((/**
- * @param {?} event
- * @return {?}
- */
- event => this.cdkAutofill.emit(event)));
- }
- /**
- * @return {?}
- */
- ngOnDestroy() {
- this._autofillMonitor.stopMonitoring(this._elementRef);
- }
- }
- CdkAutofill.decorators = [
- { type: Directive, args: [{
- selector: '[cdkAutofill]',
- },] },
- ];
- /** @nocollapse */
- CdkAutofill.ctorParameters = () => [
- { type: ElementRef },
- { type: AutofillMonitor }
- ];
- CdkAutofill.propDecorators = {
- cdkAutofill: [{ type: Output }]
- };
- /**
- * @fileoverview added by tsickle
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
- */
- /**
- * Directive to automatically resize a textarea to fit its content.
- */
- class CdkTextareaAutosize {
- /**
- * @param {?} _elementRef
- * @param {?} _platform
- * @param {?} _ngZone
- */
- constructor(_elementRef, _platform, _ngZone) {
- this._elementRef = _elementRef;
- this._platform = _platform;
- this._ngZone = _ngZone;
- this._destroyed = new Subject();
- this._enabled = true;
- /**
- * Value of minRows as of last resize. If the minRows has decreased, the
- * height of the textarea needs to be recomputed to reflect the new minimum. The maxHeight
- * does not have the same problem because it does not affect the textarea's scrollHeight.
- */
- this._previousMinRows = -1;
- this._textareaElement = (/** @type {?} */ (this._elementRef.nativeElement));
- }
- /**
- * Minimum amount of rows in the textarea.
- * @return {?}
- */
- get minRows() { return this._minRows; }
- /**
- * @param {?} value
- * @return {?}
- */
- set minRows(value) {
- this._minRows = value;
- this._setMinHeight();
- }
- /**
- * Maximum amount of rows in the textarea.
- * @return {?}
- */
- get maxRows() { return this._maxRows; }
- /**
- * @param {?} value
- * @return {?}
- */
- set maxRows(value) {
- this._maxRows = value;
- this._setMaxHeight();
- }
- /**
- * Whether autosizing is enabled or not
- * @return {?}
- */
- get enabled() { return this._enabled; }
- /**
- * @param {?} value
- * @return {?}
- */
- set enabled(value) {
- value = coerceBooleanProperty(value);
- // Only act if the actual value changed. This specifically helps to not run
- // resizeToFitContent too early (i.e. before ngAfterViewInit)
- if (this._enabled !== value) {
- (this._enabled = value) ? this.resizeToFitContent(true) : this.reset();
- }
- }
- /**
- * Sets the minimum height of the textarea as determined by minRows.
- * @return {?}
- */
- _setMinHeight() {
- /** @type {?} */
- const minHeight = this.minRows && this._cachedLineHeight ?
- `${this.minRows * this._cachedLineHeight}px` : null;
- if (minHeight) {
- this._textareaElement.style.minHeight = minHeight;
- }
- }
- /**
- * Sets the maximum height of the textarea as determined by maxRows.
- * @return {?}
- */
- _setMaxHeight() {
- /** @type {?} */
- const maxHeight = this.maxRows && this._cachedLineHeight ?
- `${this.maxRows * this._cachedLineHeight}px` : null;
- if (maxHeight) {
- this._textareaElement.style.maxHeight = maxHeight;
- }
- }
- /**
- * @return {?}
- */
- ngAfterViewInit() {
- if (this._platform.isBrowser) {
- // Remember the height which we started with in case autosizing is disabled
- this._initialHeight = this._textareaElement.style.height;
- this.resizeToFitContent();
- this._ngZone.runOutsideAngular((/**
- * @return {?}
- */
- () => {
- fromEvent(window, 'resize')
- .pipe(auditTime(16), takeUntil(this._destroyed))
- .subscribe((/**
- * @return {?}
- */
- () => this.resizeToFitContent(true)));
- }));
- }
- }
- /**
- * @return {?}
- */
- ngOnDestroy() {
- this._destroyed.next();
- this._destroyed.complete();
- }
- /**
- * Cache the height of a single-row textarea if it has not already been cached.
- *
- * We need to know how large a single "row" of a textarea is in order to apply minRows and
- * maxRows. For the initial version, we will assume that the height of a single line in the
- * textarea does not ever change.
- * @private
- * @return {?}
- */
- _cacheTextareaLineHeight() {
- if (this._cachedLineHeight) {
- return;
- }
- // Use a clone element because we have to override some styles.
- /** @type {?} */
- let textareaClone = (/** @type {?} */ (this._textareaElement.cloneNode(false)));
- textareaClone.rows = 1;
- // Use `position: absolute` so that this doesn't cause a browser layout and use
- // `visibility: hidden` so that nothing is rendered. Clear any other styles that
- // would affect the height.
- textareaClone.style.position = 'absolute';
- textareaClone.style.visibility = 'hidden';
- textareaClone.style.border = 'none';
- textareaClone.style.padding = '0';
- textareaClone.style.height = '';
- textareaClone.style.minHeight = '';
- textareaClone.style.maxHeight = '';
- // In Firefox it happens that textarea elements are always bigger than the specified amount
- // of rows. This is because Firefox tries to add extra space for the horizontal scrollbar.
- // As a workaround that removes the extra space for the scrollbar, we can just set overflow
- // to hidden. This ensures that there is no invalid calculation of the line height.
- // See Firefox bug report: https://bugzilla.mozilla.org/show_bug.cgi?id=33654
- textareaClone.style.overflow = 'hidden';
- (/** @type {?} */ (this._textareaElement.parentNode)).appendChild(textareaClone);
- this._cachedLineHeight = textareaClone.clientHeight;
- (/** @type {?} */ (this._textareaElement.parentNode)).removeChild(textareaClone);
- // Min and max heights have to be re-calculated if the cached line height changes
- this._setMinHeight();
- this._setMaxHeight();
- }
- /**
- * @return {?}
- */
- ngDoCheck() {
- if (this._platform.isBrowser) {
- this.resizeToFitContent();
- }
- }
- /**
- * Resize the textarea to fit its content.
- * @param {?=} force Whether to force a height recalculation. By default the height will be
- * recalculated only if the value changed since the last call.
- * @return {?}
- */
- resizeToFitContent(force = false) {
- // If autosizing is disabled, just skip everything else
- if (!this._enabled) {
- return;
- }
- this._cacheTextareaLineHeight();
- // If we haven't determined the line-height yet, we know we're still hidden and there's no point
- // in checking the height of the textarea.
- if (!this._cachedLineHeight) {
- return;
- }
- /** @type {?} */
- const textarea = (/** @type {?} */ (this._elementRef.nativeElement));
- /** @type {?} */
- const value = textarea.value;
- // Only resize if the value or minRows have changed since these calculations can be expensive.
- if (!force && this._minRows === this._previousMinRows && value === this._previousValue) {
- return;
- }
- /** @type {?} */
- const placeholderText = textarea.placeholder;
- // Reset the textarea height to auto in order to shrink back to its default size.
- // Also temporarily force overflow:hidden, so scroll bars do not interfere with calculations.
- // Long placeholders that are wider than the textarea width may lead to a bigger scrollHeight
- // value. To ensure that the scrollHeight is not bigger than the content, the placeholders
- // need to be removed temporarily.
- textarea.classList.add('cdk-textarea-autosize-measuring');
- textarea.placeholder = '';
- // The cdk-textarea-autosize-measuring class includes a 2px padding to workaround an issue with
- // Chrome, so we account for that extra space here by subtracting 4 (2px top + 2px bottom).
- /** @type {?} */
- const height = textarea.scrollHeight - 4;
- // Use the scrollHeight to know how large the textarea *would* be if fit its entire value.
- textarea.style.height = `${height}px`;
- textarea.classList.remove('cdk-textarea-autosize-measuring');
- textarea.placeholder = placeholderText;
- this._ngZone.runOutsideAngular((/**
- * @return {?}
- */
- () => {
- if (typeof requestAnimationFrame !== 'undefined') {
- requestAnimationFrame((/**
- * @return {?}
- */
- () => this._scrollToCaretPosition(textarea)));
- }
- else {
- setTimeout((/**
- * @return {?}
- */
- () => this._scrollToCaretPosition(textarea)));
- }
- }));
- this._previousValue = value;
- this._previousMinRows = this._minRows;
- }
- /**
- * Resets the textarea to its original size
- * @return {?}
- */
- reset() {
- // Do not try to change the textarea, if the initialHeight has not been determined yet
- // This might potentially remove styles when reset() is called before ngAfterViewInit
- if (this._initialHeight === undefined) {
- return;
- }
- this._textareaElement.style.height = this._initialHeight;
- }
- /**
- * @return {?}
- */
- _noopInputHandler() {
- // no-op handler that ensures we're running change detection on input events.
- }
- /**
- * Scrolls a textarea to the caret position. On Firefox resizing the textarea will
- * prevent it from scrolling to the caret position. We need to re-set the selection
- * in order for it to scroll to the proper position.
- * @private
- * @param {?} textarea
- * @return {?}
- */
- _scrollToCaretPosition(textarea) {
- const { selectionStart, selectionEnd } = textarea;
- // IE will throw an "Unspecified error" if we try to set the selection range after the
- // element has been removed from the DOM. Assert that the directive hasn't been destroyed
- // between the time we requested the animation frame and when it was executed.
- // Also note that we have to assert that the textarea is focused before we set the
- // selection range. Setting the selection range on a non-focused textarea will cause
- // it to receive focus on IE and Edge.
- if (!this._destroyed.isStopped && document.activeElement === textarea) {
- textarea.setSelectionRange(selectionStart, selectionEnd);
- }
- }
- }
- CdkTextareaAutosize.decorators = [
- { type: Directive, args: [{
- selector: 'textarea[cdkTextareaAutosize]',
- exportAs: 'cdkTextareaAutosize',
- host: {
- 'class': 'cdk-textarea-autosize',
- // Textarea elements that have the directive applied should have a single row by default.
- // Browsers normally show two rows by default and therefore this limits the minRows binding.
- 'rows': '1',
- '(input)': '_noopInputHandler()',
- },
- },] },
- ];
- /** @nocollapse */
- CdkTextareaAutosize.ctorParameters = () => [
- { type: ElementRef },
- { type: Platform },
- { type: NgZone }
- ];
- CdkTextareaAutosize.propDecorators = {
- minRows: [{ type: Input, args: ['cdkAutosizeMinRows',] }],
- maxRows: [{ type: Input, args: ['cdkAutosizeMaxRows',] }],
- enabled: [{ type: Input, args: ['cdkTextareaAutosize',] }]
- };
- /**
- * @fileoverview added by tsickle
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
- */
- class TextFieldModule {
- }
- TextFieldModule.decorators = [
- { type: NgModule, args: [{
- declarations: [CdkAutofill, CdkTextareaAutosize],
- imports: [PlatformModule],
- exports: [CdkAutofill, CdkTextareaAutosize],
- },] },
- ];
- /**
- * @fileoverview added by tsickle
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
- */
- /**
- * @fileoverview added by tsickle
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
- */
- export { AutofillMonitor, CdkAutofill, CdkTextareaAutosize, TextFieldModule };
- //# sourceMappingURL=text-field.js.map
|