2016-11-01 13:53:48 -07:00
|
|
|
import {
|
|
|
|
|
AfterContentInit,
|
|
|
|
|
Component,
|
|
|
|
|
ContentChildren,
|
|
|
|
|
ElementRef,
|
|
|
|
|
EventEmitter,
|
|
|
|
|
Input,
|
|
|
|
|
OnDestroy,
|
|
|
|
|
Optional,
|
|
|
|
|
Output,
|
|
|
|
|
QueryList,
|
|
|
|
|
Renderer,
|
|
|
|
|
ViewEncapsulation,
|
|
|
|
|
ViewChild
|
|
|
|
|
} from '@angular/core';
|
|
|
|
|
import {MdOption} from './option';
|
|
|
|
|
import {ENTER, SPACE} from '../core/keyboard/keycodes';
|
|
|
|
|
import {ListKeyManager} from '../core/a11y/list-key-manager';
|
|
|
|
|
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';
|
|
|
|
|
import {coerceBooleanProperty} from '../core/coersion/boolean-property';
|
2016-09-19 14:57:00 -07:00
|
|
|
|
|
|
|
|
@Component({
|
2016-10-25 12:35:35 -07:00
|
|
|
moduleId: module.id,
|
|
|
|
|
selector: 'md-select',
|
|
|
|
|
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()',
|
|
|
|
|
'[class.md-select-disabled]': 'disabled',
|
2016-11-03 16:01:03 -07:00
|
|
|
'[attr.aria-invalid]': '_control?.invalid || "false"',
|
|
|
|
|
'(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-11-01 13:53:48 -07:00
|
|
|
/** Manages keyboard events for options in the panel. */
|
|
|
|
|
_keyManager: ListKeyManager;
|
|
|
|
|
|
2016-11-03 16:01:03 -07:00
|
|
|
/** View -> model callback called when value changes */
|
|
|
|
|
_onChange: (value: any) => void;
|
|
|
|
|
|
|
|
|
|
/** View -> model callback called when select has been touched */
|
|
|
|
|
_onTouched: Function;
|
|
|
|
|
|
2016-11-01 13:53:48 -07:00
|
|
|
/** This position config ensures that the top left corner of the overlay
|
|
|
|
|
* is aligned with with the top left of the origin (overlapping the trigger
|
|
|
|
|
* completely). In RTL mode, the top right corners are aligned instead.
|
|
|
|
|
*/
|
|
|
|
|
_positions = [{
|
|
|
|
|
originX: 'start',
|
|
|
|
|
originY: 'top',
|
|
|
|
|
overlayX: 'start',
|
|
|
|
|
overlayY: 'top'
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
@ViewChild('trigger') trigger: ElementRef;
|
|
|
|
|
@ContentChildren(MdOption) options: QueryList<MdOption>;
|
|
|
|
|
|
|
|
|
|
@Input() placeholder: string;
|
2016-11-03 16:01:03 -07:00
|
|
|
|
2016-11-11 15:02:52 -08:00
|
|
|
@Input()
|
|
|
|
|
get disabled() {
|
|
|
|
|
return this._disabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set disabled(value: any) {
|
|
|
|
|
this._disabled = coerceBooleanProperty(value);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 16:01:03 -07:00
|
|
|
@Input()
|
|
|
|
|
get required() {
|
|
|
|
|
return this._required;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set required(value: any) {
|
|
|
|
|
this._required = coerceBooleanProperty(value);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 13:53:48 -07:00
|
|
|
@Output() onOpen = new EventEmitter();
|
|
|
|
|
@Output() onClose = new EventEmitter();
|
|
|
|
|
|
|
|
|
|
constructor(private _element: ElementRef, private _renderer: Renderer,
|
2016-11-03 16:01:03 -07:00
|
|
|
@Optional() private _dir: Dir, @Optional() public _control: NgControl) {
|
|
|
|
|
this._control.valueAccessor = this;
|
|
|
|
|
}
|
2016-11-01 13:53:48 -07:00
|
|
|
|
|
|
|
|
ngAfterContentInit() {
|
|
|
|
|
this._initKeyManager();
|
|
|
|
|
this._listenToOptions();
|
|
|
|
|
|
|
|
|
|
this._changeSubscription = this.options.changes.subscribe(() => {
|
|
|
|
|
this._dropSubscriptions();
|
|
|
|
|
this._listenToOptions();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-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-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.
|
|
|
|
|
*/
|
|
|
|
|
writeValue(value: any): void {
|
|
|
|
|
if (!this.options) { return; }
|
|
|
|
|
|
|
|
|
|
this.options.forEach((option: MdOption) => {
|
|
|
|
|
if (option.value === value) {
|
|
|
|
|
option.select();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
registerOnTouched(fn: Function): void {
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
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 {
|
|
|
|
|
return this.trigger.nativeElement.getBoundingClientRect().width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The animation state of the placeholder. */
|
|
|
|
|
_getPlaceholderState(): string {
|
|
|
|
|
if (this.panelOpen || this.selected) {
|
|
|
|
|
return this._isRtl() ? 'floating-rtl' : 'floating-ltr';
|
|
|
|
|
} else {
|
|
|
|
|
return 'normal';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The animation state of the overlay panel. */
|
|
|
|
|
_getPanelState(): string {
|
|
|
|
|
return this._isRtl() ? 'showing-rtl' : 'showing-ltr';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 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
|
|
|
/**
|
|
|
|
|
* When the panel is finished animating, emits an event and focuses
|
|
|
|
|
* an option if the panel is open.
|
|
|
|
|
*/
|
2016-11-01 13:53:48 -07:00
|
|
|
_onPanelDone(): void {
|
|
|
|
|
if (this.panelOpen) {
|
|
|
|
|
this._focusCorrectOption();
|
|
|
|
|
this.onOpen.emit();
|
|
|
|
|
} else {
|
|
|
|
|
this.onClose.emit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-11-01 13:53:48 -07:00
|
|
|
/** Sets up a key manager to listen to keyboard events on the overlay panel. */
|
|
|
|
|
private _initKeyManager() {
|
|
|
|
|
this._keyManager = new ListKeyManager(this.options);
|
|
|
|
|
this._tabSubscription = this._keyManager.tabOut.subscribe(() => {
|
|
|
|
|
this.close();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Listens to selection events on each option. */
|
|
|
|
|
private _listenToOptions(): void {
|
|
|
|
|
this.options.forEach((option: MdOption) => {
|
2016-11-03 16:01:03 -07:00
|
|
|
const sub = option.onSelect.subscribe((isUserInput: boolean) => {
|
|
|
|
|
if (isUserInput) {
|
|
|
|
|
this._onChange(option.value);
|
|
|
|
|
}
|
|
|
|
|
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 = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** When a new option is selected, deselects the others and closes the panel. */
|
|
|
|
|
private _onSelect(option: MdOption): void {
|
|
|
|
|
this._selected = option;
|
|
|
|
|
this._updateOptions();
|
|
|
|
|
this.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Deselect each option that doesn't match the current selection. */
|
|
|
|
|
private _updateOptions(): void {
|
|
|
|
|
this.options.forEach((option: MdOption) => {
|
|
|
|
|
if (option !== this.selected) {
|
|
|
|
|
option.deselect();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Focuses the selected item. If no option is selected, it will focus
|
|
|
|
|
* the first item instead.
|
|
|
|
|
*/
|
|
|
|
|
private _focusCorrectOption(): void {
|
|
|
|
|
if (this.selected) {
|
2016-11-11 14:23:58 -08:00
|
|
|
this._keyManager.setFocus(this._getOptionIndex(this.selected));
|
2016-11-01 13:53:48 -07:00
|
|
|
} else {
|
2016-11-11 14:23:58 -08:00
|
|
|
this._keyManager.focusFirstItem();
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|