From 35ba3f488fe4e70479a13b560fac92e3dad8e22f Mon Sep 17 00:00:00 2001 From: wnvko Date: Tue, 13 Nov 2018 10:34:49 +0200 Subject: [PATCH] feat(tree-grid): update flat data transaction, #2921 _useInUndo flag added to IgxTreeGrid. When cascading delete rows in flat data set this flag to false and push add it this way in transaction. This allow undo to skip this transactions. --- .../grids/tree-grid/tree-grid.component.ts | 49 ++++++++++++------- .../igx-hierarchical-transaction.ts | 7 +++ .../services/transaction/igx-transaction.ts | 2 +- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid.component.ts b/projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid.component.ts index 2b3cf8372ce..f75c5c5f438 100644 --- a/projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid.component.ts +++ b/projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid.component.ts @@ -57,6 +57,7 @@ let NEXT_ID = 0; }) export class IgxTreeGridComponent extends IgxGridBaseComponent { private _id = `igx-tree-grid-${NEXT_ID++}`; + private _useInUndoStack = true; /** * An @Input property that sets the value of the `id` attribute. If not provided it will be automatically generated. @@ -368,8 +369,9 @@ export class IgxTreeGridComponent extends IgxGridBaseComponent { const childKey = this.childDataKey; if (this.transactions.enabled) { const rowId = this.primaryKey ? data[this.primaryKey] : data; - const path = [...parentRecord.path]; + const path: any[] = []; path.push(parentRowID); + path.push(...this.getPath(rowId)); this.transactions.add({ id: rowId, path: path, @@ -395,34 +397,32 @@ export class IgxTreeGridComponent extends IgxGridBaseComponent { } } - /** - * @hidden - */ - public deleteRowById(rowId: any) { - if (this.transactions.enabled && this.foreignKey && this.cascadeOnDelete) { - this.transactions.startPending(); - } - - super.deleteRowById(rowId); - - if (this.transactions.enabled && this.foreignKey && this.cascadeOnDelete) { - this.transactions.endPending(true); - } - } - /** * @hidden */ protected deleteRowFromData(rowID: any, index: number) { if (this.primaryKey && this.foreignKey) { - super.deleteRowFromData(rowID, index); + if (this.transactions.enabled) { + const path = this.getPath(rowID); + const transaction: HierarchicalTransaction = { id: rowID, type: TransactionType.DELETE, newValue: null, path }; + let recordRef = this.data[index]; + if (!recordRef) { + const state: HierarchicalState = this.transactions.getState(rowID); + recordRef = state && state.recordRef; + } + this.transactions.add(transaction, recordRef, this._useInUndoStack); + } else { + super.deleteRowFromData(rowID, index); + } if (this.cascadeOnDelete) { const treeRecord = this.records.get(rowID); if (treeRecord && treeRecord.children && treeRecord.children.length > 0) { for (let i = 0; i < treeRecord.children.length; i++) { const child = treeRecord.children[i]; + this._useInUndoStack = false; super.deleteRowById(child.rowID); + this._useInUndoStack = true; } } } @@ -432,8 +432,7 @@ export class IgxTreeGridComponent extends IgxGridBaseComponent { index = this.primaryKey ? childData.map(c => c[this.primaryKey]).indexOf(rowID) : childData.indexOf(rowID); if (this.transactions.enabled) { - const path = [...record.path]; - path.push(rowID); + const path = this.getPath(rowID); this.transactions.add({ id: rowID, type: TransactionType.DELETE, @@ -447,6 +446,18 @@ export class IgxTreeGridComponent extends IgxGridBaseComponent { } } + private getPath(rowId: any): any[] { + const path: any[] = []; + let record = this.records.get(rowId); + + while (record.parent) { + path.push(record.parent.rowID); + record = record.parent; + } + + return path; + } + /** * @hidden */ diff --git a/projects/igniteui-angular/src/lib/services/transaction/igx-hierarchical-transaction.ts b/projects/igniteui-angular/src/lib/services/transaction/igx-hierarchical-transaction.ts index 4b6f6a8e537..88e4a153517 100644 --- a/projects/igniteui-angular/src/lib/services/transaction/igx-hierarchical-transaction.ts +++ b/projects/igniteui-angular/src/lib/services/transaction/igx-hierarchical-transaction.ts @@ -7,6 +7,13 @@ import { IgxTransactionService } from '..'; export class IgxHierarchicalTransactionService extends IgxTransactionService { + + public add(transaction: T, recordRef?: any, useInUndo = true): void { + const states = this._isPending ? this._pendingStates : this._states; + this.verifyAddedTransaction(states, transaction, recordRef); + super.addTransaction(transaction, states, recordRef, useInUndo); + } + public getAggregatedChanges(mergeChanges: boolean): T[] { const result: T[] = []; this._states.forEach((state: S, key: any) => { diff --git a/projects/igniteui-angular/src/lib/services/transaction/igx-transaction.ts b/projects/igniteui-angular/src/lib/services/transaction/igx-transaction.ts index 9363470f940..410f1cd48c2 100644 --- a/projects/igniteui-angular/src/lib/services/transaction/igx-transaction.ts +++ b/projects/igniteui-angular/src/lib/services/transaction/igx-transaction.ts @@ -26,7 +26,7 @@ export class IgxTransactionService exten this.addTransaction(transaction, states, recordRef); } - private addTransaction(transaction: T, states: Map, recordRef?: any, useInUndo: boolean = true) { + protected addTransaction(transaction: T, states: Map, recordRef?: any, useInUndo: boolean = true) { this.updateState(states, transaction, recordRef); const transactions = this._isPending ? this._pendingTransactions : this._transactions;