Skip to content

Commit

Permalink
feat(tree-grid): update flat data transaction, #2921
Browse files Browse the repository at this point in the history
_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.
  • Loading branch information
wnvko committed Nov 13, 2018
1 parent be64953 commit 8d4b355
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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.
Expand Down Expand Up @@ -365,8 +366,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,
Expand All @@ -392,34 +394,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;
}
}
}
Expand All @@ -429,8 +429,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,
Expand All @@ -444,6 +443,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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import { IgxTransactionService } from '..';
export class IgxHierarchicalTransactionService<T extends HierarchicalTransaction, S extends HierarchicalState>
extends IgxTransactionService<T, S> {


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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class IgxTransactionService<T extends Transaction, S extends State> exten
this.addTransaction(transaction, states, recordRef);
}

private addTransaction(transaction: T, states: Map<any, S>, recordRef?: any, useInUndo: boolean = true) {
protected addTransaction(transaction: T, states: Map<any, S>, recordRef?: any, useInUndo: boolean = true) {
this.updateState(states, transaction, recordRef);

const transactions = this._isPending ? this._pendingTransactions : this._transactions;
Expand Down

0 comments on commit 8d4b355

Please sign in to comment.