@@ -39,14 +39,14 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
3939 public updateCascadeSelectionOnFilterAndCRUD (
4040 parents : Set < any > ,
4141 crudRowID ?: any ,
42- visibleRowIDs : any [ ] = null ) {
42+ visibleRowIDs : Set < any > = null ) {
4343 if ( visibleRowIDs === null ) {
4444 // if the tree grid has flat structure
4545 // do not explicitly handle the selection state of the rows
4646 if ( ! parents . size ) {
4747 return ;
4848 }
49- visibleRowIDs = this . getRowIDs ( this . allData ) ;
49+ visibleRowIDs = new Set ( this . getRowIDs ( this . allData ) ) ;
5050 this . rowsToBeSelected = new Set ( this . rowSelection ) ;
5151 this . rowsToBeIndeterminate = new Set ( this . indeterminateRows ) ;
5252 if ( crudRowID ) {
@@ -147,8 +147,10 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
147147 * retrieve the rows which should be added/removed to/from the old selection
148148 */
149149 private handleAddedAndRemovedArgs ( args : any ) {
150- args . removed = args . oldSelection . filter ( x => args . newSelection . indexOf ( x ) < 0 ) ;
151- args . added = args . newSelection . filter ( x => args . oldSelection . indexOf ( x ) < 0 ) ;
150+ const newSelectionSet = new Set ( args . newSelection ) ;
151+ const oldSelectionSet = new Set ( args . oldSelection ) ;
152+ args . removed = args . oldSelection . filter ( x => ! newSelectionSet . has ( x ) ) ;
153+ args . added = args . newSelection . filter ( x => ! oldSelectionSet . has ( x ) ) ;
152154 }
153155
154156 /**
@@ -158,14 +160,15 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
158160 * @param visibleRowIDs list of all visible rowIds
159161 * @returns a new set with all direct parents of the rows within rowsToBeProcessed set
160162 */
161- private collectRowsChildrenAndDirectParents ( rowsToBeProcessed : Set < any > , visibleRowIDs : any [ ] ) : Set < any > {
163+ private collectRowsChildrenAndDirectParents ( rowsToBeProcessed : Set < any > , visibleRowIDs : Set < any > , adding : boolean ) : Set < any > {
162164 const processedRowsParents = new Set < any > ( ) ;
163165 Array . from ( rowsToBeProcessed ) . forEach ( ( rowID ) => {
166+ this . selectDeselectRow ( rowID , adding ) ;
164167 const rowTreeRecord = this . grid . gridAPI . get_rec_by_id ( rowID ) ;
165168 const rowAndAllChildren = this . get_all_children ( rowTreeRecord ) ;
166169 rowAndAllChildren . forEach ( row => {
167- if ( visibleRowIDs . indexOf ( row . key ) >= 0 ) {
168- rowsToBeProcessed . add ( row . key ) ;
170+ if ( visibleRowIDs . has ( row . key ) ) {
171+ this . selectDeselectRow ( row . key , adding ) ;
169172 }
170173 } ) ;
171174 if ( rowTreeRecord && rowTreeRecord . parent ) {
@@ -184,20 +187,15 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
184187 this . rowsToBeSelected = new Set < any > ( args . oldSelection ? args . oldSelection : this . getSelectedRows ( ) ) ;
185188 this . rowsToBeIndeterminate = new Set < any > ( this . getIndeterminateRows ( ) ) ;
186189
187- const visibleRowIDs = this . getRowIDs ( this . allData ) ;
190+ const visibleRowIDs = new Set ( this . getRowIDs ( this . allData ) ) ;
188191
189192 const removed = new Set ( args . removed ) ;
190193 const added = new Set ( args . added ) ;
191194
192195 if ( removed && removed . size ) {
193196 let removedRowsParents = new Set < any > ( ) ;
194197
195- removedRowsParents = this . collectRowsChildrenAndDirectParents ( removed , visibleRowIDs ) ;
196-
197- removed . forEach ( removedRow => {
198- this . rowsToBeSelected . delete ( removedRow ) ;
199- this . rowsToBeIndeterminate . delete ( removedRow ) ;
200- } ) ;
198+ removedRowsParents = this . collectRowsChildrenAndDirectParents ( removed , visibleRowIDs , false ) ;
201199
202200 Array . from ( removedRowsParents ) . forEach ( ( parent ) => {
203201 this . handleParentSelectionState ( parent , visibleRowIDs ) ;
@@ -207,12 +205,7 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
207205 if ( added && added . size ) {
208206 let addedRowsParents = new Set < any > ( ) ;
209207
210- addedRowsParents = this . collectRowsChildrenAndDirectParents ( added , visibleRowIDs ) ;
211-
212- added . forEach ( addedRow => {
213- this . rowsToBeSelected . add ( addedRow ) ;
214- this . rowsToBeIndeterminate . delete ( addedRow ) ;
215- } ) ;
208+ addedRowsParents = this . collectRowsChildrenAndDirectParents ( added , visibleRowIDs , true ) ;
216209
217210 Array . from ( addedRowsParents ) . forEach ( ( parent ) => {
218211 this . handleParentSelectionState ( parent , visibleRowIDs ) ;
@@ -223,7 +216,7 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
223216 /**
224217 * recursively handle the selection state of the direct and indirect parents
225218 */
226- private handleParentSelectionState ( treeRow : ITreeGridRecord , visibleRowIDs : any [ ] ) {
219+ private handleParentSelectionState ( treeRow : ITreeGridRecord , visibleRowIDs : Set < any > ) {
227220 if ( ! treeRow ) {
228221 return ;
229222 }
@@ -236,30 +229,26 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
236229 /**
237230 * Handle the selection state of a given row based the selection states of its direct children
238231 */
239- private handleRowSelectionState ( treeRow : ITreeGridRecord , visibleRowIDs : any [ ] ) {
232+ private handleRowSelectionState ( treeRow : ITreeGridRecord , visibleRowIDs : Set < any > ) {
240233 let visibleChildren = [ ] ;
241234 if ( treeRow && treeRow . children ) {
242- visibleChildren = treeRow . children . filter ( child => visibleRowIDs . indexOf ( child . key ) >= 0 ) ;
235+ visibleChildren = treeRow . children . filter ( child => visibleRowIDs . has ( child . key ) ) ;
243236 }
244237 if ( visibleChildren . length ) {
245238 if ( visibleChildren . every ( row => this . rowsToBeSelected . has ( row . key ) ) ) {
246- this . rowsToBeSelected . add ( treeRow . key ) ;
247- this . rowsToBeIndeterminate . delete ( treeRow . key ) ;
239+ this . selectDeselectRow ( treeRow . key , true ) ;
248240 } else if ( visibleChildren . some ( row => this . rowsToBeSelected . has ( row . key ) || this . rowsToBeIndeterminate . has ( row . key ) ) ) {
249241 this . rowsToBeIndeterminate . add ( treeRow . key ) ;
250242 this . rowsToBeSelected . delete ( treeRow . key ) ;
251243 } else {
252- this . rowsToBeIndeterminate . delete ( treeRow . key ) ;
253- this . rowsToBeSelected . delete ( treeRow . key ) ;
244+ this . selectDeselectRow ( treeRow . key , false ) ;
254245 }
255246 } else {
256247 // if the children of the row has been deleted and the row was selected do not change its state
257248 if ( this . isRowSelected ( treeRow . key ) ) {
258- this . rowsToBeSelected . add ( treeRow . key ) ;
259- this . rowsToBeIndeterminate . delete ( treeRow . key ) ;
249+ this . selectDeselectRow ( treeRow . key , true ) ;
260250 } else {
261- this . rowsToBeSelected . delete ( treeRow . key ) ;
262- this . rowsToBeIndeterminate . delete ( treeRow . key ) ;
251+ this . selectDeselectRow ( treeRow . key , false ) ;
263252 }
264253 }
265254 }
@@ -276,4 +265,14 @@ export class IgxTreeGridSelectionService extends IgxGridSelectionService {
276265
277266 }
278267
268+ private selectDeselectRow ( rowID : any , select : boolean ) {
269+ if ( select ) {
270+ this . rowsToBeSelected . add ( rowID ) ;
271+ this . rowsToBeIndeterminate . delete ( rowID ) ;
272+ } else {
273+ this . rowsToBeSelected . delete ( rowID ) ;
274+ this . rowsToBeIndeterminate . delete ( rowID ) ;
275+ }
276+ }
277+
279278}
0 commit comments