@progress/kendo-angular-listbox 21.0.0-develop.9 → 21.0.1-develop.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,17 +8,138 @@ import * as i0 from "@angular/core";
8
8
  * @hidden
9
9
  */
10
10
  export class ListBoxSelectionService {
11
+ selectedIndices = [];
12
+ selectionMode = 'single';
13
+ lastSelectedOrUnselectedIndex = null;
14
+ rangeSelectionTargetIndex = null;
15
+ rangeSelectionAnchorIndex = null;
11
16
  onSelect = new EventEmitter();
12
- selectedIndex = null;
13
- select(index) {
14
- this.onSelect.next({ index: index, prevIndex: this.selectedIndex });
15
- this.selectedIndex = index;
17
+ select(index, ctrlKey = false, shiftKey = false) {
18
+ const previousSelection = [...this.selectedIndices];
19
+ let selectedIndices = [];
20
+ let deselectedIndices = null;
21
+ const previousTargetIndex = this.rangeSelectionTargetIndex;
22
+ if (this.selectionMode === 'single') {
23
+ if (ctrlKey) {
24
+ const isSelected = this.isSelected(index);
25
+ if (isSelected) {
26
+ this.selectedIndices = [];
27
+ deselectedIndices = [index];
28
+ }
29
+ else {
30
+ this.selectedIndices = [index];
31
+ selectedIndices = [index];
32
+ if (previousSelection.length > 0) {
33
+ deselectedIndices = previousSelection;
34
+ }
35
+ }
36
+ this.lastSelectedOrUnselectedIndex = index;
37
+ }
38
+ else {
39
+ this.selectedIndices = [index];
40
+ selectedIndices = [index];
41
+ this.lastSelectedOrUnselectedIndex = index;
42
+ this.rangeSelectionAnchorIndex = index;
43
+ if (previousSelection.length > 0 && previousSelection[0] !== index) {
44
+ deselectedIndices = previousSelection;
45
+ }
46
+ }
47
+ }
48
+ else if (this.selectionMode === 'multiple') {
49
+ if (shiftKey) {
50
+ let anchorIndex = this.rangeSelectionAnchorIndex ?? this.lastSelectedOrUnselectedIndex ?? 0;
51
+ if (index === anchorIndex) {
52
+ this.selectedIndices = [anchorIndex];
53
+ this.rangeSelectionTargetIndex = index;
54
+ selectedIndices = this.selectedIndices.filter(i => !previousSelection.includes(i));
55
+ const nowDeselected = previousSelection.filter(i => !this.selectedIndices.includes(i));
56
+ deselectedIndices = nowDeselected.length > 0 ? nowDeselected : null;
57
+ }
58
+ else {
59
+ if (previousTargetIndex !== null && previousTargetIndex !== anchorIndex) {
60
+ const previousDirection = previousTargetIndex > anchorIndex ? 'down' : 'up';
61
+ const currentDirection = index > anchorIndex ? 'down' : 'up';
62
+ if (previousDirection !== currentDirection) {
63
+ this.rangeSelectionAnchorIndex = previousTargetIndex;
64
+ anchorIndex = previousTargetIndex;
65
+ }
66
+ }
67
+ const startIndex = Math.min(anchorIndex, index);
68
+ const endIndex = Math.max(anchorIndex, index);
69
+ this.selectedIndices = [];
70
+ for (let i = startIndex; i <= endIndex; i++) {
71
+ this.selectedIndices.push(i);
72
+ }
73
+ this.rangeSelectionTargetIndex = index;
74
+ selectedIndices = this.selectedIndices.filter(i => !previousSelection.includes(i));
75
+ const nowDeselected = previousSelection.filter(i => !this.selectedIndices.includes(i));
76
+ deselectedIndices = nowDeselected.length > 0 ? nowDeselected : null;
77
+ }
78
+ }
79
+ else if (ctrlKey) {
80
+ const indexInSelection = this.selectedIndices.indexOf(index);
81
+ if (indexInSelection === -1) {
82
+ this.selectedIndices.push(index);
83
+ selectedIndices = [index];
84
+ }
85
+ else {
86
+ this.selectedIndices.splice(indexInSelection, 1);
87
+ deselectedIndices = [index];
88
+ }
89
+ this.lastSelectedOrUnselectedIndex = index;
90
+ this.rangeSelectionAnchorIndex = index;
91
+ this.rangeSelectionTargetIndex = index;
92
+ }
93
+ else {
94
+ this.selectedIndices = [index];
95
+ selectedIndices = [index];
96
+ this.lastSelectedOrUnselectedIndex = index;
97
+ this.rangeSelectionAnchorIndex = index;
98
+ this.rangeSelectionTargetIndex = index;
99
+ const nowDeselected = previousSelection.filter(i => i !== index);
100
+ deselectedIndices = nowDeselected.length > 0 ? nowDeselected : null;
101
+ }
102
+ }
103
+ this.onSelect.next({
104
+ selectedIndices: selectedIndices.length > 0 ? selectedIndices : null,
105
+ deselectedIndices
106
+ });
107
+ }
108
+ selectRange(targetIndex) {
109
+ const anchorIndex = this.lastSelectedOrUnselectedIndex ?? 0;
110
+ const startIndex = Math.min(anchorIndex, targetIndex);
111
+ const endIndex = Math.max(anchorIndex, targetIndex);
112
+ this.selectedIndices = [];
113
+ for (let i = startIndex; i <= endIndex; i++) {
114
+ this.selectedIndices.push(i);
115
+ }
116
+ }
117
+ setSelectedIndices(indices) {
118
+ this.selectedIndices = [...indices];
119
+ }
120
+ addToSelectedIndices(index) {
121
+ if (this.selectionMode === 'single') {
122
+ this.selectedIndices = [index];
123
+ }
124
+ else if (this.selectedIndices.indexOf(index) === -1) {
125
+ this.selectedIndices = [...this.selectedIndices, index];
126
+ }
127
+ }
128
+ selectAll(totalItems) {
129
+ if (this.selectionMode === 'multiple') {
130
+ this.selectedIndices = Array.from({ length: totalItems }, (_, i) => i);
131
+ }
132
+ }
133
+ areAllSelected(totalItems) {
134
+ return this.selectedIndices.length === totalItems && totalItems > 0;
16
135
  }
17
136
  isSelected(index) {
18
- return index === this.selectedIndex;
137
+ return this.selectedIndices.indexOf(index) !== -1;
19
138
  }
20
139
  clearSelection() {
21
- this.selectedIndex = null;
140
+ this.selectedIndices = [];
141
+ this.lastSelectedOrUnselectedIndex = null;
142
+ this.rangeSelectionAnchorIndex = null;
22
143
  }
23
144
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: ListBoxSelectionService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
24
145
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: ListBoxSelectionService });