autocomplete.es5.js 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. /**
  2. * @license
  3. * Copyright Google LLC All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. import { __extends } from 'tslib';
  9. import { ActiveDescendantKeyManager } from '@angular/cdk/a11y';
  10. import { coerceBooleanProperty } from '@angular/cdk/coercion';
  11. import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChildren, ElementRef, EventEmitter, Inject, InjectionToken, Input, Output, TemplateRef, ViewChild, ViewEncapsulation, Directive, forwardRef, Host, NgZone, Optional, ViewContainerRef, NgModule } from '@angular/core';
  12. import { MAT_OPTION_PARENT_COMPONENT, MatOptgroup, MatOption, mixinDisableRipple, _countGroupLabelsBeforeOption, _getOptionScrollPosition, MatOptionSelectionChange, MatOptionModule, MatCommonModule } from '@angular/material/core';
  13. import { Directionality } from '@angular/cdk/bidi';
  14. import { DOWN_ARROW, ENTER, ESCAPE, TAB, UP_ARROW } from '@angular/cdk/keycodes';
  15. import { Overlay, OverlayConfig, OverlayModule } from '@angular/cdk/overlay';
  16. import { TemplatePortal } from '@angular/cdk/portal';
  17. import { DOCUMENT, CommonModule } from '@angular/common';
  18. import { filter, take, switchMap, delay, tap, map } from 'rxjs/operators';
  19. import { ViewportRuler } from '@angular/cdk/scrolling';
  20. import { NG_VALUE_ACCESSOR } from '@angular/forms';
  21. import { MatFormField } from '@angular/material/form-field';
  22. import { Subscription, defer, fromEvent, merge, of, Subject } from 'rxjs';
  23. /**
  24. * @fileoverview added by tsickle
  25. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  26. */
  27. /**
  28. * Autocomplete IDs need to be unique across components, so this counter exists outside of
  29. * the component definition.
  30. * @type {?}
  31. */
  32. var _uniqueAutocompleteIdCounter = 0;
  33. /**
  34. * Event object that is emitted when an autocomplete option is selected.
  35. */
  36. var /**
  37. * Event object that is emitted when an autocomplete option is selected.
  38. */
  39. MatAutocompleteSelectedEvent = /** @class */ (function () {
  40. function MatAutocompleteSelectedEvent(source, option) {
  41. this.source = source;
  42. this.option = option;
  43. }
  44. return MatAutocompleteSelectedEvent;
  45. }());
  46. // Boilerplate for applying mixins to MatAutocomplete.
  47. /**
  48. * \@docs-private
  49. */
  50. var
  51. // Boilerplate for applying mixins to MatAutocomplete.
  52. /**
  53. * \@docs-private
  54. */
  55. MatAutocompleteBase = /** @class */ (function () {
  56. function MatAutocompleteBase() {
  57. }
  58. return MatAutocompleteBase;
  59. }());
  60. /** @type {?} */
  61. var _MatAutocompleteMixinBase = mixinDisableRipple(MatAutocompleteBase);
  62. /**
  63. * Injection token to be used to override the default options for `mat-autocomplete`.
  64. * @type {?}
  65. */
  66. var MAT_AUTOCOMPLETE_DEFAULT_OPTIONS = new InjectionToken('mat-autocomplete-default-options', {
  67. providedIn: 'root',
  68. factory: MAT_AUTOCOMPLETE_DEFAULT_OPTIONS_FACTORY,
  69. });
  70. /**
  71. * \@docs-private
  72. * @return {?}
  73. */
  74. function MAT_AUTOCOMPLETE_DEFAULT_OPTIONS_FACTORY() {
  75. return { autoActiveFirstOption: false };
  76. }
  77. var MatAutocomplete = /** @class */ (function (_super) {
  78. __extends(MatAutocomplete, _super);
  79. function MatAutocomplete(_changeDetectorRef, _elementRef, defaults) {
  80. var _this = _super.call(this) || this;
  81. _this._changeDetectorRef = _changeDetectorRef;
  82. _this._elementRef = _elementRef;
  83. /**
  84. * Whether the autocomplete panel should be visible, depending on option length.
  85. */
  86. _this.showPanel = false;
  87. _this._isOpen = false;
  88. /**
  89. * Function that maps an option's control value to its display value in the trigger.
  90. */
  91. _this.displayWith = null;
  92. /**
  93. * Event that is emitted whenever an option from the list is selected.
  94. */
  95. _this.optionSelected = new EventEmitter();
  96. /**
  97. * Event that is emitted when the autocomplete panel is opened.
  98. */
  99. _this.opened = new EventEmitter();
  100. /**
  101. * Event that is emitted when the autocomplete panel is closed.
  102. */
  103. _this.closed = new EventEmitter();
  104. _this._classList = {};
  105. /**
  106. * Unique ID to be used by autocomplete trigger's "aria-owns" property.
  107. */
  108. _this.id = "mat-autocomplete-" + _uniqueAutocompleteIdCounter++;
  109. _this._autoActiveFirstOption = !!defaults.autoActiveFirstOption;
  110. return _this;
  111. }
  112. Object.defineProperty(MatAutocomplete.prototype, "isOpen", {
  113. /** Whether the autocomplete panel is open. */
  114. get: /**
  115. * Whether the autocomplete panel is open.
  116. * @return {?}
  117. */
  118. function () { return this._isOpen && this.showPanel; },
  119. enumerable: true,
  120. configurable: true
  121. });
  122. Object.defineProperty(MatAutocomplete.prototype, "autoActiveFirstOption", {
  123. /**
  124. * Whether the first option should be highlighted when the autocomplete panel is opened.
  125. * Can be configured globally through the `MAT_AUTOCOMPLETE_DEFAULT_OPTIONS` token.
  126. */
  127. get: /**
  128. * Whether the first option should be highlighted when the autocomplete panel is opened.
  129. * Can be configured globally through the `MAT_AUTOCOMPLETE_DEFAULT_OPTIONS` token.
  130. * @return {?}
  131. */
  132. function () { return this._autoActiveFirstOption; },
  133. set: /**
  134. * @param {?} value
  135. * @return {?}
  136. */
  137. function (value) {
  138. this._autoActiveFirstOption = coerceBooleanProperty(value);
  139. },
  140. enumerable: true,
  141. configurable: true
  142. });
  143. Object.defineProperty(MatAutocomplete.prototype, "classList", {
  144. /**
  145. * Takes classes set on the host mat-autocomplete element and applies them to the panel
  146. * inside the overlay container to allow for easy styling.
  147. */
  148. set: /**
  149. * Takes classes set on the host mat-autocomplete element and applies them to the panel
  150. * inside the overlay container to allow for easy styling.
  151. * @param {?} value
  152. * @return {?}
  153. */
  154. function (value) {
  155. if (value && value.length) {
  156. this._classList = value.split(' ').reduce((/**
  157. * @param {?} classList
  158. * @param {?} className
  159. * @return {?}
  160. */
  161. function (classList, className) {
  162. classList[className.trim()] = true;
  163. return classList;
  164. }), (/** @type {?} */ ({})));
  165. }
  166. else {
  167. this._classList = {};
  168. }
  169. this._setVisibilityClasses(this._classList);
  170. this._elementRef.nativeElement.className = '';
  171. },
  172. enumerable: true,
  173. configurable: true
  174. });
  175. /**
  176. * @return {?}
  177. */
  178. MatAutocomplete.prototype.ngAfterContentInit = /**
  179. * @return {?}
  180. */
  181. function () {
  182. this._keyManager = new ActiveDescendantKeyManager(this.options).withWrap();
  183. // Set the initial visibility state.
  184. this._setVisibility();
  185. };
  186. /**
  187. * Sets the panel scrollTop. This allows us to manually scroll to display options
  188. * above or below the fold, as they are not actually being focused when active.
  189. */
  190. /**
  191. * Sets the panel scrollTop. This allows us to manually scroll to display options
  192. * above or below the fold, as they are not actually being focused when active.
  193. * @param {?} scrollTop
  194. * @return {?}
  195. */
  196. MatAutocomplete.prototype._setScrollTop = /**
  197. * Sets the panel scrollTop. This allows us to manually scroll to display options
  198. * above or below the fold, as they are not actually being focused when active.
  199. * @param {?} scrollTop
  200. * @return {?}
  201. */
  202. function (scrollTop) {
  203. if (this.panel) {
  204. this.panel.nativeElement.scrollTop = scrollTop;
  205. }
  206. };
  207. /** Returns the panel's scrollTop. */
  208. /**
  209. * Returns the panel's scrollTop.
  210. * @return {?}
  211. */
  212. MatAutocomplete.prototype._getScrollTop = /**
  213. * Returns the panel's scrollTop.
  214. * @return {?}
  215. */
  216. function () {
  217. return this.panel ? this.panel.nativeElement.scrollTop : 0;
  218. };
  219. /** Panel should hide itself when the option list is empty. */
  220. /**
  221. * Panel should hide itself when the option list is empty.
  222. * @return {?}
  223. */
  224. MatAutocomplete.prototype._setVisibility = /**
  225. * Panel should hide itself when the option list is empty.
  226. * @return {?}
  227. */
  228. function () {
  229. this.showPanel = !!this.options.length;
  230. this._setVisibilityClasses(this._classList);
  231. this._changeDetectorRef.markForCheck();
  232. };
  233. /** Emits the `select` event. */
  234. /**
  235. * Emits the `select` event.
  236. * @param {?} option
  237. * @return {?}
  238. */
  239. MatAutocomplete.prototype._emitSelectEvent = /**
  240. * Emits the `select` event.
  241. * @param {?} option
  242. * @return {?}
  243. */
  244. function (option) {
  245. /** @type {?} */
  246. var event = new MatAutocompleteSelectedEvent(this, option);
  247. this.optionSelected.emit(event);
  248. };
  249. /** Sets the autocomplete visibility classes on a classlist based on the panel is visible. */
  250. /**
  251. * Sets the autocomplete visibility classes on a classlist based on the panel is visible.
  252. * @private
  253. * @param {?} classList
  254. * @return {?}
  255. */
  256. MatAutocomplete.prototype._setVisibilityClasses = /**
  257. * Sets the autocomplete visibility classes on a classlist based on the panel is visible.
  258. * @private
  259. * @param {?} classList
  260. * @return {?}
  261. */
  262. function (classList) {
  263. classList['mat-autocomplete-visible'] = this.showPanel;
  264. classList['mat-autocomplete-hidden'] = !this.showPanel;
  265. };
  266. MatAutocomplete.decorators = [
  267. { type: Component, args: [{selector: 'mat-autocomplete',
  268. template: "<ng-template><div class=\"mat-autocomplete-panel\" role=\"listbox\" [id]=\"id\" [ngClass]=\"_classList\" #panel><ng-content></ng-content></div></ng-template>",
  269. 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}@media (-ms-high-contrast:active){.mat-autocomplete-panel{outline:solid 1px}}"],
  270. encapsulation: ViewEncapsulation.None,
  271. changeDetection: ChangeDetectionStrategy.OnPush,
  272. exportAs: 'matAutocomplete',
  273. inputs: ['disableRipple'],
  274. host: {
  275. 'class': 'mat-autocomplete'
  276. },
  277. providers: [
  278. { provide: MAT_OPTION_PARENT_COMPONENT, useExisting: MatAutocomplete }
  279. ]
  280. },] },
  281. ];
  282. /** @nocollapse */
  283. MatAutocomplete.ctorParameters = function () { return [
  284. { type: ChangeDetectorRef },
  285. { type: ElementRef },
  286. { type: undefined, decorators: [{ type: Inject, args: [MAT_AUTOCOMPLETE_DEFAULT_OPTIONS,] }] }
  287. ]; };
  288. MatAutocomplete.propDecorators = {
  289. template: [{ type: ViewChild, args: [TemplateRef, { static: true },] }],
  290. panel: [{ type: ViewChild, args: ['panel', { static: false },] }],
  291. options: [{ type: ContentChildren, args: [MatOption, { descendants: true },] }],
  292. optionGroups: [{ type: ContentChildren, args: [MatOptgroup,] }],
  293. displayWith: [{ type: Input }],
  294. autoActiveFirstOption: [{ type: Input }],
  295. panelWidth: [{ type: Input }],
  296. optionSelected: [{ type: Output }],
  297. opened: [{ type: Output }],
  298. closed: [{ type: Output }],
  299. classList: [{ type: Input, args: ['class',] }]
  300. };
  301. return MatAutocomplete;
  302. }(_MatAutocompleteMixinBase));
  303. /**
  304. * @fileoverview added by tsickle
  305. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  306. */
  307. /**
  308. * Directive applied to an element to make it usable
  309. * as a connection point for an autocomplete panel.
  310. */
  311. var MatAutocompleteOrigin = /** @class */ (function () {
  312. function MatAutocompleteOrigin(elementRef) {
  313. this.elementRef = elementRef;
  314. }
  315. MatAutocompleteOrigin.decorators = [
  316. { type: Directive, args: [{
  317. selector: '[matAutocompleteOrigin]',
  318. exportAs: 'matAutocompleteOrigin',
  319. },] },
  320. ];
  321. /** @nocollapse */
  322. MatAutocompleteOrigin.ctorParameters = function () { return [
  323. { type: ElementRef }
  324. ]; };
  325. return MatAutocompleteOrigin;
  326. }());
  327. /**
  328. * @fileoverview added by tsickle
  329. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  330. */
  331. /**
  332. * The height of each autocomplete option.
  333. * @type {?}
  334. */
  335. var AUTOCOMPLETE_OPTION_HEIGHT = 48;
  336. /**
  337. * The total height of the autocomplete panel.
  338. * @type {?}
  339. */
  340. var AUTOCOMPLETE_PANEL_HEIGHT = 256;
  341. /**
  342. * Injection token that determines the scroll handling while the autocomplete panel is open.
  343. * @type {?}
  344. */
  345. var MAT_AUTOCOMPLETE_SCROLL_STRATEGY = new InjectionToken('mat-autocomplete-scroll-strategy');
  346. /**
  347. * \@docs-private
  348. * @param {?} overlay
  349. * @return {?}
  350. */
  351. function MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY(overlay) {
  352. return (/**
  353. * @return {?}
  354. */
  355. function () { return overlay.scrollStrategies.reposition(); });
  356. }
  357. /**
  358. * \@docs-private
  359. * @type {?}
  360. */
  361. var MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY_PROVIDER = {
  362. provide: MAT_AUTOCOMPLETE_SCROLL_STRATEGY,
  363. deps: [Overlay],
  364. useFactory: MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY,
  365. };
  366. /**
  367. * Provider that allows the autocomplete to register as a ControlValueAccessor.
  368. * \@docs-private
  369. * @type {?}
  370. */
  371. var MAT_AUTOCOMPLETE_VALUE_ACCESSOR = {
  372. provide: NG_VALUE_ACCESSOR,
  373. useExisting: forwardRef((/**
  374. * @return {?}
  375. */
  376. function () { return MatAutocompleteTrigger; })),
  377. multi: true
  378. };
  379. /**
  380. * Creates an error to be thrown when attempting to use an autocomplete trigger without a panel.
  381. * \@docs-private
  382. * @return {?}
  383. */
  384. function getMatAutocompleteMissingPanelError() {
  385. return Error('Attempting to open an undefined instance of `mat-autocomplete`. ' +
  386. 'Make sure that the id passed to the `matAutocomplete` is correct and that ' +
  387. 'you\'re attempting to open it after the ngAfterContentInit hook.');
  388. }
  389. var MatAutocompleteTrigger = /** @class */ (function () {
  390. function MatAutocompleteTrigger(_element, _overlay, _viewContainerRef, _zone, _changeDetectorRef, scrollStrategy, _dir, _formField, _document, _viewportRuler) {
  391. var _this = this;
  392. this._element = _element;
  393. this._overlay = _overlay;
  394. this._viewContainerRef = _viewContainerRef;
  395. this._zone = _zone;
  396. this._changeDetectorRef = _changeDetectorRef;
  397. this._dir = _dir;
  398. this._formField = _formField;
  399. this._document = _document;
  400. this._viewportRuler = _viewportRuler;
  401. this._componentDestroyed = false;
  402. this._autocompleteDisabled = false;
  403. /**
  404. * Whether or not the label state is being overridden.
  405. */
  406. this._manuallyFloatingLabel = false;
  407. /**
  408. * Subscription to viewport size changes.
  409. */
  410. this._viewportSubscription = Subscription.EMPTY;
  411. /**
  412. * Whether the autocomplete can open the next time it is focused. Used to prevent a focused,
  413. * closed autocomplete from being reopened if the user switches to another browser tab and then
  414. * comes back.
  415. */
  416. this._canOpenOnNextFocus = true;
  417. /**
  418. * Stream of keyboard events that can close the panel.
  419. */
  420. this._closeKeyEventStream = new Subject();
  421. /**
  422. * Event handler for when the window is blurred. Needs to be an
  423. * arrow function in order to preserve the context.
  424. */
  425. this._windowBlurHandler = (/**
  426. * @return {?}
  427. */
  428. function () {
  429. // If the user blurred the window while the autocomplete is focused, it means that it'll be
  430. // refocused when they come back. In this case we want to skip the first focus event, if the
  431. // pane was closed, in order to avoid reopening it unintentionally.
  432. _this._canOpenOnNextFocus =
  433. _this._document.activeElement !== _this._element.nativeElement || _this.panelOpen;
  434. });
  435. /**
  436. * `View -> model callback called when value changes`
  437. */
  438. this._onChange = (/**
  439. * @return {?}
  440. */
  441. function () { });
  442. /**
  443. * `View -> model callback called when autocomplete has been touched`
  444. */
  445. this._onTouched = (/**
  446. * @return {?}
  447. */
  448. function () { });
  449. /**
  450. * Position of the autocomplete panel relative to the trigger element. A position of `auto`
  451. * will render the panel underneath the trigger if there is enough space for it to fit in
  452. * the viewport, otherwise the panel will be shown above it. If the position is set to
  453. * `above` or `below`, the panel will always be shown above or below the trigger. no matter
  454. * whether it fits completely in the viewport.
  455. */
  456. this.position = 'auto';
  457. /**
  458. * `autocomplete` attribute to be set on the input element.
  459. * \@docs-private
  460. */
  461. this.autocompleteAttribute = 'off';
  462. this._overlayAttached = false;
  463. /**
  464. * Stream of autocomplete option selections.
  465. */
  466. this.optionSelections = (/** @type {?} */ (defer((/**
  467. * @return {?}
  468. */
  469. function () {
  470. if (_this.autocomplete && _this.autocomplete.options) {
  471. return merge.apply(void 0, _this.autocomplete.options.map((/**
  472. * @param {?} option
  473. * @return {?}
  474. */
  475. function (option) { return option.onSelectionChange; })));
  476. }
  477. // If there are any subscribers before `ngAfterViewInit`, the `autocomplete` will be undefined.
  478. // Return a stream that we'll replace with the real one once everything is in place.
  479. return _this._zone.onStable
  480. .asObservable()
  481. .pipe(take(1), switchMap((/**
  482. * @return {?}
  483. */
  484. function () { return _this.optionSelections; })));
  485. }))));
  486. if (typeof window !== 'undefined') {
  487. _zone.runOutsideAngular((/**
  488. * @return {?}
  489. */
  490. function () {
  491. window.addEventListener('blur', _this._windowBlurHandler);
  492. }));
  493. }
  494. this._scrollStrategy = scrollStrategy;
  495. }
  496. Object.defineProperty(MatAutocompleteTrigger.prototype, "autocompleteDisabled", {
  497. /**
  498. * Whether the autocomplete is disabled. When disabled, the element will
  499. * act as a regular input and the user won't be able to open the panel.
  500. */
  501. get: /**
  502. * Whether the autocomplete is disabled. When disabled, the element will
  503. * act as a regular input and the user won't be able to open the panel.
  504. * @return {?}
  505. */
  506. function () { return this._autocompleteDisabled; },
  507. set: /**
  508. * @param {?} value
  509. * @return {?}
  510. */
  511. function (value) {
  512. this._autocompleteDisabled = coerceBooleanProperty(value);
  513. },
  514. enumerable: true,
  515. configurable: true
  516. });
  517. /**
  518. * @param {?} changes
  519. * @return {?}
  520. */
  521. MatAutocompleteTrigger.prototype.ngOnChanges = /**
  522. * @param {?} changes
  523. * @return {?}
  524. */
  525. function (changes) {
  526. if (changes['position'] && this._positionStrategy) {
  527. this._setStrategyPositions(this._positionStrategy);
  528. if (this.panelOpen) {
  529. (/** @type {?} */ (this._overlayRef)).updatePosition();
  530. }
  531. }
  532. };
  533. /**
  534. * @return {?}
  535. */
  536. MatAutocompleteTrigger.prototype.ngOnDestroy = /**
  537. * @return {?}
  538. */
  539. function () {
  540. if (typeof window !== 'undefined') {
  541. window.removeEventListener('blur', this._windowBlurHandler);
  542. }
  543. this._viewportSubscription.unsubscribe();
  544. this._componentDestroyed = true;
  545. this._destroyPanel();
  546. this._closeKeyEventStream.complete();
  547. };
  548. Object.defineProperty(MatAutocompleteTrigger.prototype, "panelOpen", {
  549. /** Whether or not the autocomplete panel is open. */
  550. get: /**
  551. * Whether or not the autocomplete panel is open.
  552. * @return {?}
  553. */
  554. function () {
  555. return this._overlayAttached && this.autocomplete.showPanel;
  556. },
  557. enumerable: true,
  558. configurable: true
  559. });
  560. /** Opens the autocomplete suggestion panel. */
  561. /**
  562. * Opens the autocomplete suggestion panel.
  563. * @return {?}
  564. */
  565. MatAutocompleteTrigger.prototype.openPanel = /**
  566. * Opens the autocomplete suggestion panel.
  567. * @return {?}
  568. */
  569. function () {
  570. this._attachOverlay();
  571. this._floatLabel();
  572. };
  573. /** Closes the autocomplete suggestion panel. */
  574. /**
  575. * Closes the autocomplete suggestion panel.
  576. * @return {?}
  577. */
  578. MatAutocompleteTrigger.prototype.closePanel = /**
  579. * Closes the autocomplete suggestion panel.
  580. * @return {?}
  581. */
  582. function () {
  583. this._resetLabel();
  584. if (!this._overlayAttached) {
  585. return;
  586. }
  587. if (this.panelOpen) {
  588. // Only emit if the panel was visible.
  589. this.autocomplete.closed.emit();
  590. }
  591. this.autocomplete._isOpen = this._overlayAttached = false;
  592. if (this._overlayRef && this._overlayRef.hasAttached()) {
  593. this._overlayRef.detach();
  594. this._closingActionsSubscription.unsubscribe();
  595. }
  596. // Note that in some cases this can end up being called after the component is destroyed.
  597. // Add a check to ensure that we don't try to run change detection on a destroyed view.
  598. if (!this._componentDestroyed) {
  599. // We need to trigger change detection manually, because
  600. // `fromEvent` doesn't seem to do it at the proper time.
  601. // This ensures that the label is reset when the
  602. // user clicks outside.
  603. this._changeDetectorRef.detectChanges();
  604. }
  605. };
  606. /**
  607. * Updates the position of the autocomplete suggestion panel to ensure that it fits all options
  608. * within the viewport.
  609. */
  610. /**
  611. * Updates the position of the autocomplete suggestion panel to ensure that it fits all options
  612. * within the viewport.
  613. * @return {?}
  614. */
  615. MatAutocompleteTrigger.prototype.updatePosition = /**
  616. * Updates the position of the autocomplete suggestion panel to ensure that it fits all options
  617. * within the viewport.
  618. * @return {?}
  619. */
  620. function () {
  621. if (this._overlayAttached) {
  622. (/** @type {?} */ (this._overlayRef)).updatePosition();
  623. }
  624. };
  625. Object.defineProperty(MatAutocompleteTrigger.prototype, "panelClosingActions", {
  626. /**
  627. * A stream of actions that should close the autocomplete panel, including
  628. * when an option is selected, on blur, and when TAB is pressed.
  629. */
  630. get: /**
  631. * A stream of actions that should close the autocomplete panel, including
  632. * when an option is selected, on blur, and when TAB is pressed.
  633. * @return {?}
  634. */
  635. function () {
  636. var _this = this;
  637. return merge(this.optionSelections, this.autocomplete._keyManager.tabOut.pipe(filter((/**
  638. * @return {?}
  639. */
  640. function () { return _this._overlayAttached; }))), this._closeKeyEventStream, this._getOutsideClickStream(), this._overlayRef ?
  641. this._overlayRef.detachments().pipe(filter((/**
  642. * @return {?}
  643. */
  644. function () { return _this._overlayAttached; }))) :
  645. of()).pipe(
  646. // Normalize the output so we return a consistent type.
  647. map((/**
  648. * @param {?} event
  649. * @return {?}
  650. */
  651. function (event) { return event instanceof MatOptionSelectionChange ? event : null; })));
  652. },
  653. enumerable: true,
  654. configurable: true
  655. });
  656. Object.defineProperty(MatAutocompleteTrigger.prototype, "activeOption", {
  657. /** The currently active option, coerced to MatOption type. */
  658. get: /**
  659. * The currently active option, coerced to MatOption type.
  660. * @return {?}
  661. */
  662. function () {
  663. if (this.autocomplete && this.autocomplete._keyManager) {
  664. return this.autocomplete._keyManager.activeItem;
  665. }
  666. return null;
  667. },
  668. enumerable: true,
  669. configurable: true
  670. });
  671. /** Stream of clicks outside of the autocomplete panel. */
  672. /**
  673. * Stream of clicks outside of the autocomplete panel.
  674. * @private
  675. * @return {?}
  676. */
  677. MatAutocompleteTrigger.prototype._getOutsideClickStream = /**
  678. * Stream of clicks outside of the autocomplete panel.
  679. * @private
  680. * @return {?}
  681. */
  682. function () {
  683. var _this = this;
  684. return merge((/** @type {?} */ (fromEvent(this._document, 'click'))), (/** @type {?} */ (fromEvent(this._document, 'touchend'))))
  685. .pipe(filter((/**
  686. * @param {?} event
  687. * @return {?}
  688. */
  689. function (event) {
  690. /** @type {?} */
  691. var clickTarget = (/** @type {?} */ (event.target));
  692. /** @type {?} */
  693. var formField = _this._formField ?
  694. _this._formField._elementRef.nativeElement : null;
  695. return _this._overlayAttached &&
  696. clickTarget !== _this._element.nativeElement &&
  697. (!formField || !formField.contains(clickTarget)) &&
  698. (!!_this._overlayRef && !_this._overlayRef.overlayElement.contains(clickTarget));
  699. })));
  700. };
  701. // Implemented as part of ControlValueAccessor.
  702. // Implemented as part of ControlValueAccessor.
  703. /**
  704. * @param {?} value
  705. * @return {?}
  706. */
  707. MatAutocompleteTrigger.prototype.writeValue =
  708. // Implemented as part of ControlValueAccessor.
  709. /**
  710. * @param {?} value
  711. * @return {?}
  712. */
  713. function (value) {
  714. var _this = this;
  715. Promise.resolve(null).then((/**
  716. * @return {?}
  717. */
  718. function () { return _this._setTriggerValue(value); }));
  719. };
  720. // Implemented as part of ControlValueAccessor.
  721. // Implemented as part of ControlValueAccessor.
  722. /**
  723. * @param {?} fn
  724. * @return {?}
  725. */
  726. MatAutocompleteTrigger.prototype.registerOnChange =
  727. // Implemented as part of ControlValueAccessor.
  728. /**
  729. * @param {?} fn
  730. * @return {?}
  731. */
  732. function (fn) {
  733. this._onChange = fn;
  734. };
  735. // Implemented as part of ControlValueAccessor.
  736. // Implemented as part of ControlValueAccessor.
  737. /**
  738. * @param {?} fn
  739. * @return {?}
  740. */
  741. MatAutocompleteTrigger.prototype.registerOnTouched =
  742. // Implemented as part of ControlValueAccessor.
  743. /**
  744. * @param {?} fn
  745. * @return {?}
  746. */
  747. function (fn) {
  748. this._onTouched = fn;
  749. };
  750. // Implemented as part of ControlValueAccessor.
  751. // Implemented as part of ControlValueAccessor.
  752. /**
  753. * @param {?} isDisabled
  754. * @return {?}
  755. */
  756. MatAutocompleteTrigger.prototype.setDisabledState =
  757. // Implemented as part of ControlValueAccessor.
  758. /**
  759. * @param {?} isDisabled
  760. * @return {?}
  761. */
  762. function (isDisabled) {
  763. this._element.nativeElement.disabled = isDisabled;
  764. };
  765. /**
  766. * @param {?} event
  767. * @return {?}
  768. */
  769. MatAutocompleteTrigger.prototype._handleKeydown = /**
  770. * @param {?} event
  771. * @return {?}
  772. */
  773. function (event) {
  774. /** @type {?} */
  775. var keyCode = event.keyCode;
  776. // Prevent the default action on all escape key presses. This is here primarily to bring IE
  777. // in line with other browsers. By default, pressing escape on IE will cause it to revert
  778. // the input value to the one that it had on focus, however it won't dispatch any events
  779. // which means that the model value will be out of sync with the view.
  780. if (keyCode === ESCAPE) {
  781. event.preventDefault();
  782. }
  783. if (this.activeOption && keyCode === ENTER && this.panelOpen) {
  784. this.activeOption._selectViaInteraction();
  785. this._resetActiveItem();
  786. event.preventDefault();
  787. }
  788. else if (this.autocomplete) {
  789. /** @type {?} */
  790. var prevActiveItem = this.autocomplete._keyManager.activeItem;
  791. /** @type {?} */
  792. var isArrowKey = keyCode === UP_ARROW || keyCode === DOWN_ARROW;
  793. if (this.panelOpen || keyCode === TAB) {
  794. this.autocomplete._keyManager.onKeydown(event);
  795. }
  796. else if (isArrowKey && this._canOpen()) {
  797. this.openPanel();
  798. }
  799. if (isArrowKey || this.autocomplete._keyManager.activeItem !== prevActiveItem) {
  800. this._scrollToOption();
  801. }
  802. }
  803. };
  804. /**
  805. * @param {?} event
  806. * @return {?}
  807. */
  808. MatAutocompleteTrigger.prototype._handleInput = /**
  809. * @param {?} event
  810. * @return {?}
  811. */
  812. function (event) {
  813. /** @type {?} */
  814. var target = (/** @type {?} */ (event.target));
  815. /** @type {?} */
  816. var value = target.value;
  817. // Based on `NumberValueAccessor` from forms.
  818. if (target.type === 'number') {
  819. value = value == '' ? null : parseFloat(value);
  820. }
  821. // If the input has a placeholder, IE will fire the `input` event on page load,
  822. // focus and blur, in addition to when the user actually changed the value. To
  823. // filter out all of the extra events, we save the value on focus and between
  824. // `input` events, and we check whether it changed.
  825. // See: https://connect.microsoft.com/IE/feedback/details/885747/
  826. if (this._previousValue !== value) {
  827. this._previousValue = value;
  828. this._onChange(value);
  829. if (this._canOpen() && this._document.activeElement === event.target) {
  830. this.openPanel();
  831. }
  832. }
  833. };
  834. /**
  835. * @return {?}
  836. */
  837. MatAutocompleteTrigger.prototype._handleFocus = /**
  838. * @return {?}
  839. */
  840. function () {
  841. if (!this._canOpenOnNextFocus) {
  842. this._canOpenOnNextFocus = true;
  843. }
  844. else if (this._canOpen()) {
  845. this._previousValue = this._element.nativeElement.value;
  846. this._attachOverlay();
  847. this._floatLabel(true);
  848. }
  849. };
  850. /**
  851. * In "auto" mode, the label will animate down as soon as focus is lost.
  852. * This causes the value to jump when selecting an option with the mouse.
  853. * This method manually floats the label until the panel can be closed.
  854. * @param shouldAnimate Whether the label should be animated when it is floated.
  855. */
  856. /**
  857. * In "auto" mode, the label will animate down as soon as focus is lost.
  858. * This causes the value to jump when selecting an option with the mouse.
  859. * This method manually floats the label until the panel can be closed.
  860. * @private
  861. * @param {?=} shouldAnimate Whether the label should be animated when it is floated.
  862. * @return {?}
  863. */
  864. MatAutocompleteTrigger.prototype._floatLabel = /**
  865. * In "auto" mode, the label will animate down as soon as focus is lost.
  866. * This causes the value to jump when selecting an option with the mouse.
  867. * This method manually floats the label until the panel can be closed.
  868. * @private
  869. * @param {?=} shouldAnimate Whether the label should be animated when it is floated.
  870. * @return {?}
  871. */
  872. function (shouldAnimate) {
  873. if (shouldAnimate === void 0) { shouldAnimate = false; }
  874. if (this._formField && this._formField.floatLabel === 'auto') {
  875. if (shouldAnimate) {
  876. this._formField._animateAndLockLabel();
  877. }
  878. else {
  879. this._formField.floatLabel = 'always';
  880. }
  881. this._manuallyFloatingLabel = true;
  882. }
  883. };
  884. /** If the label has been manually elevated, return it to its normal state. */
  885. /**
  886. * If the label has been manually elevated, return it to its normal state.
  887. * @private
  888. * @return {?}
  889. */
  890. MatAutocompleteTrigger.prototype._resetLabel = /**
  891. * If the label has been manually elevated, return it to its normal state.
  892. * @private
  893. * @return {?}
  894. */
  895. function () {
  896. if (this._manuallyFloatingLabel) {
  897. this._formField.floatLabel = 'auto';
  898. this._manuallyFloatingLabel = false;
  899. }
  900. };
  901. /**
  902. * Given that we are not actually focusing active options, we must manually adjust scroll
  903. * to reveal options below the fold. First, we find the offset of the option from the top
  904. * of the panel. If that offset is below the fold, the new scrollTop will be the offset -
  905. * the panel height + the option height, so the active option will be just visible at the
  906. * bottom of the panel. If that offset is above the top of the visible panel, the new scrollTop
  907. * will become the offset. If that offset is visible within the panel already, the scrollTop is
  908. * not adjusted.
  909. */
  910. /**
  911. * Given that we are not actually focusing active options, we must manually adjust scroll
  912. * to reveal options below the fold. First, we find the offset of the option from the top
  913. * of the panel. If that offset is below the fold, the new scrollTop will be the offset -
  914. * the panel height + the option height, so the active option will be just visible at the
  915. * bottom of the panel. If that offset is above the top of the visible panel, the new scrollTop
  916. * will become the offset. If that offset is visible within the panel already, the scrollTop is
  917. * not adjusted.
  918. * @private
  919. * @return {?}
  920. */
  921. MatAutocompleteTrigger.prototype._scrollToOption = /**
  922. * Given that we are not actually focusing active options, we must manually adjust scroll
  923. * to reveal options below the fold. First, we find the offset of the option from the top
  924. * of the panel. If that offset is below the fold, the new scrollTop will be the offset -
  925. * the panel height + the option height, so the active option will be just visible at the
  926. * bottom of the panel. If that offset is above the top of the visible panel, the new scrollTop
  927. * will become the offset. If that offset is visible within the panel already, the scrollTop is
  928. * not adjusted.
  929. * @private
  930. * @return {?}
  931. */
  932. function () {
  933. /** @type {?} */
  934. var index = this.autocomplete._keyManager.activeItemIndex || 0;
  935. /** @type {?} */
  936. var labelCount = _countGroupLabelsBeforeOption(index, this.autocomplete.options, this.autocomplete.optionGroups);
  937. /** @type {?} */
  938. var newScrollPosition = _getOptionScrollPosition(index + labelCount, AUTOCOMPLETE_OPTION_HEIGHT, this.autocomplete._getScrollTop(), AUTOCOMPLETE_PANEL_HEIGHT);
  939. this.autocomplete._setScrollTop(newScrollPosition);
  940. };
  941. /**
  942. * This method listens to a stream of panel closing actions and resets the
  943. * stream every time the option list changes.
  944. */
  945. /**
  946. * This method listens to a stream of panel closing actions and resets the
  947. * stream every time the option list changes.
  948. * @private
  949. * @return {?}
  950. */
  951. MatAutocompleteTrigger.prototype._subscribeToClosingActions = /**
  952. * This method listens to a stream of panel closing actions and resets the
  953. * stream every time the option list changes.
  954. * @private
  955. * @return {?}
  956. */
  957. function () {
  958. var _this = this;
  959. /** @type {?} */
  960. var firstStable = this._zone.onStable.asObservable().pipe(take(1));
  961. /** @type {?} */
  962. var optionChanges = this.autocomplete.options.changes.pipe(tap((/**
  963. * @return {?}
  964. */
  965. function () { return _this._positionStrategy.reapplyLastPosition(); })),
  966. // Defer emitting to the stream until the next tick, because changing
  967. // bindings in here will cause "changed after checked" errors.
  968. delay(0));
  969. // When the zone is stable initially, and when the option list changes...
  970. return merge(firstStable, optionChanges)
  971. .pipe(
  972. // create a new stream of panelClosingActions, replacing any previous streams
  973. // that were created, and flatten it so our stream only emits closing events...
  974. switchMap((/**
  975. * @return {?}
  976. */
  977. function () {
  978. /** @type {?} */
  979. var wasOpen = _this.panelOpen;
  980. _this._resetActiveItem();
  981. _this.autocomplete._setVisibility();
  982. if (_this.panelOpen) {
  983. (/** @type {?} */ (_this._overlayRef)).updatePosition();
  984. // If the `panelOpen` state changed, we need to make sure to emit the `opened`
  985. // event, because we may not have emitted it when the panel was attached. This
  986. // can happen if the users opens the panel and there are no options, but the
  987. // options come in slightly later or as a result of the value changing.
  988. if (wasOpen !== _this.panelOpen) {
  989. _this.autocomplete.opened.emit();
  990. }
  991. }
  992. return _this.panelClosingActions;
  993. })),
  994. // when the first closing event occurs...
  995. take(1))
  996. // set the value, close the panel, and complete.
  997. .subscribe((/**
  998. * @param {?} event
  999. * @return {?}
  1000. */
  1001. function (event) { return _this._setValueAndClose(event); }));
  1002. };
  1003. /** Destroys the autocomplete suggestion panel. */
  1004. /**
  1005. * Destroys the autocomplete suggestion panel.
  1006. * @private
  1007. * @return {?}
  1008. */
  1009. MatAutocompleteTrigger.prototype._destroyPanel = /**
  1010. * Destroys the autocomplete suggestion panel.
  1011. * @private
  1012. * @return {?}
  1013. */
  1014. function () {
  1015. if (this._overlayRef) {
  1016. this.closePanel();
  1017. this._overlayRef.dispose();
  1018. this._overlayRef = null;
  1019. }
  1020. };
  1021. /**
  1022. * @private
  1023. * @param {?} value
  1024. * @return {?}
  1025. */
  1026. MatAutocompleteTrigger.prototype._setTriggerValue = /**
  1027. * @private
  1028. * @param {?} value
  1029. * @return {?}
  1030. */
  1031. function (value) {
  1032. /** @type {?} */
  1033. var toDisplay = this.autocomplete && this.autocomplete.displayWith ?
  1034. this.autocomplete.displayWith(value) :
  1035. value;
  1036. // Simply falling back to an empty string if the display value is falsy does not work properly.
  1037. // The display value can also be the number zero and shouldn't fall back to an empty string.
  1038. /** @type {?} */
  1039. var inputValue = toDisplay != null ? toDisplay : '';
  1040. // If it's used within a `MatFormField`, we should set it through the property so it can go
  1041. // through change detection.
  1042. if (this._formField) {
  1043. this._formField._control.value = inputValue;
  1044. }
  1045. else {
  1046. this._element.nativeElement.value = inputValue;
  1047. }
  1048. this._previousValue = inputValue;
  1049. };
  1050. /**
  1051. * This method closes the panel, and if a value is specified, also sets the associated
  1052. * control to that value. It will also mark the control as dirty if this interaction
  1053. * stemmed from the user.
  1054. */
  1055. /**
  1056. * This method closes the panel, and if a value is specified, also sets the associated
  1057. * control to that value. It will also mark the control as dirty if this interaction
  1058. * stemmed from the user.
  1059. * @private
  1060. * @param {?} event
  1061. * @return {?}
  1062. */
  1063. MatAutocompleteTrigger.prototype._setValueAndClose = /**
  1064. * This method closes the panel, and if a value is specified, also sets the associated
  1065. * control to that value. It will also mark the control as dirty if this interaction
  1066. * stemmed from the user.
  1067. * @private
  1068. * @param {?} event
  1069. * @return {?}
  1070. */
  1071. function (event) {
  1072. if (event && event.source) {
  1073. this._clearPreviousSelectedOption(event.source);
  1074. this._setTriggerValue(event.source.value);
  1075. this._onChange(event.source.value);
  1076. this._element.nativeElement.focus();
  1077. this.autocomplete._emitSelectEvent(event.source);
  1078. }
  1079. this.closePanel();
  1080. };
  1081. /**
  1082. * Clear any previous selected option and emit a selection change event for this option
  1083. */
  1084. /**
  1085. * Clear any previous selected option and emit a selection change event for this option
  1086. * @private
  1087. * @param {?} skip
  1088. * @return {?}
  1089. */
  1090. MatAutocompleteTrigger.prototype._clearPreviousSelectedOption = /**
  1091. * Clear any previous selected option and emit a selection change event for this option
  1092. * @private
  1093. * @param {?} skip
  1094. * @return {?}
  1095. */
  1096. function (skip) {
  1097. this.autocomplete.options.forEach((/**
  1098. * @param {?} option
  1099. * @return {?}
  1100. */
  1101. function (option) {
  1102. if (option != skip && option.selected) {
  1103. option.deselect();
  1104. }
  1105. }));
  1106. };
  1107. /**
  1108. * @private
  1109. * @return {?}
  1110. */
  1111. MatAutocompleteTrigger.prototype._attachOverlay = /**
  1112. * @private
  1113. * @return {?}
  1114. */
  1115. function () {
  1116. var _this = this;
  1117. if (!this.autocomplete) {
  1118. throw getMatAutocompleteMissingPanelError();
  1119. }
  1120. /** @type {?} */
  1121. var overlayRef = this._overlayRef;
  1122. if (!overlayRef) {
  1123. this._portal = new TemplatePortal(this.autocomplete.template, this._viewContainerRef);
  1124. overlayRef = this._overlay.create(this._getOverlayConfig());
  1125. this._overlayRef = overlayRef;
  1126. // Use the `keydownEvents` in order to take advantage of
  1127. // the overlay event targeting provided by the CDK overlay.
  1128. overlayRef.keydownEvents().subscribe((/**
  1129. * @param {?} event
  1130. * @return {?}
  1131. */
  1132. function (event) {
  1133. // Close when pressing ESCAPE or ALT + UP_ARROW, based on the a11y guidelines.
  1134. // See: https://www.w3.org/TR/wai-aria-practices-1.1/#textbox-keyboard-interaction
  1135. if (event.keyCode === ESCAPE || (event.keyCode === UP_ARROW && event.altKey)) {
  1136. _this._resetActiveItem();
  1137. _this._closeKeyEventStream.next();
  1138. // We need to stop propagation, otherwise the event will eventually
  1139. // reach the input itself and cause the overlay to be reopened.
  1140. event.stopPropagation();
  1141. event.preventDefault();
  1142. }
  1143. }));
  1144. if (this._viewportRuler) {
  1145. this._viewportSubscription = this._viewportRuler.change().subscribe((/**
  1146. * @return {?}
  1147. */
  1148. function () {
  1149. if (_this.panelOpen && overlayRef) {
  1150. overlayRef.updateSize({ width: _this._getPanelWidth() });
  1151. }
  1152. }));
  1153. }
  1154. }
  1155. else {
  1156. // Update the trigger, panel width and direction, in case anything has changed.
  1157. this._positionStrategy.setOrigin(this._getConnectedElement());
  1158. overlayRef.updateSize({ width: this._getPanelWidth() });
  1159. }
  1160. if (overlayRef && !overlayRef.hasAttached()) {
  1161. overlayRef.attach(this._portal);
  1162. this._closingActionsSubscription = this._subscribeToClosingActions();
  1163. }
  1164. /** @type {?} */
  1165. var wasOpen = this.panelOpen;
  1166. this.autocomplete._setVisibility();
  1167. this.autocomplete._isOpen = this._overlayAttached = true;
  1168. // We need to do an extra `panelOpen` check in here, because the
  1169. // autocomplete won't be shown if there are no options.
  1170. if (this.panelOpen && wasOpen !== this.panelOpen) {
  1171. this.autocomplete.opened.emit();
  1172. }
  1173. };
  1174. /**
  1175. * @private
  1176. * @return {?}
  1177. */
  1178. MatAutocompleteTrigger.prototype._getOverlayConfig = /**
  1179. * @private
  1180. * @return {?}
  1181. */
  1182. function () {
  1183. return new OverlayConfig({
  1184. positionStrategy: this._getOverlayPosition(),
  1185. scrollStrategy: this._scrollStrategy(),
  1186. width: this._getPanelWidth(),
  1187. direction: this._dir
  1188. });
  1189. };
  1190. /**
  1191. * @private
  1192. * @return {?}
  1193. */
  1194. MatAutocompleteTrigger.prototype._getOverlayPosition = /**
  1195. * @private
  1196. * @return {?}
  1197. */
  1198. function () {
  1199. /** @type {?} */
  1200. var strategy = this._overlay.position()
  1201. .flexibleConnectedTo(this._getConnectedElement())
  1202. .withFlexibleDimensions(false)
  1203. .withPush(false);
  1204. this._setStrategyPositions(strategy);
  1205. this._positionStrategy = strategy;
  1206. return strategy;
  1207. };
  1208. /** Sets the positions on a position strategy based on the directive's input state. */
  1209. /**
  1210. * Sets the positions on a position strategy based on the directive's input state.
  1211. * @private
  1212. * @param {?} positionStrategy
  1213. * @return {?}
  1214. */
  1215. MatAutocompleteTrigger.prototype._setStrategyPositions = /**
  1216. * Sets the positions on a position strategy based on the directive's input state.
  1217. * @private
  1218. * @param {?} positionStrategy
  1219. * @return {?}
  1220. */
  1221. function (positionStrategy) {
  1222. /** @type {?} */
  1223. var belowPosition = {
  1224. originX: 'start',
  1225. originY: 'bottom',
  1226. overlayX: 'start',
  1227. overlayY: 'top'
  1228. };
  1229. /** @type {?} */
  1230. var abovePosition = {
  1231. originX: 'start',
  1232. originY: 'top',
  1233. overlayX: 'start',
  1234. overlayY: 'bottom',
  1235. // The overlay edge connected to the trigger should have squared corners, while
  1236. // the opposite end has rounded corners. We apply a CSS class to swap the
  1237. // border-radius based on the overlay position.
  1238. panelClass: 'mat-autocomplete-panel-above'
  1239. };
  1240. /** @type {?} */
  1241. var positions;
  1242. if (this.position === 'above') {
  1243. positions = [abovePosition];
  1244. }
  1245. else if (this.position === 'below') {
  1246. positions = [belowPosition];
  1247. }
  1248. else {
  1249. positions = [belowPosition, abovePosition];
  1250. }
  1251. positionStrategy.withPositions(positions);
  1252. };
  1253. /**
  1254. * @private
  1255. * @return {?}
  1256. */
  1257. MatAutocompleteTrigger.prototype._getConnectedElement = /**
  1258. * @private
  1259. * @return {?}
  1260. */
  1261. function () {
  1262. if (this.connectedTo) {
  1263. return this.connectedTo.elementRef;
  1264. }
  1265. return this._formField ? this._formField.getConnectedOverlayOrigin() : this._element;
  1266. };
  1267. /**
  1268. * @private
  1269. * @return {?}
  1270. */
  1271. MatAutocompleteTrigger.prototype._getPanelWidth = /**
  1272. * @private
  1273. * @return {?}
  1274. */
  1275. function () {
  1276. return this.autocomplete.panelWidth || this._getHostWidth();
  1277. };
  1278. /** Returns the width of the input element, so the panel width can match it. */
  1279. /**
  1280. * Returns the width of the input element, so the panel width can match it.
  1281. * @private
  1282. * @return {?}
  1283. */
  1284. MatAutocompleteTrigger.prototype._getHostWidth = /**
  1285. * Returns the width of the input element, so the panel width can match it.
  1286. * @private
  1287. * @return {?}
  1288. */
  1289. function () {
  1290. return this._getConnectedElement().nativeElement.getBoundingClientRect().width;
  1291. };
  1292. /**
  1293. * Resets the active item to -1 so arrow events will activate the
  1294. * correct options, or to 0 if the consumer opted into it.
  1295. */
  1296. /**
  1297. * Resets the active item to -1 so arrow events will activate the
  1298. * correct options, or to 0 if the consumer opted into it.
  1299. * @private
  1300. * @return {?}
  1301. */
  1302. MatAutocompleteTrigger.prototype._resetActiveItem = /**
  1303. * Resets the active item to -1 so arrow events will activate the
  1304. * correct options, or to 0 if the consumer opted into it.
  1305. * @private
  1306. * @return {?}
  1307. */
  1308. function () {
  1309. this.autocomplete._keyManager.setActiveItem(this.autocomplete.autoActiveFirstOption ? 0 : -1);
  1310. };
  1311. /** Determines whether the panel can be opened. */
  1312. /**
  1313. * Determines whether the panel can be opened.
  1314. * @private
  1315. * @return {?}
  1316. */
  1317. MatAutocompleteTrigger.prototype._canOpen = /**
  1318. * Determines whether the panel can be opened.
  1319. * @private
  1320. * @return {?}
  1321. */
  1322. function () {
  1323. /** @type {?} */
  1324. var element = this._element.nativeElement;
  1325. return !element.readOnly && !element.disabled && !this._autocompleteDisabled;
  1326. };
  1327. MatAutocompleteTrigger.decorators = [
  1328. { type: Directive, args: [{
  1329. selector: "input[matAutocomplete], textarea[matAutocomplete]",
  1330. host: {
  1331. '[attr.autocomplete]': 'autocompleteAttribute',
  1332. '[attr.role]': 'autocompleteDisabled ? null : "combobox"',
  1333. '[attr.aria-autocomplete]': 'autocompleteDisabled ? null : "list"',
  1334. '[attr.aria-activedescendant]': '(panelOpen && activeOption) ? activeOption.id : null',
  1335. '[attr.aria-expanded]': 'autocompleteDisabled ? null : panelOpen.toString()',
  1336. '[attr.aria-owns]': '(autocompleteDisabled || !panelOpen) ? null : autocomplete?.id',
  1337. '[attr.aria-haspopup]': '!autocompleteDisabled',
  1338. // Note: we use `focusin`, as opposed to `focus`, in order to open the panel
  1339. // a little earlier. This avoids issues where IE delays the focusing of the input.
  1340. '(focusin)': '_handleFocus()',
  1341. '(blur)': '_onTouched()',
  1342. '(input)': '_handleInput($event)',
  1343. '(keydown)': '_handleKeydown($event)',
  1344. },
  1345. exportAs: 'matAutocompleteTrigger',
  1346. providers: [MAT_AUTOCOMPLETE_VALUE_ACCESSOR]
  1347. },] },
  1348. ];
  1349. /** @nocollapse */
  1350. MatAutocompleteTrigger.ctorParameters = function () { return [
  1351. { type: ElementRef },
  1352. { type: Overlay },
  1353. { type: ViewContainerRef },
  1354. { type: NgZone },
  1355. { type: ChangeDetectorRef },
  1356. { type: undefined, decorators: [{ type: Inject, args: [MAT_AUTOCOMPLETE_SCROLL_STRATEGY,] }] },
  1357. { type: Directionality, decorators: [{ type: Optional }] },
  1358. { type: MatFormField, decorators: [{ type: Optional }, { type: Host }] },
  1359. { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [DOCUMENT,] }] },
  1360. { type: ViewportRuler }
  1361. ]; };
  1362. MatAutocompleteTrigger.propDecorators = {
  1363. autocomplete: [{ type: Input, args: ['matAutocomplete',] }],
  1364. position: [{ type: Input, args: ['matAutocompletePosition',] }],
  1365. connectedTo: [{ type: Input, args: ['matAutocompleteConnectedTo',] }],
  1366. autocompleteAttribute: [{ type: Input, args: ['autocomplete',] }],
  1367. autocompleteDisabled: [{ type: Input, args: ['matAutocompleteDisabled',] }]
  1368. };
  1369. return MatAutocompleteTrigger;
  1370. }());
  1371. /**
  1372. * @fileoverview added by tsickle
  1373. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1374. */
  1375. var MatAutocompleteModule = /** @class */ (function () {
  1376. function MatAutocompleteModule() {
  1377. }
  1378. MatAutocompleteModule.decorators = [
  1379. { type: NgModule, args: [{
  1380. imports: [MatOptionModule, OverlayModule, MatCommonModule, CommonModule],
  1381. exports: [
  1382. MatAutocomplete,
  1383. MatOptionModule,
  1384. MatAutocompleteTrigger,
  1385. MatAutocompleteOrigin,
  1386. MatCommonModule
  1387. ],
  1388. declarations: [MatAutocomplete, MatAutocompleteTrigger, MatAutocompleteOrigin],
  1389. providers: [MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY_PROVIDER],
  1390. },] },
  1391. ];
  1392. return MatAutocompleteModule;
  1393. }());
  1394. /**
  1395. * @fileoverview added by tsickle
  1396. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1397. */
  1398. /**
  1399. * @fileoverview added by tsickle
  1400. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1401. */
  1402. export { MAT_AUTOCOMPLETE_DEFAULT_OPTIONS_FACTORY, MatAutocompleteSelectedEvent, MAT_AUTOCOMPLETE_DEFAULT_OPTIONS, MatAutocomplete, MatAutocompleteModule, MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY, getMatAutocompleteMissingPanelError, AUTOCOMPLETE_OPTION_HEIGHT, AUTOCOMPLETE_PANEL_HEIGHT, MAT_AUTOCOMPLETE_SCROLL_STRATEGY, MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY_PROVIDER, MAT_AUTOCOMPLETE_VALUE_ACCESSOR, MatAutocompleteTrigger, MatAutocompleteOrigin };
  1403. //# sourceMappingURL=autocomplete.es5.js.map