1 | import { ActiveDescendantKeyManager } from '@angular/cdk/a11y';
|
---|
2 | import { coerceBooleanProperty, coerceStringArray } from '@angular/cdk/coercion';
|
---|
3 | import { Platform, _getEventTarget } from '@angular/cdk/platform';
|
---|
4 | import { InjectionToken, EventEmitter, Directive, ChangeDetectorRef, ElementRef, Inject, ViewChild, TemplateRef, Input, Output, Component, ViewEncapsulation, ChangeDetectionStrategy, ContentChildren, forwardRef, ViewContainerRef, NgZone, Optional, Host, NgModule } from '@angular/core';
|
---|
5 | import { mixinDisableRipple, MAT_OPTION_PARENT_COMPONENT, MAT_OPTGROUP, MatOption, MatOptionSelectionChange, _countGroupLabelsBeforeOption, _getOptionScrollPosition, MatOptionModule, MatCommonModule } from '@angular/material/core';
|
---|
6 | import { Subscription, Subject, defer, merge, of, fromEvent } from 'rxjs';
|
---|
7 | import { DOCUMENT, CommonModule } from '@angular/common';
|
---|
8 | import { Overlay, OverlayConfig, OverlayModule } from '@angular/cdk/overlay';
|
---|
9 | import { ViewportRuler, CdkScrollableModule } from '@angular/cdk/scrolling';
|
---|
10 | import { Directionality } from '@angular/cdk/bidi';
|
---|
11 | import { ESCAPE, hasModifierKey, ENTER, UP_ARROW, DOWN_ARROW, TAB } from '@angular/cdk/keycodes';
|
---|
12 | import { TemplatePortal } from '@angular/cdk/portal';
|
---|
13 | import { NG_VALUE_ACCESSOR } from '@angular/forms';
|
---|
14 | import { MatFormField, MAT_FORM_FIELD } from '@angular/material/form-field';
|
---|
15 | import { take, switchMap, filter, map, tap, delay } from 'rxjs/operators';
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * @license
|
---|
19 | * Copyright Google LLC All Rights Reserved.
|
---|
20 | *
|
---|
21 | * Use of this source code is governed by an MIT-style license that can be
|
---|
22 | * found in the LICENSE file at https://angular.io/license
|
---|
23 | */
|
---|
24 | /**
|
---|
25 | * Autocomplete IDs need to be unique across components, so this counter exists outside of
|
---|
26 | * the component definition.
|
---|
27 | */
|
---|
28 | import * as ɵngcc0 from '@angular/core';
|
---|
29 | import * as ɵngcc1 from '@angular/cdk/platform';
|
---|
30 | import * as ɵngcc2 from '@angular/common';
|
---|
31 | import * as ɵngcc3 from '@angular/cdk/overlay';
|
---|
32 | import * as ɵngcc4 from '@angular/cdk/bidi';
|
---|
33 | import * as ɵngcc5 from '@angular/cdk/scrolling';
|
---|
34 | import * as ɵngcc6 from '@angular/material/form-field';
|
---|
35 |
|
---|
36 | const _c0 = ["panel"];
|
---|
37 | function MatAutocomplete_ng_template_0_Template(rf, ctx) { if (rf & 1) {
|
---|
38 | ɵngcc0.ɵɵelementStart(0, "div", 0, 1);
|
---|
39 | ɵngcc0.ɵɵprojection(2);
|
---|
40 | ɵngcc0.ɵɵelementEnd();
|
---|
41 | } if (rf & 2) {
|
---|
42 | const formFieldId_r1 = ctx.id;
|
---|
43 | const ctx_r0 = ɵngcc0.ɵɵnextContext();
|
---|
44 | ɵngcc0.ɵɵproperty("id", ctx_r0.id)("ngClass", ctx_r0._classList);
|
---|
45 | ɵngcc0.ɵɵattribute("aria-label", ctx_r0.ariaLabel || null)("aria-labelledby", ctx_r0._getPanelAriaLabelledby(formFieldId_r1));
|
---|
46 | } }
|
---|
47 | const _c1 = ["*"];
|
---|
48 | let _uniqueAutocompleteIdCounter = 0;
|
---|
49 | /** Event object that is emitted when an autocomplete option is selected. */
|
---|
50 | class MatAutocompleteSelectedEvent {
|
---|
51 | constructor(
|
---|
52 | /** Reference to the autocomplete panel that emitted the event. */
|
---|
53 | source,
|
---|
54 | /** Option that was selected. */
|
---|
55 | option) {
|
---|
56 | this.source = source;
|
---|
57 | this.option = option;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | // Boilerplate for applying mixins to MatAutocomplete.
|
---|
61 | /** @docs-private */
|
---|
62 | const _MatAutocompleteMixinBase = mixinDisableRipple(class {
|
---|
63 | });
|
---|
64 | /** Injection token to be used to override the default options for `mat-autocomplete`. */
|
---|
65 | const MAT_AUTOCOMPLETE_DEFAULT_OPTIONS = new InjectionToken('mat-autocomplete-default-options', {
|
---|
66 | providedIn: 'root',
|
---|
67 | factory: MAT_AUTOCOMPLETE_DEFAULT_OPTIONS_FACTORY,
|
---|
68 | });
|
---|
69 | /** @docs-private */
|
---|
70 | function MAT_AUTOCOMPLETE_DEFAULT_OPTIONS_FACTORY() {
|
---|
71 | return { autoActiveFirstOption: false };
|
---|
72 | }
|
---|
73 | /** Base class with all of the `MatAutocomplete` functionality. */
|
---|
74 | class _MatAutocompleteBase extends _MatAutocompleteMixinBase {
|
---|
75 | constructor(_changeDetectorRef, _elementRef, defaults, platform) {
|
---|
76 | super();
|
---|
77 | this._changeDetectorRef = _changeDetectorRef;
|
---|
78 | this._elementRef = _elementRef;
|
---|
79 | this._activeOptionChanges = Subscription.EMPTY;
|
---|
80 | /** Whether the autocomplete panel should be visible, depending on option length. */
|
---|
81 | this.showPanel = false;
|
---|
82 | this._isOpen = false;
|
---|
83 | /** Function that maps an option's control value to its display value in the trigger. */
|
---|
84 | this.displayWith = null;
|
---|
85 | /** Event that is emitted whenever an option from the list is selected. */
|
---|
86 | this.optionSelected = new EventEmitter();
|
---|
87 | /** Event that is emitted when the autocomplete panel is opened. */
|
---|
88 | this.opened = new EventEmitter();
|
---|
89 | /** Event that is emitted when the autocomplete panel is closed. */
|
---|
90 | this.closed = new EventEmitter();
|
---|
91 | /** Emits whenever an option is activated using the keyboard. */
|
---|
92 | this.optionActivated = new EventEmitter();
|
---|
93 | this._classList = {};
|
---|
94 | /** Unique ID to be used by autocomplete trigger's "aria-owns" property. */
|
---|
95 | this.id = `mat-autocomplete-${_uniqueAutocompleteIdCounter++}`;
|
---|
96 | // TODO(crisbeto): the problem that the `inertGroups` option resolves is only present on
|
---|
97 | // Safari using VoiceOver. We should occasionally check back to see whether the bug
|
---|
98 | // wasn't resolved in VoiceOver, and if it has, we can remove this and the `inertGroups`
|
---|
99 | // option altogether.
|
---|
100 | this.inertGroups = (platform === null || platform === void 0 ? void 0 : platform.SAFARI) || false;
|
---|
101 | this._autoActiveFirstOption = !!defaults.autoActiveFirstOption;
|
---|
102 | }
|
---|
103 | /** Whether the autocomplete panel is open. */
|
---|
104 | get isOpen() { return this._isOpen && this.showPanel; }
|
---|
105 | /**
|
---|
106 | * Whether the first option should be highlighted when the autocomplete panel is opened.
|
---|
107 | * Can be configured globally through the `MAT_AUTOCOMPLETE_DEFAULT_OPTIONS` token.
|
---|
108 | */
|
---|
109 | get autoActiveFirstOption() { return this._autoActiveFirstOption; }
|
---|
110 | set autoActiveFirstOption(value) {
|
---|
111 | this._autoActiveFirstOption = coerceBooleanProperty(value);
|
---|
112 | }
|
---|
113 | /**
|
---|
114 | * Takes classes set on the host mat-autocomplete element and applies them to the panel
|
---|
115 | * inside the overlay container to allow for easy styling.
|
---|
116 | */
|
---|
117 | set classList(value) {
|
---|
118 | if (value && value.length) {
|
---|
119 | this._classList = coerceStringArray(value).reduce((classList, className) => {
|
---|
120 | classList[className] = true;
|
---|
121 | return classList;
|
---|
122 | }, {});
|
---|
123 | }
|
---|
124 | else {
|
---|
125 | this._classList = {};
|
---|
126 | }
|
---|
127 | this._setVisibilityClasses(this._classList);
|
---|
128 | this._elementRef.nativeElement.className = '';
|
---|
129 | }
|
---|
130 | ngAfterContentInit() {
|
---|
131 | this._keyManager = new ActiveDescendantKeyManager(this.options).withWrap();
|
---|
132 | this._activeOptionChanges = this._keyManager.change.subscribe(index => {
|
---|
133 | if (this.isOpen) {
|
---|
134 | this.optionActivated.emit({ source: this, option: this.options.toArray()[index] || null });
|
---|
135 | }
|
---|
136 | });
|
---|
137 | // Set the initial visibility state.
|
---|
138 | this._setVisibility();
|
---|
139 | }
|
---|
140 | ngOnDestroy() {
|
---|
141 | this._activeOptionChanges.unsubscribe();
|
---|
142 | }
|
---|
143 | /**
|
---|
144 | * Sets the panel scrollTop. This allows us to manually scroll to display options
|
---|
145 | * above or below the fold, as they are not actually being focused when active.
|
---|
146 | */
|
---|
147 | _setScrollTop(scrollTop) {
|
---|
148 | if (this.panel) {
|
---|
149 | this.panel.nativeElement.scrollTop = scrollTop;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | /** Returns the panel's scrollTop. */
|
---|
153 | _getScrollTop() {
|
---|
154 | return this.panel ? this.panel.nativeElement.scrollTop : 0;
|
---|
155 | }
|
---|
156 | /** Panel should hide itself when the option list is empty. */
|
---|
157 | _setVisibility() {
|
---|
158 | this.showPanel = !!this.options.length;
|
---|
159 | this._setVisibilityClasses(this._classList);
|
---|
160 | this._changeDetectorRef.markForCheck();
|
---|
161 | }
|
---|
162 | /** Emits the `select` event. */
|
---|
163 | _emitSelectEvent(option) {
|
---|
164 | const event = new MatAutocompleteSelectedEvent(this, option);
|
---|
165 | this.optionSelected.emit(event);
|
---|
166 | }
|
---|
167 | /** Gets the aria-labelledby for the autocomplete panel. */
|
---|
168 | _getPanelAriaLabelledby(labelId) {
|
---|
169 | if (this.ariaLabel) {
|
---|
170 | return null;
|
---|
171 | }
|
---|
172 | const labelExpression = labelId ? labelId + ' ' : '';
|
---|
173 | return this.ariaLabelledby ? labelExpression + this.ariaLabelledby : labelId;
|
---|
174 | }
|
---|
175 | /** Sets the autocomplete visibility classes on a classlist based on the panel is visible. */
|
---|
176 | _setVisibilityClasses(classList) {
|
---|
177 | classList[this._visibleClass] = this.showPanel;
|
---|
178 | classList[this._hiddenClass] = !this.showPanel;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | _MatAutocompleteBase.ɵfac = function _MatAutocompleteBase_Factory(t) { return new (t || _MatAutocompleteBase)(ɵngcc0.ɵɵdirectiveInject(ɵngcc0.ChangeDetectorRef), ɵngcc0.ɵɵdirectiveInject(ɵngcc0.ElementRef), ɵngcc0.ɵɵdirectiveInject(MAT_AUTOCOMPLETE_DEFAULT_OPTIONS), ɵngcc0.ɵɵdirectiveInject(ɵngcc1.Platform)); };
|
---|
182 | _MatAutocompleteBase.ɵdir = /*@__PURE__*/ ɵngcc0.ɵɵdefineDirective({ type: _MatAutocompleteBase, viewQuery: function _MatAutocompleteBase_Query(rf, ctx) { if (rf & 1) {
|
---|
183 | ɵngcc0.ɵɵviewQuery(TemplateRef, 7);
|
---|
184 | ɵngcc0.ɵɵviewQuery(_c0, 5);
|
---|
185 | } if (rf & 2) {
|
---|
186 | let _t;
|
---|
187 | ɵngcc0.ɵɵqueryRefresh(_t = ɵngcc0.ɵɵloadQuery()) && (ctx.template = _t.first);
|
---|
188 | ɵngcc0.ɵɵqueryRefresh(_t = ɵngcc0.ɵɵloadQuery()) && (ctx.panel = _t.first);
|
---|
189 | } }, inputs: { displayWith: "displayWith", autoActiveFirstOption: "autoActiveFirstOption", classList: ["class", "classList"], ariaLabel: ["aria-label", "ariaLabel"], ariaLabelledby: ["aria-labelledby", "ariaLabelledby"], panelWidth: "panelWidth" }, outputs: { optionSelected: "optionSelected", opened: "opened", closed: "closed", optionActivated: "optionActivated" }, features: [ɵngcc0.ɵɵInheritDefinitionFeature] });
|
---|
190 | _MatAutocompleteBase.ctorParameters = () => [
|
---|
191 | { type: ChangeDetectorRef },
|
---|
192 | { type: ElementRef },
|
---|
193 | { type: undefined, decorators: [{ type: Inject, args: [MAT_AUTOCOMPLETE_DEFAULT_OPTIONS,] }] },
|
---|
194 | { type: Platform }
|
---|
195 | ];
|
---|
196 | _MatAutocompleteBase.propDecorators = {
|
---|
197 | template: [{ type: ViewChild, args: [TemplateRef, { static: true },] }],
|
---|
198 | panel: [{ type: ViewChild, args: ['panel',] }],
|
---|
199 | ariaLabel: [{ type: Input, args: ['aria-label',] }],
|
---|
200 | ariaLabelledby: [{ type: Input, args: ['aria-labelledby',] }],
|
---|
201 | displayWith: [{ type: Input }],
|
---|
202 | autoActiveFirstOption: [{ type: Input }],
|
---|
203 | panelWidth: [{ type: Input }],
|
---|
204 | optionSelected: [{ type: Output }],
|
---|
205 | opened: [{ type: Output }],
|
---|
206 | closed: [{ type: Output }],
|
---|
207 | optionActivated: [{ type: Output }],
|
---|
208 | classList: [{ type: Input, args: ['class',] }]
|
---|
209 | };
|
---|
210 | (function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(_MatAutocompleteBase, [{
|
---|
211 | type: Directive
|
---|
212 | }], function () { return [{ type: ɵngcc0.ChangeDetectorRef }, { type: ɵngcc0.ElementRef }, { type: undefined, decorators: [{
|
---|
213 | type: Inject,
|
---|
214 | args: [MAT_AUTOCOMPLETE_DEFAULT_OPTIONS]
|
---|
215 | }] }, { type: ɵngcc1.Platform }]; }, { displayWith: [{
|
---|
216 | type: Input
|
---|
217 | }], optionSelected: [{
|
---|
218 | type: Output
|
---|
219 | }], opened: [{
|
---|
220 | type: Output
|
---|
221 | }], closed: [{
|
---|
222 | type: Output
|
---|
223 | }], optionActivated: [{
|
---|
224 | type: Output
|
---|
225 | }], autoActiveFirstOption: [{
|
---|
226 | type: Input
|
---|
227 | }], classList: [{
|
---|
228 | type: Input,
|
---|
229 | args: ['class']
|
---|
230 | }], template: [{
|
---|
231 | type: ViewChild,
|
---|
232 | args: [TemplateRef, { static: true }]
|
---|
233 | }], panel: [{
|
---|
234 | type: ViewChild,
|
---|
235 | args: ['panel']
|
---|
236 | }], ariaLabel: [{
|
---|
237 | type: Input,
|
---|
238 | args: ['aria-label']
|
---|
239 | }], ariaLabelledby: [{
|
---|
240 | type: Input,
|
---|
241 | args: ['aria-labelledby']
|
---|
242 | }], panelWidth: [{
|
---|
243 | type: Input
|
---|
244 | }] }); })();
|
---|
245 | class MatAutocomplete extends _MatAutocompleteBase {
|
---|
246 | constructor() {
|
---|
247 | super(...arguments);
|
---|
248 | this._visibleClass = 'mat-autocomplete-visible';
|
---|
249 | this._hiddenClass = 'mat-autocomplete-hidden';
|
---|
250 | }
|
---|
251 | }
|
---|
252 | MatAutocomplete.ɵfac = /*@__PURE__*/ function () { let ɵMatAutocomplete_BaseFactory; return function MatAutocomplete_Factory(t) { return (ɵMatAutocomplete_BaseFactory || (ɵMatAutocomplete_BaseFactory = ɵngcc0.ɵɵgetInheritedFactory(MatAutocomplete)))(t || MatAutocomplete); }; }();
|
---|
253 | MatAutocomplete.ɵcmp = /*@__PURE__*/ ɵngcc0.ɵɵdefineComponent({ type: MatAutocomplete, selectors: [["mat-autocomplete"]], contentQueries: function MatAutocomplete_ContentQueries(rf, ctx, dirIndex) { if (rf & 1) {
|
---|
254 | ɵngcc0.ɵɵcontentQuery(dirIndex, MAT_OPTGROUP, 5);
|
---|
255 | ɵngcc0.ɵɵcontentQuery(dirIndex, MatOption, 5);
|
---|
256 | } if (rf & 2) {
|
---|
257 | let _t;
|
---|
258 | ɵngcc0.ɵɵqueryRefresh(_t = ɵngcc0.ɵɵloadQuery()) && (ctx.optionGroups = _t);
|
---|
259 | ɵngcc0.ɵɵqueryRefresh(_t = ɵngcc0.ɵɵloadQuery()) && (ctx.options = _t);
|
---|
260 | } }, hostAttrs: [1, "mat-autocomplete"], inputs: { disableRipple: "disableRipple" }, exportAs: ["matAutocomplete"], features: [ɵngcc0.ɵɵProvidersFeature([
|
---|
261 | { provide: MAT_OPTION_PARENT_COMPONENT, useExisting: MatAutocomplete }
|
---|
262 | ]), ɵngcc0.ɵɵInheritDefinitionFeature], ngContentSelectors: _c1, decls: 1, vars: 0, consts: [["role", "listbox", 1, "mat-autocomplete-panel", 3, "id", "ngClass"], ["panel", ""]], template: function MatAutocomplete_Template(rf, ctx) { if (rf & 1) {
|
---|
263 | ɵngcc0.ɵɵprojectionDef();
|
---|
264 | ɵngcc0.ɵɵtemplate(0, MatAutocomplete_ng_template_0_Template, 3, 4, "ng-template");
|
---|
265 | } }, directives: [ɵngcc2.NgClass], 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;width:100%;border-bottom-left-radius:4px;border-bottom-right-radius:4px}.mat-autocomplete-panel.mat-autocomplete-visible{visibility:visible}.mat-autocomplete-panel.mat-autocomplete-hidden{visibility:hidden}.mat-autocomplete-panel-above .mat-autocomplete-panel{border-radius:0;border-top-left-radius:4px;border-top-right-radius:4px}.mat-autocomplete-panel .mat-divider-horizontal{margin-top:-1px}.cdk-high-contrast-active .mat-autocomplete-panel{outline:solid 1px}mat-autocomplete{display:none}\n"], encapsulation: 2, changeDetection: 0 });
|
---|
266 | MatAutocomplete.propDecorators = {
|
---|
267 | optionGroups: [{ type: ContentChildren, args: [MAT_OPTGROUP, { descendants: true },] }],
|
---|
268 | options: [{ type: ContentChildren, args: [MatOption, { descendants: true },] }]
|
---|
269 | };
|
---|
270 | (function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(MatAutocomplete, [{
|
---|
271 | type: Component,
|
---|
272 | args: [{
|
---|
273 | selector: 'mat-autocomplete',
|
---|
274 | template: "<ng-template let-formFieldId=\"id\">\n <div class=\"mat-autocomplete-panel\"\n role=\"listbox\"\n [id]=\"id\"\n [attr.aria-label]=\"ariaLabel || null\"\n [attr.aria-labelledby]=\"_getPanelAriaLabelledby(formFieldId)\"\n [ngClass]=\"_classList\"\n #panel>\n <ng-content></ng-content>\n </div>\n</ng-template>\n",
|
---|
275 | encapsulation: ViewEncapsulation.None,
|
---|
276 | changeDetection: ChangeDetectionStrategy.OnPush,
|
---|
277 | exportAs: 'matAutocomplete',
|
---|
278 | inputs: ['disableRipple'],
|
---|
279 | host: {
|
---|
280 | 'class': 'mat-autocomplete'
|
---|
281 | },
|
---|
282 | providers: [
|
---|
283 | { provide: MAT_OPTION_PARENT_COMPONENT, useExisting: MatAutocomplete }
|
---|
284 | ],
|
---|
285 | 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;width:100%;border-bottom-left-radius:4px;border-bottom-right-radius:4px}.mat-autocomplete-panel.mat-autocomplete-visible{visibility:visible}.mat-autocomplete-panel.mat-autocomplete-hidden{visibility:hidden}.mat-autocomplete-panel-above .mat-autocomplete-panel{border-radius:0;border-top-left-radius:4px;border-top-right-radius:4px}.mat-autocomplete-panel .mat-divider-horizontal{margin-top:-1px}.cdk-high-contrast-active .mat-autocomplete-panel{outline:solid 1px}mat-autocomplete{display:none}\n"]
|
---|
286 | }]
|
---|
287 | }], null, { optionGroups: [{
|
---|
288 | type: ContentChildren,
|
---|
289 | args: [MAT_OPTGROUP, { descendants: true }]
|
---|
290 | }], options: [{
|
---|
291 | type: ContentChildren,
|
---|
292 | args: [MatOption, { descendants: true }]
|
---|
293 | }] }); })();
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * @license
|
---|
297 | * Copyright Google LLC All Rights Reserved.
|
---|
298 | *
|
---|
299 | * Use of this source code is governed by an MIT-style license that can be
|
---|
300 | * found in the LICENSE file at https://angular.io/license
|
---|
301 | */
|
---|
302 | /** Base class containing all of the functionality for `MatAutocompleteOrigin`. */
|
---|
303 | class _MatAutocompleteOriginBase {
|
---|
304 | constructor(
|
---|
305 | /** Reference to the element on which the directive is applied. */
|
---|
306 | elementRef) {
|
---|
307 | this.elementRef = elementRef;
|
---|
308 | }
|
---|
309 | }
|
---|
310 | _MatAutocompleteOriginBase.ɵfac = function _MatAutocompleteOriginBase_Factory(t) { return new (t || _MatAutocompleteOriginBase)(ɵngcc0.ɵɵdirectiveInject(ɵngcc0.ElementRef)); };
|
---|
311 | _MatAutocompleteOriginBase.ɵdir = /*@__PURE__*/ ɵngcc0.ɵɵdefineDirective({ type: _MatAutocompleteOriginBase });
|
---|
312 | _MatAutocompleteOriginBase.ctorParameters = () => [
|
---|
313 | { type: ElementRef }
|
---|
314 | ];
|
---|
315 | (function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(_MatAutocompleteOriginBase, [{
|
---|
316 | type: Directive
|
---|
317 | }], function () { return [{ type: ɵngcc0.ElementRef }]; }, null); })();
|
---|
318 | /**
|
---|
319 | * Directive applied to an element to make it usable
|
---|
320 | * as a connection point for an autocomplete panel.
|
---|
321 | */
|
---|
322 | class MatAutocompleteOrigin extends _MatAutocompleteOriginBase {
|
---|
323 | }
|
---|
324 | MatAutocompleteOrigin.ɵfac = /*@__PURE__*/ function () { let ɵMatAutocompleteOrigin_BaseFactory; return function MatAutocompleteOrigin_Factory(t) { return (ɵMatAutocompleteOrigin_BaseFactory || (ɵMatAutocompleteOrigin_BaseFactory = ɵngcc0.ɵɵgetInheritedFactory(MatAutocompleteOrigin)))(t || MatAutocompleteOrigin); }; }();
|
---|
325 | MatAutocompleteOrigin.ɵdir = /*@__PURE__*/ ɵngcc0.ɵɵdefineDirective({ type: MatAutocompleteOrigin, selectors: [["", "matAutocompleteOrigin", ""]], exportAs: ["matAutocompleteOrigin"], features: [ɵngcc0.ɵɵInheritDefinitionFeature] });
|
---|
326 | (function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(MatAutocompleteOrigin, [{
|
---|
327 | type: Directive,
|
---|
328 | args: [{
|
---|
329 | selector: '[matAutocompleteOrigin]',
|
---|
330 | exportAs: 'matAutocompleteOrigin'
|
---|
331 | }]
|
---|
332 | }], null, null); })();
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * @license
|
---|
336 | * Copyright Google LLC All Rights Reserved.
|
---|
337 | *
|
---|
338 | * Use of this source code is governed by an MIT-style license that can be
|
---|
339 | * found in the LICENSE file at https://angular.io/license
|
---|
340 | */
|
---|
341 | /** Injection token that determines the scroll handling while the autocomplete panel is open. */
|
---|
342 | const MAT_AUTOCOMPLETE_SCROLL_STRATEGY = new InjectionToken('mat-autocomplete-scroll-strategy');
|
---|
343 | /** @docs-private */
|
---|
344 | function MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY(overlay) {
|
---|
345 | return () => overlay.scrollStrategies.reposition();
|
---|
346 | }
|
---|
347 | /** @docs-private */
|
---|
348 | const MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY_PROVIDER = {
|
---|
349 | provide: MAT_AUTOCOMPLETE_SCROLL_STRATEGY,
|
---|
350 | deps: [Overlay],
|
---|
351 | useFactory: MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY,
|
---|
352 | };
|
---|
353 | /**
|
---|
354 | * Provider that allows the autocomplete to register as a ControlValueAccessor.
|
---|
355 | * @docs-private
|
---|
356 | */
|
---|
357 | const MAT_AUTOCOMPLETE_VALUE_ACCESSOR = {
|
---|
358 | provide: NG_VALUE_ACCESSOR,
|
---|
359 | useExisting: forwardRef(() => MatAutocompleteTrigger),
|
---|
360 | multi: true
|
---|
361 | };
|
---|
362 | /**
|
---|
363 | * Creates an error to be thrown when attempting to use an autocomplete trigger without a panel.
|
---|
364 | * @docs-private
|
---|
365 | */
|
---|
366 | function getMatAutocompleteMissingPanelError() {
|
---|
367 | return Error('Attempting to open an undefined instance of `mat-autocomplete`. ' +
|
---|
368 | 'Make sure that the id passed to the `matAutocomplete` is correct and that ' +
|
---|
369 | 'you\'re attempting to open it after the ngAfterContentInit hook.');
|
---|
370 | }
|
---|
371 | /** Base class with all of the `MatAutocompleteTrigger` functionality. */
|
---|
372 | class _MatAutocompleteTriggerBase {
|
---|
373 | constructor(_element, _overlay, _viewContainerRef, _zone, _changeDetectorRef, scrollStrategy, _dir, _formField, _document, _viewportRuler, _defaults) {
|
---|
374 | this._element = _element;
|
---|
375 | this._overlay = _overlay;
|
---|
376 | this._viewContainerRef = _viewContainerRef;
|
---|
377 | this._zone = _zone;
|
---|
378 | this._changeDetectorRef = _changeDetectorRef;
|
---|
379 | this._dir = _dir;
|
---|
380 | this._formField = _formField;
|
---|
381 | this._document = _document;
|
---|
382 | this._viewportRuler = _viewportRuler;
|
---|
383 | this._defaults = _defaults;
|
---|
384 | this._componentDestroyed = false;
|
---|
385 | this._autocompleteDisabled = false;
|
---|
386 | /** Whether or not the label state is being overridden. */
|
---|
387 | this._manuallyFloatingLabel = false;
|
---|
388 | /** Subscription to viewport size changes. */
|
---|
389 | this._viewportSubscription = Subscription.EMPTY;
|
---|
390 | /**
|
---|
391 | * Whether the autocomplete can open the next time it is focused. Used to prevent a focused,
|
---|
392 | * closed autocomplete from being reopened if the user switches to another browser tab and then
|
---|
393 | * comes back.
|
---|
394 | */
|
---|
395 | this._canOpenOnNextFocus = true;
|
---|
396 | /** Stream of keyboard events that can close the panel. */
|
---|
397 | this._closeKeyEventStream = new Subject();
|
---|
398 | /**
|
---|
399 | * Event handler for when the window is blurred. Needs to be an
|
---|
400 | * arrow function in order to preserve the context.
|
---|
401 | */
|
---|
402 | this._windowBlurHandler = () => {
|
---|
403 | // If the user blurred the window while the autocomplete is focused, it means that it'll be
|
---|
404 | // refocused when they come back. In this case we want to skip the first focus event, if the
|
---|
405 | // pane was closed, in order to avoid reopening it unintentionally.
|
---|
406 | this._canOpenOnNextFocus =
|
---|
407 | this._document.activeElement !== this._element.nativeElement || this.panelOpen;
|
---|
408 | };
|
---|
409 | /** `View -> model callback called when value changes` */
|
---|
410 | this._onChange = () => { };
|
---|
411 | /** `View -> model callback called when autocomplete has been touched` */
|
---|
412 | this._onTouched = () => { };
|
---|
413 | /**
|
---|
414 | * Position of the autocomplete panel relative to the trigger element. A position of `auto`
|
---|
415 | * will render the panel underneath the trigger if there is enough space for it to fit in
|
---|
416 | * the viewport, otherwise the panel will be shown above it. If the position is set to
|
---|
417 | * `above` or `below`, the panel will always be shown above or below the trigger. no matter
|
---|
418 | * whether it fits completely in the viewport.
|
---|
419 | */
|
---|
420 | this.position = 'auto';
|
---|
421 | /**
|
---|
422 | * `autocomplete` attribute to be set on the input element.
|
---|
423 | * @docs-private
|
---|
424 | */
|
---|
425 | this.autocompleteAttribute = 'off';
|
---|
426 | this._overlayAttached = false;
|
---|
427 | /** Stream of autocomplete option selections. */
|
---|
428 | this.optionSelections = defer(() => {
|
---|
429 | if (this.autocomplete && this.autocomplete.options) {
|
---|
430 | return merge(...this.autocomplete.options.map(option => option.onSelectionChange));
|
---|
431 | }
|
---|
432 | // If there are any subscribers before `ngAfterViewInit`, the `autocomplete` will be undefined.
|
---|
433 | // Return a stream that we'll replace with the real one once everything is in place.
|
---|
434 | return this._zone.onStable
|
---|
435 | .pipe(take(1), switchMap(() => this.optionSelections));
|
---|
436 | });
|
---|
437 | this._scrollStrategy = scrollStrategy;
|
---|
438 | }
|
---|
439 | /**
|
---|
440 | * Whether the autocomplete is disabled. When disabled, the element will
|
---|
441 | * act as a regular input and the user won't be able to open the panel.
|
---|
442 | */
|
---|
443 | get autocompleteDisabled() { return this._autocompleteDisabled; }
|
---|
444 | set autocompleteDisabled(value) {
|
---|
445 | this._autocompleteDisabled = coerceBooleanProperty(value);
|
---|
446 | }
|
---|
447 | ngAfterViewInit() {
|
---|
448 | const window = this._getWindow();
|
---|
449 | if (typeof window !== 'undefined') {
|
---|
450 | this._zone.runOutsideAngular(() => window.addEventListener('blur', this._windowBlurHandler));
|
---|
451 | }
|
---|
452 | }
|
---|
453 | ngOnChanges(changes) {
|
---|
454 | if (changes['position'] && this._positionStrategy) {
|
---|
455 | this._setStrategyPositions(this._positionStrategy);
|
---|
456 | if (this.panelOpen) {
|
---|
457 | this._overlayRef.updatePosition();
|
---|
458 | }
|
---|
459 | }
|
---|
460 | }
|
---|
461 | ngOnDestroy() {
|
---|
462 | const window = this._getWindow();
|
---|
463 | if (typeof window !== 'undefined') {
|
---|
464 | window.removeEventListener('blur', this._windowBlurHandler);
|
---|
465 | }
|
---|
466 | this._viewportSubscription.unsubscribe();
|
---|
467 | this._componentDestroyed = true;
|
---|
468 | this._destroyPanel();
|
---|
469 | this._closeKeyEventStream.complete();
|
---|
470 | }
|
---|
471 | /** Whether or not the autocomplete panel is open. */
|
---|
472 | get panelOpen() {
|
---|
473 | return this._overlayAttached && this.autocomplete.showPanel;
|
---|
474 | }
|
---|
475 | /** Opens the autocomplete suggestion panel. */
|
---|
476 | openPanel() {
|
---|
477 | this._attachOverlay();
|
---|
478 | this._floatLabel();
|
---|
479 | }
|
---|
480 | /** Closes the autocomplete suggestion panel. */
|
---|
481 | closePanel() {
|
---|
482 | this._resetLabel();
|
---|
483 | if (!this._overlayAttached) {
|
---|
484 | return;
|
---|
485 | }
|
---|
486 | if (this.panelOpen) {
|
---|
487 | // Only emit if the panel was visible.
|
---|
488 | this.autocomplete.closed.emit();
|
---|
489 | }
|
---|
490 | this.autocomplete._isOpen = this._overlayAttached = false;
|
---|
491 | if (this._overlayRef && this._overlayRef.hasAttached()) {
|
---|
492 | this._overlayRef.detach();
|
---|
493 | this._closingActionsSubscription.unsubscribe();
|
---|
494 | }
|
---|
495 | // Note that in some cases this can end up being called after the component is destroyed.
|
---|
496 | // Add a check to ensure that we don't try to run change detection on a destroyed view.
|
---|
497 | if (!this._componentDestroyed) {
|
---|
498 | // We need to trigger change detection manually, because
|
---|
499 | // `fromEvent` doesn't seem to do it at the proper time.
|
---|
500 | // This ensures that the label is reset when the
|
---|
501 | // user clicks outside.
|
---|
502 | this._changeDetectorRef.detectChanges();
|
---|
503 | }
|
---|
504 | }
|
---|
505 | /**
|
---|
506 | * Updates the position of the autocomplete suggestion panel to ensure that it fits all options
|
---|
507 | * within the viewport.
|
---|
508 | */
|
---|
509 | updatePosition() {
|
---|
510 | if (this._overlayAttached) {
|
---|
511 | this._overlayRef.updatePosition();
|
---|
512 | }
|
---|
513 | }
|
---|
514 | /**
|
---|
515 | * A stream of actions that should close the autocomplete panel, including
|
---|
516 | * when an option is selected, on blur, and when TAB is pressed.
|
---|
517 | */
|
---|
518 | get panelClosingActions() {
|
---|
519 | return merge(this.optionSelections, this.autocomplete._keyManager.tabOut.pipe(filter(() => this._overlayAttached)), this._closeKeyEventStream, this._getOutsideClickStream(), this._overlayRef ?
|
---|
520 | this._overlayRef.detachments().pipe(filter(() => this._overlayAttached)) :
|
---|
521 | of()).pipe(
|
---|
522 | // Normalize the output so we return a consistent type.
|
---|
523 | map(event => event instanceof MatOptionSelectionChange ? event : null));
|
---|
524 | }
|
---|
525 | /** The currently active option, coerced to MatOption type. */
|
---|
526 | get activeOption() {
|
---|
527 | if (this.autocomplete && this.autocomplete._keyManager) {
|
---|
528 | return this.autocomplete._keyManager.activeItem;
|
---|
529 | }
|
---|
530 | return null;
|
---|
531 | }
|
---|
532 | /** Stream of clicks outside of the autocomplete panel. */
|
---|
533 | _getOutsideClickStream() {
|
---|
534 | return merge(fromEvent(this._document, 'click'), fromEvent(this._document, 'auxclick'), fromEvent(this._document, 'touchend'))
|
---|
535 | .pipe(filter(event => {
|
---|
536 | // If we're in the Shadow DOM, the event target will be the shadow root, so we have to
|
---|
537 | // fall back to check the first element in the path of the click event.
|
---|
538 | const clickTarget = _getEventTarget(event);
|
---|
539 | const formField = this._formField ? this._formField._elementRef.nativeElement : null;
|
---|
540 | const customOrigin = this.connectedTo ? this.connectedTo.elementRef.nativeElement : null;
|
---|
541 | return this._overlayAttached && clickTarget !== this._element.nativeElement &&
|
---|
542 | (!formField || !formField.contains(clickTarget)) &&
|
---|
543 | (!customOrigin || !customOrigin.contains(clickTarget)) &&
|
---|
544 | (!!this._overlayRef && !this._overlayRef.overlayElement.contains(clickTarget));
|
---|
545 | }));
|
---|
546 | }
|
---|
547 | // Implemented as part of ControlValueAccessor.
|
---|
548 | writeValue(value) {
|
---|
549 | Promise.resolve(null).then(() => this._setTriggerValue(value));
|
---|
550 | }
|
---|
551 | // Implemented as part of ControlValueAccessor.
|
---|
552 | registerOnChange(fn) {
|
---|
553 | this._onChange = fn;
|
---|
554 | }
|
---|
555 | // Implemented as part of ControlValueAccessor.
|
---|
556 | registerOnTouched(fn) {
|
---|
557 | this._onTouched = fn;
|
---|
558 | }
|
---|
559 | // Implemented as part of ControlValueAccessor.
|
---|
560 | setDisabledState(isDisabled) {
|
---|
561 | this._element.nativeElement.disabled = isDisabled;
|
---|
562 | }
|
---|
563 | _handleKeydown(event) {
|
---|
564 | const keyCode = event.keyCode;
|
---|
565 | // Prevent the default action on all escape key presses. This is here primarily to bring IE
|
---|
566 | // in line with other browsers. By default, pressing escape on IE will cause it to revert
|
---|
567 | // the input value to the one that it had on focus, however it won't dispatch any events
|
---|
568 | // which means that the model value will be out of sync with the view.
|
---|
569 | if (keyCode === ESCAPE && !hasModifierKey(event)) {
|
---|
570 | event.preventDefault();
|
---|
571 | }
|
---|
572 | if (this.activeOption && keyCode === ENTER && this.panelOpen) {
|
---|
573 | this.activeOption._selectViaInteraction();
|
---|
574 | this._resetActiveItem();
|
---|
575 | event.preventDefault();
|
---|
576 | }
|
---|
577 | else if (this.autocomplete) {
|
---|
578 | const prevActiveItem = this.autocomplete._keyManager.activeItem;
|
---|
579 | const isArrowKey = keyCode === UP_ARROW || keyCode === DOWN_ARROW;
|
---|
580 | if (this.panelOpen || keyCode === TAB) {
|
---|
581 | this.autocomplete._keyManager.onKeydown(event);
|
---|
582 | }
|
---|
583 | else if (isArrowKey && this._canOpen()) {
|
---|
584 | this.openPanel();
|
---|
585 | }
|
---|
586 | if (isArrowKey || this.autocomplete._keyManager.activeItem !== prevActiveItem) {
|
---|
587 | this._scrollToOption(this.autocomplete._keyManager.activeItemIndex || 0);
|
---|
588 | }
|
---|
589 | }
|
---|
590 | }
|
---|
591 | _handleInput(event) {
|
---|
592 | let target = event.target;
|
---|
593 | let value = target.value;
|
---|
594 | // Based on `NumberValueAccessor` from forms.
|
---|
595 | if (target.type === 'number') {
|
---|
596 | value = value == '' ? null : parseFloat(value);
|
---|
597 | }
|
---|
598 | // If the input has a placeholder, IE will fire the `input` event on page load,
|
---|
599 | // focus and blur, in addition to when the user actually changed the value. To
|
---|
600 | // filter out all of the extra events, we save the value on focus and between
|
---|
601 | // `input` events, and we check whether it changed.
|
---|
602 | // See: https://connect.microsoft.com/IE/feedback/details/885747/
|
---|
603 | if (this._previousValue !== value) {
|
---|
604 | this._previousValue = value;
|
---|
605 | this._onChange(value);
|
---|
606 | if (this._canOpen() && this._document.activeElement === event.target) {
|
---|
607 | this.openPanel();
|
---|
608 | }
|
---|
609 | }
|
---|
610 | }
|
---|
611 | _handleFocus() {
|
---|
612 | if (!this._canOpenOnNextFocus) {
|
---|
613 | this._canOpenOnNextFocus = true;
|
---|
614 | }
|
---|
615 | else if (this._canOpen()) {
|
---|
616 | this._previousValue = this._element.nativeElement.value;
|
---|
617 | this._attachOverlay();
|
---|
618 | this._floatLabel(true);
|
---|
619 | }
|
---|
620 | }
|
---|
621 | /**
|
---|
622 | * In "auto" mode, the label will animate down as soon as focus is lost.
|
---|
623 | * This causes the value to jump when selecting an option with the mouse.
|
---|
624 | * This method manually floats the label until the panel can be closed.
|
---|
625 | * @param shouldAnimate Whether the label should be animated when it is floated.
|
---|
626 | */
|
---|
627 | _floatLabel(shouldAnimate = false) {
|
---|
628 | if (this._formField && this._formField.floatLabel === 'auto') {
|
---|
629 | if (shouldAnimate) {
|
---|
630 | this._formField._animateAndLockLabel();
|
---|
631 | }
|
---|
632 | else {
|
---|
633 | this._formField.floatLabel = 'always';
|
---|
634 | }
|
---|
635 | this._manuallyFloatingLabel = true;
|
---|
636 | }
|
---|
637 | }
|
---|
638 | /** If the label has been manually elevated, return it to its normal state. */
|
---|
639 | _resetLabel() {
|
---|
640 | if (this._manuallyFloatingLabel) {
|
---|
641 | this._formField.floatLabel = 'auto';
|
---|
642 | this._manuallyFloatingLabel = false;
|
---|
643 | }
|
---|
644 | }
|
---|
645 | /**
|
---|
646 | * This method listens to a stream of panel closing actions and resets the
|
---|
647 | * stream every time the option list changes.
|
---|
648 | */
|
---|
649 | _subscribeToClosingActions() {
|
---|
650 | const firstStable = this._zone.onStable.pipe(take(1));
|
---|
651 | const optionChanges = this.autocomplete.options.changes.pipe(tap(() => this._positionStrategy.reapplyLastPosition()),
|
---|
652 | // Defer emitting to the stream until the next tick, because changing
|
---|
653 | // bindings in here will cause "changed after checked" errors.
|
---|
654 | delay(0));
|
---|
655 | // When the zone is stable initially, and when the option list changes...
|
---|
656 | return merge(firstStable, optionChanges)
|
---|
657 | .pipe(
|
---|
658 | // create a new stream of panelClosingActions, replacing any previous streams
|
---|
659 | // that were created, and flatten it so our stream only emits closing events...
|
---|
660 | switchMap(() => {
|
---|
661 | const wasOpen = this.panelOpen;
|
---|
662 | this._resetActiveItem();
|
---|
663 | this.autocomplete._setVisibility();
|
---|
664 | if (this.panelOpen) {
|
---|
665 | this._overlayRef.updatePosition();
|
---|
666 | // If the `panelOpen` state changed, we need to make sure to emit the `opened`
|
---|
667 | // event, because we may not have emitted it when the panel was attached. This
|
---|
668 | // can happen if the users opens the panel and there are no options, but the
|
---|
669 | // options come in slightly later or as a result of the value changing.
|
---|
670 | if (wasOpen !== this.panelOpen) {
|
---|
671 | this.autocomplete.opened.emit();
|
---|
672 | }
|
---|
673 | }
|
---|
674 | return this.panelClosingActions;
|
---|
675 | }),
|
---|
676 | // when the first closing event occurs...
|
---|
677 | take(1))
|
---|
678 | // set the value, close the panel, and complete.
|
---|
679 | .subscribe(event => this._setValueAndClose(event));
|
---|
680 | }
|
---|
681 | /** Destroys the autocomplete suggestion panel. */
|
---|
682 | _destroyPanel() {
|
---|
683 | if (this._overlayRef) {
|
---|
684 | this.closePanel();
|
---|
685 | this._overlayRef.dispose();
|
---|
686 | this._overlayRef = null;
|
---|
687 | }
|
---|
688 | }
|
---|
689 | _setTriggerValue(value) {
|
---|
690 | const toDisplay = this.autocomplete && this.autocomplete.displayWith ?
|
---|
691 | this.autocomplete.displayWith(value) :
|
---|
692 | value;
|
---|
693 | // Simply falling back to an empty string if the display value is falsy does not work properly.
|
---|
694 | // The display value can also be the number zero and shouldn't fall back to an empty string.
|
---|
695 | const inputValue = toDisplay != null ? toDisplay : '';
|
---|
696 | // If it's used within a `MatFormField`, we should set it through the property so it can go
|
---|
697 | // through change detection.
|
---|
698 | if (this._formField) {
|
---|
699 | this._formField._control.value = inputValue;
|
---|
700 | }
|
---|
701 | else {
|
---|
702 | this._element.nativeElement.value = inputValue;
|
---|
703 | }
|
---|
704 | this._previousValue = inputValue;
|
---|
705 | }
|
---|
706 | /**
|
---|
707 | * This method closes the panel, and if a value is specified, also sets the associated
|
---|
708 | * control to that value. It will also mark the control as dirty if this interaction
|
---|
709 | * stemmed from the user.
|
---|
710 | */
|
---|
711 | _setValueAndClose(event) {
|
---|
712 | if (event && event.source) {
|
---|
713 | this._clearPreviousSelectedOption(event.source);
|
---|
714 | this._setTriggerValue(event.source.value);
|
---|
715 | this._onChange(event.source.value);
|
---|
716 | this._element.nativeElement.focus();
|
---|
717 | this.autocomplete._emitSelectEvent(event.source);
|
---|
718 | }
|
---|
719 | this.closePanel();
|
---|
720 | }
|
---|
721 | /**
|
---|
722 | * Clear any previous selected option and emit a selection change event for this option
|
---|
723 | */
|
---|
724 | _clearPreviousSelectedOption(skip) {
|
---|
725 | this.autocomplete.options.forEach(option => {
|
---|
726 | if (option !== skip && option.selected) {
|
---|
727 | option.deselect();
|
---|
728 | }
|
---|
729 | });
|
---|
730 | }
|
---|
731 | _attachOverlay() {
|
---|
732 | var _a;
|
---|
733 | if (!this.autocomplete && (typeof ngDevMode === 'undefined' || ngDevMode)) {
|
---|
734 | throw getMatAutocompleteMissingPanelError();
|
---|
735 | }
|
---|
736 | let overlayRef = this._overlayRef;
|
---|
737 | if (!overlayRef) {
|
---|
738 | this._portal = new TemplatePortal(this.autocomplete.template, this._viewContainerRef, { id: (_a = this._formField) === null || _a === void 0 ? void 0 : _a.getLabelId() });
|
---|
739 | overlayRef = this._overlay.create(this._getOverlayConfig());
|
---|
740 | this._overlayRef = overlayRef;
|
---|
741 | // Use the `keydownEvents` in order to take advantage of
|
---|
742 | // the overlay event targeting provided by the CDK overlay.
|
---|
743 | overlayRef.keydownEvents().subscribe(event => {
|
---|
744 | // Close when pressing ESCAPE or ALT + UP_ARROW, based on the a11y guidelines.
|
---|
745 | // See: https://www.w3.org/TR/wai-aria-practices-1.1/#textbox-keyboard-interaction
|
---|
746 | if ((event.keyCode === ESCAPE && !hasModifierKey(event)) ||
|
---|
747 | (event.keyCode === UP_ARROW && hasModifierKey(event, 'altKey'))) {
|
---|
748 | this._closeKeyEventStream.next();
|
---|
749 | this._resetActiveItem();
|
---|
750 | // We need to stop propagation, otherwise the event will eventually
|
---|
751 | // reach the input itself and cause the overlay to be reopened.
|
---|
752 | event.stopPropagation();
|
---|
753 | event.preventDefault();
|
---|
754 | }
|
---|
755 | });
|
---|
756 | this._viewportSubscription = this._viewportRuler.change().subscribe(() => {
|
---|
757 | if (this.panelOpen && overlayRef) {
|
---|
758 | overlayRef.updateSize({ width: this._getPanelWidth() });
|
---|
759 | }
|
---|
760 | });
|
---|
761 | }
|
---|
762 | else {
|
---|
763 | // Update the trigger, panel width and direction, in case anything has changed.
|
---|
764 | this._positionStrategy.setOrigin(this._getConnectedElement());
|
---|
765 | overlayRef.updateSize({ width: this._getPanelWidth() });
|
---|
766 | }
|
---|
767 | if (overlayRef && !overlayRef.hasAttached()) {
|
---|
768 | overlayRef.attach(this._portal);
|
---|
769 | this._closingActionsSubscription = this._subscribeToClosingActions();
|
---|
770 | }
|
---|
771 | const wasOpen = this.panelOpen;
|
---|
772 | this.autocomplete._setVisibility();
|
---|
773 | this.autocomplete._isOpen = this._overlayAttached = true;
|
---|
774 | // We need to do an extra `panelOpen` check in here, because the
|
---|
775 | // autocomplete won't be shown if there are no options.
|
---|
776 | if (this.panelOpen && wasOpen !== this.panelOpen) {
|
---|
777 | this.autocomplete.opened.emit();
|
---|
778 | }
|
---|
779 | }
|
---|
780 | _getOverlayConfig() {
|
---|
781 | var _a;
|
---|
782 | return new OverlayConfig({
|
---|
783 | positionStrategy: this._getOverlayPosition(),
|
---|
784 | scrollStrategy: this._scrollStrategy(),
|
---|
785 | width: this._getPanelWidth(),
|
---|
786 | direction: this._dir,
|
---|
787 | panelClass: (_a = this._defaults) === null || _a === void 0 ? void 0 : _a.overlayPanelClass,
|
---|
788 | });
|
---|
789 | }
|
---|
790 | _getOverlayPosition() {
|
---|
791 | const strategy = this._overlay.position()
|
---|
792 | .flexibleConnectedTo(this._getConnectedElement())
|
---|
793 | .withFlexibleDimensions(false)
|
---|
794 | .withPush(false);
|
---|
795 | this._setStrategyPositions(strategy);
|
---|
796 | this._positionStrategy = strategy;
|
---|
797 | return strategy;
|
---|
798 | }
|
---|
799 | /** Sets the positions on a position strategy based on the directive's input state. */
|
---|
800 | _setStrategyPositions(positionStrategy) {
|
---|
801 | // Note that we provide horizontal fallback positions, even though by default the dropdown
|
---|
802 | // width matches the input, because consumers can override the width. See #18854.
|
---|
803 | const belowPositions = [
|
---|
804 | { originX: 'start', originY: 'bottom', overlayX: 'start', overlayY: 'top' },
|
---|
805 | { originX: 'end', originY: 'bottom', overlayX: 'end', overlayY: 'top' }
|
---|
806 | ];
|
---|
807 | // The overlay edge connected to the trigger should have squared corners, while
|
---|
808 | // the opposite end has rounded corners. We apply a CSS class to swap the
|
---|
809 | // border-radius based on the overlay position.
|
---|
810 | const panelClass = this._aboveClass;
|
---|
811 | const abovePositions = [
|
---|
812 | { originX: 'start', originY: 'top', overlayX: 'start', overlayY: 'bottom', panelClass },
|
---|
813 | { originX: 'end', originY: 'top', overlayX: 'end', overlayY: 'bottom', panelClass }
|
---|
814 | ];
|
---|
815 | let positions;
|
---|
816 | if (this.position === 'above') {
|
---|
817 | positions = abovePositions;
|
---|
818 | }
|
---|
819 | else if (this.position === 'below') {
|
---|
820 | positions = belowPositions;
|
---|
821 | }
|
---|
822 | else {
|
---|
823 | positions = [...belowPositions, ...abovePositions];
|
---|
824 | }
|
---|
825 | positionStrategy.withPositions(positions);
|
---|
826 | }
|
---|
827 | _getConnectedElement() {
|
---|
828 | if (this.connectedTo) {
|
---|
829 | return this.connectedTo.elementRef;
|
---|
830 | }
|
---|
831 | return this._formField ? this._formField.getConnectedOverlayOrigin() : this._element;
|
---|
832 | }
|
---|
833 | _getPanelWidth() {
|
---|
834 | return this.autocomplete.panelWidth || this._getHostWidth();
|
---|
835 | }
|
---|
836 | /** Returns the width of the input element, so the panel width can match it. */
|
---|
837 | _getHostWidth() {
|
---|
838 | return this._getConnectedElement().nativeElement.getBoundingClientRect().width;
|
---|
839 | }
|
---|
840 | /**
|
---|
841 | * Resets the active item to -1 so arrow events will activate the
|
---|
842 | * correct options, or to 0 if the consumer opted into it.
|
---|
843 | */
|
---|
844 | _resetActiveItem() {
|
---|
845 | const autocomplete = this.autocomplete;
|
---|
846 | if (autocomplete.autoActiveFirstOption) {
|
---|
847 | // Note that we go through `setFirstItemActive`, rather than `setActiveItem(0)`, because
|
---|
848 | // the former will find the next enabled option, if the first one is disabled.
|
---|
849 | autocomplete._keyManager.setFirstItemActive();
|
---|
850 | }
|
---|
851 | else {
|
---|
852 | autocomplete._keyManager.setActiveItem(-1);
|
---|
853 | }
|
---|
854 | }
|
---|
855 | /** Determines whether the panel can be opened. */
|
---|
856 | _canOpen() {
|
---|
857 | const element = this._element.nativeElement;
|
---|
858 | return !element.readOnly && !element.disabled && !this._autocompleteDisabled;
|
---|
859 | }
|
---|
860 | /** Use defaultView of injected document if available or fallback to global window reference */
|
---|
861 | _getWindow() {
|
---|
862 | var _a;
|
---|
863 | return ((_a = this._document) === null || _a === void 0 ? void 0 : _a.defaultView) || window;
|
---|
864 | }
|
---|
865 | /** Scrolls to a particular option in the list. */
|
---|
866 | _scrollToOption(index) {
|
---|
867 | // Given that we are not actually focusing active options, we must manually adjust scroll
|
---|
868 | // to reveal options below the fold. First, we find the offset of the option from the top
|
---|
869 | // of the panel. If that offset is below the fold, the new scrollTop will be the offset -
|
---|
870 | // the panel height + the option height, so the active option will be just visible at the
|
---|
871 | // bottom of the panel. If that offset is above the top of the visible panel, the new scrollTop
|
---|
872 | // will become the offset. If that offset is visible within the panel already, the scrollTop is
|
---|
873 | // not adjusted.
|
---|
874 | const autocomplete = this.autocomplete;
|
---|
875 | const labelCount = _countGroupLabelsBeforeOption(index, autocomplete.options, autocomplete.optionGroups);
|
---|
876 | if (index === 0 && labelCount === 1) {
|
---|
877 | // If we've got one group label before the option and we're at the top option,
|
---|
878 | // scroll the list to the top. This is better UX than scrolling the list to the
|
---|
879 | // top of the option, because it allows the user to read the top group's label.
|
---|
880 | autocomplete._setScrollTop(0);
|
---|
881 | }
|
---|
882 | else if (autocomplete.panel) {
|
---|
883 | const option = autocomplete.options.toArray()[index];
|
---|
884 | if (option) {
|
---|
885 | const element = option._getHostElement();
|
---|
886 | const newScrollPosition = _getOptionScrollPosition(element.offsetTop, element.offsetHeight, autocomplete._getScrollTop(), autocomplete.panel.nativeElement.offsetHeight);
|
---|
887 | autocomplete._setScrollTop(newScrollPosition);
|
---|
888 | }
|
---|
889 | }
|
---|
890 | }
|
---|
891 | }
|
---|
892 | _MatAutocompleteTriggerBase.ɵfac = function _MatAutocompleteTriggerBase_Factory(t) { return new (t || _MatAutocompleteTriggerBase)(ɵngcc0.ɵɵdirectiveInject(ɵngcc0.ElementRef), ɵngcc0.ɵɵdirectiveInject(ɵngcc3.Overlay), ɵngcc0.ɵɵdirectiveInject(ɵngcc0.ViewContainerRef), ɵngcc0.ɵɵdirectiveInject(ɵngcc0.NgZone), ɵngcc0.ɵɵdirectiveInject(ɵngcc0.ChangeDetectorRef), ɵngcc0.ɵɵdirectiveInject(MAT_AUTOCOMPLETE_SCROLL_STRATEGY), ɵngcc0.ɵɵdirectiveInject(ɵngcc4.Directionality, 8), ɵngcc0.ɵɵdirectiveInject(MAT_FORM_FIELD, 9), ɵngcc0.ɵɵdirectiveInject(DOCUMENT, 8), ɵngcc0.ɵɵdirectiveInject(ɵngcc5.ViewportRuler), ɵngcc0.ɵɵdirectiveInject(MAT_AUTOCOMPLETE_DEFAULT_OPTIONS, 8)); };
|
---|
893 | _MatAutocompleteTriggerBase.ɵdir = /*@__PURE__*/ ɵngcc0.ɵɵdefineDirective({ type: _MatAutocompleteTriggerBase, inputs: { position: ["matAutocompletePosition", "position"], autocompleteAttribute: ["autocomplete", "autocompleteAttribute"], autocompleteDisabled: ["matAutocompleteDisabled", "autocompleteDisabled"], autocomplete: ["matAutocomplete", "autocomplete"], connectedTo: ["matAutocompleteConnectedTo", "connectedTo"] }, features: [ɵngcc0.ɵɵNgOnChangesFeature] });
|
---|
894 | _MatAutocompleteTriggerBase.ctorParameters = () => [
|
---|
895 | { type: ElementRef },
|
---|
896 | { type: Overlay },
|
---|
897 | { type: ViewContainerRef },
|
---|
898 | { type: NgZone },
|
---|
899 | { type: ChangeDetectorRef },
|
---|
900 | { type: undefined, decorators: [{ type: Inject, args: [MAT_AUTOCOMPLETE_SCROLL_STRATEGY,] }] },
|
---|
901 | { type: Directionality, decorators: [{ type: Optional }] },
|
---|
902 | { type: MatFormField, decorators: [{ type: Optional }, { type: Inject, args: [MAT_FORM_FIELD,] }, { type: Host }] },
|
---|
903 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [DOCUMENT,] }] },
|
---|
904 | { type: ViewportRuler },
|
---|
905 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [MAT_AUTOCOMPLETE_DEFAULT_OPTIONS,] }] }
|
---|
906 | ];
|
---|
907 | _MatAutocompleteTriggerBase.propDecorators = {
|
---|
908 | autocomplete: [{ type: Input, args: ['matAutocomplete',] }],
|
---|
909 | position: [{ type: Input, args: ['matAutocompletePosition',] }],
|
---|
910 | connectedTo: [{ type: Input, args: ['matAutocompleteConnectedTo',] }],
|
---|
911 | autocompleteAttribute: [{ type: Input, args: ['autocomplete',] }],
|
---|
912 | autocompleteDisabled: [{ type: Input, args: ['matAutocompleteDisabled',] }]
|
---|
913 | };
|
---|
914 | (function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(_MatAutocompleteTriggerBase, [{
|
---|
915 | type: Directive
|
---|
916 | }], function () { return [{ type: ɵngcc0.ElementRef }, { type: ɵngcc3.Overlay }, { type: ɵngcc0.ViewContainerRef }, { type: ɵngcc0.NgZone }, { type: ɵngcc0.ChangeDetectorRef }, { type: undefined, decorators: [{
|
---|
917 | type: Inject,
|
---|
918 | args: [MAT_AUTOCOMPLETE_SCROLL_STRATEGY]
|
---|
919 | }] }, { type: ɵngcc4.Directionality, decorators: [{
|
---|
920 | type: Optional
|
---|
921 | }] }, { type: ɵngcc6.MatFormField, decorators: [{
|
---|
922 | type: Optional
|
---|
923 | }, {
|
---|
924 | type: Inject,
|
---|
925 | args: [MAT_FORM_FIELD]
|
---|
926 | }, {
|
---|
927 | type: Host
|
---|
928 | }] }, { type: undefined, decorators: [{
|
---|
929 | type: Optional
|
---|
930 | }, {
|
---|
931 | type: Inject,
|
---|
932 | args: [DOCUMENT]
|
---|
933 | }] }, { type: ɵngcc5.ViewportRuler }, { type: undefined, decorators: [{
|
---|
934 | type: Optional
|
---|
935 | }, {
|
---|
936 | type: Inject,
|
---|
937 | args: [MAT_AUTOCOMPLETE_DEFAULT_OPTIONS]
|
---|
938 | }] }]; }, { position: [{
|
---|
939 | type: Input,
|
---|
940 | args: ['matAutocompletePosition']
|
---|
941 | }], autocompleteAttribute: [{
|
---|
942 | type: Input,
|
---|
943 | args: ['autocomplete']
|
---|
944 | }], autocompleteDisabled: [{
|
---|
945 | type: Input,
|
---|
946 | args: ['matAutocompleteDisabled']
|
---|
947 | }], autocomplete: [{
|
---|
948 | type: Input,
|
---|
949 | args: ['matAutocomplete']
|
---|
950 | }], connectedTo: [{
|
---|
951 | type: Input,
|
---|
952 | args: ['matAutocompleteConnectedTo']
|
---|
953 | }] }); })();
|
---|
954 | class MatAutocompleteTrigger extends _MatAutocompleteTriggerBase {
|
---|
955 | constructor() {
|
---|
956 | super(...arguments);
|
---|
957 | this._aboveClass = 'mat-autocomplete-panel-above';
|
---|
958 | }
|
---|
959 | }
|
---|
960 | MatAutocompleteTrigger.ɵfac = /*@__PURE__*/ function () { let ɵMatAutocompleteTrigger_BaseFactory; return function MatAutocompleteTrigger_Factory(t) { return (ɵMatAutocompleteTrigger_BaseFactory || (ɵMatAutocompleteTrigger_BaseFactory = ɵngcc0.ɵɵgetInheritedFactory(MatAutocompleteTrigger)))(t || MatAutocompleteTrigger); }; }();
|
---|
961 | MatAutocompleteTrigger.ɵdir = /*@__PURE__*/ ɵngcc0.ɵɵdefineDirective({ type: MatAutocompleteTrigger, selectors: [["input", "matAutocomplete", ""], ["textarea", "matAutocomplete", ""]], hostAttrs: [1, "mat-autocomplete-trigger"], hostVars: 7, hostBindings: function MatAutocompleteTrigger_HostBindings(rf, ctx) { if (rf & 1) {
|
---|
962 | ɵngcc0.ɵɵlistener("focusin", function MatAutocompleteTrigger_focusin_HostBindingHandler() { return ctx._handleFocus(); })("blur", function MatAutocompleteTrigger_blur_HostBindingHandler() { return ctx._onTouched(); })("input", function MatAutocompleteTrigger_input_HostBindingHandler($event) { return ctx._handleInput($event); })("keydown", function MatAutocompleteTrigger_keydown_HostBindingHandler($event) { return ctx._handleKeydown($event); });
|
---|
963 | } if (rf & 2) {
|
---|
964 | ɵngcc0.ɵɵattribute("autocomplete", ctx.autocompleteAttribute)("role", ctx.autocompleteDisabled ? null : "combobox")("aria-autocomplete", ctx.autocompleteDisabled ? null : "list")("aria-activedescendant", ctx.panelOpen && ctx.activeOption ? ctx.activeOption.id : null)("aria-expanded", ctx.autocompleteDisabled ? null : ctx.panelOpen.toString())("aria-owns", ctx.autocompleteDisabled || !ctx.panelOpen ? null : ctx.autocomplete == null ? null : ctx.autocomplete.id)("aria-haspopup", !ctx.autocompleteDisabled);
|
---|
965 | } }, exportAs: ["matAutocompleteTrigger"], features: [ɵngcc0.ɵɵProvidersFeature([MAT_AUTOCOMPLETE_VALUE_ACCESSOR]), ɵngcc0.ɵɵInheritDefinitionFeature] });
|
---|
966 | (function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(MatAutocompleteTrigger, [{
|
---|
967 | type: Directive,
|
---|
968 | args: [{
|
---|
969 | selector: `input[matAutocomplete], textarea[matAutocomplete]`,
|
---|
970 | host: {
|
---|
971 | 'class': 'mat-autocomplete-trigger',
|
---|
972 | '[attr.autocomplete]': 'autocompleteAttribute',
|
---|
973 | '[attr.role]': 'autocompleteDisabled ? null : "combobox"',
|
---|
974 | '[attr.aria-autocomplete]': 'autocompleteDisabled ? null : "list"',
|
---|
975 | '[attr.aria-activedescendant]': '(panelOpen && activeOption) ? activeOption.id : null',
|
---|
976 | '[attr.aria-expanded]': 'autocompleteDisabled ? null : panelOpen.toString()',
|
---|
977 | '[attr.aria-owns]': '(autocompleteDisabled || !panelOpen) ? null : autocomplete?.id',
|
---|
978 | '[attr.aria-haspopup]': '!autocompleteDisabled',
|
---|
979 | // Note: we use `focusin`, as opposed to `focus`, in order to open the panel
|
---|
980 | // a little earlier. This avoids issues where IE delays the focusing of the input.
|
---|
981 | '(focusin)': '_handleFocus()',
|
---|
982 | '(blur)': '_onTouched()',
|
---|
983 | '(input)': '_handleInput($event)',
|
---|
984 | '(keydown)': '_handleKeydown($event)'
|
---|
985 | },
|
---|
986 | exportAs: 'matAutocompleteTrigger',
|
---|
987 | providers: [MAT_AUTOCOMPLETE_VALUE_ACCESSOR]
|
---|
988 | }]
|
---|
989 | }], null, null); })();
|
---|
990 |
|
---|
991 | /**
|
---|
992 | * @license
|
---|
993 | * Copyright Google LLC All Rights Reserved.
|
---|
994 | *
|
---|
995 | * Use of this source code is governed by an MIT-style license that can be
|
---|
996 | * found in the LICENSE file at https://angular.io/license
|
---|
997 | */
|
---|
998 | class MatAutocompleteModule {
|
---|
999 | }
|
---|
1000 | MatAutocompleteModule.ɵfac = function MatAutocompleteModule_Factory(t) { return new (t || MatAutocompleteModule)(); };
|
---|
1001 | MatAutocompleteModule.ɵmod = /*@__PURE__*/ ɵngcc0.ɵɵdefineNgModule({ type: MatAutocompleteModule });
|
---|
1002 | MatAutocompleteModule.ɵinj = /*@__PURE__*/ ɵngcc0.ɵɵdefineInjector({ providers: [MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY_PROVIDER], imports: [[
|
---|
1003 | OverlayModule,
|
---|
1004 | MatOptionModule,
|
---|
1005 | MatCommonModule,
|
---|
1006 | CommonModule
|
---|
1007 | ], CdkScrollableModule,
|
---|
1008 | MatOptionModule,
|
---|
1009 | MatCommonModule] });
|
---|
1010 | (function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵngcc0.ɵsetClassMetadata(MatAutocompleteModule, [{
|
---|
1011 | type: NgModule,
|
---|
1012 | args: [{
|
---|
1013 | imports: [
|
---|
1014 | OverlayModule,
|
---|
1015 | MatOptionModule,
|
---|
1016 | MatCommonModule,
|
---|
1017 | CommonModule
|
---|
1018 | ],
|
---|
1019 | exports: [
|
---|
1020 | MatAutocomplete,
|
---|
1021 | MatAutocompleteTrigger,
|
---|
1022 | MatAutocompleteOrigin,
|
---|
1023 | CdkScrollableModule,
|
---|
1024 | MatOptionModule,
|
---|
1025 | MatCommonModule,
|
---|
1026 | ],
|
---|
1027 | declarations: [MatAutocomplete, MatAutocompleteTrigger, MatAutocompleteOrigin],
|
---|
1028 | providers: [MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY_PROVIDER]
|
---|
1029 | }]
|
---|
1030 | }], null, null); })();
|
---|
1031 | (function () { (typeof ngJitMode === "undefined" || ngJitMode) && ɵngcc0.ɵɵsetNgModuleScope(MatAutocompleteModule, { declarations: function () { return [MatAutocomplete, MatAutocompleteTrigger, MatAutocompleteOrigin]; }, imports: function () { return [OverlayModule,
|
---|
1032 | MatOptionModule,
|
---|
1033 | MatCommonModule,
|
---|
1034 | CommonModule]; }, exports: function () { return [MatAutocomplete, MatAutocompleteTrigger, MatAutocompleteOrigin, CdkScrollableModule,
|
---|
1035 | MatOptionModule,
|
---|
1036 | MatCommonModule]; } }); })();
|
---|
1037 |
|
---|
1038 | /**
|
---|
1039 | * @license
|
---|
1040 | * Copyright Google LLC All Rights Reserved.
|
---|
1041 | *
|
---|
1042 | * Use of this source code is governed by an MIT-style license that can be
|
---|
1043 | * found in the LICENSE file at https://angular.io/license
|
---|
1044 | */
|
---|
1045 |
|
---|
1046 | /**
|
---|
1047 | * Generated bundle index. Do not edit.
|
---|
1048 | */
|
---|
1049 |
|
---|
1050 | export { MAT_AUTOCOMPLETE_DEFAULT_OPTIONS, MAT_AUTOCOMPLETE_DEFAULT_OPTIONS_FACTORY, MAT_AUTOCOMPLETE_SCROLL_STRATEGY, MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY, MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY_PROVIDER, MAT_AUTOCOMPLETE_VALUE_ACCESSOR, MatAutocomplete, MatAutocompleteModule, MatAutocompleteOrigin, MatAutocompleteSelectedEvent, MatAutocompleteTrigger, _MatAutocompleteBase, _MatAutocompleteOriginBase, _MatAutocompleteTriggerBase, getMatAutocompleteMissingPanelError };
|
---|
1051 |
|
---|
1052 | //# sourceMappingURL=autocomplete.js.map |
---|