-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathlist-selection.ts
More file actions
225 lines (183 loc) · 6.96 KB
/
list-selection.ts
File metadata and controls
225 lines (183 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* @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.dev/license
*/
import {computed, signal, SignalLike, WritableSignalLike} from '../signal-like/signal-like';
import {ListFocus, ListFocusInputs, ListFocusItem} from '../list-focus/list-focus';
/** Represents an item in a collection, such as a listbox option, that can be selected. */
export interface ListSelectionItem<V> extends ListFocusItem {
/** The value of the item. */
value: SignalLike<V>;
/** Whether the item is selectable. */
selectable: SignalLike<boolean>;
}
/** Represents the required inputs for a collection that contains selectable items. */
export interface ListSelectionInputs<T extends ListSelectionItem<V>, V> extends ListFocusInputs<T> {
/** Whether multiple items in the list can be selected at once. */
multi: SignalLike<boolean>;
/** The current value of the list selection. */
values: WritableSignalLike<V[]>;
/** The selection strategy used by the list. */
selectionMode: SignalLike<'follow' | 'explicit'>;
}
/** Controls selection for a list of items. */
export class ListSelection<T extends ListSelectionItem<V>, V> {
/** The start index to use for range selection. */
rangeStartIndex = signal<number>(0);
/** The end index to use for range selection. */
rangeEndIndex = signal<number>(0);
/** The currently selected items. */
selectedItems = computed(() =>
this.inputs.items().filter(item => this.inputs.values().includes(item.value())),
);
constructor(readonly inputs: ListSelectionInputs<T, V> & {focusManager: ListFocus<T>}) {}
/** Selects the item at the current active index. */
select(item?: ListSelectionItem<V>, opts = {anchor: true}) {
item = item ?? (this.inputs.focusManager.inputs.activeItem() as ListSelectionItem<V>);
if (
!item ||
item.disabled() ||
!item.selectable() ||
!this.inputs.focusManager.isFocusable(item as T) ||
this.inputs.values().includes(item.value())
) {
return;
}
if (!this.inputs.multi()) {
this.deselectAll();
}
const index = this.inputs.items().findIndex(i => i === item);
if (opts.anchor) {
this.beginRangeSelection(index);
}
this.inputs.values.update(values => values.concat(item.value()));
}
/** Deselects the item at the current active index. */
deselect(item?: ListSelectionItem<V>) {
item = item ?? this.inputs.focusManager.inputs.activeItem();
if (item && !item.disabled() && item.selectable()) {
this.inputs.values.update(values => values.filter(value => value !== item.value()));
}
}
/** Toggles the item at the current active index. */
toggle(item?: ListSelectionItem<V>) {
item = item ?? this.inputs.focusManager.inputs.activeItem();
if (item) {
this.inputs.values().includes(item.value()) ? this.deselect(item) : this.select(item);
}
}
/** Toggles only the item at the current active index. */
toggleOne() {
const item = this.inputs.focusManager.inputs.activeItem();
if (item) {
this.inputs.values().includes(item.value()) ? this.deselect() : this.selectOne();
}
}
/** Selects all items in the list. */
selectAll() {
if (!this.inputs.multi()) {
return; // Should we log a warning?
}
for (const item of this.inputs.items()) {
this.select(item, {anchor: false});
}
this.beginRangeSelection();
}
/** Deselects all items in the list. */
deselectAll() {
// If an item is not in the list, it forcefully gets deselected.
// This actually creates a bug for the following edge case:
//
// Setup: An item is not in the list (maybe it's lazily loaded), and it is disabled & selected.
// Expected: If deselectAll() is called, it should NOT get deselected (because it is disabled).
// Actual: Calling deselectAll() will still deselect the item.
//
// Why? Because we can't check if the item is disabled if it's not in the list.
//
// Alternatively, we could NOT deselect items that are not in the list, but this has the
// inverse (and more common) effect of keeping enabled items selected when they aren't in the
// list.
for (const value of this.inputs.values()) {
const item = this.inputs.items().find(i => i.value() === value);
item
? this.deselect(item)
: this.inputs.values.update(values => values.filter(v => v !== value));
}
}
/**
* Selects all items in the list or deselects all
* items in the list if all items are already selected.
*/
toggleAll() {
const selectableValues = this.inputs
.items()
.filter(i => !i.disabled() && i.selectable() && this.inputs.focusManager.isFocusable(i))
.map(i => i.value());
selectableValues.every(i => this.inputs.values().includes(i))
? this.deselectAll()
: this.selectAll();
}
/** Sets the selection to only the current active item. */
selectOne() {
const item = this.inputs.focusManager.inputs.activeItem();
if (item && (item.disabled() || !item.selectable())) {
return;
}
this.deselectAll();
if (this.inputs.values().length > 0 && !this.inputs.multi()) {
return;
}
this.select();
}
/**
* Selects all items in the list up to the anchor item.
*
* Deselects all items that were previously within the
* selected range that are now outside of the selected range
*/
selectRange(opts = {anchor: true}) {
const isStartOfRange = this.inputs.focusManager.prevActiveIndex() === this.rangeStartIndex();
if (isStartOfRange && opts.anchor) {
this.beginRangeSelection(this.inputs.focusManager.prevActiveIndex());
}
const itemsInRange = this._getItemsFromIndex(this.rangeStartIndex());
const itemsOutOfRange = this._getItemsFromIndex(this.rangeEndIndex()).filter(
i => !itemsInRange.includes(i),
);
for (const item of itemsOutOfRange) {
this.deselect(item);
}
for (const item of itemsInRange) {
this.select(item, {anchor: false});
}
if (itemsInRange.length) {
const item = itemsInRange.pop();
const index = this.inputs.items().findIndex(i => i === item);
this.rangeEndIndex.set(index);
}
}
/** Marks the given index as the start of a range selection. */
beginRangeSelection(index: number = this.inputs.focusManager.activeIndex()) {
this.rangeStartIndex.set(index);
this.rangeEndIndex.set(index);
}
/** Returns the items in the list starting from the given index. */
private _getItemsFromIndex(index: number) {
if (index === -1) {
return [];
}
const upper = Math.max(this.inputs.focusManager.activeIndex(), index);
const lower = Math.min(this.inputs.focusManager.activeIndex(), index);
const items = [];
for (let i = lower; i <= upper; i++) {
items.push(this.inputs.items()[i]);
}
if (this.inputs.focusManager.activeIndex() < index) {
return items.reverse();
}
return items;
}
}