UNPKG

@angular/cdk

Version:

Angular Material Component Development Kit

1 lines 13.7 kB
{"version":3,"file":"_selection-model-chunk.mjs","sources":["../../../../../k8-fastbuild-ST-199a4f3c4e20/bin/src/cdk/collections/selection-model.ts"],"sourcesContent":["/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {Subject} from 'rxjs';\n\n/**\n * Class to be used to power selecting one or more options from a list.\n */\nexport class SelectionModel<T> {\n /** Currently-selected values. */\n private _selection = new Set<T>();\n\n /** Keeps track of the deselected options that haven't been emitted by the change event. */\n private _deselectedToEmit: T[] = [];\n\n /** Keeps track of the selected options that haven't been emitted by the change event. */\n private _selectedToEmit: T[] = [];\n\n /** Cache for the array value of the selected items. */\n private _selected: T[] | null;\n\n /** Selected values. */\n get selected(): T[] {\n if (!this._selected) {\n this._selected = Array.from(this._selection.values());\n }\n\n return this._selected;\n }\n\n /** Event emitted when the value has changed. */\n readonly changed = new Subject<SelectionChange<T>>();\n\n constructor(\n private _multiple = false,\n initiallySelectedValues?: T[],\n private _emitChanges = true,\n public compareWith?: (o1: T, o2: T) => boolean,\n ) {\n if (initiallySelectedValues && initiallySelectedValues.length) {\n if (_multiple) {\n initiallySelectedValues.forEach(value => this._markSelected(value));\n } else {\n this._markSelected(initiallySelectedValues[0]);\n }\n\n // Clear the array in order to avoid firing the change event for preselected values.\n this._selectedToEmit.length = 0;\n }\n }\n\n /**\n * Selects a value or an array of values.\n * @param values The values to select\n * @return Whether the selection changed as a result of this call\n */\n select(...values: T[]): boolean {\n this._verifyValueAssignment(values);\n values.forEach(value => this._markSelected(value));\n const changed = this._hasQueuedChanges();\n this._emitChangeEvent();\n return changed;\n }\n\n /**\n * Deselects a value or an array of values.\n * @param values The values to deselect\n * @return Whether the selection changed as a result of this call\n */\n deselect(...values: T[]): boolean {\n this._verifyValueAssignment(values);\n values.forEach(value => this._unmarkSelected(value));\n const changed = this._hasQueuedChanges();\n this._emitChangeEvent();\n return changed;\n }\n\n /**\n * Sets the selected values\n * @param values The new selected values\n * @return Whether the selection changed as a result of this call\n */\n setSelection(...values: T[]): boolean {\n this._verifyValueAssignment(values);\n const oldValues = this.selected;\n const newSelectedSet = new Set(values.map(value => this._getConcreteValue(value)));\n values.forEach(value => this._markSelected(value));\n oldValues\n .filter(value => !newSelectedSet.has(this._getConcreteValue(value, newSelectedSet)))\n .forEach(value => this._unmarkSelected(value));\n const changed = this._hasQueuedChanges();\n this._emitChangeEvent();\n return changed;\n }\n\n /**\n * Toggles a value between selected and deselected.\n * @param value The value to toggle\n * @return Whether the selection changed as a result of this call\n */\n toggle(value: T): boolean {\n return this.isSelected(value) ? this.deselect(value) : this.select(value);\n }\n\n /**\n * Clears all of the selected values.\n * @param flushEvent Whether to flush the changes in an event.\n * If false, the changes to the selection will be flushed along with the next event.\n * @return Whether the selection changed as a result of this call\n */\n clear(flushEvent = true): boolean {\n this._unmarkAll();\n const changed = this._hasQueuedChanges();\n if (flushEvent) {\n this._emitChangeEvent();\n }\n return changed;\n }\n\n /**\n * Determines whether a value is selected.\n */\n isSelected(value: T): boolean {\n return this._selection.has(this._getConcreteValue(value));\n }\n\n /**\n * Determines whether the model does not have a value.\n */\n isEmpty(): boolean {\n return this._selection.size === 0;\n }\n\n /**\n * Determines whether the model has a value.\n */\n hasValue(): boolean {\n return !this.isEmpty();\n }\n\n /**\n * Sorts the selected values based on a predicate function.\n */\n sort(predicate?: (a: T, b: T) => number): void {\n if (this._multiple && this.selected) {\n this._selected!.sort(predicate);\n }\n }\n\n /**\n * Gets whether multiple values can be selected.\n */\n isMultipleSelection() {\n return this._multiple;\n }\n\n /** Emits a change event and clears the records of selected and deselected values. */\n private _emitChangeEvent() {\n // Clear the selected values so they can be re-cached.\n this._selected = null;\n\n if (this._selectedToEmit.length || this._deselectedToEmit.length) {\n this.changed.next({\n source: this,\n added: this._selectedToEmit,\n removed: this._deselectedToEmit,\n });\n\n this._deselectedToEmit = [];\n this._selectedToEmit = [];\n }\n }\n\n /** Selects a value. */\n private _markSelected(value: T) {\n value = this._getConcreteValue(value);\n if (!this.isSelected(value)) {\n if (!this._multiple) {\n this._unmarkAll();\n }\n\n if (!this.isSelected(value)) {\n this._selection.add(value);\n }\n\n if (this._emitChanges) {\n this._selectedToEmit.push(value);\n }\n }\n }\n\n /** Deselects a value. */\n private _unmarkSelected(value: T) {\n value = this._getConcreteValue(value);\n if (this.isSelected(value)) {\n this._selection.delete(value);\n\n if (this._emitChanges) {\n this._deselectedToEmit.push(value);\n }\n }\n }\n\n /** Clears out the selected values. */\n private _unmarkAll() {\n if (!this.isEmpty()) {\n this._selection.forEach(value => this._unmarkSelected(value));\n }\n }\n\n /**\n * Verifies the value assignment and throws an error if the specified value array is\n * including multiple values while the selection model is not supporting multiple values.\n */\n private _verifyValueAssignment(values: T[]) {\n if (values.length > 1 && !this._multiple && (typeof ngDevMode === 'undefined' || ngDevMode)) {\n throw getMultipleValuesInSingleSelectionError();\n }\n }\n\n /** Whether there are queued up change to be emitted. */\n private _hasQueuedChanges() {\n return !!(this._deselectedToEmit.length || this._selectedToEmit.length);\n }\n\n /** Returns a value that is comparable to inputValue by applying compareWith function, returns the same inputValue otherwise. */\n private _getConcreteValue(inputValue: T, selection?: Set<T>): T {\n if (!this.compareWith) {\n return inputValue;\n } else {\n selection = selection ?? this._selection;\n for (let selectedValue of selection) {\n if (this.compareWith!(inputValue, selectedValue)) {\n return selectedValue;\n }\n }\n return inputValue;\n }\n }\n}\n\n/**\n * Event emitted when the value of a MatSelectionModel has changed.\n * @docs-private\n */\nexport interface SelectionChange<T> {\n /** Model that dispatched the event. */\n source: SelectionModel<T>;\n /** Options that were added to the model. */\n added: T[];\n /** Options that were removed from the model. */\n removed: T[];\n}\n\n/**\n * Returns an error that reports that multiple values are passed into a selection model\n * with a single value.\n * @docs-private\n */\nexport function getMultipleValuesInSingleSelectionError() {\n return Error('Cannot pass multiple values into SelectionModel with single-value mode.');\n}\n"],"names":["SelectionModel","_multiple","_emitChanges","compareWith","_selection","Set","_deselectedToEmit","_selectedToEmit","_selected","selected","Array","from","values","changed","Subject","constructor","initiallySelectedValues","length","forEach","value","_markSelected","select","_verifyValueAssignment","_hasQueuedChanges","_emitChangeEvent","deselect","_unmarkSelected","setSelection","oldValues","newSelectedSet","map","_getConcreteValue","filter","has","toggle","isSelected","clear","flushEvent","_unmarkAll","isEmpty","size","hasValue","sort","predicate","isMultipleSelection","next","source","added","removed","add","push","delete","ngDevMode","getMultipleValuesInSingleSelectionError","inputValue","selection","selectedValue","Error"],"mappings":";;MAaaA,cAAc,CAAA;EA0BfC,SAAA;EAEAC,YAAA;EACDC,WAAA;AA3BDC,EAAAA,UAAU,GAAG,IAAIC,GAAG,EAAK;AAGzBC,EAAAA,iBAAiB,GAAQ,EAAE;AAG3BC,EAAAA,eAAe,GAAQ,EAAE;EAGzBC,SAAS;EAGjB,IAAIC,QAAQA,GAAA;AACV,IAAA,IAAI,CAAC,IAAI,CAACD,SAAS,EAAE;AACnB,MAAA,IAAI,CAACA,SAAS,GAAGE,KAAK,CAACC,IAAI,CAAC,IAAI,CAACP,UAAU,CAACQ,MAAM,EAAE,CAAC;AACvD;IAEA,OAAO,IAAI,CAACJ,SAAS;AACvB;AAGSK,EAAAA,OAAO,GAAG,IAAIC,OAAO,EAAsB;AAEpDC,EAAAA,WACUA,CAAAd,SAAA,GAAY,KAAK,EACzBe,uBAA6B,EACrBd,YAAe,GAAA,IAAI,EACpBC,WAAuC,EAAA;IAHtC,IAAS,CAAAF,SAAA,GAATA,SAAS;IAET,IAAY,CAAAC,YAAA,GAAZA,YAAY;IACb,IAAW,CAAAC,WAAA,GAAXA,WAAW;AAElB,IAAA,IAAIa,uBAAuB,IAAIA,uBAAuB,CAACC,MAAM,EAAE;AAC7D,MAAA,IAAIhB,SAAS,EAAE;QACbe,uBAAuB,CAACE,OAAO,CAACC,KAAK,IAAI,IAAI,CAACC,aAAa,CAACD,KAAK,CAAC,CA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