2016-11-01 13:53:48 -07:00
|
|
|
import {
|
|
|
|
|
AfterContentInit,
|
|
|
|
|
Component,
|
|
|
|
|
ContentChildren,
|
|
|
|
|
ElementRef,
|
|
|
|
|
EventEmitter,
|
|
|
|
|
Input,
|
|
|
|
|
OnDestroy,
|
|
|
|
|
Optional,
|
|
|
|
|
Output,
|
|
|
|
|
QueryList,
|
2017-04-21 18:21:59 +02:00
|
|
|
Renderer2,
|
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,
|
2017-03-10 19:38:40 +01:00
|
|
|
Attribute,
|
2017-03-29 23:32:10 +02:00
|
|
|
OnInit,
|
2016-11-01 13:53:48 -07:00
|
|
|
} from '@angular/core';
|
2017-03-09 22:57:12 +01:00
|
|
|
import {MdOption, MdOptionSelectionChange} 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';
|
2017-03-09 22:57:12 +01:00
|
|
|
import {Observable} from 'rxjs/Observable';
|
2016-11-01 13:53:48 -07:00
|
|
|
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';
|
2017-03-09 22:57:12 +01:00
|
|
|
import {SelectionModel} from '../core/selection/selection';
|
2017-04-22 00:03:18 +02:00
|
|
|
import {ScrollDispatcher} from '../core/overlay/scroll/scroll-dispatcher';
|
2017-03-09 22:57:12 +01:00
|
|
|
import {MdSelectDynamicMultipleError, MdSelectNonArrayValueError} from './select-errors';
|
|
|
|
|
import 'rxjs/add/observable/merge';
|
2017-02-09 20:46:47 +01:00
|
|
|
import 'rxjs/add/operator/startWith';
|
|
|
|
|
|
2016-12-02 13:34:33 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
|
2017-03-09 22:57:12 +01:00
|
|
|
/**
|
|
|
|
|
* Distance between the panel edge and the option text in
|
|
|
|
|
* multi-selection mode.
|
|
|
|
|
*
|
|
|
|
|
* (SELECT_PADDING * 1.75) + 20 = 48
|
|
|
|
|
* The padding is multiplied by 1.75 because the checkbox's margin is half the padding, and
|
|
|
|
|
* the browser adds ~4px, because we're using inline elements.
|
|
|
|
|
* The checkbox width is 20px.
|
|
|
|
|
*/
|
|
|
|
|
export const SELECT_MULTIPLE_PANEL_PADDING_X = SELECT_PANEL_PADDING_X * 1.75 + 20;
|
|
|
|
|
|
2016-12-02 13:34:33 -08:00
|
|
|
/**
|
|
|
|
|
* 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) { }
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 20:28:55 +01:00
|
|
|
/** Allowed values for the floatPlaceholder option. */
|
|
|
|
|
export type MdSelectFloatPlaceholderType = 'always' | 'never' | 'auto';
|
|
|
|
|
|
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',
|
2017-03-10 19:38:40 +01:00
|
|
|
'[attr.tabindex]': 'tabIndex',
|
2017-03-28 01:39:21 +02:00
|
|
|
'[attr.aria-label]': '_ariaLabel',
|
|
|
|
|
'[attr.aria-labelledby]': 'ariaLabelledby',
|
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)',
|
2017-04-21 18:21:59 +02:00
|
|
|
'(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
|
|
|
})
|
2017-03-29 23:32:10 +02:00
|
|
|
export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlValueAccessor {
|
2016-11-01 13:53:48 -07:00
|
|
|
/** Whether or not the overlay panel is open. */
|
|
|
|
|
private _panelOpen = false;
|
|
|
|
|
|
|
|
|
|
/** Subscriptions to option events. */
|
2017-03-09 22:57:12 +01:00
|
|
|
private _optionSubscription: Subscription;
|
2016-11-01 13:53:48 -07:00
|
|
|
|
|
|
|
|
/** Subscription to changes in the option list. */
|
|
|
|
|
private _changeSubscription: Subscription;
|
|
|
|
|
|
|
|
|
|
/** Subscription to tab events while overlay is focused. */
|
|
|
|
|
private _tabSubscription: Subscription;
|
|
|
|
|
|
2017-04-22 00:03:18 +02:00
|
|
|
/** Subscription to global scrolled events while the select is open. */
|
|
|
|
|
private _scrollSubscription: 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;
|
|
|
|
|
|
2017-03-09 22:57:12 +01:00
|
|
|
/** Whether the component is in multiple selection mode. */
|
|
|
|
|
private _multiple: boolean = false;
|
|
|
|
|
|
|
|
|
|
/** Deals with the selection logic. */
|
|
|
|
|
_selectionModel: SelectionModel<MdOption>;
|
|
|
|
|
|
2016-12-05 12:51:46 -08:00
|
|
|
/** The animation state of the placeholder. */
|
2017-02-21 20:28:55 +01:00
|
|
|
private _placeholderState = '';
|
2016-12-05 12:51:46 -08:00
|
|
|
|
2017-03-10 19:38:40 +01:00
|
|
|
/** Tab index for the element. */
|
|
|
|
|
private _tabIndex: number;
|
|
|
|
|
|
2017-04-21 18:21:59 +02:00
|
|
|
/** Theme color for the component. */
|
|
|
|
|
private _color: string;
|
|
|
|
|
|
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.
|
2017-04-11 08:47:39 -07:00
|
|
|
Promise.resolve(null).then(() => this._setTriggerWidth());
|
2016-12-06 19:15:49 -08:00
|
|
|
}
|
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
|
|
|
|
2017-03-09 22:57:12 +01:00
|
|
|
/** Whether the user should be allowed to select multiple options. */
|
|
|
|
|
@Input()
|
|
|
|
|
get multiple(): boolean { return this._multiple; }
|
|
|
|
|
set multiple(value: boolean) {
|
|
|
|
|
if (this._selectionModel) {
|
|
|
|
|
throw new MdSelectDynamicMultipleError();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._multiple = coerceBooleanProperty(value);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 20:28:55 +01:00
|
|
|
/** Whether to float the placeholder text. */
|
|
|
|
|
@Input()
|
|
|
|
|
get floatPlaceholder(): MdSelectFloatPlaceholderType { return this._floatPlaceholder; }
|
|
|
|
|
set floatPlaceholder(value: MdSelectFloatPlaceholderType) {
|
|
|
|
|
this._floatPlaceholder = value || 'auto';
|
|
|
|
|
}
|
|
|
|
|
private _floatPlaceholder: MdSelectFloatPlaceholderType = 'auto';
|
|
|
|
|
|
2017-03-10 19:38:40 +01:00
|
|
|
/** Tab index for the select element. */
|
|
|
|
|
@Input()
|
|
|
|
|
get tabIndex(): number { return this._disabled ? -1 : this._tabIndex; }
|
|
|
|
|
set tabIndex(value: number) {
|
|
|
|
|
if (typeof value !== 'undefined') {
|
|
|
|
|
this._tabIndex = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 01:39:21 +02:00
|
|
|
/** Aria label of the select. If not specified, the placeholder will be used as label. */
|
|
|
|
|
@Input('aria-label') ariaLabel: string = '';
|
|
|
|
|
|
|
|
|
|
/** Input that can be used to specify the `aria-labelledby` attribute. */
|
|
|
|
|
@Input('aria-labelledby') ariaLabelledby: string = '';
|
|
|
|
|
|
2017-04-21 18:21:59 +02:00
|
|
|
/** Theme color for the component. */
|
|
|
|
|
@Input()
|
|
|
|
|
get color(): string { return this._color; }
|
|
|
|
|
set color(value: string) {
|
|
|
|
|
if (value && value !== this._color) {
|
|
|
|
|
this._renderer.removeClass(this._element.nativeElement, `mat-${this._color}`);
|
|
|
|
|
this._renderer.addClass(this._element.nativeElement, `mat-${value}`);
|
|
|
|
|
this._color = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-09 22:57:12 +01:00
|
|
|
/** Combined stream of all of the child options' change events. */
|
|
|
|
|
get optionSelectionChanges(): Observable<MdOptionSelectionChange> {
|
|
|
|
|
return Observable.merge(...this.options.map(option => option.onSelectionChange));
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2017-04-21 18:21:59 +02:00
|
|
|
constructor(private _element: ElementRef, private _renderer: Renderer2,
|
2017-02-09 19:31:55 +01:00
|
|
|
private _viewportRuler: ViewportRuler, private _changeDetectorRef: ChangeDetectorRef,
|
2017-04-22 00:03:18 +02:00
|
|
|
private _scrollDispatcher: ScrollDispatcher, @Optional() private _dir: Dir,
|
|
|
|
|
@Self() @Optional() public _control: NgControl,
|
2017-03-10 19:38:40 +01:00
|
|
|
@Attribute('tabindex') tabIndex: string) {
|
2017-04-22 00:03:18 +02:00
|
|
|
|
2016-11-16 16:02:28 -08:00
|
|
|
if (this._control) {
|
|
|
|
|
this._control.valueAccessor = this;
|
|
|
|
|
}
|
2017-03-10 19:38:40 +01:00
|
|
|
|
|
|
|
|
this._tabIndex = parseInt(tabIndex) || 0;
|
2016-11-03 16:01:03 -07:00
|
|
|
}
|
2016-11-01 13:53:48 -07:00
|
|
|
|
2017-03-29 23:32:10 +02:00
|
|
|
ngOnInit() {
|
2017-03-09 22:57:12 +01:00
|
|
|
this._selectionModel = new SelectionModel<MdOption>(this.multiple, null, false);
|
2017-04-21 18:21:59 +02:00
|
|
|
this.color = this.color || 'primary';
|
2017-03-29 23:32:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngAfterContentInit() {
|
2016-11-01 13:53:48 -07:00
|
|
|
this._initKeyManager();
|
2017-02-09 20:46:47 +01:00
|
|
|
|
|
|
|
|
this._changeSubscription = this.options.changes.startWith(null).subscribe(() => {
|
2017-01-13 23:55:41 +01:00
|
|
|
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();
|
2017-02-09 20:46:47 +01:00
|
|
|
|
|
|
|
|
if (this._changeSubscription) {
|
|
|
|
|
this._changeSubscription.unsubscribe();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this._tabSubscription) {
|
|
|
|
|
this._tabSubscription.unsubscribe();
|
|
|
|
|
}
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Toggles the overlay panel open or closed. */
|
|
|
|
|
toggle(): void {
|
|
|
|
|
this.panelOpen ? this.close() : this.open();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Opens the overlay panel. */
|
|
|
|
|
open(): void {
|
2017-02-09 19:32:42 +01:00
|
|
|
if (this.disabled || !this.options.length) {
|
2016-11-11 15:02:52 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2017-04-11 08:47:39 -07:00
|
|
|
|
|
|
|
|
if (!this._triggerWidth) {
|
|
|
|
|
this._setTriggerWidth();
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-02 13:34:33 -08:00
|
|
|
this._calculateOverlayPosition();
|
2017-02-21 20:28:55 +01:00
|
|
|
this._placeholderState = this._floatPlaceholderState();
|
2016-11-01 13:53:48 -07:00
|
|
|
this._panelOpen = true;
|
2017-04-22 00:03:18 +02:00
|
|
|
this._scrollSubscription = this._scrollDispatcher.scrolled(0, () => {
|
|
|
|
|
this.overlayDir.overlayRef.updatePosition();
|
|
|
|
|
});
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
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 {
|
2017-03-09 22:57:12 +01:00
|
|
|
if (this._panelOpen) {
|
|
|
|
|
this._panelOpen = false;
|
2017-04-22 00:03:18 +02:00
|
|
|
|
2017-03-09 22:57:12 +01:00
|
|
|
if (this._selectionModel.isEmpty()) {
|
|
|
|
|
this._placeholderState = '';
|
|
|
|
|
}
|
2017-04-22 00:03:18 +02:00
|
|
|
|
|
|
|
|
if (this._scrollSubscription) {
|
|
|
|
|
this._scrollSubscription.unsubscribe();
|
|
|
|
|
this._scrollSubscription = null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-09 22:57:12 +01:00
|
|
|
this._focusHost();
|
2016-12-05 12:51:46 -08:00
|
|
|
}
|
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 {
|
2017-02-09 20:46:47 +01:00
|
|
|
if (this.options) {
|
|
|
|
|
this._setSelectionByValue(value);
|
2016-11-30 17:08:56 -08:00
|
|
|
}
|
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. */
|
2017-03-09 22:57:12 +01:00
|
|
|
get selected(): MdOption | MdOption[] {
|
|
|
|
|
return this.multiple ? this._selectionModel.selected : this._selectionModel.selected[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The value displayed in the trigger. */
|
|
|
|
|
get triggerValue(): string {
|
2017-03-29 23:30:32 +02:00
|
|
|
if (this._multiple) {
|
|
|
|
|
let selectedOptions = this._selectionModel.selected.map(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;
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
2017-03-09 22:57:12 +01:00
|
|
|
/** Whether the element is in RTL mode. */
|
2016-11-01 13:53:48 -07:00
|
|
|
_isRtl(): boolean {
|
|
|
|
|
return this._dir ? this._dir.value === 'rtl' : false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 08:47:39 -07:00
|
|
|
/**
|
|
|
|
|
* Sets the width of the trigger element. This is necessary to match
|
2016-11-01 13:53:48 -07:00
|
|
|
* the overlay width to the trigger width.
|
|
|
|
|
*/
|
2017-04-11 08:47:39 -07:00
|
|
|
private _setTriggerWidth(): void {
|
|
|
|
|
this._triggerWidth = 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-03-23 22:46:47 +01:00
|
|
|
this._panelDoneAnimating = false;
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
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-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.
|
|
|
|
|
*/
|
2017-03-09 22:57:12 +01:00
|
|
|
private _setSelectionByValue(value: any | any[]): void {
|
|
|
|
|
const isArray = Array.isArray(value);
|
|
|
|
|
|
|
|
|
|
if (this.multiple && value && !isArray) {
|
|
|
|
|
throw new MdSelectNonArrayValueError();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isArray) {
|
|
|
|
|
this._clearSelection();
|
|
|
|
|
value.forEach((currentValue: any) => this._selectValue(currentValue));
|
|
|
|
|
this._sortValues();
|
|
|
|
|
} else if (!this._selectValue(value)) {
|
|
|
|
|
this._clearSelection();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._setValueWidth();
|
|
|
|
|
|
|
|
|
|
if (this._selectionModel.isEmpty()) {
|
|
|
|
|
this._placeholderState = '';
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 01:52:08 +01:00
|
|
|
this._changeDetectorRef.markForCheck();
|
2016-12-17 04:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
2017-03-09 22:57:12 +01:00
|
|
|
/**
|
|
|
|
|
* Finds and selects and option based on its value.
|
|
|
|
|
* @returns Option that has the corresponding value.
|
|
|
|
|
*/
|
|
|
|
|
private _selectValue(value: any): MdOption {
|
|
|
|
|
let correspondingOption = this.options.find(option => option.value === value);
|
|
|
|
|
|
|
|
|
|
if (correspondingOption) {
|
|
|
|
|
correspondingOption.select();
|
|
|
|
|
this._selectionModel.select(correspondingOption);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return correspondingOption;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clears the select trigger and deselects every option in the list.
|
|
|
|
|
* @param skip Option that should not be deselected.
|
|
|
|
|
*/
|
|
|
|
|
private _clearSelection(skip?: MdOption): void {
|
|
|
|
|
this._selectionModel.clear();
|
|
|
|
|
this.options.forEach(option => {
|
|
|
|
|
if (option !== skip) {
|
|
|
|
|
option.deselect();
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-12-17 04:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
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);
|
2017-03-09 22:57:12 +01:00
|
|
|
this._tabSubscription = this._keyManager.tabOut.subscribe(() => this.close());
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
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();
|
2017-03-09 22:57:12 +01:00
|
|
|
this._setOptionMultiple();
|
2016-11-16 16:02:28 -08:00
|
|
|
}
|
|
|
|
|
|
2017-03-09 22:57:12 +01:00
|
|
|
/** Listens to user-generated selection events on each option. */
|
2016-11-01 13:53:48 -07:00
|
|
|
private _listenToOptions(): void {
|
2017-03-09 22:57:12 +01:00
|
|
|
this._optionSubscription = this.optionSelectionChanges
|
|
|
|
|
.filter(event => event.isUserInput)
|
|
|
|
|
.subscribe(event => {
|
|
|
|
|
this._onSelect(event.source);
|
|
|
|
|
this._setValueWidth();
|
|
|
|
|
|
|
|
|
|
if (!this.multiple) {
|
|
|
|
|
this.close();
|
2016-11-03 16:01:03 -07:00
|
|
|
}
|
|
|
|
|
});
|
2017-03-09 22:57:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Invoked when an option is clicked. */
|
|
|
|
|
private _onSelect(option: MdOption): void {
|
|
|
|
|
const wasSelected = this._selectionModel.isSelected(option);
|
|
|
|
|
|
|
|
|
|
if (this.multiple) {
|
|
|
|
|
this._selectionModel.toggle(option);
|
|
|
|
|
wasSelected ? option.deselect() : option.select();
|
|
|
|
|
this._sortValues();
|
|
|
|
|
} else {
|
|
|
|
|
this._clearSelection(option);
|
|
|
|
|
this._selectionModel.select(option);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (wasSelected !== this._selectionModel.isSelected(option)) {
|
|
|
|
|
this._propagateChanges();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sorts the model values, ensuring that they keep the same
|
|
|
|
|
* order that they have in the panel.
|
|
|
|
|
*/
|
|
|
|
|
private _sortValues(): void {
|
|
|
|
|
if (this._multiple) {
|
|
|
|
|
this._selectionModel.clear();
|
|
|
|
|
|
|
|
|
|
this.options.forEach(option => {
|
|
|
|
|
if (option.selected) {
|
|
|
|
|
this._selectionModel.select(option);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Unsubscribes from all option subscriptions. */
|
|
|
|
|
private _dropSubscriptions(): void {
|
2017-03-09 22:57:12 +01:00
|
|
|
if (this._optionSubscription) {
|
|
|
|
|
this._optionSubscription.unsubscribe();
|
|
|
|
|
this._optionSubscription = null;
|
|
|
|
|
}
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
2017-03-09 22:57:12 +01:00
|
|
|
/** Emits change event to set the model value. */
|
|
|
|
|
private _propagateChanges(): void {
|
|
|
|
|
let valueToEmit = Array.isArray(this.selected) ?
|
|
|
|
|
this.selected.map(option => option.value) :
|
|
|
|
|
this.selected.value;
|
|
|
|
|
|
|
|
|
|
this._onChange(valueToEmit);
|
|
|
|
|
this.change.emit(new MdSelectChange(this, valueToEmit));
|
2017-01-12 22:24:25 +01:00
|
|
|
}
|
|
|
|
|
|
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(' ');
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-09 22:57:12 +01:00
|
|
|
/**
|
|
|
|
|
* Sets the `multiple` property on each option. The promise is necessary
|
|
|
|
|
* in order to avoid Angular errors when modifying the property after init.
|
|
|
|
|
*/
|
|
|
|
|
private _setOptionMultiple() {
|
|
|
|
|
if (this.multiple) {
|
|
|
|
|
Promise.resolve(null).then(() => {
|
|
|
|
|
this.options.forEach(option => option.multiple = this.multiple);
|
|
|
|
|
});
|
2016-12-05 12:51:46 -08:00
|
|
|
}
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-09 22:57:12 +01:00
|
|
|
/**
|
|
|
|
|
* Focuses the selected item. If no option is selected, it will focus
|
2016-11-01 13:53:48 -07:00
|
|
|
* the first item instead.
|
|
|
|
|
*/
|
|
|
|
|
private _focusCorrectOption(): void {
|
2017-03-09 22:57:12 +01:00
|
|
|
if (this._selectionModel.isEmpty()) {
|
2017-01-18 21:33:06 -08:00
|
|
|
this._keyManager.setFirstItemActive();
|
2017-03-09 22:57:12 +01:00
|
|
|
} else {
|
|
|
|
|
this._keyManager.setActiveItem(this._getOptionIndex(this._selectionModel.selected[0]));
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Focuses the host element when the panel closes. */
|
|
|
|
|
private _focusHost(): void {
|
2017-04-21 18:21:59 +02:00
|
|
|
this._element.nativeElement.focus();
|
2016-11-01 13:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 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 {
|
2017-03-09 22:57:12 +01:00
|
|
|
this._offsetX = this.multiple ? SELECT_MULTIPLE_PANEL_PADDING_X : SELECT_PANEL_PADDING_X;
|
|
|
|
|
|
|
|
|
|
if (!this._isRtl()) {
|
|
|
|
|
this._offsetX *= -1;
|
|
|
|
|
}
|
2016-12-02 13:34:33 -08:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2017-03-09 22:57:12 +01:00
|
|
|
if (this._selectionModel.hasValue()) {
|
|
|
|
|
const selectedIndex = this._getOptionIndex(this._selectionModel.selected[0]);
|
2016-12-02 13:34:33 -08:00
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 20:28:55 +01:00
|
|
|
/**
|
|
|
|
|
* Figures out the appropriate animation state for the placeholder.
|
|
|
|
|
*/
|
|
|
|
|
_getPlaceholderAnimationState(): string {
|
|
|
|
|
if (this.floatPlaceholder === 'never') {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.floatPlaceholder === 'always') {
|
|
|
|
|
return this._floatPlaceholderState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this._placeholderState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines the CSS `visibility` of the placeholder element.
|
|
|
|
|
*/
|
|
|
|
|
_getPlaceholderVisibility(): 'visible'|'hidden' {
|
2017-03-09 22:57:12 +01:00
|
|
|
return (this.floatPlaceholder !== 'never' || this._selectionModel.isEmpty()) ?
|
|
|
|
|
'visible' : 'hidden';
|
2017-02-21 20:28:55 +01:00
|
|
|
}
|
|
|
|
|
|
2017-03-28 01:39:21 +02:00
|
|
|
/** Returns the aria-label of the select component. */
|
|
|
|
|
get _ariaLabel(): string {
|
|
|
|
|
// If an ariaLabelledby value has been set, the select should not overwrite the
|
|
|
|
|
// `aria-labelledby` value by setting the ariaLabel to the placeholder.
|
|
|
|
|
return this.ariaLabelledby ? null : this.ariaLabel || this.placeholder;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-02 13:34:33 -08:00
|
|
|
/**
|
|
|
|
|
* 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);
|
2017-03-09 22:57:12 +01:00
|
|
|
const panelHeightBottom = totalPanelHeight - panelHeightTop - triggerRect.height;
|
2016-12-02 13:34:33 -08:00
|
|
|
|
|
|
|
|
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`;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 20:28:55 +01:00
|
|
|
/** Figures out the floating placeholder state value. */
|
|
|
|
|
private _floatPlaceholderState(): string {
|
|
|
|
|
return this._isRtl() ? 'floating-rtl' : 'floating-ltr';
|
|
|
|
|
}
|
2016-12-02 13:34:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 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
|
|
|
}
|