| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701 |
- /**
- * @license
- * Copyright Google LLC All Rights Reserved.
- *
- * Use of this source code is governed by an MIT-style license that can be
- * found in the LICENSE file at https://angular.io/license
- */
- import { animate, animateChild, query, state, style, transition, trigger } from '@angular/animations';
- import { ActiveDescendantKeyManager, LiveAnnouncer } from '@angular/cdk/a11y';
- import { Directionality } from '@angular/cdk/bidi';
- import { coerceBooleanProperty } from '@angular/cdk/coercion';
- import { SelectionModel } from '@angular/cdk/collections';
- import { A, DOWN_ARROW, END, ENTER, HOME, LEFT_ARROW, RIGHT_ARROW, SPACE, UP_ARROW, hasModifierKey } from '@angular/cdk/keycodes';
- import { CdkConnectedOverlay, Overlay, OverlayModule } from '@angular/cdk/overlay';
- import { ViewportRuler } from '@angular/cdk/scrolling';
- import { Attribute, ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, ContentChildren, Directive, ElementRef, EventEmitter, Inject, InjectionToken, Input, isDevMode, NgZone, Optional, Output, Self, ViewChild, ViewEncapsulation, NgModule } from '@angular/core';
- import { FormGroupDirective, NgControl, NgForm } from '@angular/forms';
- import { _countGroupLabelsBeforeOption, _getOptionScrollPosition, ErrorStateMatcher, MAT_OPTION_PARENT_COMPONENT, MatOptgroup, MatOption, mixinDisabled, mixinDisableRipple, mixinErrorState, mixinTabIndex, MatCommonModule, MatOptionModule } from '@angular/material/core';
- import { MatFormField, MatFormFieldControl, MatFormFieldModule } from '@angular/material/form-field';
- import { defer, merge, Subject } from 'rxjs';
- import { distinctUntilChanged, filter, map, startWith, switchMap, take, takeUntil } from 'rxjs/operators';
- import { CommonModule } from '@angular/common';
- /**
- * @fileoverview added by tsickle
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
- */
- /**
- * The following are all the animations for the mat-select component, with each
- * const containing the metadata for one animation.
- *
- * The values below match the implementation of the AngularJS Material mat-select animation.
- * \@docs-private
- * @type {?}
- */
- const matSelectAnimations = {
- /**
- * This animation ensures the select's overlay panel animation (transformPanel) is called when
- * closing the select.
- * This is needed due to https://github.com/angular/angular/issues/23302
- */
- transformPanelWrap: trigger('transformPanelWrap', [
- transition('* => void', query('@transformPanel', [animateChild()], { optional: true }))
- ]),
- /**
- * This animation transforms the select's overlay panel on and off the page.
- *
- * When the panel is attached to the DOM, it expands its width by the amount of padding, scales it
- * up to 100% on the Y axis, fades in its border, and translates slightly up and to the
- * side to ensure the option text correctly overlaps the trigger text.
- *
- * When the panel is removed from the DOM, it simply fades out linearly.
- */
- transformPanel: trigger('transformPanel', [
- state('void', style({
- transform: 'scaleY(0.8)',
- minWidth: '100%',
- opacity: 0
- })),
- state('showing', style({
- opacity: 1,
- minWidth: 'calc(100% + 32px)',
- // 32px = 2 * 16px padding
- transform: 'scaleY(1)'
- })),
- state('showing-multiple', style({
- opacity: 1,
- minWidth: 'calc(100% + 64px)',
- // 64px = 48px padding on the left + 16px padding on the right
- transform: 'scaleY(1)'
- })),
- transition('void => *', animate('120ms cubic-bezier(0, 0, 0.2, 1)')),
- transition('* => void', animate('100ms 25ms linear', style({ opacity: 0 })))
- ]),
- /**
- * This animation fades in the background color and text content of the
- * select's options. It is time delayed to occur 100ms after the overlay
- * panel has transformed in.
- * @deprecated Not used anymore. To be removed.
- * \@breaking-change 8.0.0
- */
- fadeInContent: trigger('fadeInContent', [
- state('showing', style({ opacity: 1 })),
- transition('void => showing', [
- style({ opacity: 0 }),
- animate('150ms 100ms cubic-bezier(0.55, 0, 0.55, 0.2)')
- ])
- ])
- };
- /**
- * @deprecated
- * \@breaking-change 8.0.0
- * \@docs-private
- * @type {?}
- */
- const transformPanel = matSelectAnimations.transformPanel;
- /**
- * @deprecated
- * \@breaking-change 8.0.0
- * \@docs-private
- * @type {?}
- */
- const fadeInContent = matSelectAnimations.fadeInContent;
- /**
- * @fileoverview added by tsickle
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
- */
- /**
- * Returns an exception to be thrown when attempting to change a select's `multiple` option
- * after initialization.
- * \@docs-private
- * @return {?}
- */
- function getMatSelectDynamicMultipleError() {
- return Error('Cannot change `multiple` mode of select after initialization.');
- }
- /**
- * Returns an exception to be thrown when attempting to assign a non-array value to a select
- * in `multiple` mode. Note that `undefined` and `null` are still valid values to allow for
- * resetting the value.
- * \@docs-private
- * @return {?}
- */
- function getMatSelectNonArrayValueError() {
- return Error('Value must be an array in multiple-selection mode.');
- }
- /**
- * Returns an exception to be thrown when assigning a non-function value to the comparator
- * used to determine if a value corresponds to an option. Note that whether the function
- * actually takes two values and returns a boolean is not checked.
- * @return {?}
- */
- function getMatSelectNonFunctionValueError() {
- return Error('`compareWith` must be a function.');
- }
- /**
- * @fileoverview added by tsickle
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
- */
- /** @type {?} */
- let nextUniqueId = 0;
- /**
- * The max height of the select's overlay panel
- * @type {?}
- */
- const SELECT_PANEL_MAX_HEIGHT = 256;
- /**
- * The panel's padding on the x-axis
- * @type {?}
- */
- const SELECT_PANEL_PADDING_X = 16;
- /**
- * The panel's x axis padding if it is indented (e.g. there is an option group).
- * @type {?}
- */
- const SELECT_PANEL_INDENT_PADDING_X = SELECT_PANEL_PADDING_X * 2;
- /**
- * The height of the select items in `em` units.
- * @type {?}
- */
- const SELECT_ITEM_HEIGHT_EM = 3;
- // TODO(josephperrott): Revert to a constant after 2018 spec updates are fully merged.
- /**
- * Distance between the panel edge and the option text in
- * multi-selection mode.
- *
- * Calculated as:
- * (SELECT_PANEL_PADDING_X * 1.5) + 16 = 40
- * The padding is multiplied by 1.5 because the checkbox's margin is half the padding.
- * The checkbox width is 16px.
- * @type {?}
- */
- const SELECT_MULTIPLE_PANEL_PADDING_X = SELECT_PANEL_PADDING_X * 1.5 + 16;
- /**
- * The select panel will only "fit" inside the viewport if it is positioned at
- * this value or more away from the viewport boundary.
- * @type {?}
- */
- const SELECT_PANEL_VIEWPORT_PADDING = 8;
- /**
- * Injection token that determines the scroll handling while a select is open.
- * @type {?}
- */
- const MAT_SELECT_SCROLL_STRATEGY = new InjectionToken('mat-select-scroll-strategy');
- /**
- * \@docs-private
- * @param {?} overlay
- * @return {?}
- */
- function MAT_SELECT_SCROLL_STRATEGY_PROVIDER_FACTORY(overlay) {
- return (/**
- * @return {?}
- */
- () => overlay.scrollStrategies.reposition());
- }
- /**
- * \@docs-private
- * @type {?}
- */
- const MAT_SELECT_SCROLL_STRATEGY_PROVIDER = {
- provide: MAT_SELECT_SCROLL_STRATEGY,
- deps: [Overlay],
- useFactory: MAT_SELECT_SCROLL_STRATEGY_PROVIDER_FACTORY,
- };
- /**
- * Change event object that is emitted when the select value has changed.
- */
- class MatSelectChange {
- /**
- * @param {?} source
- * @param {?} value
- */
- constructor(source, value) {
- this.source = source;
- this.value = value;
- }
- }
- // Boilerplate for applying mixins to MatSelect.
- /**
- * \@docs-private
- */
- class MatSelectBase {
- /**
- * @param {?} _elementRef
- * @param {?} _defaultErrorStateMatcher
- * @param {?} _parentForm
- * @param {?} _parentFormGroup
- * @param {?} ngControl
- */
- constructor(_elementRef, _defaultErrorStateMatcher, _parentForm, _parentFormGroup, ngControl) {
- this._elementRef = _elementRef;
- this._defaultErrorStateMatcher = _defaultErrorStateMatcher;
- this._parentForm = _parentForm;
- this._parentFormGroup = _parentFormGroup;
- this.ngControl = ngControl;
- }
- }
- /** @type {?} */
- const _MatSelectMixinBase = mixinDisableRipple(mixinTabIndex(mixinDisabled(mixinErrorState(MatSelectBase))));
- /**
- * Allows the user to customize the trigger that is displayed when the select has a value.
- */
- class MatSelectTrigger {
- }
- MatSelectTrigger.decorators = [
- { type: Directive, args: [{
- selector: 'mat-select-trigger'
- },] },
- ];
- class MatSelect extends _MatSelectMixinBase {
- /**
- * @param {?} _viewportRuler
- * @param {?} _changeDetectorRef
- * @param {?} _ngZone
- * @param {?} _defaultErrorStateMatcher
- * @param {?} elementRef
- * @param {?} _dir
- * @param {?} _parentForm
- * @param {?} _parentFormGroup
- * @param {?} _parentFormField
- * @param {?} ngControl
- * @param {?} tabIndex
- * @param {?} scrollStrategyFactory
- * @param {?=} _liveAnnouncer
- */
- constructor(_viewportRuler, _changeDetectorRef, _ngZone, _defaultErrorStateMatcher, elementRef, _dir, _parentForm, _parentFormGroup, _parentFormField, ngControl, tabIndex, scrollStrategyFactory, _liveAnnouncer) {
- super(elementRef, _defaultErrorStateMatcher, _parentForm, _parentFormGroup, ngControl);
- this._viewportRuler = _viewportRuler;
- this._changeDetectorRef = _changeDetectorRef;
- this._ngZone = _ngZone;
- this._dir = _dir;
- this._parentFormField = _parentFormField;
- this.ngControl = ngControl;
- this._liveAnnouncer = _liveAnnouncer;
- /**
- * Whether or not the overlay panel is open.
- */
- this._panelOpen = false;
- /**
- * Whether filling out the select is required in the form.
- */
- this._required = false;
- /**
- * The scroll position of the overlay panel, calculated to center the selected option.
- */
- this._scrollTop = 0;
- /**
- * Whether the component is in multiple selection mode.
- */
- this._multiple = false;
- /**
- * Comparison function to specify which option is displayed. Defaults to object equality.
- */
- this._compareWith = (/**
- * @param {?} o1
- * @param {?} o2
- * @return {?}
- */
- (o1, o2) => o1 === o2);
- /**
- * Unique id for this input.
- */
- this._uid = `mat-select-${nextUniqueId++}`;
- /**
- * Emits whenever the component is destroyed.
- */
- this._destroy = new Subject();
- /**
- * The cached font-size of the trigger element.
- */
- this._triggerFontSize = 0;
- /**
- * `View -> model callback called when value changes`
- */
- this._onChange = (/**
- * @return {?}
- */
- () => { });
- /**
- * `View -> model callback called when select has been touched`
- */
- this._onTouched = (/**
- * @return {?}
- */
- () => { });
- /**
- * The IDs of child options to be passed to the aria-owns attribute.
- */
- this._optionIds = '';
- /**
- * The value of the select panel's transform-origin property.
- */
- this._transformOrigin = 'top';
- /**
- * Emits when the panel element is finished transforming in.
- */
- this._panelDoneAnimatingStream = new Subject();
- /**
- * The y-offset of the overlay panel in relation to the trigger's top start corner.
- * This must be adjusted to align the selected option text over the trigger text.
- * when the panel opens. Will change based on the y-position of the selected option.
- */
- this._offsetY = 0;
- /**
- * This position config ensures that the top "start" corner of the overlay
- * is aligned with with the top "start" of the origin by default (overlapping
- * the trigger completely). If the panel cannot fit below the trigger, it
- * will fall back to a position above the trigger.
- */
- this._positions = [
- {
- originX: 'start',
- originY: 'top',
- overlayX: 'start',
- overlayY: 'top',
- },
- {
- originX: 'start',
- originY: 'bottom',
- overlayX: 'start',
- overlayY: 'bottom',
- },
- ];
- /**
- * Whether the component is disabling centering of the active option over the trigger.
- */
- this._disableOptionCentering = false;
- this._focused = false;
- /**
- * A name for this control that can be used by `mat-form-field`.
- */
- this.controlType = 'mat-select';
- /**
- * Aria label of the select. If not specified, the placeholder will be used as label.
- */
- this.ariaLabel = '';
- /**
- * Combined stream of all of the child options' change events.
- */
- this.optionSelectionChanges = (/** @type {?} */ (defer((/**
- * @return {?}
- */
- () => {
- /** @type {?} */
- const options = this.options;
- if (options) {
- return options.changes.pipe(startWith(options), switchMap((/**
- * @return {?}
- */
- () => merge(...options.map((/**
- * @param {?} option
- * @return {?}
- */
- option => option.onSelectionChange))))));
- }
- return this._ngZone.onStable
- .asObservable()
- .pipe(take(1), switchMap((/**
- * @return {?}
- */
- () => this.optionSelectionChanges)));
- }))));
- /**
- * Event emitted when the select panel has been toggled.
- */
- this.openedChange = new EventEmitter();
- /**
- * Event emitted when the select has been opened.
- */
- this._openedStream = this.openedChange.pipe(filter((/**
- * @param {?} o
- * @return {?}
- */
- o => o)), map((/**
- * @return {?}
- */
- () => { })));
- /**
- * Event emitted when the select has been closed.
- */
- this._closedStream = this.openedChange.pipe(filter((/**
- * @param {?} o
- * @return {?}
- */
- o => !o)), map((/**
- * @return {?}
- */
- () => { })));
- /**
- * Event emitted when the selected value has been changed by the user.
- */
- this.selectionChange = new EventEmitter();
- /**
- * Event that emits whenever the raw value of the select changes. This is here primarily
- * to facilitate the two-way binding for the `value` input.
- * \@docs-private
- */
- this.valueChange = new EventEmitter();
- if (this.ngControl) {
- // Note: we provide the value accessor through here, instead of
- // the `providers` to avoid running into a circular import.
- this.ngControl.valueAccessor = this;
- }
- this._scrollStrategyFactory = scrollStrategyFactory;
- this._scrollStrategy = this._scrollStrategyFactory();
- this.tabIndex = parseInt(tabIndex) || 0;
- // Force setter to be called in case id was not specified.
- this.id = this.id;
- }
- /**
- * Whether the select is focused.
- * @return {?}
- */
- get focused() {
- return this._focused || this._panelOpen;
- }
- /**
- * @deprecated Setter to be removed as this property is intended to be readonly.
- * \@breaking-change 8.0.0
- * @param {?} value
- * @return {?}
- */
- set focused(value) {
- this._focused = value;
- }
- /**
- * Placeholder to be shown if no value has been selected.
- * @return {?}
- */
- get placeholder() { return this._placeholder; }
- /**
- * @param {?} value
- * @return {?}
- */
- set placeholder(value) {
- this._placeholder = value;
- this.stateChanges.next();
- }
- /**
- * Whether the component is required.
- * @return {?}
- */
- get required() { return this._required; }
- /**
- * @param {?} value
- * @return {?}
- */
- set required(value) {
- this._required = coerceBooleanProperty(value);
- this.stateChanges.next();
- }
- /**
- * Whether the user should be allowed to select multiple options.
- * @return {?}
- */
- get multiple() { return this._multiple; }
- /**
- * @param {?} value
- * @return {?}
- */
- set multiple(value) {
- if (this._selectionModel) {
- throw getMatSelectDynamicMultipleError();
- }
- this._multiple = coerceBooleanProperty(value);
- }
- /**
- * Whether to center the active option over the trigger.
- * @return {?}
- */
- get disableOptionCentering() { return this._disableOptionCentering; }
- /**
- * @param {?} value
- * @return {?}
- */
- set disableOptionCentering(value) {
- this._disableOptionCentering = coerceBooleanProperty(value);
- }
- /**
- * Function to compare the option values with the selected values. The first argument
- * is a value from an option. The second is a value from the selection. A boolean
- * should be returned.
- * @return {?}
- */
- get compareWith() { return this._compareWith; }
- /**
- * @param {?} fn
- * @return {?}
- */
- set compareWith(fn) {
- if (typeof fn !== 'function') {
- throw getMatSelectNonFunctionValueError();
- }
- this._compareWith = fn;
- if (this._selectionModel) {
- // A different comparator means the selection could change.
- this._initializeSelection();
- }
- }
- /**
- * Value of the select control.
- * @return {?}
- */
- get value() { return this._value; }
- /**
- * @param {?} newValue
- * @return {?}
- */
- set value(newValue) {
- if (newValue !== this._value) {
- this.writeValue(newValue);
- this._value = newValue;
- }
- }
- /**
- * Unique id of the element.
- * @return {?}
- */
- get id() { return this._id; }
- /**
- * @param {?} value
- * @return {?}
- */
- set id(value) {
- this._id = value || this._uid;
- this.stateChanges.next();
- }
- /**
- * @return {?}
- */
- ngOnInit() {
- this._selectionModel = new SelectionModel(this.multiple);
- this.stateChanges.next();
- // We need `distinctUntilChanged` here, because some browsers will
- // fire the animation end event twice for the same animation. See:
- // https://github.com/angular/angular/issues/24084
- this._panelDoneAnimatingStream
- .pipe(distinctUntilChanged(), takeUntil(this._destroy))
- .subscribe((/**
- * @return {?}
- */
- () => {
- if (this.panelOpen) {
- this._scrollTop = 0;
- this.openedChange.emit(true);
- }
- else {
- this.openedChange.emit(false);
- this.overlayDir.offsetX = 0;
- this._changeDetectorRef.markForCheck();
- }
- }));
- this._viewportRuler.change()
- .pipe(takeUntil(this._destroy))
- .subscribe((/**
- * @return {?}
- */
- () => {
- if (this._panelOpen) {
- this._triggerRect = this.trigger.nativeElement.getBoundingClientRect();
- this._changeDetectorRef.markForCheck();
- }
- }));
- }
- /**
- * @return {?}
- */
- ngAfterContentInit() {
- this._initKeyManager();
- this._selectionModel.onChange.pipe(takeUntil(this._destroy)).subscribe((/**
- * @param {?} event
- * @return {?}
- */
- event => {
- event.added.forEach((/**
- * @param {?} option
- * @return {?}
- */
- option => option.select()));
- event.removed.forEach((/**
- * @param {?} option
- * @return {?}
- */
- option => option.deselect()));
- }));
- this.options.changes.pipe(startWith(null), takeUntil(this._destroy)).subscribe((/**
- * @return {?}
- */
- () => {
- this._resetOptions();
- this._initializeSelection();
- }));
- }
- /**
- * @return {?}
- */
- ngDoCheck() {
- if (this.ngControl) {
- this.updateErrorState();
- }
- }
- /**
- * @param {?} changes
- * @return {?}
- */
- ngOnChanges(changes) {
- // Updating the disabled state is handled by `mixinDisabled`, but we need to additionally let
- // the parent form field know to run change detection when the disabled state changes.
- if (changes['disabled']) {
- this.stateChanges.next();
- }
- }
- /**
- * @return {?}
- */
- ngOnDestroy() {
- this._destroy.next();
- this._destroy.complete();
- this.stateChanges.complete();
- }
- /**
- * Toggles the overlay panel open or closed.
- * @return {?}
- */
- toggle() {
- this.panelOpen ? this.close() : this.open();
- }
- /**
- * Opens the overlay panel.
- * @return {?}
- */
- open() {
- if (this.disabled || !this.options || !this.options.length || this._panelOpen) {
- return;
- }
- this._triggerRect = this.trigger.nativeElement.getBoundingClientRect();
- // Note: The computed font-size will be a string pixel value (e.g. "16px").
- // `parseInt` ignores the trailing 'px' and converts this to a number.
- this._triggerFontSize = parseInt(getComputedStyle(this.trigger.nativeElement).fontSize || '0');
- this._panelOpen = true;
- this._keyManager.withHorizontalOrientation(null);
- this._calculateOverlayPosition();
- this._highlightCorrectOption();
- this._changeDetectorRef.markForCheck();
- // Set the font size on the panel element once it exists.
- this._ngZone.onStable.asObservable().pipe(take(1)).subscribe((/**
- * @return {?}
- */
- () => {
- if (this._triggerFontSize && this.overlayDir.overlayRef &&
- this.overlayDir.overlayRef.overlayElement) {
- this.overlayDir.overlayRef.overlayElement.style.fontSize = `${this._triggerFontSize}px`;
- }
- }));
- }
- /**
- * Closes the overlay panel and focuses the host element.
- * @return {?}
- */
- close() {
- if (this._panelOpen) {
- this._panelOpen = false;
- this._keyManager.withHorizontalOrientation(this._isRtl() ? 'rtl' : 'ltr');
- this._changeDetectorRef.markForCheck();
- this._onTouched();
- }
- }
- /**
- * Sets the select's value. Part of the ControlValueAccessor interface
- * required to integrate with Angular's core forms API.
- *
- * @param {?} value New value to be written to the model.
- * @return {?}
- */
- writeValue(value) {
- if (this.options) {
- this._setSelectionByValue(value);
- }
- }
- /**
- * Saves a callback function to be invoked when the select's value
- * changes from user input. Part of the ControlValueAccessor interface
- * required to integrate with Angular's core forms API.
- *
- * @param {?} fn Callback to be triggered when the value changes.
- * @return {?}
- */
- registerOnChange(fn) {
- this._onChange = fn;
- }
- /**
- * Saves a callback function to be invoked when the select is blurred
- * by the user. Part of the ControlValueAccessor interface required
- * to integrate with Angular's core forms API.
- *
- * @param {?} fn Callback to be triggered when the component has been touched.
- * @return {?}
- */
- registerOnTouched(fn) {
- this._onTouched = fn;
- }
- /**
- * Disables the select. Part of the ControlValueAccessor interface required
- * to integrate with Angular's core forms API.
- *
- * @param {?} isDisabled Sets whether the component is disabled.
- * @return {?}
- */
- setDisabledState(isDisabled) {
- this.disabled = isDisabled;
- this._changeDetectorRef.markForCheck();
- this.stateChanges.next();
- }
- /**
- * Whether or not the overlay panel is open.
- * @return {?}
- */
- get panelOpen() {
- return this._panelOpen;
- }
- /**
- * The currently selected option.
- * @return {?}
- */
- get selected() {
- return this.multiple ? this._selectionModel.selected : this._selectionModel.selected[0];
- }
- /**
- * The value displayed in the trigger.
- * @return {?}
- */
- get triggerValue() {
- if (this.empty) {
- return '';
- }
- if (this._multiple) {
- /** @type {?} */
- const selectedOptions = this._selectionModel.selected.map((/**
- * @param {?} option
- * @return {?}
- */
- option => option.viewValue));
- if (this._isRtl()) {
- selectedOptions.reverse();
- }
- // TODO(crisbeto): delimiter should be configurable for proper localization.
- return selectedOptions.join(', ');
- }
- return this._selectionModel.selected[0].viewValue;
- }
- /**
- * Whether the element is in RTL mode.
- * @return {?}
- */
- _isRtl() {
- return this._dir ? this._dir.value === 'rtl' : false;
- }
- /**
- * Handles all keydown events on the select.
- * @param {?} event
- * @return {?}
- */
- _handleKeydown(event) {
- if (!this.disabled) {
- this.panelOpen ? this._handleOpenKeydown(event) : this._handleClosedKeydown(event);
- }
- }
- /**
- * Handles keyboard events while the select is closed.
- * @private
- * @param {?} event
- * @return {?}
- */
- _handleClosedKeydown(event) {
- /** @type {?} */
- const keyCode = event.keyCode;
- /** @type {?} */
- const isArrowKey = keyCode === DOWN_ARROW || keyCode === UP_ARROW ||
- keyCode === LEFT_ARROW || keyCode === RIGHT_ARROW;
- /** @type {?} */
- const isOpenKey = keyCode === ENTER || keyCode === SPACE;
- /** @type {?} */
- const manager = this._keyManager;
- // Open the select on ALT + arrow key to match the native <select>
- if ((isOpenKey && !hasModifierKey(event)) || ((this.multiple || event.altKey) && isArrowKey)) {
- event.preventDefault(); // prevents the page from scrolling down when pressing space
- this.open();
- }
- else if (!this.multiple) {
- /** @type {?} */
- const previouslySelectedOption = this.selected;
- if (keyCode === HOME || keyCode === END) {
- keyCode === HOME ? manager.setFirstItemActive() : manager.setLastItemActive();
- event.preventDefault();
- }
- else {
- manager.onKeydown(event);
- }
- /** @type {?} */
- const selectedOption = this.selected;
- // Since the value has changed, we need to announce it ourselves.
- // @breaking-change 8.0.0 remove null check for _liveAnnouncer.
- if (this._liveAnnouncer && selectedOption && previouslySelectedOption !== selectedOption) {
- // We set a duration on the live announcement, because we want the live element to be
- // cleared after a while so that users can't navigate to it using the arrow keys.
- this._liveAnnouncer.announce(((/** @type {?} */ (selectedOption))).viewValue, 10000);
- }
- }
- }
- /**
- * Handles keyboard events when the selected is open.
- * @private
- * @param {?} event
- * @return {?}
- */
- _handleOpenKeydown(event) {
- /** @type {?} */
- const keyCode = event.keyCode;
- /** @type {?} */
- const isArrowKey = keyCode === DOWN_ARROW || keyCode === UP_ARROW;
- /** @type {?} */
- const manager = this._keyManager;
- if (keyCode === HOME || keyCode === END) {
- event.preventDefault();
- keyCode === HOME ? manager.setFirstItemActive() : manager.setLastItemActive();
- }
- else if (isArrowKey && event.altKey) {
- // Close the select on ALT + arrow key to match the native <select>
- event.preventDefault();
- this.close();
- }
- else if ((keyCode === ENTER || keyCode === SPACE) && manager.activeItem &&
- !hasModifierKey(event)) {
- event.preventDefault();
- manager.activeItem._selectViaInteraction();
- }
- else if (this._multiple && keyCode === A && event.ctrlKey) {
- event.preventDefault();
- /** @type {?} */
- const hasDeselectedOptions = this.options.some((/**
- * @param {?} opt
- * @return {?}
- */
- opt => !opt.disabled && !opt.selected));
- this.options.forEach((/**
- * @param {?} option
- * @return {?}
- */
- option => {
- if (!option.disabled) {
- hasDeselectedOptions ? option.select() : option.deselect();
- }
- }));
- }
- else {
- /** @type {?} */
- const previouslyFocusedIndex = manager.activeItemIndex;
- manager.onKeydown(event);
- if (this._multiple && isArrowKey && event.shiftKey && manager.activeItem &&
- manager.activeItemIndex !== previouslyFocusedIndex) {
- manager.activeItem._selectViaInteraction();
- }
- }
- }
- /**
- * @return {?}
- */
- _onFocus() {
- if (!this.disabled) {
- this._focused = true;
- this.stateChanges.next();
- }
- }
- /**
- * Calls the touched callback only if the panel is closed. Otherwise, the trigger will
- * "blur" to the panel when it opens, causing a false positive.
- * @return {?}
- */
- _onBlur() {
- this._focused = false;
- if (!this.disabled && !this.panelOpen) {
- this._onTouched();
- this._changeDetectorRef.markForCheck();
- this.stateChanges.next();
- }
- }
- /**
- * Callback that is invoked when the overlay panel has been attached.
- * @return {?}
- */
- _onAttached() {
- this.overlayDir.positionChange.pipe(take(1)).subscribe((/**
- * @return {?}
- */
- () => {
- this._changeDetectorRef.detectChanges();
- this._calculateOverlayOffsetX();
- this.panel.nativeElement.scrollTop = this._scrollTop;
- }));
- }
- /**
- * Returns the theme to be used on the panel.
- * @return {?}
- */
- _getPanelTheme() {
- return this._parentFormField ? `mat-${this._parentFormField.color}` : '';
- }
- /**
- * Whether the select has a value.
- * @return {?}
- */
- get empty() {
- return !this._selectionModel || this._selectionModel.isEmpty();
- }
- /**
- * @private
- * @return {?}
- */
- _initializeSelection() {
- // Defer setting the value in order to avoid the "Expression
- // has changed after it was checked" errors from Angular.
- Promise.resolve().then((/**
- * @return {?}
- */
- () => {
- this._setSelectionByValue(this.ngControl ? this.ngControl.value : this._value);
- this.stateChanges.next();
- }));
- }
- /**
- * Sets the selected option based on a value. If no option can be
- * found with the designated value, the select trigger is cleared.
- * @private
- * @param {?} value
- * @return {?}
- */
- _setSelectionByValue(value) {
- if (this.multiple && value) {
- if (!Array.isArray(value)) {
- throw getMatSelectNonArrayValueError();
- }
- this._selectionModel.clear();
- value.forEach((/**
- * @param {?} currentValue
- * @return {?}
- */
- (currentValue) => this._selectValue(currentValue)));
- this._sortValues();
- }
- else {
- this._selectionModel.clear();
- /** @type {?} */
- const correspondingOption = this._selectValue(value);
- // Shift focus to the active item. Note that we shouldn't do this in multiple
- // mode, because we don't know what option the user interacted with last.
- if (correspondingOption) {
- this._keyManager.setActiveItem(correspondingOption);
- }
- }
- this._changeDetectorRef.markForCheck();
- }
- /**
- * Finds and selects and option based on its value.
- * @private
- * @param {?} value
- * @return {?} Option that has the corresponding value.
- */
- _selectValue(value) {
- /** @type {?} */
- const correspondingOption = this.options.find((/**
- * @param {?} option
- * @return {?}
- */
- (option) => {
- try {
- // Treat null as a special reset value.
- return option.value != null && this._compareWith(option.value, value);
- }
- catch (error) {
- if (isDevMode()) {
- // Notify developers of errors in their comparator.
- console.warn(error);
- }
- return false;
- }
- }));
- if (correspondingOption) {
- this._selectionModel.select(correspondingOption);
- }
- return correspondingOption;
- }
- /**
- * Sets up a key manager to listen to keyboard events on the overlay panel.
- * @private
- * @return {?}
- */
- _initKeyManager() {
- this._keyManager = new ActiveDescendantKeyManager(this.options)
- .withTypeAhead()
- .withVerticalOrientation()
- .withHorizontalOrientation(this._isRtl() ? 'rtl' : 'ltr')
- .withAllowedModifierKeys(['shiftKey']);
- this._keyManager.tabOut.pipe(takeUntil(this._destroy)).subscribe((/**
- * @return {?}
- */
- () => {
- // Restore focus to the trigger before closing. Ensures that the focus
- // position won't be lost if the user got focus into the overlay.
- this.focus();
- this.close();
- }));
- this._keyManager.change.pipe(takeUntil(this._destroy)).subscribe((/**
- * @return {?}
- */
- () => {
- if (this._panelOpen && this.panel) {
- this._scrollActiveOptionIntoView();
- }
- else if (!this._panelOpen && !this.multiple && this._keyManager.activeItem) {
- this._keyManager.activeItem._selectViaInteraction();
- }
- }));
- }
- /**
- * Drops current option subscriptions and IDs and resets from scratch.
- * @private
- * @return {?}
- */
- _resetOptions() {
- /** @type {?} */
- const changedOrDestroyed = merge(this.options.changes, this._destroy);
- this.optionSelectionChanges.pipe(takeUntil(changedOrDestroyed)).subscribe((/**
- * @param {?} event
- * @return {?}
- */
- event => {
- this._onSelect(event.source, event.isUserInput);
- if (event.isUserInput && !this.multiple && this._panelOpen) {
- this.close();
- this.focus();
- }
- }));
- // Listen to changes in the internal state of the options and react accordingly.
- // Handles cases like the labels of the selected options changing.
- merge(...this.options.map((/**
- * @param {?} option
- * @return {?}
- */
- option => option._stateChanges)))
- .pipe(takeUntil(changedOrDestroyed))
- .subscribe((/**
- * @return {?}
- */
- () => {
- this._changeDetectorRef.markForCheck();
- this.stateChanges.next();
- }));
- this._setOptionIds();
- }
- /**
- * Invoked when an option is clicked.
- * @private
- * @param {?} option
- * @param {?} isUserInput
- * @return {?}
- */
- _onSelect(option, isUserInput) {
- /** @type {?} */
- const wasSelected = this._selectionModel.isSelected(option);
- if (option.value == null && !this._multiple) {
- option.deselect();
- this._selectionModel.clear();
- this._propagateChanges(option.value);
- }
- else {
- option.selected ? this._selectionModel.select(option) : this._selectionModel.deselect(option);
- if (isUserInput) {
- this._keyManager.setActiveItem(option);
- }
- if (this.multiple) {
- this._sortValues();
- if (isUserInput) {
- // In case the user selected the option with their mouse, we
- // want to restore focus back to the trigger, in order to
- // prevent the select keyboard controls from clashing with
- // the ones from `mat-option`.
- this.focus();
- }
- }
- }
- if (wasSelected !== this._selectionModel.isSelected(option)) {
- this._propagateChanges();
- }
- this.stateChanges.next();
- }
- /**
- * Sorts the selected values in the selected based on their order in the panel.
- * @private
- * @return {?}
- */
- _sortValues() {
- if (this.multiple) {
- /** @type {?} */
- const options = this.options.toArray();
- this._selectionModel.sort((/**
- * @param {?} a
- * @param {?} b
- * @return {?}
- */
- (a, b) => {
- return this.sortComparator ? this.sortComparator(a, b, options) :
- options.indexOf(a) - options.indexOf(b);
- }));
- this.stateChanges.next();
- }
- }
- /**
- * Emits change event to set the model value.
- * @private
- * @param {?=} fallbackValue
- * @return {?}
- */
- _propagateChanges(fallbackValue) {
- /** @type {?} */
- let valueToEmit = null;
- if (this.multiple) {
- valueToEmit = ((/** @type {?} */ (this.selected))).map((/**
- * @param {?} option
- * @return {?}
- */
- option => option.value));
- }
- else {
- valueToEmit = this.selected ? ((/** @type {?} */ (this.selected))).value : fallbackValue;
- }
- this._value = valueToEmit;
- this.valueChange.emit(valueToEmit);
- this._onChange(valueToEmit);
- this.selectionChange.emit(new MatSelectChange(this, valueToEmit));
- this._changeDetectorRef.markForCheck();
- }
- /**
- * Records option IDs to pass to the aria-owns property.
- * @private
- * @return {?}
- */
- _setOptionIds() {
- this._optionIds = this.options.map((/**
- * @param {?} option
- * @return {?}
- */
- option => option.id)).join(' ');
- }
- /**
- * Highlights the selected item. If no option is selected, it will highlight
- * the first item instead.
- * @private
- * @return {?}
- */
- _highlightCorrectOption() {
- if (this._keyManager) {
- if (this.empty) {
- this._keyManager.setFirstItemActive();
- }
- else {
- this._keyManager.setActiveItem(this._selectionModel.selected[0]);
- }
- }
- }
- /**
- * Scrolls the active option into view.
- * @private
- * @return {?}
- */
- _scrollActiveOptionIntoView() {
- /** @type {?} */
- const activeOptionIndex = this._keyManager.activeItemIndex || 0;
- /** @type {?} */
- const labelCount = _countGroupLabelsBeforeOption(activeOptionIndex, this.options, this.optionGroups);
- this.panel.nativeElement.scrollTop = _getOptionScrollPosition(activeOptionIndex + labelCount, this._getItemHeight(), this.panel.nativeElement.scrollTop, SELECT_PANEL_MAX_HEIGHT);
- }
- /**
- * Focuses the select element.
- * @param {?=} options
- * @return {?}
- */
- focus(options) {
- this._elementRef.nativeElement.focus(options);
- }
- /**
- * Gets the index of the provided option in the option list.
- * @private
- * @param {?} option
- * @return {?}
- */
- _getOptionIndex(option) {
- return this.options.reduce((/**
- * @param {?} result
- * @param {?} current
- * @param {?} index
- * @return {?}
- */
- (result, current, index) => {
- return result === undefined ? (option === current ? index : undefined) : result;
- }), undefined);
- }
- /**
- * Calculates the scroll position and x- and y-offsets of the overlay panel.
- * @private
- * @return {?}
- */
- _calculateOverlayPosition() {
- /** @type {?} */
- const itemHeight = this._getItemHeight();
- /** @type {?} */
- const items = this._getItemCount();
- /** @type {?} */
- const panelHeight = Math.min(items * itemHeight, SELECT_PANEL_MAX_HEIGHT);
- /** @type {?} */
- const scrollContainerHeight = items * itemHeight;
- // The farthest the panel can be scrolled before it hits the bottom
- /** @type {?} */
- const maxScroll = scrollContainerHeight - panelHeight;
- // If no value is selected we open the popup to the first item.
- /** @type {?} */
- let selectedOptionOffset = this.empty ? 0 : (/** @type {?} */ (this._getOptionIndex(this._selectionModel.selected[0])));
- selectedOptionOffset += _countGroupLabelsBeforeOption(selectedOptionOffset, this.options, this.optionGroups);
- // We must maintain a scroll buffer so the selected option will be scrolled to the
- // center of the overlay panel rather than the top.
- /** @type {?} */
- const scrollBuffer = panelHeight / 2;
- this._scrollTop = this._calculateOverlayScroll(selectedOptionOffset, scrollBuffer, maxScroll);
- this._offsetY = this._calculateOverlayOffsetY(selectedOptionOffset, scrollBuffer, maxScroll);
- this._checkOverlayWithinViewport(maxScroll);
- }
- /**
- * Calculates the scroll position of the select's overlay panel.
- *
- * Attempts to center the selected option in the panel. If the option is
- * too high or too low in the panel to be scrolled to the center, it clamps the
- * scroll position to the min or max scroll positions respectively.
- * @param {?} selectedIndex
- * @param {?} scrollBuffer
- * @param {?} maxScroll
- * @return {?}
- */
- _calculateOverlayScroll(selectedIndex, scrollBuffer, maxScroll) {
- /** @type {?} */
- const itemHeight = this._getItemHeight();
- /** @type {?} */
- const optionOffsetFromScrollTop = itemHeight * selectedIndex;
- /** @type {?} */
- const halfOptionHeight = itemHeight / 2;
- // Starts at the optionOffsetFromScrollTop, which scrolls the option to the top of the
- // scroll container, then subtracts the scroll buffer to scroll the option down to
- // the center of the overlay panel. Half the option height must be re-added to the
- // scrollTop so the option is centered based on its middle, not its top edge.
- /** @type {?} */
- const optimalScrollPosition = optionOffsetFromScrollTop - scrollBuffer + halfOptionHeight;
- return Math.min(Math.max(0, optimalScrollPosition), maxScroll);
- }
- /**
- * Returns the aria-label of the select component.
- * @return {?}
- */
- _getAriaLabel() {
- // If an ariaLabelledby value has been set by the consumer, the select should not overwrite the
- // `aria-labelledby` value by setting the ariaLabel to the placeholder.
- return this.ariaLabelledby ? null : this.ariaLabel || this.placeholder;
- }
- /**
- * Returns the aria-labelledby of the select component.
- * @return {?}
- */
- _getAriaLabelledby() {
- if (this.ariaLabelledby) {
- return this.ariaLabelledby;
- }
- // Note: we use `_getAriaLabel` here, because we want to check whether there's a
- // computed label. `this.ariaLabel` is only the user-specified label.
- if (!this._parentFormField || !this._parentFormField._hasFloatingLabel() ||
- this._getAriaLabel()) {
- return null;
- }
- return this._parentFormField._labelId || null;
- }
- /**
- * Determines the `aria-activedescendant` to be set on the host.
- * @return {?}
- */
- _getAriaActiveDescendant() {
- if (this.panelOpen && this._keyManager && this._keyManager.activeItem) {
- return this._keyManager.activeItem.id;
- }
- return null;
- }
- /**
- * Sets the x-offset of the overlay panel in relation to the trigger's top start corner.
- * This must be adjusted to align the selected option text over the trigger text when
- * the panel opens. Will change based on LTR or RTL text direction. Note that the offset
- * can't be calculated until the panel has been attached, because we need to know the
- * content width in order to constrain the panel within the viewport.
- * @private
- * @return {?}
- */
- _calculateOverlayOffsetX() {
- /** @type {?} */
- const overlayRect = this.overlayDir.overlayRef.overlayElement.getBoundingClientRect();
- /** @type {?} */
- const viewportSize = this._viewportRuler.getViewportSize();
- /** @type {?} */
- const isRtl = this._isRtl();
- /** @type {?} */
- const paddingWidth = this.multiple ? SELECT_MULTIPLE_PANEL_PADDING_X + SELECT_PANEL_PADDING_X :
- SELECT_PANEL_PADDING_X * 2;
- /** @type {?} */
- let offsetX;
- // Adjust the offset, depending on the option padding.
- if (this.multiple) {
- offsetX = SELECT_MULTIPLE_PANEL_PADDING_X;
- }
- else {
- /** @type {?} */
- let selected = this._selectionModel.selected[0] || this.options.first;
- offsetX = selected && selected.group ? SELECT_PANEL_INDENT_PADDING_X : SELECT_PANEL_PADDING_X;
- }
- // Invert the offset in LTR.
- if (!isRtl) {
- offsetX *= -1;
- }
- // Determine how much the select overflows on each side.
- /** @type {?} */
- const leftOverflow = 0 - (overlayRect.left + offsetX - (isRtl ? paddingWidth : 0));
- /** @type {?} */
- const rightOverflow = overlayRect.right + offsetX - viewportSize.width
- + (isRtl ? 0 : paddingWidth);
- // If the element overflows on either side, reduce the offset to allow it to fit.
- if (leftOverflow > 0) {
- offsetX += leftOverflow + SELECT_PANEL_VIEWPORT_PADDING;
- }
- else if (rightOverflow > 0) {
- offsetX -= rightOverflow + SELECT_PANEL_VIEWPORT_PADDING;
- }
- // Set the offset directly in order to avoid having to go through change detection and
- // potentially triggering "changed after it was checked" errors. Round the value to avoid
- // blurry content in some browsers.
- this.overlayDir.offsetX = Math.round(offsetX);
- this.overlayDir.overlayRef.updatePosition();
- }
- /**
- * Calculates the y-offset of the select's overlay panel in relation to the
- * top start corner of the trigger. It has to be adjusted in order for the
- * selected option to be aligned over the trigger when the panel opens.
- * @private
- * @param {?} selectedIndex
- * @param {?} scrollBuffer
- * @param {?} maxScroll
- * @return {?}
- */
- _calculateOverlayOffsetY(selectedIndex, scrollBuffer, maxScroll) {
- /** @type {?} */
- const itemHeight = this._getItemHeight();
- /** @type {?} */
- const optionHeightAdjustment = (itemHeight - this._triggerRect.height) / 2;
- /** @type {?} */
- const maxOptionsDisplayed = Math.floor(SELECT_PANEL_MAX_HEIGHT / itemHeight);
- /** @type {?} */
- let optionOffsetFromPanelTop;
- // Disable offset if requested by user by returning 0 as value to offset
- if (this._disableOptionCentering) {
- return 0;
- }
- if (this._scrollTop === 0) {
- optionOffsetFromPanelTop = selectedIndex * itemHeight;
- }
- else if (this._scrollTop === maxScroll) {
- /** @type {?} */
- const firstDisplayedIndex = this._getItemCount() - maxOptionsDisplayed;
- /** @type {?} */
- const selectedDisplayIndex = selectedIndex - firstDisplayedIndex;
- // The first item is partially out of the viewport. Therefore we need to calculate what
- // portion of it is shown in the viewport and account for it in our offset.
- /** @type {?} */
- let partialItemHeight = itemHeight - (this._getItemCount() * itemHeight - SELECT_PANEL_MAX_HEIGHT) % itemHeight;
- // Because the panel height is longer than the height of the options alone,
- // there is always extra padding at the top or bottom of the panel. When
- // scrolled to the very bottom, this padding is at the top of the panel and
- // must be added to the offset.
- optionOffsetFromPanelTop = selectedDisplayIndex * itemHeight + partialItemHeight;
- }
- else {
- // If the option was scrolled to the middle of the panel using a scroll buffer,
- // its offset will be the scroll buffer minus the half height that was added to
- // center it.
- optionOffsetFromPanelTop = scrollBuffer - itemHeight / 2;
- }
- // The final offset is the option's offset from the top, adjusted for the height difference,
- // multiplied by -1 to ensure that the overlay moves in the correct direction up the page.
- // The value is rounded to prevent some browsers from blurring the content.
- return Math.round(optionOffsetFromPanelTop * -1 - optionHeightAdjustment);
- }
- /**
- * Checks that the attempted overlay position will fit within the viewport.
- * If it will not fit, tries to adjust the scroll position and the associated
- * y-offset so the panel can open fully on-screen. If it still won't fit,
- * sets the offset back to 0 to allow the fallback position to take over.
- * @private
- * @param {?} maxScroll
- * @return {?}
- */
- _checkOverlayWithinViewport(maxScroll) {
- /** @type {?} */
- const itemHeight = this._getItemHeight();
- /** @type {?} */
- const viewportSize = this._viewportRuler.getViewportSize();
- /** @type {?} */
- const topSpaceAvailable = this._triggerRect.top - SELECT_PANEL_VIEWPORT_PADDING;
- /** @type {?} */
- const bottomSpaceAvailable = viewportSize.height - this._triggerRect.bottom - SELECT_PANEL_VIEWPORT_PADDING;
- /** @type {?} */
- const panelHeightTop = Math.abs(this._offsetY);
- /** @type {?} */
- const totalPanelHeight = Math.min(this._getItemCount() * itemHeight, SELECT_PANEL_MAX_HEIGHT);
- /** @type {?} */
- const panelHeightBottom = totalPanelHeight - panelHeightTop - this._triggerRect.height;
- if (panelHeightBottom > bottomSpaceAvailable) {
- this._adjustPanelUp(panelHeightBottom, bottomSpaceAvailable);
- }
- else if (panelHeightTop > topSpaceAvailable) {
- this._adjustPanelDown(panelHeightTop, topSpaceAvailable, maxScroll);
- }
- else {
- this._transformOrigin = this._getOriginBasedOnOption();
- }
- }
- /**
- * Adjusts the overlay panel up to fit in the viewport.
- * @private
- * @param {?} panelHeightBottom
- * @param {?} bottomSpaceAvailable
- * @return {?}
- */
- _adjustPanelUp(panelHeightBottom, bottomSpaceAvailable) {
- // Browsers ignore fractional scroll offsets, so we need to round.
- /** @type {?} */
- const distanceBelowViewport = Math.round(panelHeightBottom - bottomSpaceAvailable);
- // Scrolls the panel up by the distance it was extending past the boundary, then
- // adjusts the offset by that amount to move the panel up into the viewport.
- this._scrollTop -= distanceBelowViewport;
- this._offsetY -= distanceBelowViewport;
- this._transformOrigin = this._getOriginBasedOnOption();
- // If the panel is scrolled to the very top, it won't be able to fit the panel
- // by scrolling, so set the offset to 0 to allow the fallback position to take
- // effect.
- if (this._scrollTop <= 0) {
- this._scrollTop = 0;
- this._offsetY = 0;
- this._transformOrigin = `50% bottom 0px`;
- }
- }
- /**
- * Adjusts the overlay panel down to fit in the viewport.
- * @private
- * @param {?} panelHeightTop
- * @param {?} topSpaceAvailable
- * @param {?} maxScroll
- * @return {?}
- */
- _adjustPanelDown(panelHeightTop, topSpaceAvailable, maxScroll) {
- // Browsers ignore fractional scroll offsets, so we need to round.
- /** @type {?} */
- const distanceAboveViewport = Math.round(panelHeightTop - topSpaceAvailable);
- // Scrolls the panel down by the distance it was extending past the boundary, then
- // adjusts the offset by that amount to move the panel down into the viewport.
- this._scrollTop += distanceAboveViewport;
- this._offsetY += distanceAboveViewport;
- this._transformOrigin = this._getOriginBasedOnOption();
- // If the panel is scrolled to the very bottom, it won't be able to fit the
- // panel by scrolling, so set the offset to 0 to allow the fallback position
- // to take effect.
- if (this._scrollTop >= maxScroll) {
- this._scrollTop = maxScroll;
- this._offsetY = 0;
- this._transformOrigin = `50% top 0px`;
- return;
- }
- }
- /**
- * Sets the transform origin point based on the selected option.
- * @private
- * @return {?}
- */
- _getOriginBasedOnOption() {
- /** @type {?} */
- const itemHeight = this._getItemHeight();
- /** @type {?} */
- const optionHeightAdjustment = (itemHeight - this._triggerRect.height) / 2;
- /** @type {?} */
- const originY = Math.abs(this._offsetY) - optionHeightAdjustment + itemHeight / 2;
- return `50% ${originY}px 0px`;
- }
- /**
- * Calculates the amount of items in the select. This includes options and group labels.
- * @private
- * @return {?}
- */
- _getItemCount() {
- return this.options.length + this.optionGroups.length;
- }
- /**
- * Calculates the height of the select's options.
- * @private
- * @return {?}
- */
- _getItemHeight() {
- return this._triggerFontSize * SELECT_ITEM_HEIGHT_EM;
- }
- /**
- * Implemented as part of MatFormFieldControl.
- * \@docs-private
- * @param {?} ids
- * @return {?}
- */
- setDescribedByIds(ids) {
- this._ariaDescribedby = ids.join(' ');
- }
- /**
- * Implemented as part of MatFormFieldControl.
- * \@docs-private
- * @return {?}
- */
- onContainerClick() {
- this.focus();
- this.open();
- }
- /**
- * Implemented as part of MatFormFieldControl.
- * \@docs-private
- * @return {?}
- */
- get shouldLabelFloat() {
- return this._panelOpen || !this.empty;
- }
- }
- MatSelect.decorators = [
- { type: Component, args: [{selector: 'mat-select',
- exportAs: 'matSelect',
- template: "<div cdk-overlay-origin class=\"mat-select-trigger\" aria-hidden=\"true\" (click)=\"toggle()\" #origin=\"cdkOverlayOrigin\" #trigger><div class=\"mat-select-value\" [ngSwitch]=\"empty\"><span class=\"mat-select-placeholder\" *ngSwitchCase=\"true\">{{placeholder || '\u00A0'}}</span> <span class=\"mat-select-value-text\" *ngSwitchCase=\"false\" [ngSwitch]=\"!!customTrigger\"><span *ngSwitchDefault>{{triggerValue || '\u00A0'}}</span><ng-content select=\"mat-select-trigger\" *ngSwitchCase=\"true\"></ng-content></span></div><div class=\"mat-select-arrow-wrapper\"><div class=\"mat-select-arrow\"></div></div></div><ng-template cdk-connected-overlay cdkConnectedOverlayLockPosition cdkConnectedOverlayHasBackdrop cdkConnectedOverlayBackdropClass=\"cdk-overlay-transparent-backdrop\" [cdkConnectedOverlayScrollStrategy]=\"_scrollStrategy\" [cdkConnectedOverlayOrigin]=\"origin\" [cdkConnectedOverlayOpen]=\"panelOpen\" [cdkConnectedOverlayPositions]=\"_positions\" [cdkConnectedOverlayMinWidth]=\"_triggerRect?.width\" [cdkConnectedOverlayOffsetY]=\"_offsetY\" (backdropClick)=\"close()\" (attach)=\"_onAttached()\" (detach)=\"close()\"><div class=\"mat-select-panel-wrap\" [@transformPanelWrap]><div #panel class=\"mat-select-panel {{ _getPanelTheme() }}\" [ngClass]=\"panelClass\" [@transformPanel]=\"multiple ? 'showing-multiple' : 'showing'\" (@transformPanel.done)=\"_panelDoneAnimatingStream.next($event.toState)\" [style.transformOrigin]=\"_transformOrigin\" [style.font-size.px]=\"_triggerFontSize\" (keydown)=\"_handleKeydown($event)\"><ng-content></ng-content></div></div></ng-template>",
- styles: [".mat-select{display:inline-block;width:100%;outline:0}.mat-select-trigger{display:inline-table;cursor:pointer;position:relative;box-sizing:border-box}.mat-select-disabled .mat-select-trigger{-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:default}.mat-select-value{display:table-cell;max-width:0;width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.mat-select-value-text{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.mat-select-arrow-wrapper{display:table-cell;vertical-align:middle}.mat-form-field-appearance-fill .mat-select-arrow-wrapper{transform:translateY(-50%)}.mat-form-field-appearance-outline .mat-select-arrow-wrapper{transform:translateY(-25%)}.mat-form-field-appearance-standard.mat-form-field-has-label .mat-select:not(.mat-select-empty) .mat-select-arrow-wrapper{transform:translateY(-50%)}.mat-form-field-appearance-standard .mat-select.mat-select-empty .mat-select-arrow-wrapper{transition:transform .4s cubic-bezier(.25,.8,.25,1)}._mat-animation-noopable.mat-form-field-appearance-standard .mat-select.mat-select-empty .mat-select-arrow-wrapper{transition:none}.mat-select-arrow{width:0;height:0;border-left:5px solid transparent;border-right:5px solid transparent;border-top:5px solid;margin:0 4px}.mat-select-panel-wrap{flex-basis:100%}.mat-select-panel{min-width:112px;max-width:280px;overflow:auto;-webkit-overflow-scrolling:touch;padding-top:0;padding-bottom:0;max-height:256px;min-width:100%;border-radius:4px}@media (-ms-high-contrast:active){.mat-select-panel{outline:solid 1px}}.mat-select-panel .mat-optgroup-label,.mat-select-panel .mat-option{font-size:inherit;line-height:3em;height:3em}.mat-form-field-type-mat-select:not(.mat-form-field-disabled) .mat-form-field-flex{cursor:pointer}.mat-form-field-type-mat-select .mat-form-field-label{width:calc(100% - 18px)}.mat-select-placeholder{transition:color .4s .133s cubic-bezier(.25,.8,.25,1)}._mat-animation-noopable .mat-select-placeholder{transition:none}.mat-form-field-hide-placeholder .mat-select-placeholder{color:transparent;-webkit-text-fill-color:transparent;transition:none;display:block}"],
- inputs: ['disabled', 'disableRipple', 'tabIndex'],
- encapsulation: ViewEncapsulation.None,
- changeDetection: ChangeDetectionStrategy.OnPush,
- host: {
- 'role': 'listbox',
- '[attr.id]': 'id',
- '[attr.tabindex]': 'tabIndex',
- '[attr.aria-label]': '_getAriaLabel()',
- '[attr.aria-labelledby]': '_getAriaLabelledby()',
- '[attr.aria-required]': 'required.toString()',
- '[attr.aria-disabled]': 'disabled.toString()',
- '[attr.aria-invalid]': 'errorState',
- '[attr.aria-owns]': 'panelOpen ? _optionIds : null',
- '[attr.aria-multiselectable]': 'multiple',
- '[attr.aria-describedby]': '_ariaDescribedby || null',
- '[attr.aria-activedescendant]': '_getAriaActiveDescendant()',
- '[class.mat-select-disabled]': 'disabled',
- '[class.mat-select-invalid]': 'errorState',
- '[class.mat-select-required]': 'required',
- '[class.mat-select-empty]': 'empty',
- 'class': 'mat-select',
- '(keydown)': '_handleKeydown($event)',
- '(focus)': '_onFocus()',
- '(blur)': '_onBlur()',
- },
- animations: [
- matSelectAnimations.transformPanelWrap,
- matSelectAnimations.transformPanel
- ],
- providers: [
- { provide: MatFormFieldControl, useExisting: MatSelect },
- { provide: MAT_OPTION_PARENT_COMPONENT, useExisting: MatSelect }
- ],
- },] },
- ];
- /** @nocollapse */
- MatSelect.ctorParameters = () => [
- { type: ViewportRuler },
- { type: ChangeDetectorRef },
- { type: NgZone },
- { type: ErrorStateMatcher },
- { type: ElementRef },
- { type: Directionality, decorators: [{ type: Optional }] },
- { type: NgForm, decorators: [{ type: Optional }] },
- { type: FormGroupDirective, decorators: [{ type: Optional }] },
- { type: MatFormField, decorators: [{ type: Optional }] },
- { type: NgControl, decorators: [{ type: Self }, { type: Optional }] },
- { type: String, decorators: [{ type: Attribute, args: ['tabindex',] }] },
- { type: undefined, decorators: [{ type: Inject, args: [MAT_SELECT_SCROLL_STRATEGY,] }] },
- { type: LiveAnnouncer }
- ];
- MatSelect.propDecorators = {
- trigger: [{ type: ViewChild, args: ['trigger', { static: false },] }],
- panel: [{ type: ViewChild, args: ['panel', { static: false },] }],
- overlayDir: [{ type: ViewChild, args: [CdkConnectedOverlay, { static: false },] }],
- options: [{ type: ContentChildren, args: [MatOption, { descendants: true },] }],
- optionGroups: [{ type: ContentChildren, args: [MatOptgroup,] }],
- panelClass: [{ type: Input }],
- customTrigger: [{ type: ContentChild, args: [MatSelectTrigger, { static: false },] }],
- placeholder: [{ type: Input }],
- required: [{ type: Input }],
- multiple: [{ type: Input }],
- disableOptionCentering: [{ type: Input }],
- compareWith: [{ type: Input }],
- value: [{ type: Input }],
- ariaLabel: [{ type: Input, args: ['aria-label',] }],
- ariaLabelledby: [{ type: Input, args: ['aria-labelledby',] }],
- errorStateMatcher: [{ type: Input }],
- sortComparator: [{ type: Input }],
- id: [{ type: Input }],
- openedChange: [{ type: Output }],
- _openedStream: [{ type: Output, args: ['opened',] }],
- _closedStream: [{ type: Output, args: ['closed',] }],
- selectionChange: [{ type: Output }],
- valueChange: [{ type: Output }]
- };
- /**
- * @fileoverview added by tsickle
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
- */
- class MatSelectModule {
- }
- MatSelectModule.decorators = [
- { type: NgModule, args: [{
- imports: [
- CommonModule,
- OverlayModule,
- MatOptionModule,
- MatCommonModule,
- ],
- exports: [MatFormFieldModule, MatSelect, MatSelectTrigger, MatOptionModule, MatCommonModule],
- declarations: [MatSelect, MatSelectTrigger],
- providers: [MAT_SELECT_SCROLL_STRATEGY_PROVIDER]
- },] },
- ];
- /**
- * @fileoverview added by tsickle
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
- */
- /**
- * @fileoverview added by tsickle
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
- */
- export { MatSelectModule, MAT_SELECT_SCROLL_STRATEGY_PROVIDER_FACTORY, SELECT_PANEL_MAX_HEIGHT, SELECT_PANEL_PADDING_X, SELECT_PANEL_INDENT_PADDING_X, SELECT_ITEM_HEIGHT_EM, SELECT_MULTIPLE_PANEL_PADDING_X, SELECT_PANEL_VIEWPORT_PADDING, MAT_SELECT_SCROLL_STRATEGY, MAT_SELECT_SCROLL_STRATEGY_PROVIDER, MatSelectChange, MatSelectTrigger, MatSelect, matSelectAnimations, transformPanel, fadeInContent };
- //# sourceMappingURL=select.js.map
|