UNPKG

@angular/material

Version:
713 lines (707 loc) 29.3 kB
/** * @license * Copyright Google Inc. 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 { ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChildren, Directive, ElementRef, EventEmitter, Host, Inject, InjectionToken, Input, NgModule, NgZone, Optional, Output, TemplateRef, ViewChild, ViewContainerRef, ViewEncapsulation, forwardRef } from '@angular/core'; import { MatCommonModule, MatOptgroup, MatOption, MatOptionModule } from '@angular/material/core'; import { ActiveDescendantKeyManager } from '@angular/cdk/a11y'; import { CommonModule } from '@angular/common'; import { Overlay, OverlayConfig, OverlayModule } from '@angular/cdk/overlay'; import { Directionality } from '@angular/cdk/bidi'; import { DOWN_ARROW, ENTER, ESCAPE, TAB, UP_ARROW } from '@angular/cdk/keycodes'; import { TemplatePortal } from '@angular/cdk/portal'; import { RxChain, delay, doOperator, filter, first, switchMap } from '@angular/cdk/rxjs'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; import { MatFormField } from '@angular/material/form-field'; import { DOCUMENT } from '@angular/platform-browser'; import { Subject } from 'rxjs/Subject'; import { fromEvent } from 'rxjs/observable/fromEvent'; import { merge } from 'rxjs/observable/merge'; import { of } from 'rxjs/observable/of'; /** * Autocomplete IDs need to be unique across components, so this counter exists outside of * the component definition. */ var _uniqueAutocompleteIdCounter = 0; /** * Event object that is emitted when an autocomplete option is selected */ var MatAutocompleteSelectedEvent = (function () { /** * @param {?} source * @param {?} option */ function MatAutocompleteSelectedEvent(source, option) { this.source = source; this.option = option; } return MatAutocompleteSelectedEvent; }()); var MatAutocomplete = (function () { /** * @param {?} _changeDetectorRef * @param {?} _elementRef */ function MatAutocomplete(_changeDetectorRef, _elementRef) { this._changeDetectorRef = _changeDetectorRef; this._elementRef = _elementRef; /** * Whether the autocomplete panel should be visible, depending on option length. */ this.showPanel = false; this._isOpen = false; /** * Function that maps an option's control value to its display value in the trigger. */ this.displayWith = null; /** * Event that is emitted whenever an option from the list is selected. */ this.optionSelected = new EventEmitter(); this._classList = {}; /** * Unique ID to be used by autocomplete trigger's "aria-owns" property. */ this.id = "mat-autocomplete-" + _uniqueAutocompleteIdCounter++; } Object.defineProperty(MatAutocomplete.prototype, "isOpen", { /** * Whether the autocomplete panel is open. * @return {?} */ get: function () { return this._isOpen && this.showPanel; }, enumerable: true, configurable: true }); Object.defineProperty(MatAutocomplete.prototype, "classList", { /** * Takes classes set on the host md-autocomplete element and applies them to the panel * inside the overlay container to allow for easy styling. * @param {?} classList * @return {?} */ set: function (classList) { var _this = this; if (classList && classList.length) { classList.split(' ').forEach(function (className) { return _this._classList[className.trim()] = true; }); this._elementRef.nativeElement.className = ''; } }, enumerable: true, configurable: true }); /** * @return {?} */ MatAutocomplete.prototype.ngAfterContentInit = function () { this._keyManager = new ActiveDescendantKeyManager(this.options).withWrap(); // Set the initial visibiity state. this._setVisibility(); }; /** * Sets the panel scrollTop. This allows us to manually scroll to display options * above or below the fold, as they are not actually being focused when active. * @param {?} scrollTop * @return {?} */ MatAutocomplete.prototype._setScrollTop = function (scrollTop) { if (this.panel) { this.panel.nativeElement.scrollTop = scrollTop; } }; /** * Returns the panel's scrollTop. * @return {?} */ MatAutocomplete.prototype._getScrollTop = function () { return this.panel ? this.panel.nativeElement.scrollTop : 0; }; /** * Panel should hide itself when the option list is empty. * @return {?} */ MatAutocomplete.prototype._setVisibility = function () { this.showPanel = !!this.options.length; this._classList['mat-autocomplete-visible'] = this.showPanel; this._classList['mat-autocomplete-hidden'] = !this.showPanel; this._changeDetectorRef.markForCheck(); }; /** * Emits the `select` event. * @param {?} option * @return {?} */ MatAutocomplete.prototype._emitSelectEvent = function (option) { var /** @type {?} */ event = new MatAutocompleteSelectedEvent(this, option); this.optionSelected.emit(event); }; MatAutocomplete.decorators = [ { type: Component, args: [{selector: 'mat-autocomplete', template: "<ng-template><div class=\"mat-autocomplete-panel\" role=\"listbox\" [id]=\"id\" [ngClass]=\"_classList\" #panel><ng-content></ng-content></div></ng-template>", styles: [".mat-autocomplete-panel{min-width:112px;max-width:280px;overflow:auto;-webkit-overflow-scrolling:touch;visibility:hidden;max-width:none;max-height:256px;position:relative}.mat-autocomplete-panel:not([class*=mat-elevation-z]){box-shadow:0 5px 5px -3px rgba(0,0,0,.2),0 8px 10px 1px rgba(0,0,0,.14),0 3px 14px 2px rgba(0,0,0,.12)}.mat-autocomplete-panel.mat-autocomplete-visible{visibility:visible}.mat-autocomplete-panel.mat-autocomplete-hidden{visibility:hidden}"], encapsulation: ViewEncapsulation.None, preserveWhitespaces: false, changeDetection: ChangeDetectionStrategy.OnPush, exportAs: 'matAutocomplete', host: { 'class': 'mat-autocomplete' } },] }, ]; /** * @nocollapse */ MatAutocomplete.ctorParameters = function () { return [ { type: ChangeDetectorRef, }, { type: ElementRef, }, ]; }; MatAutocomplete.propDecorators = { 'template': [{ type: ViewChild, args: [TemplateRef,] },], 'panel': [{ type: ViewChild, args: ['panel',] },], 'options': [{ type: ContentChildren, args: [MatOption, { descendants: true },] },], 'optionGroups': [{ type: ContentChildren, args: [MatOptgroup,] },], 'displayWith': [{ type: Input },], 'optionSelected': [{ type: Output },], 'classList': [{ type: Input, args: ['class',] },], }; return MatAutocomplete; }()); /** * The height of each autocomplete option. */ var AUTOCOMPLETE_OPTION_HEIGHT = 48; /** * The total height of the autocomplete panel. */ var AUTOCOMPLETE_PANEL_HEIGHT = 256; /** * Injection token that determines the scroll handling while the autocomplete panel is open. */ var MAT_AUTOCOMPLETE_SCROLL_STRATEGY = new InjectionToken('mat-autocomplete-scroll-strategy'); /** * \@docs-private * @param {?} overlay * @return {?} */ function MAT_AUTOCOMPLETE_SCROLL_STRATEGY_PROVIDER_FACTORY(overlay) { return function () { return overlay.scrollStrategies.reposition(); }; } /** * \@docs-private */ var MAT_AUTOCOMPLETE_SCROLL_STRATEGY_PROVIDER = { provide: MAT_AUTOCOMPLETE_SCROLL_STRATEGY, deps: [Overlay], useFactory: MAT_AUTOCOMPLETE_SCROLL_STRATEGY_PROVIDER_FACTORY, }; /** * Provider that allows the autocomplete to register as a ControlValueAccessor. * \@docs-private */ var MAT_AUTOCOMPLETE_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(function () { return MatAutocompleteTrigger; }), multi: true }; /** * Creates an error to be thrown when attempting to use an autocomplete trigger without a panel. * @return {?} */ function getMatAutocompleteMissingPanelError() { return Error('Attempting to open an undefined instance of `mat-autocomplete`. ' + 'Make sure that the id passed to the `matAutocomplete` is correct and that ' + 'you\'re attempting to open it after the ngAfterContentInit hook.'); } var MatAutocompleteTrigger = (function () { /** * @param {?} _element * @param {?} _overlay * @param {?} _viewContainerRef * @param {?} _zone * @param {?} _changeDetectorRef * @param {?} _scrollStrategy * @param {?} _dir * @param {?} _formField * @param {?} _document */ function MatAutocompleteTrigger(_element, _overlay, _viewContainerRef, _zone, _changeDetectorRef, _scrollStrategy, _dir, _formField, _document) { this._element = _element; this._overlay = _overlay; this._viewContainerRef = _viewContainerRef; this._zone = _zone; this._changeDetectorRef = _changeDetectorRef; this._scrollStrategy = _scrollStrategy; this._dir = _dir; this._formField = _formField; this._document = _document; this._panelOpen = false; /** * Whether or not the placeholder state is being overridden. */ this._manuallyFloatingPlaceholder = false; /** * Stream of escape keyboard events. */ this._escapeEventStream = new Subject(); /** * View -> model callback called when value changes */ this._onChange = function () { }; /** * View -> model callback called when autocomplete has been touched */ this._onTouched = function () { }; } /** * @return {?} */ MatAutocompleteTrigger.prototype.ngOnDestroy = function () { this._destroyPanel(); this._escapeEventStream.complete(); }; Object.defineProperty(MatAutocompleteTrigger.prototype, "panelOpen", { /** * @return {?} */ get: function () { return this._panelOpen && this.autocomplete.showPanel; }, enumerable: true, configurable: true }); /** * Opens the autocomplete suggestion panel. * @return {?} */ MatAutocompleteTrigger.prototype.openPanel = function () { this._attachOverlay(); this._floatPlaceholder(); }; /** * Closes the autocomplete suggestion panel. * @return {?} */ MatAutocompleteTrigger.prototype.closePanel = function () { if (this._overlayRef && this._overlayRef.hasAttached()) { this._overlayRef.detach(); this._closingActionsSubscription.unsubscribe(); } this._resetPlaceholder(); if (this._panelOpen) { this.autocomplete._isOpen = this._panelOpen = false; // We need to trigger change detection manually, because // `fromEvent` doesn't seem to do it at the proper time. // This ensures that the placeholder is reset when the // user clicks outside. this._changeDetectorRef.detectChanges(); } }; Object.defineProperty(MatAutocompleteTrigger.prototype, "panelClosingActions", { /** * A stream of actions that should close the autocomplete panel, including * when an option is selected, on blur, and when TAB is pressed. * @return {?} */ get: function () { return merge(this.optionSelections, this.autocomplete._keyManager.tabOut, this._escapeEventStream, this._outsideClickStream); }, enumerable: true, configurable: true }); Object.defineProperty(MatAutocompleteTrigger.prototype, "optionSelections", { /** * Stream of autocomplete option selections. * @return {?} */ get: function () { return merge.apply(void 0, this.autocomplete.options.map(function (option) { return option.onSelectionChange; })); }, enumerable: true, configurable: true }); Object.defineProperty(MatAutocompleteTrigger.prototype, "activeOption", { /** * The currently active option, coerced to MatOption type. * @return {?} */ get: function () { if (this.autocomplete && this.autocomplete._keyManager) { return this.autocomplete._keyManager.activeItem; } return null; }, enumerable: true, configurable: true }); Object.defineProperty(MatAutocompleteTrigger.prototype, "_outsideClickStream", { /** * Stream of clicks outside of the autocomplete panel. * @return {?} */ get: function () { var _this = this; if (!this._document) { return of(null); } return RxChain.from(merge(fromEvent(this._document, 'click'), fromEvent(this._document, 'touchend'))).call(filter, function (event) { var /** @type {?} */ clickTarget = (event.target); var /** @type {?} */ formField = _this._formField ? _this._formField._elementRef.nativeElement : null; return _this._panelOpen && clickTarget !== _this._element.nativeElement && (!formField || !formField.contains(clickTarget)) && (!!_this._overlayRef && !_this._overlayRef.overlayElement.contains(clickTarget)); }).result(); }, enumerable: true, configurable: true }); /** * Sets the autocomplete's value. Part of the ControlValueAccessor interface * required to integrate with Angular's core forms API. * * @param {?} value New value to be written to the model. * @return {?} */ MatAutocompleteTrigger.prototype.writeValue = function (value) { var _this = this; Promise.resolve(null).then(function () { return _this._setTriggerValue(value); }); }; /** * Saves a callback function to be invoked when the autocomplete's value * changes from user input. Part of the ControlValueAccessor interface * required to integrate with Angular's core forms API. * * @param {?} fn Callback to be triggered when the value changes. * @return {?} */ MatAutocompleteTrigger.prototype.registerOnChange = function (fn) { this._onChange = fn; }; /** * Saves a callback function to be invoked when the autocomplete is blurred * by the user. Part of the ControlValueAccessor interface required * to integrate with Angular's core forms API. * * @param {?} fn Callback to be triggered when the component has been touched. * @return {?} */ MatAutocompleteTrigger.prototype.registerOnTouched = function (fn) { this._onTouched = fn; }; /** * @param {?} event * @return {?} */ MatAutocompleteTrigger.prototype._handleKeydown = function (event) { var /** @type {?} */ keyCode = event.keyCode; if (keyCode === ESCAPE && this.panelOpen) { this._resetActiveItem(); this._escapeEventStream.next(); event.stopPropagation(); } else if (this.activeOption && keyCode === ENTER && this.panelOpen) { this.activeOption._selectViaInteraction(); this._resetActiveItem(); event.preventDefault(); } else { var /** @type {?} */ prevActiveItem = this.autocomplete._keyManager.activeItem; var /** @type {?} */ isArrowKey = keyCode === UP_ARROW || keyCode === DOWN_ARROW; if (this.panelOpen || keyCode === TAB) { this.autocomplete._keyManager.onKeydown(event); } else if (isArrowKey) { this.openPanel(); } if (isArrowKey || this.autocomplete._keyManager.activeItem !== prevActiveItem) { this._scrollToOption(); } } }; /** * @param {?} event * @return {?} */ MatAutocompleteTrigger.prototype._handleInput = function (event) { // We need to ensure that the input is focused, because IE will fire the `input` // event on focus/blur/load if the input has a placeholder. See: // https://connect.microsoft.com/IE/feedback/details/885747/ if (document.activeElement === event.target) { this._onChange(((event.target)).value); this.openPanel(); } }; /** * @return {?} */ MatAutocompleteTrigger.prototype._handleFocus = function () { if (!this._element.nativeElement.readOnly) { this._attachOverlay(); this._floatPlaceholder(true); } }; /** * In "auto" mode, the placeholder will animate down as soon as focus is lost. * This causes the value to jump when selecting an option with the mouse. * This method manually floats the placeholder until the panel can be closed. * @param {?=} shouldAnimate Whether the placeholder should be animated when it is floated. * @return {?} */ MatAutocompleteTrigger.prototype._floatPlaceholder = function (shouldAnimate) { if (shouldAnimate === void 0) { shouldAnimate = false; } if (this._formField && this._formField.floatPlaceholder === 'auto') { if (shouldAnimate) { this._formField._animateAndLockPlaceholder(); } else { this._formField.floatPlaceholder = 'always'; } this._manuallyFloatingPlaceholder = true; } }; /** * If the placeholder has been manually elevated, return it to its normal state. * @return {?} */ MatAutocompleteTrigger.prototype._resetPlaceholder = function () { if (this._manuallyFloatingPlaceholder) { this._formField.floatPlaceholder = 'auto'; this._manuallyFloatingPlaceholder = false; } }; /** * Given that we are not actually focusing active options, we must manually adjust scroll * to reveal options below the fold. First, we find the offset of the option from the top * of the panel. If that offset is below the fold, the new scrollTop will be the offset - * the panel height + the option height, so the active option will be just visible at the * bottom of the panel. If that offset is above the top of the visible panel, the new scrollTop * will become the offset. If that offset is visible within the panel already, the scrollTop is * not adjusted. * @return {?} */ MatAutocompleteTrigger.prototype._scrollToOption = function () { var /** @type {?} */ activeOptionIndex = this.autocomplete._keyManager.activeItemIndex || 0; var /** @type {?} */ labelCount = MatOption.countGroupLabelsBeforeOption(activeOptionIndex, this.autocomplete.options, this.autocomplete.optionGroups); var /** @type {?} */ optionOffset = (activeOptionIndex + labelCount) * AUTOCOMPLETE_OPTION_HEIGHT; var /** @type {?} */ panelTop = this.autocomplete._getScrollTop(); if (optionOffset < panelTop) { // Scroll up to reveal selected option scrolled above the panel top this.autocomplete._setScrollTop(optionOffset); } else if (optionOffset + AUTOCOMPLETE_OPTION_HEIGHT > panelTop + AUTOCOMPLETE_PANEL_HEIGHT) { // Scroll down to reveal selected option scrolled below the panel bottom var /** @type {?} */ newScrollTop = optionOffset - AUTOCOMPLETE_PANEL_HEIGHT + AUTOCOMPLETE_OPTION_HEIGHT; this.autocomplete._setScrollTop(Math.max(0, newScrollTop)); } }; /** * This method listens to a stream of panel closing actions and resets the * stream every time the option list changes. * @return {?} */ MatAutocompleteTrigger.prototype._subscribeToClosingActions = function () { var _this = this; var /** @type {?} */ firstStable = first.call(this._zone.onStable.asObservable()); var /** @type {?} */ optionChanges = RxChain.from(this.autocomplete.options.changes) .call(doOperator, function () { return _this._positionStrategy.recalculateLastPosition(); }) .call(delay, 0) .result(); // When the zone is stable initially, and when the option list changes... return RxChain.from(merge(firstStable, optionChanges)) .call(switchMap, function () { _this._resetActiveItem(); _this.autocomplete._setVisibility(); return _this.panelClosingActions; }) .call(first) .subscribe(function (event) { return _this._setValueAndClose(event); }); }; /** * Destroys the autocomplete suggestion panel. * @return {?} */ MatAutocompleteTrigger.prototype._destroyPanel = function () { if (this._overlayRef) { this.closePanel(); this._overlayRef.dispose(); this._overlayRef = null; } }; /** * @param {?} value * @return {?} */ MatAutocompleteTrigger.prototype._setTriggerValue = function (value) { var /** @type {?} */ toDisplay = this.autocomplete && this.autocomplete.displayWith ? this.autocomplete.displayWith(value) : value; // Simply falling back to an empty string if the display value is falsy does not work properly. // The display value can also be the number zero and shouldn't fall back to an empty string. var /** @type {?} */ inputValue = toDisplay != null ? toDisplay : ''; // If it's used within a `MatFormField`, we should set it through the property so it can go // through change detection. if (this._formField) { this._formField._control.value = inputValue; } else { this._element.nativeElement.value = inputValue; } }; /** * This method closes the panel, and if a value is specified, also sets the associated * control to that value. It will also mark the control as dirty if this interaction * stemmed from the user. * @param {?} event * @return {?} */ MatAutocompleteTrigger.prototype._setValueAndClose = function (event) { if (event && event.source) { this._clearPreviousSelectedOption(event.source); this._setTriggerValue(event.source.value); this._onChange(event.source.value); this._element.nativeElement.focus(); this.autocomplete._emitSelectEvent(event.source); } this.closePanel(); }; /** * Clear any previous selected option and emit a selection change event for this option * @param {?} skip * @return {?} */ MatAutocompleteTrigger.prototype._clearPreviousSelectedOption = function (skip) { this.autocomplete.options.forEach(function (option) { if (option != skip && option.selected) { option.deselect(); } }); }; /** * @return {?} */ MatAutocompleteTrigger.prototype._attachOverlay = function () { if (!this.autocomplete) { throw getMatAutocompleteMissingPanelError(); } if (!this._overlayRef) { this._portal = new TemplatePortal(this.autocomplete.template, this._viewContainerRef); this._overlayRef = this._overlay.create(this._getOverlayConfig()); } else { /** Update the panel width, in case the host width has changed */ this._overlayRef.getConfig().width = this._getHostWidth(); this._overlayRef.updateSize(); } if (this._overlayRef && !this._overlayRef.hasAttached()) { this._overlayRef.attach(this._portal); this._closingActionsSubscription = this._subscribeToClosingActions(); } this.autocomplete._setVisibility(); this.autocomplete._isOpen = this._panelOpen = true; }; /** * @return {?} */ MatAutocompleteTrigger.prototype._getOverlayConfig = function () { return new OverlayConfig({ positionStrategy: this._getOverlayPosition(), scrollStrategy: this._scrollStrategy(), width: this._getHostWidth(), direction: this._dir ? this._dir.value : 'ltr' }); }; /** * @return {?} */ MatAutocompleteTrigger.prototype._getOverlayPosition = function () { this._positionStrategy = this._overlay.position().connectedTo(this._getConnectedElement(), { originX: 'start', originY: 'bottom' }, { overlayX: 'start', overlayY: 'top' }) .withFallbackPosition({ originX: 'start', originY: 'top' }, { overlayX: 'start', overlayY: 'bottom' }); return this._positionStrategy; }; /** * @return {?} */ MatAutocompleteTrigger.prototype._getConnectedElement = function () { return this._formField ? this._formField._connectionContainerRef : this._element; }; /** * Returns the width of the input element, so the panel width can match it. * @return {?} */ MatAutocompleteTrigger.prototype._getHostWidth = function () { return this._getConnectedElement().nativeElement.getBoundingClientRect().width; }; /** * Reset active item to -1 so arrow events will activate the correct options. * @return {?} */ MatAutocompleteTrigger.prototype._resetActiveItem = function () { this.autocomplete._keyManager.setActiveItem(-1); }; MatAutocompleteTrigger.decorators = [ { type: Directive, args: [{ selector: "input[matAutocomplete], textarea[matAutocomplete]", host: { 'role': 'combobox', 'autocomplete': 'off', 'aria-autocomplete': 'list', '[attr.aria-activedescendant]': 'activeOption?.id', '[attr.aria-expanded]': 'panelOpen.toString()', '[attr.aria-owns]': 'autocomplete?.id', // Note: we use `focusin`, as opposed to `focus`, in order to open the panel // a little earlier. This avoids issues where IE delays the focusing of the input. '(focusin)': '_handleFocus()', '(blur)': '_onTouched()', '(input)': '_handleInput($event)', '(keydown)': '_handleKeydown($event)', }, providers: [MAT_AUTOCOMPLETE_VALUE_ACCESSOR] },] }, ]; /** * @nocollapse */ MatAutocompleteTrigger.ctorParameters = function () { return [ { type: ElementRef, }, { type: Overlay, }, { type: ViewContainerRef, }, { type: NgZone, }, { type: ChangeDetectorRef, }, { type: undefined, decorators: [{ type: Inject, args: [MAT_AUTOCOMPLETE_SCROLL_STRATEGY,] },] }, { type: Directionality, decorators: [{ type: Optional },] }, { type: MatFormField, decorators: [{ type: Optional }, { type: Host },] }, { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [DOCUMENT,] },] }, ]; }; MatAutocompleteTrigger.propDecorators = { 'autocomplete': [{ type: Input, args: ['matAutocomplete',] },], }; return MatAutocompleteTrigger; }()); var MatAutocompleteModule = (function () { function MatAutocompleteModule() { } MatAutocompleteModule.decorators = [ { type: NgModule, args: [{ imports: [MatOptionModule, OverlayModule, MatCommonModule, CommonModule], exports: [MatAutocomplete, MatOptionModule, MatAutocompleteTrigger, MatCommonModule], declarations: [MatAutocomplete, MatAutocompleteTrigger], providers: [MAT_AUTOCOMPLETE_SCROLL_STRATEGY_PROVIDER], },] }, ]; /** * @nocollapse */ MatAutocompleteModule.ctorParameters = function () { return []; }; return MatAutocompleteModule; }()); /** * Generated bundle index. Do not edit. */ export { MatAutocompleteSelectedEvent, MatAutocomplete, MatAutocompleteModule, AUTOCOMPLETE_OPTION_HEIGHT, AUTOCOMPLETE_PANEL_HEIGHT, MAT_AUTOCOMPLETE_SCROLL_STRATEGY, MAT_AUTOCOMPLETE_SCROLL_STRATEGY_PROVIDER_FACTORY, MAT_AUTOCOMPLETE_SCROLL_STRATEGY_PROVIDER, MAT_AUTOCOMPLETE_VALUE_ACCESSOR, getMatAutocompleteMissingPanelError, MatAutocompleteTrigger }; //# sourceMappingURL=autocomplete.es5.js.map