UNPKG

@angular/material

Version:
1,333 lines (1,326 loc) 43.5 kB
/** * @license * Copyright Google Inc. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChildren, Directive, ElementRef, EventEmitter, Input, NgModule, Optional, Output, Renderer2, Self, ViewEncapsulation } from '@angular/core'; import { FocusKeyManager } from '@angular/cdk/a11y'; import { coerceBooleanProperty } from '@angular/cdk/coercion'; import { SelectionModel } from '@angular/cdk/collections'; import { startWith } from '@angular/cdk/rxjs'; import { FormGroupDirective, NgControl, NgForm } from '@angular/forms'; import { BACKSPACE, DELETE, Directionality, ENTER, LEFT_ARROW, RIGHT_ARROW, SPACE, UP_ARROW, mixinColor, mixinDisabled } from '@angular/material/core'; import { MdFormFieldControl } from '@angular/material/form-field'; import { merge } from 'rxjs/observable/merge'; import { Subject } from 'rxjs/Subject'; import { Subscription } from 'rxjs/Subscription'; /** * Event object emitted by MdChip when selected or deselected. */ class MdChipSelectionChange { /** * @param {?} source * @param {?} selected * @param {?=} isUserInput */ constructor(source, selected, isUserInput = false) { this.source = source; this.selected = selected; this.isUserInput = isUserInput; } } /** * \@docs-private */ class MdChipBase { /** * @param {?} _renderer * @param {?} _elementRef */ constructor(_renderer, _elementRef) { this._renderer = _renderer; this._elementRef = _elementRef; } } const _MdChipMixinBase = mixinColor(mixinDisabled(MdChipBase), 'primary'); /** * Dummy directive to add CSS class to basic chips. * \@docs-private */ class MdBasicChip { } MdBasicChip.decorators = [ { type: Directive, args: [{ selector: `md-basic-chip, [md-basic-chip], mat-basic-chip, [mat-basic-chip]`, host: { 'class': 'mat-basic-chip' } },] }, ]; /** * @nocollapse */ MdBasicChip.ctorParameters = () => []; /** * Material design styled Chip component. Used inside the MdChipList component. */ class MdChip extends _MdChipMixinBase { /** * @param {?} renderer * @param {?} _elementRef */ constructor(renderer, _elementRef) { super(renderer, _elementRef); this._elementRef = _elementRef; this._selected = false; this._selectable = true; this._removable = true; /** * Whether the chip has focus. */ this._hasFocus = false; /** * Emits when the chip is focused. */ this._onFocus = new Subject(); /** * Emits when the chip is blured. */ this._onBlur = new Subject(); /** * Emitted when the chip is selected or deselected. */ this.selectionChange = new EventEmitter(); /** * Emitted when the chip is destroyed. */ this.destroyed = new EventEmitter(); /** * Emitted when the chip is destroyed. * @deprecated Use 'destroyed' instead. */ this.destroy = this.destroyed; /** * Emitted when a chip is to be removed. */ this.removed = new EventEmitter(); /** * Emitted when a chip is to be removed. * @deprecated Use `removed` instead. */ this.onRemove = this.removed; } /** * Whether the chip is selected. * @return {?} */ get selected() { return this._selected; } /** * @param {?} value * @return {?} */ set selected(value) { this._selected = coerceBooleanProperty(value); this.selectionChange.emit({ source: this, isUserInput: false, selected: value }); } /** * The value of the chip. Defaults to the content inside <md-chip> tags. * @return {?} */ get value() { return this._value != undefined ? this._value : this._elementRef.nativeElement.textContent; } /** * @param {?} newValue * @return {?} */ set value(newValue) { this._value = newValue; } /** * Whether or not the chips are selectable. When a chip is not selectable, * changes to it's selected state are always ignored. * @return {?} */ get selectable() { return this._selectable; } /** * @param {?} value * @return {?} */ set selectable(value) { this._selectable = coerceBooleanProperty(value); } /** * Determines whether or not the chip displays the remove styling and emits (remove) events. * @return {?} */ get removable() { return this._removable; } /** * @param {?} value * @return {?} */ set removable(value) { this._removable = coerceBooleanProperty(value); } /** * @return {?} */ get ariaSelected() { return this.selectable ? this.selected.toString() : null; } /** * @return {?} */ ngOnDestroy() { this.destroyed.emit({ chip: this }); } /** * Selects the chip. * @return {?} */ select() { this._selected = true; this.selectionChange.emit({ source: this, isUserInput: false, selected: true }); } /** * Deselects the chip. * @return {?} */ deselect() { this._selected = false; this.selectionChange.emit({ source: this, isUserInput: false, selected: false }); } /** * Select this chip and emit selected event * @return {?} */ selectViaInteraction() { this._selected = true; // Emit select event when selected changes. this.selectionChange.emit({ source: this, isUserInput: true, selected: true }); } /** * Toggles the current selected state of this chip. * @param {?=} isUserInput * @return {?} */ toggleSelected(isUserInput = false) { this._selected = !this.selected; this.selectionChange.emit({ source: this, isUserInput, selected: this._selected }); return this.selected; } /** * Allows for programmatic focusing of the chip. * @return {?} */ focus() { this._elementRef.nativeElement.focus(); this._onFocus.next({ chip: this }); } /** * Allows for programmatic removal of the chip. Called by the MdChipList when the DELETE or * BACKSPACE keys are pressed. * * Informs any listeners of the removal request. Does not remove the chip from the DOM. * @return {?} */ remove() { if (this.removable) { this.removed.emit({ chip: this }); } } /** * Ensures events fire properly upon click. * @param {?} event * @return {?} */ _handleClick(event) { // Check disabled if (this.disabled) { return; } event.preventDefault(); event.stopPropagation(); this.focus(); } /** * Handle custom key presses. * @param {?} event * @return {?} */ _handleKeydown(event) { if (this.disabled) { return; } switch (event.keyCode) { case DELETE: case BACKSPACE: // If we are removable, remove the focused chip this.remove(); // Always prevent so page navigation does not occur event.preventDefault(); break; case SPACE: // If we are selectable, toggle the focused chip if (this.selectable) { this.toggleSelected(true); } // Always prevent space from scrolling the page since the list has focus event.preventDefault(); break; } } /** * @return {?} */ _blur() { this._hasFocus = false; this._onBlur.next({ chip: this }); } } MdChip.decorators = [ { type: Directive, args: [{ selector: `md-basic-chip, [md-basic-chip], md-chip, [md-chip], mat-basic-chip, [mat-basic-chip], mat-chip, [mat-chip]`, inputs: ['color', 'disabled'], exportAs: 'mdChip, matChip', host: { 'class': 'mat-chip', 'tabindex': '-1', 'role': 'option', '[class.mat-chip-selected]': 'selected', '[attr.disabled]': 'disabled || null', '[attr.aria-disabled]': 'disabled.toString()', '[attr.aria-selected]': 'ariaSelected', '(click)': '_handleClick($event)', '(keydown)': '_handleKeydown($event)', '(focus)': '_hasFocus = true', '(blur)': '_blur()', }, },] }, ]; /** * @nocollapse */ MdChip.ctorParameters = () => [ { type: Renderer2, }, { type: ElementRef, }, ]; MdChip.propDecorators = { 'selected': [{ type: Input },], 'value': [{ type: Input },], 'selectable': [{ type: Input },], 'removable': [{ type: Input },], 'selectionChange': [{ type: Output },], 'destroyed': [{ type: Output },], 'destroy': [{ type: Output },], 'removed': [{ type: Output },], 'onRemove': [{ type: Output, args: ['remove',] },], }; /** * Applies proper (click) support and adds styling for use with the Material Design "cancel" icon * available at https://material.io/icons/#ic_cancel. * * Example: * * <md-chip> * <md-icon mdChipRemove>cancel</md-icon> * </md-chip> * * You *may* use a custom icon, but you may need to override the `md-chip-remove` positioning styles * to properly center the icon within the chip. */ class MdChipRemove { /** * @param {?} _parentChip */ constructor(_parentChip) { this._parentChip = _parentChip; } /** * Calls the parent chip's public `remove()` method if applicable. * @return {?} */ _handleClick() { if (this._parentChip.removable) { this._parentChip.remove(); } } } MdChipRemove.decorators = [ { type: Directive, args: [{ selector: '[mdChipRemove], [matChipRemove]', host: { 'class': 'mat-chip-remove', '(click)': '_handleClick($event)' } },] }, ]; /** * @nocollapse */ MdChipRemove.ctorParameters = () => [ { type: MdChip, }, ]; // Increasing integer for generating unique ids for chip-list components. let nextUniqueId = 0; /** * Change event object that is emitted when the chip list value has changed. */ class MdChipListChange { /** * @param {?} source * @param {?} value */ constructor(source, value) { this.source = source; this.value = value; } } /** * A material design chips component (named ChipList for it's similarity to the List component). */ class MdChipList { /** * @param {?} _renderer * @param {?} _elementRef * @param {?} _changeDetectorRef * @param {?} _dir * @param {?} _parentForm * @param {?} _parentFormGroup * @param {?} ngControl */ constructor(_renderer, _elementRef, _changeDetectorRef, _dir, _parentForm, _parentFormGroup, ngControl) { this._renderer = _renderer; this._elementRef = _elementRef; this._changeDetectorRef = _changeDetectorRef; this._dir = _dir; this._parentForm = _parentForm; this._parentFormGroup = _parentFormGroup; this.ngControl = ngControl; /** * Stream that emits whenever the state of the input changes such that the wrapping `MdFormField` * needs to run change detection. */ this.stateChanges = new Subject(); /** * When a chip is destroyed, we track the index so we can focus the appropriate next chip. */ this._lastDestroyedIndex = null; /** * Track which chips we're listening to for focus/destruction. */ this._chipSet = new WeakMap(); /** * Subscription to tabbing out from the chip list. */ this._tabOutSubscription = Subscription.EMPTY; /** * Whether or not the chip is selectable. */ this._selectable = true; /** * Whether the component is in multiple selection mode. */ this._multiple = false; /** * Uid of the chip list */ this._uid = `md-chip-list-${nextUniqueId++}`; /** * Whether this is required */ this._required = false; /** * Whether this is disabled */ this._disabled = false; /** * Tab index for the chip list. */ this._tabIndex = 0; /** * User defined tab index. * When it is not null, use user defined tab index. Otherwise use _tabIndex */ this._userTabIndex = null; /** * Function when touched */ this._onTouched = () => { }; /** * Function when changed */ this._onChange = () => { }; /** * Comparison function to specify which option is displayed. Defaults to object equality. */ this._compareWith = (o1, o2) => o1 === o2; /** * Orientation of the chip list. */ this.ariaOrientation = 'horizontal'; /** * Event emitted when the selected chip list value has been changed by the user. */ this.change = new EventEmitter(); /** * Event that emits whenever the raw value of the chip-list changes. This is here primarily * to facilitate the two-way binding for the `value` input. * \@docs-private */ this.valueChange = new EventEmitter(); if (this.ngControl) { this.ngControl.valueAccessor = this; } } /** * The array of selected chips inside chip list. * @return {?} */ get selected() { return this.multiple ? this._selectionModel.selected : this._selectionModel.selected[0]; } /** * Whether the user should be allowed to select multiple chips. * @return {?} */ get multiple() { return this._multiple; } /** * @param {?} value * @return {?} */ set multiple(value) { this._multiple = coerceBooleanProperty(value); } /** * A function to compare the option values with the selected values. The first argument * is a value from an option. The second is a value from the selection. A boolean * should be returned. * @return {?} */ get compareWith() { return this._compareWith; } /** * @param {?} fn * @return {?} */ set compareWith(fn) { this._compareWith = fn; if (this._selectionModel) { // A different comparator means the selection could change. this._initializeSelection(); } } /** * Required for FormFieldControl * @return {?} */ get value() { return this._value; } /** * @param {?} newValue * @return {?} */ set value(newValue) { this.writeValue(newValue); this._value = newValue; } /** * Required for FormFieldControl. The ID of the chip list * @param {?} value * @return {?} */ set id(value) { this._id = value; this.stateChanges.next(); } /** * @return {?} */ get id() { return this._id || this._uid; } /** * Required for FormFieldControl. Whether the chip list is required. * @param {?} value * @return {?} */ set required(value) { this._required = coerceBooleanProperty(value); this.stateChanges.next(); } /** * @return {?} */ get required() { return this._required; } /** * For FormFieldControl. Use chip input's placholder if there's a chip input * @param {?} value * @return {?} */ set placeholder(value) { this._placeholder = value; this.stateChanges.next(); } /** * @return {?} */ get placeholder() { return this._chipInput ? this._chipInput.placeholder : this._placeholder; } /** * Whether any chips or the mdChipInput inside of this chip-list has focus. * @return {?} */ get focused() { return this.chips.some(chip => chip._hasFocus) || (this._chipInput && this._chipInput.focused); } /** * Whether this chip-list contains no chips and no mdChipInput. * @return {?} */ get empty() { return (!this._chipInput || this._chipInput.empty) && this.chips.length === 0; } /** * Whether this chip-list is disabled. * @return {?} */ get disabled() { return this.ngControl ? this.ngControl.disabled : this._disabled; } /** * @param {?} value * @return {?} */ set disabled(value) { this._disabled = coerceBooleanProperty(value); } /** * Whether the chip list is in an error state. * @return {?} */ get errorState() { const /** @type {?} */ isInvalid = this.ngControl && this.ngControl.invalid; const /** @type {?} */ isTouched = this.ngControl && this.ngControl.touched; const /** @type {?} */ isSubmitted = (this._parentFormGroup && this._parentFormGroup.submitted) || (this._parentForm && this._parentForm.submitted); return !!(isInvalid && (isTouched || isSubmitted)); } /** * Whether or not this chip is selectable. When a chip is not selectable, * its selected state is always ignored. * @return {?} */ get selectable() { return this._selectable; } /** * @param {?} value * @return {?} */ set selectable(value) { this._selectable = coerceBooleanProperty(value); } /** * @param {?} value * @return {?} */ set tabIndex(value) { this._userTabIndex = value; this._tabIndex = value; } /** * Combined stream of all of the child chips' selection change events. * @return {?} */ get chipSelectionChanges() { return merge(...this.chips.map(chip => chip.selectionChange)); } /** * Combined stream of all of the child chips' focus change events. * @return {?} */ get chipFocusChanges() { return merge(...this.chips.map(chip => chip._onFocus)); } /** * Combined stream of all of the child chips' blur change events. * @return {?} */ get chipBlurChanges() { return merge(...this.chips.map(chip => chip._onBlur)); } /** * Combined stream of all of the child chips' remove change events. * @return {?} */ get chipRemoveChanges() { return merge(...this.chips.map(chip => chip.destroy)); } /** * @return {?} */ ngAfterContentInit() { this._keyManager = new FocusKeyManager(this.chips).withWrap(); // Prevents the chip list from capturing focus and redirecting // it back to the first chip when the user tabs out. this._tabOutSubscription = this._keyManager.tabOut.subscribe(() => { this._tabIndex = -1; setTimeout(() => this._tabIndex = this._userTabIndex || 0); }); // When the list changes, re-subscribe this._changeSubscription = startWith.call(this.chips.changes, null).subscribe(() => { this._resetChips(); // Reset chips selected/deselected status this._initializeSelection(); // Check to see if we need to update our tab index this._updateTabIndex(); // Check to see if we have a destroyed chip and need to refocus this._updateFocusForDestroyedChips(); }); } /** * @return {?} */ ngOnInit() { this._selectionModel = new SelectionModel(this.multiple, undefined, false); this.stateChanges.next(); } /** * @return {?} */ ngOnDestroy() { this._tabOutSubscription.unsubscribe(); if (this._changeSubscription) { this._changeSubscription.unsubscribe(); } this._dropSubscriptions(); } /** * Associates an HTML input element with this chip list. * @param {?} inputElement * @return {?} */ registerInput(inputElement) { this._chipInput = inputElement; } /** * @param {?} ids * @return {?} */ setDescribedByIds(ids) { this._ariaDescribedby = ids.join(' '); } /** * @param {?} value * @return {?} */ writeValue(value) { if (this.chips) { this._setSelectionByValue(value, false); } } /** * @param {?} fn * @return {?} */ registerOnChange(fn) { this._onChange = fn; } /** * @param {?} fn * @return {?} */ registerOnTouched(fn) { this._onTouched = fn; } /** * @param {?} disabled * @return {?} */ setDisabledState(disabled) { this.disabled = disabled; this._renderer.setProperty(this._elementRef.nativeElement, 'disabled', disabled); this.stateChanges.next(); } /** * Focuses the the first non-disabled chip in this chip list, or the associated input when there * are no eligible chips. * @return {?} */ focus() { // TODO: ARIA says this should focus the first `selected` chip if any are selected. // Focus on first element if there's no chipInput inside chip-list if (this._chipInput && this._chipInput.focused) { // do nothing } else if (this.chips.length > 0) { this._keyManager.setFirstItemActive(); this.stateChanges.next(); } else { this._focusInput(); this.stateChanges.next(); } } /** * Attempt to focus an input if we have one. * @return {?} */ _focusInput() { if (this._chipInput) { this._chipInput.focus(); } } /** * Pass events to the keyboard manager. Available here for tests. * @param {?} event * @return {?} */ _keydown(event) { let /** @type {?} */ code = event.keyCode; let /** @type {?} */ target = (event.target); let /** @type {?} */ isInputEmpty = this._isInputEmpty(target); let /** @type {?} */ isRtl = this._dir && this._dir.value == 'rtl'; let /** @type {?} */ isPrevKey = (code === (isRtl ? RIGHT_ARROW : LEFT_ARROW)); let /** @type {?} */ isNextKey = (code === (isRtl ? LEFT_ARROW : RIGHT_ARROW)); let /** @type {?} */ isBackKey = (code === BACKSPACE || code == DELETE || code == UP_ARROW || isPrevKey); // If they are on an empty input and hit backspace/delete/left arrow, focus the last chip if (isInputEmpty && isBackKey) { this._keyManager.setLastItemActive(); event.preventDefault(); return; } // If they are on a chip, check for space/left/right, otherwise pass to our key manager (like // up/down keys) if (target && target.classList.contains('mat-chip')) { if (isPrevKey) { this._keyManager.setPreviousItemActive(); event.preventDefault(); } else if (isNextKey) { this._keyManager.setNextItemActive(); event.preventDefault(); } else { this._keyManager.onKeydown(event); } } this.stateChanges.next(); } /** * Check the tab index as you should not be allowed to focus an empty list. * @return {?} */ _updateTabIndex() { // If we have 0 chips, we should not allow keyboard focus this._tabIndex = this._userTabIndex || (this.chips.length === 0 ? -1 : 0); } /** * Update key manager's active item when chip is deleted. * If the deleted chip is the last chip in chip list, focus the new last chip. * Otherwise focus the next chip in the list. * Save `_lastDestroyedIndex` so we can set the correct focus. * @param {?} chip * @return {?} */ _updateKeyManager(chip) { let /** @type {?} */ chipIndex = this.chips.toArray().indexOf(chip); if (this._isValidIndex(chipIndex)) { if (chip._hasFocus) { // Check whether the chip is not the last item if (chipIndex < this.chips.length - 1) { this._keyManager.setActiveItem(chipIndex); } else if (chipIndex - 1 >= 0) { this._keyManager.setActiveItem(chipIndex - 1); } } if (this._keyManager.activeItemIndex === chipIndex) { this._lastDestroyedIndex = chipIndex; } } } /** * Checks to see if a focus chip was recently destroyed so that we can refocus the next closest * one. * @return {?} */ _updateFocusForDestroyedChips() { let /** @type {?} */ chipsArray = this.chips; if (this._lastDestroyedIndex != null && chipsArray.length > 0) { // Check whether the destroyed chip was the last item const /** @type {?} */ newFocusIndex = Math.min(this._lastDestroyedIndex, chipsArray.length - 1); this._keyManager.setActiveItem(newFocusIndex); let /** @type {?} */ focusChip = this._keyManager.activeItem; // Focus the chip if (focusChip) { focusChip.focus(); } } else if (chipsArray.length === 0) { this._focusInput(); } // Reset our destroyed index this._lastDestroyedIndex = null; } /** * Utility to ensure all indexes are valid. * * @param {?} index The index to be checked. * @return {?} True if the index is valid for our list of chips. */ _isValidIndex(index) { return index >= 0 && index < this.chips.length; } /** * @param {?} element * @return {?} */ _isInputEmpty(element) { if (element && element.nodeName.toLowerCase() === 'input') { let /** @type {?} */ input = (element); return !input.value; } return false; } /** * @param {?} value * @param {?=} isUserInput * @return {?} */ _setSelectionByValue(value, isUserInput = true) { this._clearSelection(); this.chips.forEach(chip => chip.deselect()); if (Array.isArray(value)) { value.forEach(currentValue => this._selectValue(currentValue, isUserInput)); this._sortValues(); } else { const /** @type {?} */ correspondingChip = this._selectValue(value, isUserInput); // Shift focus to the active item. Note that we shouldn't do this in multiple // mode, because we don't know what chip the user interacted with last. if (correspondingChip) { this._keyManager.setActiveItem(this.chips.toArray().indexOf(correspondingChip)); } } } /** * Finds and selects the chip based on its value. * @param {?} value * @param {?=} isUserInput * @return {?} Chip that has the corresponding value. */ _selectValue(value, isUserInput = true) { const /** @type {?} */ correspondingChip = this.chips.find(chip => { return chip.value != null && this._compareWith(chip.value, value); }); if (correspondingChip) { isUserInput ? correspondingChip.selectViaInteraction() : correspondingChip.select(); this._selectionModel.select(correspondingChip); } return correspondingChip; } /** * @return {?} */ _initializeSelection() { // Defer setting the value in order to avoid the "Expression // has changed after it was checked" errors from Angular. Promise.resolve().then(() => { this._setSelectionByValue(this.ngControl ? this.ngControl.value : this._value, false); this.stateChanges.next(); }); } /** * Deselects every chip in the list. * @param {?=} skip Chip that should not be deselected. * @return {?} */ _clearSelection(skip) { this._selectionModel.clear(); this.chips.forEach(chip => { if (chip !== skip) { chip.deselect(); } }); this.stateChanges.next(); } /** * Sorts the model values, ensuring that they keep the same * order that they have in the panel. * @return {?} */ _sortValues() { if (this._multiple) { this._selectionModel.clear(); this.chips.forEach(chip => { if (chip.selected) { this._selectionModel.select(chip); } }); this.stateChanges.next(); } } /** * Emits change event to set the model value. * @param {?=} fallbackValue * @return {?} */ _propagateChanges(fallbackValue) { let /** @type {?} */ valueToEmit = null; if (Array.isArray(this.selected)) { valueToEmit = this.selected.map(chip => chip.value); } else { valueToEmit = this.selected ? this.selected.value : fallbackValue; } this._value = valueToEmit; this.change.emit(new MdChipListChange(this, valueToEmit)); this.valueChange.emit(valueToEmit); this._onChange(valueToEmit); this._changeDetectorRef.markForCheck(); } /** * When blurred, mark the field as touched when focus moved outside the chip list. * @return {?} */ _blur() { if (!this.disabled) { if (this._chipInput) { // If there's a chip input, we should check whether the focus moved to chip input. // If the focus is not moved to chip input, mark the field as touched. If the focus moved // to chip input, do nothing. // Timeout is needed to wait for the focus() event trigger on chip input. setTimeout(() => { if (!this.focused) { this._markAsTouched(); } }); } else { // If there's no chip input, then mark the field as touched. this._markAsTouched(); } } } /** * Mark the field as touched * @return {?} */ _markAsTouched() { this._onTouched(); this._changeDetectorRef.markForCheck(); this.stateChanges.next(); } /** * @return {?} */ _resetChips() { this._dropSubscriptions(); this._listenToChipsFocus(); this._listenToChipsSelection(); this._listenToChipsRemoved(); } /** * @return {?} */ _dropSubscriptions() { if (this._chipFocusSubscription) { this._chipFocusSubscription.unsubscribe(); this._chipFocusSubscription = null; } if (this._chipBlurSubscription) { this._chipBlurSubscription.unsubscribe(); this._chipBlurSubscription = null; } if (this._chipSelectionSubscription) { this._chipSelectionSubscription.unsubscribe(); this._chipSelectionSubscription = null; } } /** * Listens to user-generated selection events on each chip. * @return {?} */ _listenToChipsSelection() { this._chipSelectionSubscription = this.chipSelectionChanges.subscribe(event => { event.source.selected ? this._selectionModel.select(event.source) : this._selectionModel.deselect(event.source); // For single selection chip list, make sure the deselected value is unselected. if (!this.multiple) { this.chips.forEach(chip => { if (!this._selectionModel.isSelected(chip) && chip.selected) { chip.deselect(); } }); } if (event.isUserInput) { this._propagateChanges(); } }); } /** * Listens to user-generated selection events on each chip. * @return {?} */ _listenToChipsFocus() { this._chipFocusSubscription = this.chipFocusChanges.subscribe(event => { let /** @type {?} */ chipIndex = this.chips.toArray().indexOf(event.chip); if (this._isValidIndex(chipIndex)) { this._keyManager.updateActiveItemIndex(chipIndex); } this.stateChanges.next(); }); this._chipBlurSubscription = this.chipBlurChanges.subscribe(_ => { this._blur(); this.stateChanges.next(); }); } /** * @return {?} */ _listenToChipsRemoved() { this._chipRemoveSubscription = this.chipRemoveChanges.subscribe((event) => { this._updateKeyManager(event.chip); }); } } MdChipList.decorators = [ { type: Component, args: [{selector: 'md-chip-list, mat-chip-list', template: `<div class="mat-chip-list-wrapper"><ng-content></ng-content></div>`, exportAs: 'mdChipList, matChipList', host: { '[attr.tabindex]': '_tabIndex', '[attr.aria-describedby]': '_ariaDescribedby || null', '[attr.aria-required]': 'required.toString()', '[attr.aria-disabled]': 'disabled.toString()', '[attr.aria-invalid]': 'errorState', '[attr.aria-multiselectable]': 'multiple', '[class.mat-chip-list-disabled]': 'disabled', '[class.mat-chip-list-invalid]': 'errorState', '[class.mat-chip-list-required]': 'required', 'role': 'listbox', '[attr.aria-orientation]': 'ariaOrientation', 'class': 'mat-chip-list', '(focus)': 'focus()', '(blur)': '_blur()', '(keydown)': '_keydown($event)' }, providers: [{ provide: MdFormFieldControl, useExisting: MdChipList }], styles: [".mat-chip-list-wrapper{display:flex;flex-direction:row;flex-wrap:wrap;align-items:baseline}.mat-chip:not(.mat-basic-chip){transition:box-shadow 280ms cubic-bezier(.4,0,.2,1);display:inline-flex;padding:7px 12px;border-radius:24px;align-items:center;cursor:default}.mat-chip:not(.mat-basic-chip)+.mat-chip:not(.mat-basic-chip){margin:0 0 3px 8px}[dir=rtl] .mat-chip:not(.mat-basic-chip)+.mat-chip:not(.mat-basic-chip){margin:0 8px 3px 0}.mat-form-field-prefix .mat-chip:not(.mat-basic-chip):last-child{margin-right:8px}[dir=rtl] .mat-form-field-prefix .mat-chip:not(.mat-basic-chip):last-child{margin-left:8px}.mat-chip:not(.mat-basic-chip) .mat-chip-remove.mat-icon{width:1em;height:1em}.mat-chip:not(.mat-basic-chip):focus{box-shadow:0 3px 3px -2px rgba(0,0,0,.2),0 3px 4px 0 rgba(0,0,0,.14),0 1px 8px 0 rgba(0,0,0,.12);outline:0}@media screen and (-ms-high-contrast:active){.mat-chip:not(.mat-basic-chip){outline:solid 1px}}.mat-chip-list-stacked .mat-chip-list-wrapper{display:block}.mat-chip-list-stacked .mat-chip-list-wrapper .mat-chip:not(.mat-basic-chip){display:block;margin:0;margin-bottom:8px}[dir=rtl] .mat-chip-list-stacked .mat-chip-list-wrapper .mat-chip:not(.mat-basic-chip){margin:0;margin-bottom:8px}.mat-chip-list-stacked .mat-chip-list-wrapper .mat-chip:not(.mat-basic-chip):last-child,[dir=rtl] .mat-chip-list-stacked .mat-chip-list-wrapper .mat-chip:not(.mat-basic-chip):last-child{margin-bottom:0}.mat-form-field-prefix .mat-chip-list-wrapper{margin-bottom:8px}.mat-chip-remove{margin-right:-4px;margin-left:6px;cursor:pointer}[dir=rtl] .mat-chip-remove{margin-right:6px;margin-left:-4px}input.mat-chip-input{width:150px;margin:3px}"], encapsulation: ViewEncapsulation.None, preserveWhitespaces: false, changeDetection: ChangeDetectionStrategy.OnPush },] }, ]; /** * @nocollapse */ MdChipList.ctorParameters = () => [ { type: Renderer2, }, { type: ElementRef, }, { type: ChangeDetectorRef, }, { type: Directionality, decorators: [{ type: Optional },] }, { type: NgForm, decorators: [{ type: Optional },] }, { type: FormGroupDirective, decorators: [{ type: Optional },] }, { type: NgControl, decorators: [{ type: Optional }, { type: Self },] }, ]; MdChipList.propDecorators = { 'multiple': [{ type: Input },], 'compareWith': [{ type: Input },], 'value': [{ type: Input },], 'id': [{ type: Input },], 'required': [{ type: Input },], 'placeholder': [{ type: Input },], 'disabled': [{ type: Input },], 'ariaOrientation': [{ type: Input, args: ['aria-orientation',] },], 'selectable': [{ type: Input },], 'tabIndex': [{ type: Input },], 'change': [{ type: Output },], 'valueChange': [{ type: Output },], 'chips': [{ type: ContentChildren, args: [MdChip,] },], }; /** * Directive that adds chip-specific behaviors to an input element inside <md-form-field>. * May be placed inside or outside of an <md-chip-list>. */ class MdChipInput { /** * @param {?} _elementRef */ constructor(_elementRef) { this._elementRef = _elementRef; this.focused = false; this._addOnBlur = false; /** * The list of key codes that will trigger a chipEnd event. * * Defaults to `[ENTER]`. */ // TODO(tinayuangao): Support Set here this.separatorKeyCodes = [ENTER]; /** * Emitted when a chip is to be added. */ this.chipEnd = new EventEmitter(); this._matChipInputTokenEnd = this.chipEnd; this.placeholder = ''; this._inputElement = this._elementRef.nativeElement; } /** * Register input for chip list * @param {?} value * @return {?} */ set chipList(value) { if (value) { this._chipList = value; this._chipList.registerInput(this); } } /** * Whether or not the chipEnd event will be emitted when the input is blurred. * @return {?} */ get addOnBlur() { return this._addOnBlur; } /** * @param {?} value * @return {?} */ set addOnBlur(value) { this._addOnBlur = coerceBooleanProperty(value); } /** * @param {?} value * @return {?} */ set matChipList(value) { this.chipList = value; } /** * @return {?} */ get matAddOnBlur() { return this._addOnBlur; } /** * @param {?} value * @return {?} */ set matAddOnBlur(value) { this.addOnBlur = value; } /** * @return {?} */ get matSeparatorKeyCodes() { return this.separatorKeyCodes; } /** * @param {?} v * @return {?} */ set matSeparatorKeyCodes(v) { this.separatorKeyCodes = v; } /** * @return {?} */ get empty() { let /** @type {?} */ value = this._inputElement.value; return value == null || value === ''; } /** * Utility method to make host definition/tests more clear. * @param {?=} event * @return {?} */ _keydown(event) { this._emitChipEnd(event); } /** * Checks to see if the blur should emit the (chipEnd) event. * @return {?} */ _blur() { if (this.addOnBlur) { this._emitChipEnd(); } this.focused = false; // Blur the chip list if it is not focused if (!this._chipList.focused) { this._chipList._blur(); } this._chipList.stateChanges.next(); } /** * @return {?} */ _focus() { this.focused = true; this._chipList.stateChanges.next(); } /** * Checks to see if the (chipEnd) event needs to be emitted. * @param {?=} event * @return {?} */ _emitChipEnd(event) { if (!this._inputElement.value && !!event) { this._chipList._keydown(event); } if (!event || this.separatorKeyCodes.indexOf(event.keyCode) > -1) { this.chipEnd.emit({ input: this._inputElement, value: this._inputElement.value }); if (event) { event.preventDefault(); } } } /** * @return {?} */ focus() { this._inputElement.focus(); } } MdChipInput.decorators = [ { type: Directive, args: [{ selector: 'input[mdChipInputFor], input[matChipInputFor]', host: { 'class': 'mat-chip-input mat-input-element', '(keydown)': '_keydown($event)', '(blur)': '_blur()', '(focus)': '_focus()', } },] }, ]; /** * @nocollapse */ MdChipInput.ctorParameters = () => [ { type: ElementRef, }, ]; MdChipInput.propDecorators = { 'chipList': [{ type: Input, args: ['mdChipInputFor',] },], 'addOnBlur': [{ type: Input, args: ['mdChipInputAddOnBlur',] },], 'separatorKeyCodes': [{ type: Input, args: ['mdChipInputSeparatorKeyCodes',] },], 'chipEnd': [{ type: Output, args: ['mdChipInputTokenEnd',] },], '_matChipInputTokenEnd': [{ type: Output, args: ['matChipInputTokenEnd',] },], 'matChipList': [{ type: Input, args: ['matChipInputFor',] },], 'matAddOnBlur': [{ type: Input, args: ['matChipInputAddOnBlur',] },], 'matSeparatorKeyCodes': [{ type: Input, args: ['matChipInputSeparatorKeyCodes',] },], 'placeholder': [{ type: Input },], }; class MdChipsModule { } MdChipsModule.decorators = [ { type: NgModule, args: [{ imports: [], exports: [MdChipList, MdChip, MdChipInput, MdChipRemove, MdChipRemove, MdBasicChip], declarations: [MdChipList, MdChip, MdChipInput, MdChipRemove, MdChipRemove, MdBasicChip] },] }, ]; /** * @nocollapse */ MdChipsModule.ctorParameters = () => []; /** * Generated bundle index. Do not edit. */ export { MdChipsModule, MdChipListChange, MdChipList, MdChipSelectionChange, MdChipBase, _MdChipMixinBase, MdBasicChip, MdChip, MdChipRemove, MdChipInput, MdBasicChip as MatBasicChip, MdChip as MatChip, MdChipBase as MatChipBase, MdChipInput as MatChipInput, MdChipListChange as MatChipListChange, MdChipList as MatChipList, MdChipRemove as MatChipRemove, MdChipsModule as MatChipsModule }; //# sourceMappingURL=chips.js.map