2016-11-01 13:53:48 -07:00
|
|
|
import {
|
|
|
|
|
AfterContentInit,
|
|
|
|
|
Component,
|
|
|
|
|
ContentChildren,
|
|
|
|
|
ElementRef,
|
|
|
|
|
EventEmitter,
|
|
|
|
|
Input,
|
|
|
|
|
OnDestroy,
|
|
|
|
|
Optional,
|
|
|
|
|
Output,
|
|
|
|
|
QueryList,
|
|
|
|
|
Renderer,
|
2017-01-18 21:36:48 -08:00
|
|
|
Self,
|
2016-11-01 13:53:48 -07:00
|
|
|
ViewEncapsulation,
|
2016-12-02 13:34:33 -08:00
|
|
|
ViewChild,
|
2017-02-09 19:31:55 +01:00
|
|
|
ChangeDetectorRef,
|
2016-11-01 13:53:48 -07:00
|
|
|
} from '@angular/core';
|
2017-01-19 14:07:40 -08:00
|
|
|
import {MdOption, MdOptionSelectEvent} from '../core/option/option';
|
2016-11-01 13:53:48 -07:00
|
|
|
import {ENTER, SPACE} from '../core/keyboard/keycodes';
|
2017-01-18 21:33:06 -08:00
|
|
|
import {FocusKeyManager} from '../core/a11y/focus-key-manager';
|
2016-11-01 13:53:48 -07:00
|
|
|
import {Dir} from '../core/rtl/dir';
|
|
|
|
|
import {Subscription} from 'rxjs/Subscription';
|
|
|
|
|
import {transformPlaceholder, transformPanel, fadeInContent} from './select-animations';
|
2016-11-03 16:01:03 -07:00
|
|
|
import {ControlValueAccessor, NgControl} from '@angular/forms';
|
2016-12-14 03:55:37 +02:00
|
|
|
import {coerceBooleanProperty} from '../core/coercion/boolean-property';
|
2016-12-02 13:34:33 -08:00
|
|
|
import {ConnectedOverlayDirective} from '../core/overlay/overlay-directives';
|
|
|
|
|
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 fixed height of every option element. */
|
|
|
|
|
export const SELECT_OPTION_HEIGHT = 48;
|
|
|
|
|
|
|
|
|
|
/** The max height of the select's overlay panel */
|
|
|
|
|
export const SELECT_PANEL_MAX_HEIGHT = 256;
|
|
|
|
|
|
|
|
|
|
/** The max number of options visible at once in the select panel. */
|
|
|
|
|
export const SELECT_MAX_OPTIONS_DISPLAYED = 5;
|
|
|
|
|
|
|
|
|
|
/** The fixed height of the select's trigger element. */
|
|
|
|
|
export const SELECT_TRIGGER_HEIGHT = 30;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Must adjust for the difference in height between the option and the trigger,
|
|
|
|
|
* so the text will align on the y axis.
|
|
|
|
|
* (SELECT_OPTION_HEIGHT (48) - SELECT_TRIGGER_HEIGHT (30)) / 2 = 9
|
|
|
|
|
*/
|
|
|
|
|
export const SELECT_OPTION_HEIGHT_ADJUSTMENT = 9;
|
|
|
|
|
|
|
|
|
|
/** The panel's padding on the x-axis */
|
|
|
|
|
export const SELECT_PANEL_PADDING_X = 16;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The panel's padding on the y-axis. This padding indicates there are more
|
|
|
|
|
* options available if you scroll.
|
|
|
|
|
*/
|
|
|
|
|
export const SELECT_PANEL_PADDING_Y = 16;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The select panel will only "fit" inside the viewport if it is positioned at
|
|
|
|
|
* this value or more away from the viewport boundary.
|
|
|
|
|
*/
|
|
|
|
|
export const SELECT_PANEL_VIEWPORT_PADDING = 8;
|
2016-09-19 14:57:00 -07:00
|
|
|
|
2017-01-12 22:24:25 +01:00
|
|
|
/** Change event object that is emitted when the select value has changed. */
|
|
|
|
|
export class MdSelectChange {
|
|
|
|
|
constructor(public source: MdSelect, public value: any) { }
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-19 14:57:00 -07:00
|
|
|
@Component({
|
2016-10-25 12:35:35 -07:00
|
|
|
moduleId: module.id,
|
2016-12-02 13:32:59 -08:00
|
|
|
selector: 'md-select, mat-select',
|
2016-10-25 12:35:35 -07:00
|
|
|
templateUrl: 'select.html',
|
|
|
|
|
styleUrls: ['select.css'],
|
2016-11-01 13:53:48 -07:00
|
|
|
encapsulation: ViewEncapsulation.None,
|
|
|
|
|
host: {
|
|
|
|
|
'role': 'listbox',
|
2016-11-11 15:02:52 -08:00
|
|
|
'[attr.tabindex]': '_getTabIndex()',
|
2016-11-01 13:53:48 -07:00
|
|
|
'[attr.aria-label]': 'placeholder',
|
2016-11-03 16:01:03 -07:00
|
|
|
'[attr.aria-required]': 'required.toString()',
|
2016-11-11 15:02:52 -08:00
|
|
|
'[attr.aria-disabled]': 'disabled.toString()',
|
2016-11-03 16:01:03 -07:00
|
|
|
'[attr.aria-invalid]': '_control?.invalid || "false"',
|
2016-11-16 16:02:28 -08:00
|
|
|
'[attr.aria-owns]': '_optionIds',
|
2017-02-08 11:24:31 -08:00
|
|
|
'[class.mat-select-disabled]': 'disabled',
|
|
|
|
|
'[class.mat-select]': 'true',
|
2016-11-03 16:01:03 -07:00
|
|
|
'(keydown)': '_handleKeydown($event)',
|
|
|
|
|
'(blur)': '_onBlur()'
|
2016-11-01 13:53:48 -07:00
|
|
|
},
|
|
|
|
|
animations: [
|
|
|
|
|
transformPlaceholder,
|
|
|
|
|
transformPanel,
|
|
|
|
|
fadeInContent
|
|
|
|
|
],
|
2016-11-03 16:01:03 -07:00
|
|
|
exportAs: 'mdSelect',
|
2016-09-19 14:57:00 -07:00
|
|
|
})
|
2016-11-03 16:01:03 -07:00
|
|
|
export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestroy {
|
2016-11-01 13:53:48 -07:00
|
|
|
/** Whether or not the overlay panel is open. */
|
|
|
|
|
private _panelOpen = false;
|
|
|
|
|
|
|
|
|
|
/** The currently selected option. */
|
|
|
|
|
private _selected: MdOption;
|
|
|
|
|
|
|
|
|
|
/** Subscriptions to option events. */
|
|
|
|
|
private _subscriptions: Subscription[] = [];
|
|
|
|
|
|
|
|
|
|
/** Subscription to changes in the option list. */
|
|
|
|
|
private _changeSubscription: Subscription;
|
|
|
|
|
|
|
|
|
|
/** Subscription to tab events while overlay is focused. */
|
|
|
|
|
private _tabSubscription: Subscription;
|
|
|
|
|
|
2016-11-03 16:01:03 -07:00
|
|
|
/** Whether filling out the select is required in the form. */
|
|
|
|
|
private _required: boolean = false;
|
|
|
|
|
|
2016-11-11 15:02:52 -08:00
|
|
|
/** Whether the select is disabled. */
|
|
|
|
|
private _disabled: boolean = false;
|
|
|
|
|
|
2016-12-02 13:34:33 -08:00
|
|
|
/** The scroll position of the overlay panel, calculated to center the selected option. */
|
|
|
|
|
private _scrollTop = 0;
|
|
|
|
|
|
2016-12-06 19:15:49 -08:00
|
|
|
/** The placeholder displayed in the trigger of the select. */
|
|
|
|
|
private _placeholder: string;
|
|
|
|
|
|
2016-12-05 12:51:46 -08:00
|
|
|
/** The animation state of the placeholder. */
|
|
|
|
|
_placeholderState = '';
|
|
|
|
|
|
2016-12-06 19:15:49 -08:00
|
|
|
/**
|
|
|
|
|
* The width of the trigger. Must be saved to set the min width of the overlay panel
|
|
|
|
|
* and the width of the selected value.
|
|
|
|
|
*/
|
|
|
|
|
_triggerWidth: number;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The width of the selected option's value. Must be set programmatically
|
|
|
|
|
* to ensure its overflow is clipped, as it's absolutely positioned.
|
|
|
|
|
*/
|
|
|
|
|
_selectedValueWidth: number;
|
|
|
|
|
|
2016-11-01 13:53:48 -07:00
|
|
|
/** Manages keyboard events for options in the panel. */
|
2017-01-18 21:33:06 -08:00
|
|
|
_keyManager: FocusKeyManager;
|
2016-11-01 13:53:48 -07:00
|
|
|
|
2016-11-03 16:01:03 -07:00
|
|
|
/** View -> model callback called when value changes */
|
2016-12-02 13:34:33 -08:00
|
|
|
_onChange = (value: any) => {};
|
2016-11-03 16:01:03 -07:00
|
|
|
|
|
|
|
|
/** View -> model callback called when select has been touched */
|
2016-11-16 16:02:28 -08:00
|
|
|
_onTouched = () => {};
|
|
|
|
|
|
|
|
|
|
/** The IDs of child options to be passed to the aria-owns attribute. */
|
|
|
|
|
_optionIds: string = '';
|
2016-11-03 16:01:03 -07:00
|
|
|
|
2016-11-16 10:43:11 -08:00
|
|
|
/** The value of the select panel's transform-origin property. */
|
|
|
|
|
_transformOrigin: string = 'top';
|
|
|
|
|
|
2017-01-12 22:23:25 +01:00
|
|
|
/** Whether the panel's animation is done. */
|
|
|
|
|
_panelDoneAnimating: boolean = false;
|
|
|
|
|
|
2016-12-02 13:34:33 -08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
_offsetX = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
_offsetY = 0;
|
|
|
|
|
|
2016-11-16 10:43:11 -08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
2016-11-01 13:53:48 -07:00
|
|
|
*/
|
2016-11-16 10:43:11 -08:00
|
|
|
_positions = [
|
|
|
|
|
{
|
|
|
|
|
originX: 'start',
|
|
|
|
|
originY: 'top',
|
|
|
|
|
overlayX: 'start',
|
|
|
|
|
overlayY: 'top',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
originX: 'start',
|
|
|
|
|
originY: 'bottom',
|
|
|
|
|
overlayX: 'start',
|
|
|
|
|
overlayY: 'bottom',
|
|
|
|
|
},
|
|
|
|
|
];
|
2016-11-01 13:53:48 -07:00
|
|
|
|
2016-12-21 20:16:31 +02:00
|
|
|
/** Trigger that opens the select. */
|
2016-11-01 13:53:48 -07:00
|
|
|
@ViewChild('trigger') trigger: ElementRef;
|
2016-12-21 20:16:31 +02:00
|
|
|
|
|
|
|
|
/** Overlay pane containing the options. */
|
2016-12-02 13:34:33 -08:00
|
|
|
@ViewChild(ConnectedOverlayDirective) overlayDir: ConnectedOverlayDirective;
|
2016-12-21 20:16:31 +02:00
|
|
|
|
|
|
|
|
/** All of the defined select options. */
|
2016-11-01 13:53:48 -07:00
|
|
|
@ContentChildren(MdOption) options: QueryList<MdOption>;
|
|
|
|
|
|
2016-12-21 20:16:31 +02:00
|
|
|
/** Placeholder to be shown if no value has been selected. */
|
2016-12-06 19:15:49 -08:00
|
|
|
@Input()
|
2016-12-21 20:16:31 +02:00
|
|
|
get placeholder() { return this._placeholder; }
|
2016-12-06 19:15:49 -08:00
|
|
|
set placeholder(value: string) {
|
|
|
|
|
this._placeholder = value;
|
|
|
|
|
|
|
|
|
|
// Must wait to record the trigger width to ensure placeholder width is included.
|
|
|
|
|
Promise.resolve(null).then(() => this._triggerWidth = this._getWidth());
|
|
|
|
|
}
|
2016-11-03 16:01:03 -07:00
|
|
|
|
2016-12-21 20:16:31 +02:00
|
|
|
/** Whether the component is disabled. */
|
2016-11-11 15:02:52 -08:00
|
|
|
@Input()
|
2016-12-21 20:16:31 +02:00
|
|
|
get disabled() { return this._disabled; }
|
2016-11-11 15:02:52 -08:00
|
|
|
set disabled(value: any) {
|
|
|
|
|
this._disabled = coerceBooleanProperty(value);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-21 20:16:31 +02:00
|
|
|
/** Whether the component is required. */
|
2016-11-03 16:01:03 -07:00
|
|
|
@Input()
|
2016-12-21 20:16:31 +02:00
|
|
|
get required() { return this._required; }
|
|
|
|
|
set required(value: any) { this._required = coerceBooleanProperty(value); }
|
2016-11-03 16:01:03 -07:00
|
|
|
|
2016-12-21 20:16:31 +02:00
|
|
|
/** Event emitted when the select has been opened. */
|
2017-01-12 22:24:25 +01:00
|
|
|
@Output() onOpen: EventEmitter<void> = new EventEmitter<void>();
|
2016-12-21 20:16:31 +02:00
|
|
|
|
|
|
|
|
/** Event emitted when the select has been closed. */
|
2017-01-12 22:24:25 +01:00
|
|
|
@Output() onClose: EventEmitter<void> = new EventEmitter<void>();
|
|
|
|
|
|
|
|
|
|
/** Event emitted when the selected value has been changed by the user. */
|
|
|
|
|
@Output() change: EventEmitter<MdSelectChange> = new EventEmitter<MdSelectChange>();
|
2016-11-01 13:53:48 -07:00
|
|
|
|
|
|
|
|
constructor(private _element: ElementRef, private _renderer: Renderer,
|
2017-02-09 19:31:55 +01:00
|
|
|
private _viewportRuler: ViewportRuler, private _changeDetectorRef: ChangeDetectorRef,
|
|
|
|
|
@Optional() private _dir: Dir, @Self() @Optional() public _control: NgControl) {
|
2016-11-16 16:02:28 -08:00
|
|
|
if (this._control) {
|
|
|
|
|
this._control.valueAccessor = this;
|
|
|
|
|
}
|
2016-11-03 16:01:03 -07:00
|
|
|
}
|
2016-11-01 13:53:48 -07:00
|
|
|
|
|
|
|
|
ngAfterContentInit() {
|
|
|
|
|
this._initKeyManager();
|
2016-11-16 16:02:28 -08:00
|
|
|
this._resetOptions();
|
2017-01-13 23:55:41 +01:00
|
|
|
this._changeSubscription = this.options.changes.subscribe(() => {
|
|
|
|
|
this._resetOptions();
|
|
|
|
|
|
|
|
|
|
if (this._control) {
|
|
|
|
|
// Defer setting the value in order to avoid the "Expression
|
|
|
|
|
// has changed after it was checked" errors from Angular.
|
|
|
|
|
Promise.resolve(null).then(() => this._setSelectionByValue(this._control.value));
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
this._dropSubscriptions();
|
|
|
|
|
this._changeSubscription.unsubscribe();
|
|
|
|
|
this._tabSubscription.unsubscribe();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Toggles the overlay panel open or closed. */
|
|
|
|
|
toggle(): void {
|
|
|
|
|
this.panelOpen ? this.close() : this.open();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Opens the overlay panel. */
|
|
|
|
|
open(): void {
|
2016-11-11 15:02:52 -08:00
|
|
|
if (this.disabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-12-02 13:34:33 -08:00
|
|
|
this._calculateOverlayPosition();
|
2016-12-05 12:51:46 -08:00
|
|
|
this._placeholderState = this._isRtl() ? 'floating-rtl' : 'floating-ltr';
|
2016-11-01 13:53:48 -07:00
|
|
|
this._panelOpen = true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 15:54:56 -07:00
|
|
|
/** Closes the overlay panel and focuses the host element. */
|
2016-11-01 13:53:48 -07:00
|
|
|
close(): void {
|
|
|
|
|
this._panelOpen = false;
|
2016-12-05 12:51:46 -08:00
|
|
|
if (!this._selected) {
|
|
|
|
|
this._placeholderState = '';
|
|
|
|
|
}
|
2016-11-03 15:54:56 -07:00
|
|
|
this._focusHost();
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-03 16:01:03 -07:00
|
|
|
/**
|
|
|
|
|
* Sets the select's value. Part of the ControlValueAccessor interface
|
|
|
|
|
* required to integrate with Angular's core forms API.
|
2016-12-21 20:16:31 +02:00
|
|
|
*
|
|
|
|
|
* @param value New value to be written to the model.
|
2016-11-03 16:01:03 -07:00
|
|
|
*/
|
|
|
|
|
writeValue(value: any): void {
|
2016-11-30 17:08:56 -08:00
|
|
|
if (!this.options) {
|
|
|
|
|
// In reactive forms, writeValue() will be called synchronously before
|
|
|
|
|
// the select's child options have been created. It's necessary to call
|
|
|
|
|
// writeValue() again after the options have been created to ensure any
|
|
|
|
|
// initial view value is set.
|
2016-12-05 12:51:46 -08:00
|
|
|
Promise.resolve(null).then(() => this.writeValue(value));
|
2016-11-30 17:08:56 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2016-11-03 16:01:03 -07:00
|
|
|
|
2016-12-17 04:34:17 +08:00
|
|
|
this._setSelectionByValue(value);
|
2017-02-09 19:31:55 +01:00
|
|
|
this._changeDetectorRef.markForCheck();
|
2016-11-03 16:01:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
2016-12-21 20:16:31 +02:00
|
|
|
*
|
|
|
|
|
* @param fn Callback to be triggered when the value changes.
|
2016-11-03 16:01:03 -07:00
|
|
|
*/
|
|
|
|
|
registerOnChange(fn: (value: any) => void): void {
|
|
|
|
|
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.
|
2016-12-21 20:16:31 +02:00
|
|
|
*
|
|
|
|
|
* @param fn Callback to be triggered when the component has been touched.
|
2016-11-03 16:01:03 -07:00
|
|
|
*/
|
2016-11-16 16:02:28 -08:00
|
|
|
registerOnTouched(fn: () => {}): void {
|
2016-11-03 16:01:03 -07:00
|
|
|
this._onTouched = fn;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-11 15:02:52 -08:00
|
|
|
/**
|
|
|
|
|
* Disables the select. Part of the ControlValueAccessor interface required
|
|
|
|
|
* to integrate with Angular's core forms API.
|
2016-12-21 20:16:31 +02:00
|
|
|
*
|
|
|
|
|
* @param isDisabled Sets whether the component is disabled.
|
2016-11-11 15:02:52 -08:00
|
|
|
*/
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 13:53:48 -07:00
|
|
|
/** Whether or not the overlay panel is open. */
|
|
|
|
|
get panelOpen(): boolean {
|
|
|
|
|
return this._panelOpen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The currently selected option. */
|
|
|
|
|
get selected(): MdOption {
|
|
|
|
|
return this._selected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_isRtl(): boolean {
|
|
|
|
|
return this._dir ? this._dir.value === 'rtl' : false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The width of the trigger element. This is necessary to match
|
|
|
|
|
* the overlay width to the trigger width.
|
|
|
|
|
*/
|
|
|
|
|
_getWidth(): number {
|
2016-12-02 13:34:33 -08:00
|
|
|
return this._getTriggerRect().width;
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Ensures the panel opens if activated by the keyboard. */
|
2016-11-03 16:01:03 -07:00
|
|
|
_handleKeydown(event: KeyboardEvent): void {
|
2016-11-01 13:53:48 -07:00
|
|
|
if (event.keyCode === ENTER || event.keyCode === SPACE) {
|
|
|
|
|
this.open();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 15:54:56 -07:00
|
|
|
/**
|
2017-01-17 21:28:03 -08:00
|
|
|
* When the panel element is finished transforming in (though not fading in), it
|
|
|
|
|
* emits an event and focuses an option if the panel is open.
|
2016-11-03 15:54:56 -07:00
|
|
|
*/
|
2016-11-01 13:53:48 -07:00
|
|
|
_onPanelDone(): void {
|
|
|
|
|
if (this.panelOpen) {
|
|
|
|
|
this._focusCorrectOption();
|
|
|
|
|
this.onOpen.emit();
|
|
|
|
|
} else {
|
|
|
|
|
this.onClose.emit();
|
|
|
|
|
}
|
2017-01-17 21:28:03 -08:00
|
|
|
}
|
2017-01-12 22:23:25 +01:00
|
|
|
|
2017-01-17 21:28:03 -08:00
|
|
|
/**
|
|
|
|
|
* When the panel content is done fading in, the _panelDoneAnimating property is
|
|
|
|
|
* set so the proper class can be added to the panel.
|
|
|
|
|
*/
|
|
|
|
|
_onFadeInDone(): void {
|
2017-01-12 22:23:25 +01:00
|
|
|
this._panelDoneAnimating = this.panelOpen;
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-03 16:01:03 -07:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
_onBlur() {
|
|
|
|
|
if (!this.panelOpen) {
|
|
|
|
|
this._onTouched();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-11 15:02:52 -08:00
|
|
|
/** Returns the correct tabindex for the select depending on disabled state. */
|
|
|
|
|
_getTabIndex() {
|
|
|
|
|
return this.disabled ? '-1' : '0';
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-02 13:34:33 -08:00
|
|
|
|
2016-11-16 10:43:11 -08:00
|
|
|
/**
|
2016-12-02 13:34:33 -08:00
|
|
|
* Sets the scroll position of the scroll container. This must be called after
|
|
|
|
|
* the overlay pane is attached or the scroll container element will not yet be
|
|
|
|
|
* present in the DOM.
|
2016-11-16 10:43:11 -08:00
|
|
|
*/
|
2016-12-02 13:34:33 -08:00
|
|
|
_setScrollTop(): void {
|
|
|
|
|
const scrollContainer =
|
2017-02-08 11:24:31 -08:00
|
|
|
this.overlayDir.overlayRef.overlayElement.querySelector('.mat-select-panel');
|
2016-12-02 13:34:33 -08:00
|
|
|
scrollContainer.scrollTop = this._scrollTop;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-17 04:34:17 +08:00
|
|
|
/**
|
|
|
|
|
* Sets the selected option based on a value. If no option can be
|
|
|
|
|
* found with the designated value, the select trigger is cleared.
|
|
|
|
|
*/
|
|
|
|
|
private _setSelectionByValue(value: any): void {
|
|
|
|
|
const options = this.options.toArray();
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < this.options.length; i++) {
|
|
|
|
|
if (options[i].value === value) {
|
|
|
|
|
options[i].select();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear selection if no item was selected.
|
|
|
|
|
this._clearSelection();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Clears the select trigger and deselects every option in the list. */
|
|
|
|
|
private _clearSelection(): void {
|
|
|
|
|
this._selected = null;
|
|
|
|
|
this._updateOptions();
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-02 13:34:33 -08:00
|
|
|
private _getTriggerRect(): ClientRect {
|
|
|
|
|
return this.trigger.nativeElement.getBoundingClientRect();
|
2016-11-16 10:43:11 -08:00
|
|
|
}
|
|
|
|
|
|
2016-11-01 13:53:48 -07:00
|
|
|
/** Sets up a key manager to listen to keyboard events on the overlay panel. */
|
|
|
|
|
private _initKeyManager() {
|
2017-01-18 21:33:06 -08:00
|
|
|
this._keyManager = new FocusKeyManager(this.options);
|
2016-11-01 13:53:48 -07:00
|
|
|
this._tabSubscription = this._keyManager.tabOut.subscribe(() => {
|
|
|
|
|
this.close();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-16 16:02:28 -08:00
|
|
|
/** Drops current option subscriptions and IDs and resets from scratch. */
|
|
|
|
|
private _resetOptions(): void {
|
|
|
|
|
this._dropSubscriptions();
|
|
|
|
|
this._listenToOptions();
|
|
|
|
|
this._setOptionIds();
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 13:53:48 -07:00
|
|
|
/** Listens to selection events on each option. */
|
|
|
|
|
private _listenToOptions(): void {
|
|
|
|
|
this.options.forEach((option: MdOption) => {
|
2017-01-19 14:07:40 -08:00
|
|
|
const sub = option.onSelect.subscribe((event: MdOptionSelectEvent) => {
|
|
|
|
|
if (event.isUserInput && this._selected !== option) {
|
2017-01-12 22:24:25 +01:00
|
|
|
this._emitChangeEvent(option);
|
2016-11-03 16:01:03 -07:00
|
|
|
}
|
|
|
|
|
this._onSelect(option);
|
|
|
|
|
});
|
2016-11-01 13:53:48 -07:00
|
|
|
this._subscriptions.push(sub);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Unsubscribes from all option subscriptions. */
|
|
|
|
|
private _dropSubscriptions(): void {
|
|
|
|
|
this._subscriptions.forEach((sub: Subscription) => sub.unsubscribe());
|
|
|
|
|
this._subscriptions = [];
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-12 22:24:25 +01:00
|
|
|
/** Emits an event when the user selects an option. */
|
|
|
|
|
private _emitChangeEvent(option: MdOption): void {
|
|
|
|
|
this._onChange(option.value);
|
|
|
|
|
this.change.emit(new MdSelectChange(this, option.value));
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-16 16:02:28 -08:00
|
|
|
/** Records option IDs to pass to the aria-owns property. */
|
|
|
|
|
private _setOptionIds() {
|
|
|
|
|
this._optionIds = this.options.map(option => option.id).join(' ');
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 13:53:48 -07:00
|
|
|
/** When a new option is selected, deselects the others and closes the panel. */
|
|
|
|
|
private _onSelect(option: MdOption): void {
|
|
|
|
|
this._selected = option;
|
|
|
|
|
this._updateOptions();
|
2016-12-06 19:15:49 -08:00
|
|
|
this._setValueWidth();
|
2016-12-17 04:34:17 +08:00
|
|
|
this._placeholderState = '';
|
2016-12-05 12:51:46 -08:00
|
|
|
if (this.panelOpen) {
|
|
|
|
|
this.close();
|
|
|
|
|
}
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Deselect each option that doesn't match the current selection. */
|
|
|
|
|
private _updateOptions(): void {
|
|
|
|
|
this.options.forEach((option: MdOption) => {
|
|
|
|
|
if (option !== this.selected) {
|
|
|
|
|
option.deselect();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-06 19:15:49 -08:00
|
|
|
/**
|
|
|
|
|
* Must set the width of the selected option's value programmatically
|
|
|
|
|
* because it is absolutely positioned and otherwise will not clip
|
|
|
|
|
* overflow. The selection arrow is 9px wide, add 4px of padding = 13
|
|
|
|
|
*/
|
|
|
|
|
private _setValueWidth() {
|
|
|
|
|
this._selectedValueWidth = this._triggerWidth - 13;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 13:53:48 -07:00
|
|
|
/** Focuses the selected item. If no option is selected, it will focus
|
|
|
|
|
* the first item instead.
|
|
|
|
|
*/
|
|
|
|
|
private _focusCorrectOption(): void {
|
|
|
|
|
if (this.selected) {
|
2017-01-18 21:33:06 -08:00
|
|
|
this._keyManager.setActiveItem(this._getOptionIndex(this.selected));
|
2016-11-01 13:53:48 -07:00
|
|
|
} else {
|
2017-01-18 21:33:06 -08:00
|
|
|
this._keyManager.setFirstItemActive();
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Focuses the host element when the panel closes. */
|
|
|
|
|
private _focusHost(): void {
|
|
|
|
|
this._renderer.invokeElementMethod(this._element.nativeElement, 'focus');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Gets the index of the provided option in the option list. */
|
|
|
|
|
private _getOptionIndex(option: MdOption): number {
|
|
|
|
|
return this.options.reduce((result: number, current: MdOption, index: number) => {
|
|
|
|
|
return result === undefined ? (option === current ? index : undefined) : result;
|
|
|
|
|
}, undefined);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-02 13:34:33 -08:00
|
|
|
/** Calculates the scroll position and x- and y-offsets of the overlay panel. */
|
|
|
|
|
private _calculateOverlayPosition(): void {
|
|
|
|
|
this._offsetX = this._isRtl() ? SELECT_PANEL_PADDING_X : -SELECT_PANEL_PADDING_X;
|
|
|
|
|
|
|
|
|
|
const panelHeight =
|
|
|
|
|
Math.min(this.options.length * SELECT_OPTION_HEIGHT, SELECT_PANEL_MAX_HEIGHT);
|
|
|
|
|
const scrollContainerHeight = this.options.length * SELECT_OPTION_HEIGHT;
|
|
|
|
|
|
|
|
|
|
// The farthest the panel can be scrolled before it hits the bottom
|
|
|
|
|
const maxScroll = scrollContainerHeight - panelHeight;
|
|
|
|
|
|
|
|
|
|
if (this.selected) {
|
|
|
|
|
const selectedIndex = this._getOptionIndex(this.selected);
|
|
|
|
|
// We must maintain a scroll buffer so the selected option will be scrolled to the
|
|
|
|
|
// center of the overlay panel rather than the top.
|
|
|
|
|
const scrollBuffer = panelHeight / 2;
|
|
|
|
|
this._scrollTop = this._calculateOverlayScroll(selectedIndex, scrollBuffer, maxScroll);
|
|
|
|
|
this._offsetY = this._calculateOverlayOffset(selectedIndex, scrollBuffer, maxScroll);
|
|
|
|
|
} else {
|
|
|
|
|
// If no option is selected, the panel centers on the first option. In this case,
|
|
|
|
|
// we must only adjust for the height difference between the option element
|
|
|
|
|
// and the trigger element, then multiply it by -1 to ensure the panel moves
|
|
|
|
|
// in the correct direction up the page.
|
|
|
|
|
this._offsetY = (SELECT_OPTION_HEIGHT - SELECT_TRIGGER_HEIGHT) / 2 * -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
_calculateOverlayScroll(selectedIndex: number, scrollBuffer: number,
|
|
|
|
|
maxScroll: number): number {
|
|
|
|
|
const optionOffsetFromScrollTop = SELECT_OPTION_HEIGHT * selectedIndex;
|
|
|
|
|
const halfOptionHeight = SELECT_OPTION_HEIGHT / 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.
|
|
|
|
|
const optimalScrollPosition = optionOffsetFromScrollTop - scrollBuffer + halfOptionHeight;
|
|
|
|
|
return clampValue(0, optimalScrollPosition, maxScroll);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 _calculateOverlayOffset(selectedIndex: number, scrollBuffer: number,
|
|
|
|
|
maxScroll: number): number {
|
|
|
|
|
let optionOffsetFromPanelTop: number;
|
|
|
|
|
|
|
|
|
|
if (this._scrollTop === 0) {
|
|
|
|
|
optionOffsetFromPanelTop = selectedIndex * SELECT_OPTION_HEIGHT;
|
|
|
|
|
} else if (this._scrollTop === maxScroll) {
|
|
|
|
|
const firstDisplayedIndex = this.options.length - SELECT_MAX_OPTIONS_DISPLAYED;
|
|
|
|
|
const selectedDisplayIndex = selectedIndex - firstDisplayedIndex;
|
|
|
|
|
|
|
|
|
|
// 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 * SELECT_OPTION_HEIGHT + SELECT_PANEL_PADDING_Y;
|
|
|
|
|
} 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 - SELECT_OPTION_HEIGHT / 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.
|
|
|
|
|
return optionOffsetFromPanelTop * -1 - SELECT_OPTION_HEIGHT_ADJUSTMENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 _checkOverlayWithinViewport(maxScroll: number): void {
|
|
|
|
|
const viewportRect = this._viewportRuler.getViewportRect();
|
|
|
|
|
const triggerRect = this._getTriggerRect();
|
|
|
|
|
|
2016-12-15 06:30:28 +08:00
|
|
|
const topSpaceAvailable = triggerRect.top - SELECT_PANEL_VIEWPORT_PADDING;
|
2016-12-02 13:34:33 -08:00
|
|
|
const bottomSpaceAvailable =
|
2016-12-15 06:30:28 +08:00
|
|
|
viewportRect.height - triggerRect.bottom - SELECT_PANEL_VIEWPORT_PADDING;
|
2016-12-02 13:34:33 -08:00
|
|
|
|
|
|
|
|
const panelHeightTop = Math.abs(this._offsetY);
|
|
|
|
|
const totalPanelHeight =
|
|
|
|
|
Math.min(this.options.length * SELECT_OPTION_HEIGHT, SELECT_PANEL_MAX_HEIGHT);
|
|
|
|
|
const panelHeightBottom = totalPanelHeight - panelHeightTop - 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 _adjustPanelUp(panelHeightBottom: number, bottomSpaceAvailable: number) {
|
|
|
|
|
const distanceBelowViewport = 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 _adjustPanelDown(panelHeightTop: number, topSpaceAvailable: number,
|
|
|
|
|
maxScroll: number) {
|
|
|
|
|
const distanceAboveViewport = 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 _getOriginBasedOnOption(): string {
|
|
|
|
|
const originY =
|
|
|
|
|
Math.abs(this._offsetY) - SELECT_OPTION_HEIGHT_ADJUSTMENT + SELECT_OPTION_HEIGHT / 2;
|
|
|
|
|
return `50% ${originY}px 0px`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Clamps a value n between min and max values. */
|
|
|
|
|
function clampValue(min: number, n: number, max: number): number {
|
|
|
|
|
return Math.min(Math.max(min, n), max);
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|