UNPKG

@angular/material

Version:
1,070 lines (1,042 loc) 77.1 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/cdk/keycodes'), require('@angular/core'), require('@angular/material/core'), require('@angular/common'), require('@angular/cdk/coercion'), require('@angular/cdk/platform'), require('rxjs'), require('rxjs/operators'), require('@angular/platform-browser/animations'), require('@angular/cdk/a11y'), require('@angular/cdk/bidi'), require('@angular/cdk/collections'), require('@angular/forms'), require('@angular/material/form-field')) : typeof define === 'function' && define.amd ? define('@angular/material/chips', ['exports', '@angular/cdk/keycodes', '@angular/core', '@angular/material/core', '@angular/common', '@angular/cdk/coercion', '@angular/cdk/platform', 'rxjs', 'rxjs/operators', '@angular/platform-browser/animations', '@angular/cdk/a11y', '@angular/cdk/bidi', '@angular/cdk/collections', '@angular/forms', '@angular/material/form-field'], factory) : (global = global || self, factory((global.ng = global.ng || {}, global.ng.material = global.ng.material || {}, global.ng.material.chips = {}), global.ng.cdk.keycodes, global.ng.core, global.ng.material.core, global.ng.common, global.ng.cdk.coercion, global.ng.cdk.platform, global.rxjs, global.rxjs.operators, global.ng.platformBrowser.animations, global.ng.cdk.a11y, global.ng.cdk.bidi, global.ng.cdk.collections, global.ng.forms, global.ng.material.formField)); }(this, (function (exports, keycodes, core, core$1, common, coercion, platform, rxjs, operators, animations, a11y, bidi, collections, forms, formField) { 'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ /* global Reflect, Promise */ var extendStatics = function(d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; function __extends(d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } function __decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } function __param(paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } } function __metadata(metadataKey, metadataValue) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue); } function __awaiter(thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } function __exportStar(m, exports) { for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; } function __values(o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; } function __read(o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; } function __spread() { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; } function __spreadArrays() { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; function __await(v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } function __asyncGenerator(thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } } function __asyncDelegator(o) { var i, p; return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; } } function __asyncValues(o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } } function __makeTemplateObject(cooked, raw) { if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } return cooked; }; function __importStar(mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result.default = mod; return result; } function __importDefault(mod) { return (mod && mod.__esModule) ? mod : { default: mod }; } /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ /** Event object emitted by MatChip when selected or deselected. */ var MatChipSelectionChange = /** @class */ (function () { function MatChipSelectionChange( /** Reference to the chip that emitted the event. */ source, /** Whether the chip that emitted the event is selected. */ selected, /** Whether the selection change was a result of a user interaction. */ isUserInput) { if (isUserInput === void 0) { isUserInput = false; } this.source = source; this.selected = selected; this.isUserInput = isUserInput; } return MatChipSelectionChange; }()); // Boilerplate for applying mixins to MatChip. /** @docs-private */ var MatChipBase = /** @class */ (function () { function MatChipBase(_elementRef) { this._elementRef = _elementRef; } return MatChipBase; }()); var _MatChipMixinBase = core$1.mixinTabIndex(core$1.mixinColor(core$1.mixinDisableRipple(core$1.mixinDisabled(MatChipBase)), 'primary'), -1); /** * Dummy directive to add CSS class to chip avatar. * @docs-private */ var MatChipAvatar = /** @class */ (function () { function MatChipAvatar() { } MatChipAvatar.decorators = [ { type: core.Directive, args: [{ selector: 'mat-chip-avatar, [matChipAvatar]', host: { 'class': 'mat-chip-avatar' } },] } ]; return MatChipAvatar; }()); /** * Dummy directive to add CSS class to chip trailing icon. * @docs-private */ var MatChipTrailingIcon = /** @class */ (function () { function MatChipTrailingIcon() { } MatChipTrailingIcon.decorators = [ { type: core.Directive, args: [{ selector: 'mat-chip-trailing-icon, [matChipTrailingIcon]', host: { 'class': 'mat-chip-trailing-icon' } },] } ]; return MatChipTrailingIcon; }()); /** * Material design styled Chip component. Used inside the MatChipList component. */ var MatChip = /** @class */ (function (_super) { __extends(MatChip, _super); function MatChip(_elementRef, _ngZone, platform, globalRippleOptions, // @breaking-change 8.0.0 `animationMode` parameter to become required. animationMode, // @breaking-change 9.0.0 `_changeDetectorRef` parameter to become required. _changeDetectorRef, tabIndex, // @breaking-change 11.0.0 `_document` parameter to become required. _document) { var _this = _super.call(this, _elementRef) || this; _this._elementRef = _elementRef; _this._ngZone = _ngZone; _this._changeDetectorRef = _changeDetectorRef; /** Whether the chip has focus. */ _this._hasFocus = false; /** Whether the chip list is selectable */ _this.chipListSelectable = true; /** Whether the chip list is in multi-selection mode. */ _this._chipListMultiple = false; _this._selected = false; _this._selectable = true; _this._removable = true; /** Emits when the chip is focused. */ _this._onFocus = new rxjs.Subject(); /** Emits when the chip is blured. */ _this._onBlur = new rxjs.Subject(); /** Emitted when the chip is selected or deselected. */ _this.selectionChange = new core.EventEmitter(); /** Emitted when the chip is destroyed. */ _this.destroyed = new core.EventEmitter(); /** Emitted when a chip is to be removed. */ _this.removed = new core.EventEmitter(); _this._addHostClassName(); // Dynamically create the ripple target, append it within the chip, and use it as the // chip's ripple target. Adding the class '.mat-chip-ripple' ensures that it will have // the proper styles. _this._chipRippleTarget = (_document || document).createElement('div'); _this._chipRippleTarget.classList.add('mat-chip-ripple'); _this._elementRef.nativeElement.appendChild(_this._chipRippleTarget); _this._chipRipple = new core$1.RippleRenderer(_this, _ngZone, _this._chipRippleTarget, platform); _this._chipRipple.setupTriggerEvents(_elementRef); _this.rippleConfig = globalRippleOptions || {}; _this._animationsDisabled = animationMode === 'NoopAnimations'; _this.tabIndex = tabIndex != null ? (parseInt(tabIndex) || -1) : -1; return _this; } Object.defineProperty(MatChip.prototype, "rippleDisabled", { /** * Whether ripples are disabled on interaction * @docs-private */ get: function () { return this.disabled || this.disableRipple || !!this.rippleConfig.disabled; }, enumerable: true, configurable: true }); Object.defineProperty(MatChip.prototype, "selected", { /** Whether the chip is selected. */ get: function () { return this._selected; }, set: function (value) { var coercedValue = coercion.coerceBooleanProperty(value); if (coercedValue !== this._selected) { this._selected = coercedValue; this._dispatchSelectionChange(); } }, enumerable: true, configurable: true }); Object.defineProperty(MatChip.prototype, "value", { /** The value of the chip. Defaults to the content inside `<mat-chip>` tags. */ get: function () { return this._value !== undefined ? this._value : this._elementRef.nativeElement.textContent; }, set: function (value) { this._value = value; }, enumerable: true, configurable: true }); Object.defineProperty(MatChip.prototype, "selectable", { /** * Whether or not the chip is selectable. When a chip is not selectable, * changes to its selected state are always ignored. By default a chip is * selectable, and it becomes non-selectable if its parent chip list is * not selectable. */ get: function () { return this._selectable && this.chipListSelectable; }, set: function (value) { this._selectable = coercion.coerceBooleanProperty(value); }, enumerable: true, configurable: true }); Object.defineProperty(MatChip.prototype, "removable", { /** * Determines whether or not the chip displays the remove styling and emits (removed) events. */ get: function () { return this._removable; }, set: function (value) { this._removable = coercion.coerceBooleanProperty(value); }, enumerable: true, configurable: true }); Object.defineProperty(MatChip.prototype, "ariaSelected", { /** The ARIA selected applied to the chip. */ get: function () { // Remove the `aria-selected` when the chip is deselected in single-selection mode, because // it adds noise to NVDA users where "not selected" will be read out for each chip. return this.selectable && (this._chipListMultiple || this.selected) ? this.selected.toString() : null; }, enumerable: true, configurable: true }); MatChip.prototype._addHostClassName = function () { var basicChipAttrName = 'mat-basic-chip'; var element = this._elementRef.nativeElement; if (element.hasAttribute(basicChipAttrName) || element.tagName.toLowerCase() === basicChipAttrName) { element.classList.add(basicChipAttrName); return; } else { element.classList.add('mat-standard-chip'); } }; MatChip.prototype.ngOnDestroy = function () { this.destroyed.emit({ chip: this }); this._chipRipple._removeTriggerEvents(); }; /** Selects the chip. */ MatChip.prototype.select = function () { if (!this._selected) { this._selected = true; this._dispatchSelectionChange(); this._markForCheck(); } }; /** Deselects the chip. */ MatChip.prototype.deselect = function () { if (this._selected) { this._selected = false; this._dispatchSelectionChange(); this._markForCheck(); } }; /** Select this chip and emit selected event */ MatChip.prototype.selectViaInteraction = function () { if (!this._selected) { this._selected = true; this._dispatchSelectionChange(true); this._markForCheck(); } }; /** Toggles the current selected state of this chip. */ MatChip.prototype.toggleSelected = function (isUserInput) { if (isUserInput === void 0) { isUserInput = false; } this._selected = !this.selected; this._dispatchSelectionChange(isUserInput); this._markForCheck(); return this.selected; }; /** Allows for programmatic focusing of the chip. */ MatChip.prototype.focus = function () { if (!this._hasFocus) { this._elementRef.nativeElement.focus(); this._onFocus.next({ chip: this }); } this._hasFocus = true; }; /** * Allows for programmatic removal of the chip. Called by the MatChipList when the DELETE or * BACKSPACE keys are pressed. * * Informs any listeners of the removal request. Does not remove the chip from the DOM. */ MatChip.prototype.remove = function () { if (this.removable) { this.removed.emit({ chip: this }); } }; /** Handles click events on the chip. */ MatChip.prototype._handleClick = function (event) { if (this.disabled) { event.preventDefault(); } else { event.stopPropagation(); } }; /** Handle custom key presses. */ MatChip.prototype._handleKeydown = function (event) { if (this.disabled) { return; } switch (event.keyCode) { case keycodes.DELETE: case keycodes.BACKSPACE: // If we are removable, remove the focused chip this.remove(); // Always prevent so page navigation does not occur event.preventDefault(); break; case keycodes.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; } }; MatChip.prototype._blur = function () { var _this = this; // When animations are enabled, Angular may end up removing the chip from the DOM a little // earlier than usual, causing it to be blurred and throwing off the logic in the chip list // that moves focus not the next item. To work around the issue, we defer marking the chip // as not focused until the next time the zone stabilizes. this._ngZone.onStable .asObservable() .pipe(operators.take(1)) .subscribe(function () { _this._ngZone.run(function () { _this._hasFocus = false; _this._onBlur.next({ chip: _this }); }); }); }; MatChip.prototype._dispatchSelectionChange = function (isUserInput) { if (isUserInput === void 0) { isUserInput = false; } this.selectionChange.emit({ source: this, isUserInput: isUserInput, selected: this._selected }); }; MatChip.prototype._markForCheck = function () { // @breaking-change 9.0.0 Remove this method once the _changeDetectorRef is a required param. if (this._changeDetectorRef) { this._changeDetectorRef.markForCheck(); } }; MatChip.decorators = [ { type: core.Directive, args: [{ selector: "mat-basic-chip, [mat-basic-chip], mat-chip, [mat-chip]", inputs: ['color', 'disabled', 'disableRipple', 'tabIndex'], exportAs: 'matChip', host: { 'class': 'mat-chip mat-focus-indicator', '[attr.tabindex]': 'disabled ? null : tabIndex', 'role': 'option', '[class.mat-chip-selected]': 'selected', '[class.mat-chip-with-avatar]': 'avatar', '[class.mat-chip-with-trailing-icon]': 'trailingIcon || removeIcon', '[class.mat-chip-disabled]': 'disabled', '[class._mat-animation-noopable]': '_animationsDisabled', '[attr.disabled]': 'disabled || null', '[attr.aria-disabled]': 'disabled.toString()', '[attr.aria-selected]': 'ariaSelected', '(click)': '_handleClick($event)', '(keydown)': '_handleKeydown($event)', '(focus)': 'focus()', '(blur)': '_blur()', }, },] } ]; /** @nocollapse */ MatChip.ctorParameters = function () { return [ { type: core.ElementRef }, { type: core.NgZone }, { type: platform.Platform }, { type: undefined, decorators: [{ type: core.Optional }, { type: core.Inject, args: [core$1.MAT_RIPPLE_GLOBAL_OPTIONS,] }] }, { type: String, decorators: [{ type: core.Optional }, { type: core.Inject, args: [animations.ANIMATION_MODULE_TYPE,] }] }, { type: core.ChangeDetectorRef }, { type: String, decorators: [{ type: core.Attribute, args: ['tabindex',] }] }, { type: undefined, decorators: [{ type: core.Optional }, { type: core.Inject, args: [common.DOCUMENT,] }] } ]; }; MatChip.propDecorators = { avatar: [{ type: core.ContentChild, args: [MatChipAvatar,] }], trailingIcon: [{ type: core.ContentChild, args: [MatChipTrailingIcon,] }], removeIcon: [{ type: core.ContentChild, args: [core.forwardRef(function () { return MatChipRemove; }),] }], selected: [{ type: core.Input }], value: [{ type: core.Input }], selectable: [{ type: core.Input }], removable: [{ type: core.Input }], selectionChange: [{ type: core.Output }], destroyed: [{ type: core.Output }], removed: [{ type: core.Output }] }; return MatChip; }(_MatChipMixinBase)); /** * Applies proper (click) support and adds styling for use with the Material Design "cancel" icon * available at https://material.io/icons/#ic_cancel. * * Example: * * `<mat-chip> * <mat-icon matChipRemove>cancel</mat-icon> * </mat-chip>` * * You *may* use a custom icon, but you may need to override the `mat-chip-remove` positioning * styles to properly center the icon within the chip. */ var MatChipRemove = /** @class */ (function () { function MatChipRemove(_parentChip, // @breaking-change 11.0.0 `elementRef` parameter to be made required. elementRef) { this._parentChip = _parentChip; // @breaking-change 11.0.0 Remove null check for `elementRef`. if (elementRef && elementRef.nativeElement.nodeName === 'BUTTON') { elementRef.nativeElement.setAttribute('type', 'button'); } } /** Calls the parent chip's public `remove()` method if applicable. */ MatChipRemove.prototype._handleClick = function (event) { var parentChip = this._parentChip; if (parentChip.removable && !parentChip.disabled) { parentChip.remove(); } // We need to stop event propagation because otherwise the event will bubble up to the // form field and cause the `onContainerClick` method to be invoked. This method would then // reset the focused chip that has been focused after chip removal. Usually the parent // the parent click listener of the `MatChip` would prevent propagation, but it can happen // that the chip is being removed before the event bubbles up. event.stopPropagation(); }; MatChipRemove.decorators = [ { type: core.Directive, args: [{ selector: '[matChipRemove]', host: { 'class': 'mat-chip-remove mat-chip-trailing-icon', '(click)': '_handleClick($event)', } },] } ]; /** @nocollapse */ MatChipRemove.ctorParameters = function () { return [ { type: MatChip }, { type: core.ElementRef } ]; }; return MatChipRemove; }()); /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ /** Injection token to be used to override the default options for the chips module. */ var MAT_CHIPS_DEFAULT_OPTIONS = new core.InjectionToken('mat-chips-default-options'); /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ // Boilerplate for applying mixins to MatChipList. /** @docs-private */ var MatChipListBase = /** @class */ (function () { function MatChipListBase(_defaultErrorStateMatcher, _parentForm, _parentFormGroup, /** @docs-private */ ngControl) { this._defaultErrorStateMatcher = _defaultErrorStateMatcher; this._parentForm = _parentForm; this._parentFormGroup = _parentFormGroup; this.ngControl = ngControl; } return MatChipListBase; }()); var _MatChipListMixinBase = core$1.mixinErrorState(MatChipListBase); // Increasing integer for generating unique ids for chip-list components. var nextUniqueId = 0; /** Change event object that is emitted when the chip list value has changed. */ var MatChipListChange = /** @class */ (function () { function MatChipListChange( /** Chip list that emitted the event. */ source, /** Value of the chip list when the event was emitted. */ value) { this.source = source; this.value = value; } return MatChipListChange; }()); /** * A material design chips component (named ChipList for its similarity to the List component). */ var MatChipList = /** @class */ (function (_super) { __extends(MatChipList, _super); function MatChipList(_elementRef, _changeDetectorRef, _dir, _parentForm, _parentFormGroup, _defaultErrorStateMatcher, /** @docs-private */ ngControl) { var _this = _super.call(this, _defaultErrorStateMatcher, _parentForm, _parentFormGroup, ngControl) || this; _this._elementRef = _elementRef; _this._changeDetectorRef = _changeDetectorRef; _this._dir = _dir; _this.ngControl = ngControl; /** * Implemented as part of MatFormFieldControl. * @docs-private */ _this.controlType = 'mat-chip-list'; /** * When a chip is destroyed, we store the index of the destroyed chip until the chips * query list notifies about the update. This is necessary because we cannot determine an * appropriate chip that should receive focus until the array of chips updated completely. */ _this._lastDestroyedChipIndex = null; /** Subject that emits when the component has been destroyed. */ _this._destroyed = new rxjs.Subject(); /** Uid of the chip list */ _this._uid = "mat-chip-list-" + nextUniqueId++; /** 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 () { }; /** Function when changed */ _this._onChange = function () { }; _this._multiple = false; _this._compareWith = function (o1, o2) { return o1 === o2; }; _this._required = false; _this._disabled = false; /** Orientation of the chip list. */ _this.ariaOrientation = 'horizontal'; _this._selectable = true; /** Event emitted when the selected chip list value has been changed by the user. */ _this.change = new core.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 core.EventEmitter(); if (_this.ngControl) { _this.ngControl.valueAccessor = _this; } return _this; } Object.defineProperty(MatChipList.prototype, "selected", { /** The array of selected chips inside chip list. */ get: function () { return this.multiple ? this._selectionModel.selected : this._selectionModel.selected[0]; }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "role", { /** The ARIA role applied to the chip list. */ get: function () { return this.empty ? null : 'listbox'; }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "multiple", { /** Whether the user should be allowed to select multiple chips. */ get: function () { return this._multiple; }, set: function (value) { this._multiple = coercion.coerceBooleanProperty(value); this._syncChipsState(); }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "compareWith", { /** * 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. */ get: function () { return this._compareWith; }, set: function (fn) { this._compareWith = fn; if (this._selectionModel) { // A different comparator means the selection could change. this._initializeSelection(); } }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "value", { /** * Implemented as part of MatFormFieldControl. * @docs-private */ get: function () { return this._value; }, set: function (value) { this.writeValue(value); this._value = value; }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "id", { /** * Implemented as part of MatFormFieldControl. * @docs-private */ get: function () { return this._chipInput ? this._chipInput.id : this._uid; }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "required", { /** * Implemented as part of MatFormFieldControl. * @docs-private */ get: function () { return this._required; }, set: function (value) { this._required = coercion.coerceBooleanProperty(value); this.stateChanges.next(); }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "placeholder", { /** * Implemented as part of MatFormFieldControl. * @docs-private */ get: function () { return this._chipInput ? this._chipInput.placeholder : this._placeholder; }, set: function (value) { this._placeholder = value; this.stateChanges.next(); }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "focused", { /** Whether any chips or the matChipInput inside of this chip-list has focus. */ get: function () { return (this._chipInput && this._chipInput.focused) || this._hasFocusedChip(); }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "empty", { /** * Implemented as part of MatFormFieldControl. * @docs-private */ get: function () { return (!this._chipInput || this._chipInput.empty) && this.chips.length === 0; }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "shouldLabelFloat", { /** * Implemented as part of MatFormFieldControl. * @docs-private */ get: function () { return !this.empty || this.focused; }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "disabled", { /** * Implemented as part of MatFormFieldControl. * @docs-private */ get: function () { return this.ngControl ? !!this.ngControl.disabled : this._disabled; }, set: function (value) { this._disabled = coercion.coerceBooleanProperty(value); this._syncChipsState(); }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "selectable", { /** * Whether or not this chip list is selectable. When a chip list is not selectable, * the selected states for all the chips inside the chip list are always ignored. */ get: function () { return this._selectable; }, set: function (value) { var _this = this; this._selectable = coercion.coerceBooleanProperty(value); if (this.chips) { this.chips.forEach(function (chip) { return chip.chipListSelectable = _this._selectable; }); } }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "tabIndex", { set: function (value) { this._userTabIndex = value; this._tabIndex = value; }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "chipSelectionChanges", { /** Combined stream of all of the child chips' selection change events. */ get: function () { return rxjs.merge.apply(void 0, __spread(this.chips.map(function (chip) { return chip.selectionChange; }))); }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "chipFocusChanges", { /** Combined stream of all of the child chips' focus change events. */ get: function () { return rxjs.merge.apply(void 0, __spread(this.chips.map(function (chip) { return chip._onFocus; }))); }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "chipBlurChanges", { /** Combined stream of all of the child chips' blur change events. */ get: function () { return rxjs.merge.apply(void 0, __spread(this.chips.map(function (chip) { return chip._onBlur; }))); }, enumerable: true, configurable: true }); Object.defineProperty(MatChipList.prototype, "chipRemoveChanges", { /** Combined stream of all of the child chips' remove change events. */ get: function () { return rxjs.merge.apply(void 0, __spread(this.chips.map(function (chip) { return chip.destroyed; }))); }, enumerable: true, configurable: true }); MatChipList.prototype.ngAfterContentInit = function () { var _this = this; this._keyManager = new a11y.FocusKeyManager(this.chips) .withWrap() .withVerticalOrientation() .withHorizontalOrientation(this._dir ? this._dir.value : 'ltr'); if (this._dir) { this._dir.change .pipe(operators.takeUntil(this._destroyed)) .subscribe(function (dir) { return _this._keyManager.withHorizontalOrientation(dir); }); } this._keyManager.tabOut.pipe(operators.takeUntil(this._destroyed)).subscribe(function () { _this._allowFocusEscape(); }); // When the list changes, re-subscribe this.chips.changes.pipe(operators.startWith(null), operators.takeUntil(this._destroyed)).subscribe(function () { if (_this.disabled) { // Since this happens after the content has been // checked, we need to defer it to the next tick. Promise.resolve().then(function () { _this._syncChipsState(); }); } _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(); _this.stateChanges.next(); }); }; MatChipList.prototype.ngOnInit = function () { this._selectionModel = new collections.SelectionModel(this.multiple, undefined, false); this.stateChanges.next(); }; MatChipList.prototype.ngDoCheck = function () { if (this.ngControl) { // We need to re-evaluate this on every change detection cycle, because there are some // error triggers that we can't subscribe to (e.g. parent form submissions). This means // that whatever logic is in here has to be super lean or we risk destroying the performance. this.updateErrorState(); } }; MatChipList.prototype.ngOnDestroy = function () { this._destroyed.next(); this._destroyed.complete(); this.stateChanges.complete(); this._dropSubscriptions(); }; /** Associates an HTML input element with this chip list. */ MatChipList.prototype.registerInput = function (inputElement) { this._chipInput = inputElement; }; /** * Implemented as part of MatFormFieldControl. * @docs-private */ MatChipList.prototype.setDescribedByIds = function (ids) { this._ariaDescribedby = ids.join(' '); }; // Implemented as part of ControlValueAccessor. MatChipList.prototype.writeValue = function (value) { if (this.chips) { this._setSelectionByValue(value, false); } }; // Implemented as part of ControlValueAccessor. MatChipList.prototype.registerOnChange = function (fn) { this._onChange = fn; }; // Implemented as part of ControlValueAccessor. MatChipList.prototype.registerOnTouched = function (fn) { this._onTouched = fn; }; // Implemented as part of ControlValueAccessor. MatChipList.prototype.setDisabledState = function (isDisabled) { this.disabled = isDisabled; this.stateChanges.next(); }; /** * Implemented as part of MatFormFieldControl. * @docs-private */ MatChipList.prototype.onContainerClick = function (event) { if (!this._originatesFromChip(event)) { this.focus(); } }; /** * Focuses the first non-disabled chip in this chip list, or the associated input when there * are no eligible chips. */ MatChipList.prototype.focus = function (options) { if (this.disabled) { return; } // 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(options); this.stateChanges.next(); } }; /** Attempt to focus an input if we have one. */ MatChipList.prototype._focusInput = function (options) { if (this._chipInput) { this._chipInput.focus(options); } }; /** * Pass events to the keyboard manager. Available here for tests. */ MatChipList.prototype._keydown = function (event) { var target = event.target; // If they are on an empty input and hit backspace, focus the last chip if (event.keyCode === keycodes.BACKSPACE && this._isInputEmpty(target)) { this._keyManager.setLastItemActive(); event.preventDefault(); } else if (target && target.classList.contains('mat-chip')) { if (event.keyCode === keycodes.HOME) { this._keyManager.setFirstItemActive(); event.preventDefault(); } else if (event.keyCode === keycodes.END) { this._keyManager.setLastItemActive(); 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. */ MatChipList.prototype._updateTabIndex = function () { // If we have 0 chips, we should not allow keyboard focus this._tabIndex = this._userTabIndex || (this.chips.length === 0 ? -1 : 0); }; /** * If the amount of chips changed, we need to update the * key manager state and focus the next closest chip. */ MatChipList.prototype._updateFocusForDestroyedChips = function () { // Move focus to the closest chip. If no other chips remain, focus the chip-list itself. if (this._lastDestroyedChipIndex != null) { if (this.chips.length) { var newChipIndex = Math.min(this._lastDestroyedChipIndex, this.chips.length - 1); this._keyManager.setActiveItem(newChipIndex); }