| 45 | * expected to be set when calling `isSelected`, `select` and `deselect`. |
| 46 | */ |
| 47 | export class SelectionSet<T> implements TrackBySelection<T> { |
| 48 | private _selectionMap = new Map<T | ReturnType<TrackByFunction<T>>, SelectableWithIndex<T>>(); |
| 49 | changed = new Subject<SelectionChange<T>>(); |
| 50 | |
| 51 | constructor( |
| 52 | private _multiple = false, |
| 53 | private _trackByFn?: TrackByFunction<T>, |
| 54 | ) {} |
| 55 | |
| 56 | isSelected(value: SelectableWithIndex<T>): boolean { |
| 57 | return this._selectionMap.has(this._getTrackedByValue(value)); |
| 58 | } |
| 59 | |
| 60 | select(...selects: SelectableWithIndex<T>[]) { |
| 61 | if (!this._multiple && selects.length > 1 && (typeof ngDevMode === 'undefined' || ngDevMode)) { |
| 62 | throw Error('SelectionSet: not multiple selection'); |
| 63 | } |
| 64 | |
| 65 | const before = this._getCurrentSelection(); |
| 66 | |
| 67 | if (!this._multiple) { |
| 68 | this._selectionMap.clear(); |
| 69 | } |
| 70 | |
| 71 | const toSelect: SelectableWithIndex<T>[] = []; |
| 72 | for (const select of selects) { |
| 73 | if (this.isSelected(select)) { |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | toSelect.push(select); |
| 78 | this._markSelected(this._getTrackedByValue(select), select); |
| 79 | } |
| 80 | |
| 81 | const after = this._getCurrentSelection(); |
| 82 | |
| 83 | this.changed.next({before, after}); |
| 84 | } |
| 85 | |
| 86 | deselect(...selects: SelectableWithIndex<T>[]) { |
| 87 | if (!this._multiple && selects.length > 1 && (typeof ngDevMode === 'undefined' || ngDevMode)) { |
| 88 | throw Error('SelectionSet: not multiple selection'); |
| 89 | } |
| 90 | |
| 91 | const before = this._getCurrentSelection(); |
| 92 | const toDeselect: SelectableWithIndex<T>[] = []; |
| 93 | |
| 94 | for (const select of selects) { |
| 95 | if (!this.isSelected(select)) { |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | toDeselect.push(select); |
| 100 | this._markDeselected(this._getTrackedByValue(select)); |
| 101 | } |
| 102 | |
| 103 | const after = this._getCurrentSelection(); |
| 104 | this.changed.next({before, after}); |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…