UNPKG

@angular/material

Version:
1,073 lines (1,070 loc) 62.9 kB
import { Overlay, CdkConnectedOverlay, OverlayModule } from '@angular/cdk/overlay'; import { CommonModule } from '@angular/common'; import { InjectionToken, Directive, EventEmitter, isDevMode, Component, ViewEncapsulation, ChangeDetectionStrategy, ChangeDetectorRef, NgZone, ElementRef, Optional, Self, Attribute, Inject, ViewChild, ContentChildren, Input, ContentChild, Output, NgModule } from '@angular/core'; import { mixinDisableRipple, mixinTabIndex, mixinDisabled, mixinErrorState, _countGroupLabelsBeforeOption, _getOptionScrollPosition, MAT_OPTION_PARENT_COMPONENT, ErrorStateMatcher, MatOption, MatOptgroup, MatOptionModule, MatCommonModule } from '@angular/material/core'; import { MatFormFieldControl, MatFormField, MatFormFieldModule } from '@angular/material/form-field'; import { __extends, __spread } from 'tslib'; import { ActiveDescendantKeyManager, LiveAnnouncer } from '@angular/cdk/a11y'; import { Directionality } from '@angular/cdk/bidi'; import { coerceBooleanProperty, coerceNumberProperty } from '@angular/cdk/coercion'; import { SelectionModel } from '@angular/cdk/collections'; import { DOWN_ARROW, UP_ARROW, LEFT_ARROW, RIGHT_ARROW, ENTER, SPACE, hasModifierKey, HOME, END, A } from '@angular/cdk/keycodes'; import { ViewportRuler } from '@angular/cdk/scrolling'; import { NgForm, FormGroupDirective, NgControl } from '@angular/forms'; import { Subject, defer, merge } from 'rxjs'; import { startWith, switchMap, take, filter, map, distinctUntilChanged, takeUntil } from 'rxjs/operators'; import { trigger, transition, query, animateChild, state, style, animate } from '@angular/animations'; /** * @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 */ /** * 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 */ var 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)', transform: 'scaleY(1)' })), state('showing-multiple', style({ opacity: 1, minWidth: 'calc(100% + 64px)', transform: 'scaleY(1)' })), transition('void => *', animate('120ms cubic-bezier(0, 0, 0.2, 1)')), transition('* => void', animate('100ms 25ms linear', style({ opacity: 0 }))) ]) }; /** * @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 */ /** * Returns an exception to be thrown when attempting to change a select's `multiple` option * after initialization. * @docs-private */ 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 */ 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. */ function getMatSelectNonFunctionValueError() { return Error('`compareWith` must be a function.'); } /** * @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 */ var nextUniqueId = 0; /** * The following style constants are necessary to save here in order * to properly calculate the alignment of the selected option over * the trigger element. */ /** The max height of the select's overlay panel */ var SELECT_PANEL_MAX_HEIGHT = 256; /** The panel's padding on the x-axis */ var SELECT_PANEL_PADDING_X = 16; /** The panel's x axis padding if it is indented (e.g. there is an option group). */ var SELECT_PANEL_INDENT_PADDING_X = SELECT_PANEL_PADDING_X * 2; /** The height of the select items in `em` units. */ var 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. */ var 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. */ var SELECT_PANEL_VIEWPORT_PADDING = 8; /** Injection token that determines the scroll handling while a select is open. */ var MAT_SELECT_SCROLL_STRATEGY = new InjectionToken('mat-select-scroll-strategy'); /** @docs-private */ function MAT_SELECT_SCROLL_STRATEGY_PROVIDER_FACTORY(overlay) { return function () { return overlay.scrollStrategies.reposition(); }; } /** @docs-private */ var 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. */ var MatSelectChange = /** @class */ (function () { function MatSelectChange( /** Reference to the select that emitted the change event. */ source, /** Current value of the select that emitted the event. */ value) { this.source = source; this.value = value; } return MatSelectChange; }()); // Boilerplate for applying mixins to MatSelect. /** @docs-private */ var MatSelectBase = /** @class */ (function () { function MatSelectBase(_elementRef, _defaultErrorStateMatcher, _parentForm, _parentFormGroup, ngControl) { this._elementRef = _elementRef; this._defaultErrorStateMatcher = _defaultErrorStateMatcher; this._parentForm = _parentForm; this._parentFormGroup = _parentFormGroup; this.ngControl = ngControl; } return MatSelectBase; }()); var _MatSelectMixinBase = mixinDisableRipple(mixinTabIndex(mixinDisabled(mixinErrorState(MatSelectBase)))); /** * Allows the user to customize the trigger that is displayed when the select has a value. */ var MatSelectTrigger = /** @class */ (function () { function MatSelectTrigger() { } MatSelectTrigger.decorators = [ { type: Directive, args: [{ selector: 'mat-select-trigger' },] } ]; return MatSelectTrigger; }()); var MatSelect = /** @class */ (function (_super) { __extends(MatSelect, _super); function MatSelect(_viewportRuler, _changeDetectorRef, _ngZone, _defaultErrorStateMatcher, elementRef, _dir, _parentForm, _parentFormGroup, _parentFormField, ngControl, tabIndex, scrollStrategyFactory, _liveAnnouncer) { var _this = _super.call(this, elementRef, _defaultErrorStateMatcher, _parentForm, _parentFormGroup, ngControl) || this; _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 = function (o1, o2) { return 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 = function () { }; /** `View -> model callback called when select has been touched` */ _this._onTouched = function () { }; /** 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 = defer(function () { var options = _this.options; if (options) { return options.changes.pipe(startWith(options), switchMap(function () { return merge.apply(void 0, __spread(options.map(function (option) { return option.onSelectionChange; }))); })); } return _this._ngZone.onStable .asObservable() .pipe(take(1), switchMap(function () { 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(function (o) { return o; }), map(function () { })); /** Event emitted when the select has been closed. */ _this._closedStream = _this.openedChange.pipe(filter(function (o) { return !o; }), map(function () { })); /** 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; return _this; } Object.defineProperty(MatSelect.prototype, "focused", { /** Whether the select is focused. */ get: function () { return this._focused || this._panelOpen; }, enumerable: true, configurable: true }); Object.defineProperty(MatSelect.prototype, "placeholder", { /** Placeholder to be shown if no value has been selected. */ get: function () { return this._placeholder; }, set: function (value) { this._placeholder = value; this.stateChanges.next(); }, enumerable: true, configurable: true }); Object.defineProperty(MatSelect.prototype, "required", { /** Whether the component is required. */ get: function () { return this._required; }, set: function (value) { this._required = coerceBooleanProperty(value); this.stateChanges.next(); }, enumerable: true, configurable: true }); Object.defineProperty(MatSelect.prototype, "multiple", { /** Whether the user should be allowed to select multiple options. */ get: function () { return this._multiple; }, set: function (value) { if (this._selectionModel) { throw getMatSelectDynamicMultipleError(); } this._multiple = coerceBooleanProperty(value); }, enumerable: true, configurable: true }); Object.defineProperty(MatSelect.prototype, "disableOptionCentering", { /** Whether to center the active option over the trigger. */ get: function () { return this._disableOptionCentering; }, set: function (value) { this._disableOptionCentering = coerceBooleanProperty(value); }, enumerable: true, configurable: true }); Object.defineProperty(MatSelect.prototype, "compareWith", { /** * 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. */ get: function () { return this._compareWith; }, set: function (fn) { if (typeof fn !== 'function') { throw getMatSelectNonFunctionValueError(); } this._compareWith = fn; if (this._selectionModel) { // A different comparator means the selection could change. this._initializeSelection(); } }, enumerable: true, configurable: true }); Object.defineProperty(MatSelect.prototype, "value", { /** Value of the select control. */ get: function () { return this._value; }, set: function (newValue) { if (newValue !== this._value) { this.writeValue(newValue); this._value = newValue; } }, enumerable: true, configurable: true }); Object.defineProperty(MatSelect.prototype, "typeaheadDebounceInterval", { /** Time to wait in milliseconds after the last keystroke before moving focus to an item. */ get: function () { return this._typeaheadDebounceInterval; }, set: function (value) { this._typeaheadDebounceInterval = coerceNumberProperty(value); }, enumerable: true, configurable: true }); Object.defineProperty(MatSelect.prototype, "id", { /** Unique id of the element. */ get: function () { return this._id; }, set: function (value) { this._id = value || this._uid; this.stateChanges.next(); }, enumerable: true, configurable: true }); MatSelect.prototype.ngOnInit = function () { var _this = this; 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(function () { 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(function () { if (_this._panelOpen) { _this._triggerRect = _this.trigger.nativeElement.getBoundingClientRect(); _this._changeDetectorRef.markForCheck(); } }); }; MatSelect.prototype.ngAfterContentInit = function () { var _this = this; this._initKeyManager(); this._selectionModel.changed.pipe(takeUntil(this._destroy)).subscribe(function (event) { event.added.forEach(function (option) { return option.select(); }); event.removed.forEach(function (option) { return option.deselect(); }); }); this.options.changes.pipe(startWith(null), takeUntil(this._destroy)).subscribe(function () { _this._resetOptions(); _this._initializeSelection(); }); }; MatSelect.prototype.ngDoCheck = function () { if (this.ngControl) { this.updateErrorState(); } }; MatSelect.prototype.ngOnChanges = function (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(); } if (changes['typeaheadDebounceInterval'] && this._keyManager) { this._keyManager.withTypeAhead(this._typeaheadDebounceInterval); } }; MatSelect.prototype.ngOnDestroy = function () { this._destroy.next(); this._destroy.complete(); this.stateChanges.complete(); }; /** Toggles the overlay panel open or closed. */ MatSelect.prototype.toggle = function () { this.panelOpen ? this.close() : this.open(); }; /** Opens the overlay panel. */ MatSelect.prototype.open = function () { var _this = this; 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(function () { 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. */ MatSelect.prototype.close = function () { 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. */ MatSelect.prototype.writeValue = function (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. */ MatSelect.prototype.registerOnChange = function (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. */ MatSelect.prototype.registerOnTouched = function (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. */ MatSelect.prototype.setDisabledState = function (isDisabled) { this.disabled = isDisabled; this._changeDetectorRef.markForCheck(); this.stateChanges.next(); }; Object.defineProperty(MatSelect.prototype, "panelOpen", { /** Whether or not the overlay panel is open. */ get: function () { return this._panelOpen; }, enumerable: true, configurable: true }); Object.defineProperty(MatSelect.prototype, "selected", { /** The currently selected option. */ get: function () { return this.multiple ? this._selectionModel.selected : this._selectionModel.selected[0]; }, enumerable: true, configurable: true }); Object.defineProperty(MatSelect.prototype, "triggerValue", { /** The value displayed in the trigger. */ get: function () { if (this.empty) { return ''; } if (this._multiple) { var selectedOptions = this._selectionModel.selected.map(function (option) { return 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; }, enumerable: true, configurable: true }); /** Whether the element is in RTL mode. */ MatSelect.prototype._isRtl = function () { return this._dir ? this._dir.value === 'rtl' : false; }; /** Handles all keydown events on the select. */ MatSelect.prototype._handleKeydown = function (event) { if (!this.disabled) { this.panelOpen ? this._handleOpenKeydown(event) : this._handleClosedKeydown(event); } }; /** Handles keyboard events while the select is closed. */ MatSelect.prototype._handleClosedKeydown = function (event) { var keyCode = event.keyCode; var isArrowKey = keyCode === DOWN_ARROW || keyCode === UP_ARROW || keyCode === LEFT_ARROW || keyCode === RIGHT_ARROW; var isOpenKey = keyCode === ENTER || keyCode === SPACE; var manager = this._keyManager; // Open the select on ALT + arrow key to match the native <select> if (!manager.isTyping() && (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) { var previouslySelectedOption = this.selected; if (keyCode === HOME || keyCode === END) { keyCode === HOME ? manager.setFirstItemActive() : manager.setLastItemActive(); event.preventDefault(); } else { manager.onKeydown(event); } var selectedOption = this.selected; // Since the value has changed, we need to announce it ourselves. if (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(selectedOption.viewValue, 10000); } } }; /** Handles keyboard events when the selected is open. */ MatSelect.prototype._handleOpenKeydown = function (event) { var manager = this._keyManager; var keyCode = event.keyCode; var isArrowKey = keyCode === DOWN_ARROW || keyCode === UP_ARROW; var isTyping = manager.isTyping(); 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(); // Don't do anything in this case if the user is typing, // because the typing sequence can include the space key. } else if (!isTyping && (keyCode === ENTER || keyCode === SPACE) && manager.activeItem && !hasModifierKey(event)) { event.preventDefault(); manager.activeItem._selectViaInteraction(); } else if (!isTyping && this._multiple && keyCode === A && event.ctrlKey) { event.preventDefault(); var hasDeselectedOptions_1 = this.options.some(function (opt) { return !opt.disabled && !opt.selected; }); this.options.forEach(function (option) { if (!option.disabled) { hasDeselectedOptions_1 ? option.select() : option.deselect(); } }); } else { var previouslyFocusedIndex = manager.activeItemIndex; manager.onKeydown(event); if (this._multiple && isArrowKey && event.shiftKey && manager.activeItem && manager.activeItemIndex !== previouslyFocusedIndex) { manager.activeItem._selectViaInteraction(); } } }; MatSelect.prototype._onFocus = function () { 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. */ MatSelect.prototype._onBlur = function () { 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. */ MatSelect.prototype._onAttached = function () { var _this = this; this.overlayDir.positionChange.pipe(take(1)).subscribe(function () { _this._changeDetectorRef.detectChanges(); _this._calculateOverlayOffsetX(); _this.panel.nativeElement.scrollTop = _this._scrollTop; }); }; /** Returns the theme to be used on the panel. */ MatSelect.prototype._getPanelTheme = function () { return this._parentFormField ? "mat-" + this._parentFormField.color : ''; }; Object.defineProperty(MatSelect.prototype, "empty", { /** Whether the select has a value. */ get: function () { return !this._selectionModel || this._selectionModel.isEmpty(); }, enumerable: true, configurable: true }); MatSelect.prototype._initializeSelection = function () { var _this = this; // Defer setting the value in order to avoid the "Expression // has changed after it was checked" errors from Angular. Promise.resolve().then(function () { _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. */ MatSelect.prototype._setSelectionByValue = function (value) { var _this = this; if (this.multiple && value) { if (!Array.isArray(value)) { throw getMatSelectNonArrayValueError(); } this._selectionModel.clear(); value.forEach(function (currentValue) { return _this._selectValue(currentValue); }); this._sortValues(); } else { this._selectionModel.clear(); var 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); } else if (!this.panelOpen) { // Otherwise reset the highlighted option. Note that we only want to do this while // closed, because doing it while open can shift the user's focus unnecessarily. this._keyManager.setActiveItem(-1); } } this._changeDetectorRef.markForCheck(); }; /** * Finds and selects and option based on its value. * @returns Option that has the corresponding value. */ MatSelect.prototype._selectValue = function (value) { var _this = this; var correspondingOption = this.options.find(function (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. */ MatSelect.prototype._initKeyManager = function () { var _this = this; this._keyManager = new ActiveDescendantKeyManager(this.options) .withTypeAhead(this._typeaheadDebounceInterval) .withVerticalOrientation() .withHorizontalOrientation(this._isRtl() ? 'rtl' : 'ltr') .withAllowedModifierKeys(['shiftKey']); this._keyManager.tabOut.pipe(takeUntil(this._destroy)).subscribe(function () { // Select the active item when tabbing away. This is consistent with how the native // select behaves. Note that we only want to do this in single selection mode. if (!_this.multiple && _this._keyManager.activeItem) { _this._keyManager.activeItem._selectViaInteraction(); } // 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(function () { 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. */ MatSelect.prototype._resetOptions = function () { var _this = this; var changedOrDestroyed = merge(this.options.changes, this._destroy); this.optionSelectionChanges.pipe(takeUntil(changedOrDestroyed)).subscribe(function (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.apply(void 0, __spread(this.options.map(function (option) { return option._stateChanges; }))).pipe(takeUntil(changedOrDestroyed)) .subscribe(function () { _this._changeDetectorRef.markForCheck(); _this.stateChanges.next(); }); this._setOptionIds(); }; /** Invoked when an option is clicked. */ MatSelect.prototype._onSelect = function (option, isUserInput) { var wasSelected = this._selectionModel.isSelected(option); if (option.value == null && !this._multiple) { option.deselect(); this._selectionModel.clear(); this._propagateChanges(option.value); } else { if (wasSelected !== option.selected) { 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. */ MatSelect.prototype._sortValues = function () { var _this = this; if (this.multiple) { var options_1 = this.options.toArray(); this._selectionModel.sort(function (a, b) { return _this.sortComparator ? _this.sortComparator(a, b, options_1) : options_1.indexOf(a) - options_1.indexOf(b); }); this.stateChanges.next(); } }; /** Emits change event to set the model value. */ MatSelect.prototype._propagateChanges = function (fallbackValue) { var valueToEmit = null; if (this.multiple) { valueToEmit = this.selected.map(function (option) { return option.value; }); } else { valueToEmit = this.selected ? 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. */ MatSelect.prototype._setOptionIds = function () { this._optionIds = this.options.map(function (option) { return option.id; }).join(' '); }; /** * Highlights the selected item. If no option is selected, it will highlight * the first item instead. */ MatSelect.prototype._highlightCorrectOption = function () { if (this._keyManager) { if (this.empty) { this._keyManager.setFirstItemActive(); } else { this._keyManager.setActiveItem(this._selectionModel.selected[0]); } } }; /** Scrolls the active option into view. */ MatSelect.prototype._scrollActiveOptionIntoView = function () { var activeOptionIndex = this._keyManager.activeItemIndex || 0; var 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. */ MatSelect.prototype.focus = function (options) { this._elementRef.nativeElement.focus(options); }; /** Gets the index of the provided option in the option list. */ MatSelect.prototype._getOptionIndex = function (option) { return this.options.reduce(function (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. */ MatSelect.prototype._calculateOverlayPosition = function () { var itemHeight = this._getItemHeight(); var items = this._getItemCount(); var panelHeight = Math.min(items * itemHeight, SELECT_PANEL_MAX_HEIGHT); var scrollContainerHeight = items * itemHeight; // The farthest the panel can be scrolled before it hits the bottom var maxScroll = scrollContainerHeight - panelHeight; // If no value is selected we open the popup to the first item. var selectedOptionOffset = this.empty ? 0 : 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. var 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. */ MatSelect.prototype._calculateOverlayScroll = function (selectedIndex, scrollBuffer, maxScroll) { var itemHeight = this._getItemHeight(); var optionOffsetFromScrollTop = itemHeight * selectedIndex; var 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. var optimalScrollPosition = optionOffsetFromScrollTop - scrollBuffer + halfOptionHeight; return Math.min(Math.max(0, optimalScrollPosition), maxScroll); }; /** Returns the aria-label of the select component. */ MatSelect.prototype._getAriaLabel = function () { // 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. */ MatSelect.prototype._getAriaLabelledby = function () { 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. */ MatSelect.prototype._getAriaActiveDescendant = function () { 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. */ MatSelect.prototype._calculateOverlayOffsetX = function () { var overlayRect = this.overlayDir.overlayRef.overlayElement.getBoundingClientRect(); var viewportSize = this._viewportRuler.getViewportSize(); var isRtl = this._isRtl(); var paddingWidth = this.multiple ? SELECT_MULTIPLE_PANEL_PADDING_X + SELECT_PANEL_PADDING_X : SELECT_PANEL_PADDING_X * 2; var offsetX; // Adjust the offset, depending on the option padding. if (this.multiple) { offsetX = SELECT_MULTIPLE_PANEL_PADDING_X; } else { var 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. var leftOverflow = 0 - (overlayRect.left + offsetX - (isRtl ? paddingWidth : 0)); var 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. */ MatSelect.prototype._calculateOverlayOffsetY = function (selectedIndex, scrollBuffer, maxScroll) { var itemHeight = this._getItemHeight(); var optionHeightAdjustment = (itemHeight - this._triggerRect.height) / 2; var maxOptionsDisplayed = Math.floor(SELECT_PANEL_MAX_HEIGHT / itemHeight); var 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) { var firstDisplayedIndex = this._getItemCount() - maxOptionsDisplayed; var 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. var 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. */ MatSelect.prototype._checkOverlayWithinViewport = function (maxScroll) { var itemHeight = this._getItemHeight(); var viewportSize = this._viewportRuler.getViewportSize(); var topSpaceAvailable = this._triggerRect.top - SELECT_PANEL_VIEWPORT_PADDING; var bottomSpaceAvailable = viewportSize.height - this._triggerRect.bottom - SELECT_PANEL_VIEWPORT_PADDING; var panelHeightTop = Math.abs(this._offsetY); var totalPanelHeight = Math.min(this._getItemCount() * itemHeight, SELECT_PANEL_MAX_HEIGHT); var 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. */ MatSelect.prototype._adjustPanelUp = function (panelHeightBottom, bottomSpaceAvailable) { // Browsers ignore fractional scroll offsets, so we need to round. var 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 -= distanceBelowViewp