Skip to content

Commit 8d3e822

Browse files
authored
ROU-12056: Fixing an issue that caused lines to be removed (#460)
* Fixing the issue * Fixing sonarcloud warnings * Removing any usage * Improving comments * Protecting the code
1 parent 7205c3b commit 8d3e822

File tree

3 files changed

+68
-32
lines changed

3 files changed

+68
-32
lines changed

src/OSFramework/DataGrid/Feature/IRows.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace OSFramework.DataGrid.Feature {
2020
/**
2121
* Get data from a specific row.
2222
*/
23-
getRowData(rowNumber: number): string;
23+
getRowData(rowNumber: number): unknown;
2424
/**
2525
* Clear all classes from a specific row.
2626
*/

src/Providers/DataGrid/Wijmo/Features/Rows.ts

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// eslint-disable-next-line @typescript-eslint/no-unused-vars
22
namespace Providers.DataGrid.Wijmo.Feature {
33
export class GridInsertRowAction extends wijmo.undo.UndoableAction {
4-
private _grid: Grid.IGridWijmo;
4+
private readonly _grid: Grid.IGridWijmo;
55

66
constructor(
77
grid: Grid.IGridWijmo,
@@ -18,9 +18,9 @@ namespace Providers.DataGrid.Wijmo.Feature {
1818
collectionView.itemsAdded.push(...chunk)
1919
);
2020
}
21-
// eslint-disable-next-line
22-
public applyState(state: any): void {
23-
const collectionView = this._target.itemsSource;
21+
22+
public applyState(state: { action?: string; datasourceIdx?: number; items?: unknown[] }): void {
23+
const collectionView = this._target.itemsSource as wijmo.collections.CollectionView;
2424
if (collectionView) {
2525
if (state.action === 'remove') {
2626
//undo
@@ -32,7 +32,7 @@ namespace Providers.DataGrid.Wijmo.Feature {
3232
// which is 9.
3333
const startingIndex = collectionView.itemsAdded.length - state.items.length;
3434

35-
collectionView.itemsAdded.splice(startingIndex, state.items.length);
35+
collectionView.trackChanges && collectionView.itemsAdded.splice(startingIndex, state.items.length);
3636
} else {
3737
//redo
3838
collectionView.sourceCollection.splice(state.datasourceIdx, 0, ...state.items);
@@ -57,7 +57,7 @@ namespace Providers.DataGrid.Wijmo.Feature {
5757
}
5858

5959
export class GridRemoveRowAction extends wijmo.undo.UndoableAction {
60-
private _grid: Grid.IGridWijmo;
60+
private readonly _grid: Grid.IGridWijmo;
6161

6262
constructor(
6363
grid: Grid.IGridWijmo,
@@ -70,24 +70,37 @@ namespace Providers.DataGrid.Wijmo.Feature {
7070
this._newState = undoableItems;
7171
}
7272

73-
private _addItemToCollectionView(collectionView, item) {
74-
if (collectionView.itemsRemoved.indexOf(item.item) === -1) {
75-
collectionView.sourceCollection.splice(item.datasourceIdx, 1);
76-
collectionView.trackChanges && collectionView.itemsRemoved.push(item.item);
73+
/** Method to redo the operation */
74+
private _addItemToCollectionView(
75+
collectionView: wijmo.collections.CollectionView,
76+
item: { datasourceIdx: number; item: unknown }
77+
) {
78+
// Let's remove the item from the collectionView, as it was added before.
79+
collectionView.sourceCollection.splice(item.datasourceIdx, 1);
80+
if (collectionView.trackChanges && collectionView.itemsRemoved.indexOf(item.item) === -1) {
81+
collectionView.itemsRemoved.push(item.item);
7782
}
7883
}
7984

80-
private _removeItemFromCollectionView(collectionView, item) {
81-
if (collectionView.itemsRemoved.indexOf(item.item) > -1) {
82-
collectionView.sourceCollection.splice(item.datasourceIdx, 0, item.item);
83-
collectionView.trackChanges && collectionView.itemsRemoved.remove(item.item);
85+
/** Method to undo the operation */
86+
private _removeItemFromCollectionView(
87+
collectionView: wijmo.collections.CollectionView,
88+
item: { datasourceIdx: number; item: unknown }
89+
) {
90+
// Let's add the item back to the collectionView, as it was removed before.
91+
collectionView.sourceCollection.splice(item.datasourceIdx, 0, item.item);
92+
// If the row existed before, we need to remove it from the itemsRemoved list.
93+
// When the row is added and then removed, it does not go to the itemsRemoved list.
94+
if (collectionView.trackChanges && collectionView.itemsRemoved.indexOf(item.item) > -1) {
95+
collectionView.itemsRemoved.remove(item.item);
8496
}
8597
}
8698
// eslint-disable-next-line
8799
public applyState(state: any): void {
88100
const collectionView = this._target.itemsSource;
89101
if (collectionView) {
90102
if (state.action === 'insert') {
103+
//undo
91104
state.items
92105
.sort((a, b) => a.datasourceIdx - b.datasourceIdx)
93106
.forEach((item) => {
@@ -105,7 +118,7 @@ namespace Providers.DataGrid.Wijmo.Feature {
105118

106119
if (this._grid.gridEvents.hasHandlers(OSFramework.DataGrid.Event.Grid.GridEventType.OnDataChange)) {
107120
const dataChanges = new OSFramework.DataGrid.OSStructure.DataChanges();
108-
dataChanges.changedRows = state.items || state.map((state) => state.item);
121+
dataChanges.changedRows = state.items ?? state.map((state) => state.item);
109122
dataChanges.totalRows = this._grid.features.pagination.rowTotal;
110123
this._grid.gridEvents.trigger(
111124
OSFramework.DataGrid.Event.Grid.GridEventType.OnDataChange,
@@ -118,40 +131,44 @@ namespace Providers.DataGrid.Wijmo.Feature {
118131
}
119132

120133
export class Rows implements OSFramework.DataGrid.Interface.IBuilder, OSFramework.DataGrid.Feature.IRows {
121-
private _grid: Grid.IGridWijmo;
134+
private readonly _grid: Grid.IGridWijmo;
122135

123136
/** This is going to be used as a label for the css classes saved on the metadata of the Row */
124137
private readonly _internalLabel = OSFramework.DataGrid.Enum.RowMetadata.RowCss;
125138

126-
private _metadata: OSFramework.DataGrid.Interface.IRowMetadata;
139+
private readonly _metadata: OSFramework.DataGrid.Interface.IRowMetadata;
127140

128141
constructor(grid: Grid.IGridWijmo) {
129142
this._grid = grid;
130143
this._metadata = this._grid.rowMetadata;
131144
}
132145

133146
/**
134-
* Check if it is possible to add rows to the grid.
135-
* @returns Boolean indicating if it is possible to add rows to the grid.
147+
* Getter that checks if it is possible to add or remove rows from the grid.
148+
* This is true when the grid is not sorted, grouped or filtered.
136149
*/
137-
private _canAddRows(): boolean {
150+
private get _canAddRemoveRows(): boolean {
138151
return (
139152
!this._grid.features.sort.isGridSorted &&
140153
!this._grid.features.groupPanel.isGridGrouped &&
141154
!this._grid.features.filter.isGridFiltered
142155
);
143156
}
144157

158+
/**
159+
* Check if it is possible to add/remove rows to the grid.
160+
* @returns Boolean indicating if it is possible to add rows to the grid.
161+
*/
162+
private _canAddRows(): boolean {
163+
return this._canAddRemoveRows;
164+
}
165+
145166
/**
146167
* Check if it is possible to remove rows from the grid.
147168
* @returns Boolean indicating if it is possible to remove rows from the grid.
148169
*/
149170
private _canRemoveRows(): boolean {
150-
return (
151-
!this._grid.features.sort.isGridSorted &&
152-
!this._grid.features.groupPanel.isGridGrouped &&
153-
!this._grid.features.filter.isGridFiltered
154-
);
171+
return this._canAddRemoveRows;
155172
}
156173

157174
/**
@@ -165,6 +182,9 @@ namespace Providers.DataGrid.Wijmo.Feature {
165182
}
166183
}
167184

185+
/**
186+
* Get the data item from a specific row.
187+
*/
168188
private _getDataItemFromRow(rowNumber: number) {
169189
return this._grid.isSingleEntity
170190
? OSFramework.DataGrid.Helper.Flatten(this._grid.provider.rows[rowNumber]?.dataItem)
@@ -191,6 +211,9 @@ namespace Providers.DataGrid.Wijmo.Feature {
191211
return topRow === Infinity ? 0 : topRow;
192212
}
193213

214+
/**
215+
* Validates if it is possible to add new rows to the grid.
216+
*/
194217
private _validateAddNewRow(rowsAmount: number, topRowIndex: number): void {
195218
if (!this._canAddRows()) {
196219
throw new Error(OSFramework.DataGrid.Enum.ErrorMessages.AddRowWithActiveFilterOrSort);
@@ -292,11 +315,20 @@ namespace Providers.DataGrid.Wijmo.Feature {
292315
return addedRowsNumber;
293316
}
294317

318+
/**
319+
* Builds the rows feature.
320+
*
321+
* @memberof Rows
322+
*/
295323
public build(): void {
296324
this._grid.provider.formatItem.addHandler(this._formatItems.bind(this));
297325
}
298326

299-
/** Clears all the cssClass metadata associated to the rows */
327+
/**
328+
* Clears all the cssClass metadata associated to the rows.
329+
*
330+
* @memberof Rows
331+
*/
300332
public clear(): void {
301333
this._metadata.clearProperty(this._internalLabel);
302334
this._grid.provider.invalidate(); //Mark to be refreshed
@@ -321,8 +353,14 @@ namespace Providers.DataGrid.Wijmo.Feature {
321353
) as OSFramework.DataGrid.Feature.Auxiliar.RowStyleInfo;
322354
}
323355

324-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
325-
public getRowData(rowNumber: number): any {
356+
/**
357+
* Gets the data from a specific row.
358+
*
359+
* @param {number} rowNumber
360+
* @return {*} {unknown}
361+
* @memberof Rows
362+
*/
363+
public getRowData(rowNumber: number): unknown {
326364
const row = this._getDataItemFromRow(rowNumber);
327365

328366
if (!row) {

src/Providers/DataGrid/Wijmo/Grid/FlexGrid.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ namespace Providers.DataGrid.Wijmo.Grid {
6868
});
6969
}
7070

71-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
72-
private _getProviderConfig(): any {
71+
private _getProviderConfig(): unknown {
7372
if (this.hasColumnsDefined()) {
7473
this.config.autoGenerateColumns = false;
7574
}
@@ -270,7 +269,6 @@ namespace Providers.DataGrid.Wijmo.Grid {
270269
}
271270

272271
public getChangesMade(): OSFramework.DataGrid.OSStructure.GridChanges {
273-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
274272
const changes = this.dataSource.getChanges(OSFramework.DataGrid.OSStructure.GridChanges);
275273

276274
if (this._features.validationMark.invalidRows.size > 0) {

0 commit comments

Comments
 (0)