/** * @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 */ import { Subject } from 'rxjs'; /** * Class to be used to power selecting one or more options from a list. */ export class SelectionModel { /** Selected values. */ get selected() { if (!this._selected) { this._selected = Array.from(this._selection.values()); } return this._selected; } constructor(_multiple = false, initiallySelectedValues, _emitChanges = true, compareWith) { this._multiple = _multiple; this._emitChanges = _emitChanges; this.compareWith = compareWith; /** Currently-selected values. */ this._selection = new Set(); /** Keeps track of the deselected options that haven't been emitted by the change event. */ this._deselectedToEmit = []; /** Keeps track of the selected options that haven't been emitted by the change event. */ this._selectedToEmit = []; /** Event emitted when the value has changed. */ this.changed = new Subject(); if (initiallySelectedValues && initiallySelectedValues.length) { if (_multiple) { initiallySelectedValues.forEach(value => this._markSelected(value)); } else { this._markSelected(initiallySelectedValues[0]); } // Clear the array in order to avoid firing the change event for preselected values. this._selectedToEmit.length = 0; } } /** * Selects a value or an array of values. * @param values The values to select * @return Whether the selection changed as a result of this call * @breaking-change 16.0.0 make return type boolean */ select(...values) { this._verifyValueAssignment(values); values.forEach(value => this._markSelected(value)); const changed = this._hasQueuedChanges(); this._emitChangeEvent(); return changed; } /** * Deselects a value or an array of values. * @param values The values to deselect * @return Whether the selection changed as a result of this call * @breaking-change 16.0.0 make return type boolean */ deselect(...values) { this._verifyValueAssignment(values); values.forEach(value => this._unmarkSelected(value)); const changed = this._hasQueuedChanges(); this._emitChangeEvent(); return changed; } /** * Sets the selected values * @param values The new selected values * @return Whether the selection changed as a result of this call * @breaking-change 16.0.0 make return type boolean */ setSelection(...values) { this._verifyValueAssignment(values); const oldValues = this.selected; const newSelectedSet = new Set(values); values.forEach(value => this._markSelected(value)); oldValues .filter(value => !newSelectedSet.has(value)) .forEach(value => this._unmarkSelected(value)); const changed = this._hasQueuedChanges(); this._emitChangeEvent(); return changed; } /** * Toggles a value between selected and deselected. * @param value The value to toggle * @return Whether the selection changed as a result of this call * @breaking-change 16.0.0 make return type boolean */ toggle(value) { return this.isSelected(value) ? this.deselect(value) : this.select(value); } /** * Clears all of the selected values. * @param flushEvent Whether to flush the changes in an event. * If false, the changes to the selection will be flushed along with the next event. * @return Whether the selection changed as a result of this call * @breaking-change 16.0.0 make return type boolean */ clear(flushEvent = true) { this._unmarkAll(); const changed = this._hasQueuedChanges(); if (flushEvent) { this._emitChangeEvent(); } return changed; } /** * Determines whether a value is selected. */ isSelected(value) { return this._selection.has(this._getConcreteValue(value)); } /** * Determines whether the model does not have a value. */ isEmpty() { return this._selection.size === 0; } /** * Determines whether the model has a value. */ hasValue() { return !this.isEmpty(); } /** * Sorts the selected values based on a predicate function. */ sort(predicate) { if (this._multiple && this.selected) { this._selected.sort(predicate); } } /** * Gets whether multiple values can be selected. */ isMultipleSelection() { return this._multiple; } /** Emits a change event and clears the records of selected and deselected values. */ _emitChangeEvent() { // Clear the selected values so they can be re-cached. this._selected = null; if (this._selectedToEmit.length || this._deselectedToEmit.length) { this.changed.next({ source: this, added: this._selectedToEmit, removed: this._deselectedToEmit, }); this._deselectedToEmit = []; this._selectedToEmit = []; } } /** Selects a value. */ _markSelected(value) { value = this._getConcreteValue(value); if (!this.isSelected(value)) { if (!this._multiple) { this._unmarkAll(); } if (!this.isSelected(value)) { this._selection.add(value); } if (this._emitChanges) { this._selectedToEmit.push(value); } } } /** Deselects a value. */ _unmarkSelected(value) { value = this._getConcreteValue(value); if (this.isSelected(value)) { this._selection.delete(value); if (this._emitChanges) { this._deselectedToEmit.push(value); } } } /** Clears out the selected values. */ _unmarkAll() { if (!this.isEmpty()) { this._selection.forEach(value => this._unmarkSelected(value)); } } /** * Verifies the value assignment and throws an error if the specified value array is * including multiple values while the selection model is not supporting multiple values. */ _verifyValueAssignment(values) { if (values.length > 1 && !this._multiple && (typeof ngDevMode === 'undefined' || ngDevMode)) { throw getMultipleValuesInSingleSelectionError(); } } /** Whether there are queued up change to be emitted. */ _hasQueuedChanges() { return !!(this._deselectedToEmit.length || this._selectedToEmit.length); } /** Returns a value that is comparable to inputValue by applying compareWith function, returns the same inputValue otherwise. */ _getConcreteValue(inputValue) { if (!this.compareWith) { return inputValue; } else { for (let selectedValue of this._selection) { if (this.compareWith(inputValue, selectedValue)) { return selectedValue; } } return inputValue; } } } /** * Returns an error that reports that multiple values are passed into a selection model * with a single value. * @docs-private */ export function getMultipleValuesInSingleSelectionError() { return Error('Cannot pass multiple values into SelectionModel with single-value mode.'); } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2VsZWN0aW9uLW1vZGVsLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vLi4vc3JjL2Nkay9jb2xsZWN0aW9ucy9zZWxlY3Rpb24tbW9kZWwudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7OztHQU1HO0FBRUgsT0FBTyxFQUFDLE9BQU8sRUFBQyxNQUFNLE1BQU0sQ0FBQztBQUU3Qjs7R0FFRztBQUNILE1BQU0sT0FBTyxjQUFjO0lBYXpCLHVCQUF1QjtJQUN2QixJQUFJLFFBQVE7UUFDVixJQUFJLENBQUMsSUFBSSxDQUFDLFNBQVMsRUFBRTtZQUNuQixJQUFJLENBQUMsU0FBUyxHQUFHLEtBQUssQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLFVBQVUsQ0FBQyxNQUFNLEVBQUUsQ0FBQyxDQUFDO1NBQ3ZEO1FBRUQsT0FBTyxJQUFJLENBQUMsU0FBUyxDQUFDO0lBQ3hCLENBQUM7SUFLRCxZQUNVLFlBQVksS0FBSyxFQUN6Qix1QkFBNkIsRUFDckIsZUFBZSxJQUFJLEVBQ3BCLFdBQXVDO1FBSHRDLGNBQVMsR0FBVCxTQUFTLENBQVE7UUFFakIsaUJBQVksR0FBWixZQUFZLENBQU87UUFDcEIsZ0JBQVcsR0FBWCxXQUFXLENBQTRCO1FBNUJoRCxpQ0FBaUM7UUFDekIsZ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