• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IgniteUI / igniteui-angular / 13331632524

14 Feb 2025 02:51PM CUT coverage: 22.015% (-69.6%) from 91.622%
13331632524

Pull #15372

github

web-flow
Merge d52d57714 into bcb78ae0a
Pull Request #15372: chore(*): test ci passing

1990 of 15592 branches covered (12.76%)

431 of 964 new or added lines in 18 files covered. (44.71%)

19956 existing lines in 307 files now uncovered.

6452 of 29307 relevant lines covered (22.02%)

249.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

51.11
/projects/igniteui-angular/src/lib/core/selection.ts
1
import { Injectable } from '@angular/core';
2

3
/** @hidden */
4
@Injectable({
5
    providedIn: 'root',
6
})
7
export class IgxSelectionAPIService {
2✔
8
    /**
9
     * If primaryKey is defined, then multiple selection is based on the primaryKey, and it is array of numbers, strings, etc.
10
     * If the primaryKey is omitted, then selection is based on the item data
11
     */
12
    protected selection: Map<string,  Set<any>> = new Map<string, Set<any>>();
31✔
13

14
    /**
15
     * Get current component selection.
16
     *
17
     * @param componentID ID of the component.
18
     */
19
    public get(componentID: string): Set<any> {
20
        return this.selection.get(componentID);
33,126✔
21
    }
22

23
    /**
24
     * Set new component selection.
25
     *
26
     * @param componentID ID of the component.
27
     * @param newSelection The new component selection to be set.
28
     */
29
    public set(componentID: string, newSelection: Set<any>) {
30
        if (!componentID) {
281!
UNCOV
31
            throw Error('Invalid value for component id!');
×
32
        }
33
        this.selection.set(componentID, newSelection);
281✔
34
    }
35

36
    /**
37
     * Clears selection for component.
38
     *
39
     * @param componentID ID of the component.
40
     */
41
    public clear(componentID: string) {
42
        this.selection.set(componentID, this.get_empty());
414✔
43
    }
44

45
    /**
46
     * Removes selection for a component.
47
     * @param componentID
48
     */
49
      public delete(componentID: string) {
50
        this.selection.delete(componentID);
406✔
51
    }
52

53
    /**
54
     * Get current component selection length.
55
     *
56
     * @param componentID ID of the component.
57
     */
58
    public size(componentID: string): number {
59
        const sel = this.get(componentID);
×
60
        return sel ? sel.size : 0;
×
61
    }
62

63
    /**
64
     * Creates new selection that consist of the new item added to the current component selection.
65
     * The returned collection is new Set,
66
     * therefore if you want to update component selection you need to call in addition the set_selection() method
67
     * or instead use the select_item() one.
68
     *
69
     * @param componentID ID of the component, which we add new item to.
70
     * @param itemID ID of the item to add to component selection.
71
     * @param sel Used internally only by the selection (add_items method) to accumulate selection for multiple items.
72
     *
73
     * @returns Selection after the new item is added.
74
     */
75
    public add_item(componentID: string, itemID, sel?: Set<any>): Set<any> {
76
        if (!sel) {
193!
UNCOV
77
            sel = new Set(this.get(componentID));
×
78
        }
79
        if (sel === undefined) {
193!
80
            sel = this.get_empty();
×
81
        }
82
        sel.add(itemID);
193✔
83
        return sel;
193✔
84
    }
85

86
    /**
87
     * Creates new selection that consist of the new items added to the current component selection.
88
     * The returned collection is new Set,
89
     * therefore if you want to update component selection you need to call in addition the set_selection() method
90
     * or instead use the select_items() one.
91
     *
92
     * @param componentID ID of the component, which we add new items to.
93
     * @param itemIDs Array of IDs of the items to add to component selection.
94
     * @param clearSelection If true it will clear previous selection.
95
     *
96
     * @returns Selection after the new items are added.
97
     */
98
    public add_items(componentID: string, itemIDs: any[], clearSelection?: boolean): Set<any> {
99
        let selection: Set<any>;
100
        if (clearSelection) {
98!
101
            selection = this.get_empty();
98✔
UNCOV
102
        } else if (itemIDs && itemIDs.length === 0) {
×
UNCOV
103
            selection = new Set(this.get(componentID));
×
104
        }
105
        itemIDs.forEach((item) => selection = this.add_item(componentID, item, selection));
193✔
106
        return selection;
98✔
107
    }
108

109
    /**
110
     * Add item to the current component selection.
111
     *
112
     * @param componentID ID of the component, which we add new item to.
113
     * @param itemID ID of the item to add to component selection.
114
     * @param sel Used internally only by the selection (select_items method) to accumulate selection for multiple items.
115
     */
116
    public select_item(componentID: string, itemID, sel?: Set<any>) {
117
        this.set(componentID, this.add_item(componentID, itemID, sel));
×
118
    }
119

120
    /**
121
     * Add items to the current component selection.
122
     *
123
     * @param componentID ID of the component, which we add new items to.
124
     * @param itemIDs Array of IDs of the items to add to component selection.
125
     * @param clearSelection If true it will clear previous selection.
126
     */
127
    public select_items(componentID: string, itemID: any[], clearSelection?: boolean) {
128
        this.set(componentID, this.add_items(componentID, itemID, clearSelection));
98✔
129
    }
130

131
    /**
132
     * Creates new selection that consist of the new items excluded from the current component selection.
133
     * The returned collection is new Set,
134
     * therefore if you want to update component selection you need to call in addition the set_selection() method
135
     * or instead use the deselect_item() one.
136
     *
137
     * @param componentID ID of the component, which we remove items from.
138
     * @param itemID ID of the item to remove from component selection.
139
     * @param sel Used internally only by the selection (delete_items method) to accumulate deselected items.
140
     *
141
     * @returns Selection after the item is removed.
142
     */
143
    public delete_item(componentID: string, itemID, sel?: Set<any>) {
UNCOV
144
        if (!sel) {
×
UNCOV
145
            sel = new Set(this.get(componentID));
×
146
        }
UNCOV
147
        if (sel === undefined) {
×
148
            return;
×
149
        }
UNCOV
150
        sel.delete(itemID);
×
UNCOV
151
        return sel;
×
152
    }
153

154
    /**
155
     * Creates new selection that consist of the new items removed to the current component selection.
156
     * The returned collection is new Set,
157
     * therefore if you want to update component selection you need to call in addition the set_selection() method
158
     * or instead use the deselect_items() one.
159
     *
160
     * @param componentID ID of the component, which we remove items from.
161
     * @param itemID ID of the items to remove from component selection.
162
     *
163
     * @returns Selection after the items are removed.
164
     */
165
    public delete_items(componentID: string, itemIDs: any[]): Set<any> {
166
        let selection: Set<any>;
UNCOV
167
        itemIDs.forEach((deselectedItem) => selection = this.delete_item(componentID, deselectedItem, selection));
×
UNCOV
168
        return selection;
×
169
    }
170

171
    /**
172
     * Remove item from the current component selection.
173
     *
174
     * @param componentID ID of the component, which we remove item from.
175
     * @param itemID ID of the item to remove from component selection.
176
     * @param sel Used internally only by the selection (deselect_items method) to accumulate selection for multiple items.
177
     */
178
    public deselect_item(componentID: string, itemID, sel?: Set<any>) {
179
        this.set(componentID, this.delete_item(componentID, itemID, sel));
×
180
    }
181

182
    /**
183
     * Remove items to the current component selection.
184
     *
185
     * @param componentID ID of the component, which we add new items to.
186
     * @param itemIDs Array of IDs of the items to add to component selection.
187
     */
188
    public deselect_items(componentID: string, itemID: any[], _clearSelection?: boolean) {
189
        this.set(componentID, this.delete_items(componentID, itemID));
×
190
    }
191

192
    /**
193
     * Check if the item is selected in the component selection.
194
     *
195
     * @param componentID ID of the component.
196
     * @param itemID ID of the item to search.
197
     *
198
     * @returns If item is selected.
199
     */
200
    public is_item_selected(componentID: string, itemID): boolean {
201
        const sel = this.get(componentID);
25,302✔
202
        if (!sel) {
25,302!
UNCOV
203
            return false;
×
204
        }
205
        return sel.has(itemID);
25,302✔
206
    }
207

208
    /**
209
     * Get first element in the selection.
210
     * This is correct when we have only one item in the collection (for single selection purposes)
211
     * and the method returns that item.
212
     *
213
     * @param componentID ID of the component.
214
     *
215
     * @returns First element in the set.
216
     */
217
    public first_item(componentID: string) {
218
        const sel = this.get(componentID);
7,043✔
219
        if (sel && sel.size > 0) {
7,043✔
220
            return sel.values().next().value;
452✔
221
       }
222
    }
223

224
    /**
225
     * Returns whether all items are selected.
226
     *
227
     * @param componentID ID of the component.
228
     * @param dataCount: number Number of items in the data.
229
     *
230
     * @returns If all items are selected.
231
     */
232
    public are_all_selected(componentID: string, dataCount: number): boolean {
233
        return dataCount > 0 && dataCount === this.size(componentID);
×
234
    }
235

236
    /**
237
     * Returns whether any of the items is selected.
238
     *
239
     * @param componentID ID of the component.
240
     * @param data Entire data array.
241
     *
242
     * @returns If there is any item selected.
243
     */
244
    public are_none_selected(componentID: string): boolean {
245
        return this.size(componentID) === 0;
×
246
    }
247

248
    /**
249
     * Get all primary key values from a data array. If there isn't a primary key defined that the entire data is returned instead.
250
     *
251
     * @param data Entire data array.
252
     * @param primaryKey Data primary key.
253
     *
254
     * @returns Array of identifiers, either primary key values or the entire data array.
255
     */
256
    public get_all_ids(data, primaryKey?) {
257
        // If primaryKey is 0, this should still map to the property
UNCOV
258
        return primaryKey !== undefined && primaryKey !== null ? data.map((x) => x[primaryKey]) : data;
×
259
    }
260

261
    /**
262
     * Returns empty selection collection.
263
     *
264
     * @returns empty set.
265
     */
266
    public get_empty() {
267
        return new Set();
512✔
268
    }
269
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc