Commit abd30907 authored by PECQUOT's avatar PECQUOT
Browse files

[fix] reselect saved row

parent a3e51beb
......@@ -7,7 +7,7 @@ import { catchError, debounceTime, distinctUntilChanged, filter, first, map, mer
import { TableElement } from '@e-is/ngx-material-table';
import { EntitiesTableDataSource } from './entities-table-datasource.class';
import { SelectionModel } from '@angular/cdk/collections';
import { IEntity } from '../services/model/entity.model';
import { EntityUtils, IEntity } from '../services/model/entity.model';
import { AlertController, ModalController, Platform, ToastController } from '@ionic/angular';
import { ActivatedRoute, Router } from '@angular/router';
import { ColumnItem, TableSelectColumnsComponent } from './table-select-columns.component';
......@@ -82,6 +82,7 @@ export abstract class AppTable<
protected alertCtrl: AlertController;
protected toastController: ToastController;
protected _focusColumn: string;
protected previouslySavedEditedRowId: number;
excludesColumns: string[] = [];
displayedColumns: string[];
......@@ -258,13 +259,16 @@ export abstract class AppTable<
}
markAsUntouched(opts?: {onlySelf?: boolean; emitEvent?: boolean }) {
if (this.dirtySubject.value !== false || this.editedRow) {
if (this.dirtySubject.value !== false || (this.editedRow && this.editedRow.id !== this.previouslySavedEditedRowId)) {
// Cancel the current editing row only if it was not previously saved
this.dirtySubject.next(false);
this.dataSource.cancelOrDelete(this.editedRow);
this.editedRow = null;
if (!opts || opts.emitEvent !== false) {
this.markForCheck();
}
}
this.previouslySavedEditedRowId = undefined;
}
markAsTouched(opts?: {onlySelf?: boolean; emitEvent?: boolean }) {
......@@ -454,7 +458,6 @@ export abstract class AppTable<
(event: any) => {
this.dirtySubject.next(false);
this.selection.clear();
this.editedRow = undefined;
if (event === 'skip') {
return of(undefined);
}
......@@ -774,7 +777,7 @@ export abstract class AppTable<
return true;
}
async save(opts?: {keepEditing: boolean; }): Promise<boolean> {
async save(opts?: {keepEditing?: boolean; }): Promise<boolean> {
opts = {
keepEditing: this.keepEditedRowOnSave,
...opts
......@@ -786,7 +789,7 @@ export abstract class AppTable<
this.resetError();
// Keep edited row id (should be done BEFORE confirmEditCreate() )
const editedRowId = opts.keepEditing && this.editedRow && this.editedRow.editing && this.editedRow.id;
this.previouslySavedEditedRowId = opts.keepEditing && this.editedRow?.editing ? this.editedRow.id : undefined;
if (!this.confirmEditCreate()) {
throw {code: ErrorCodes.TABLE_INVALID_ROW_ERROR, message: 'ERROR.TABLE_INVALID_ROW_ERROR'};
......@@ -813,14 +816,12 @@ export abstract class AppTable<
this.markAsSaved();
// Restoring previous row
if (isNotNil(editedRowId)) {
if (editedRowId !== -1) {
this.dataSource.waitIdle()
.then(() => this.dataSource.getRow(editedRowId))
// Select by row id
.then(row => row && this.clickRow(null, row));
if (isNotNil(this.previouslySavedEditedRowId)) {
if (this.previouslySavedEditedRowId !== -1) {
// Select by row id
this.selectRowById(this.previouslySavedEditedRowId);
} else {
// TODO: find a way to restore the row (find it by data ?)
this.selectRowByData(this.singleSelectedRow?.currentData);
}
}
}
......@@ -987,6 +988,20 @@ export abstract class AppTable<
return true;
}
selectRowById(id: number) {
if (id === undefined) return;
this.dataSource.waitIdle()
.then(() => this.dataSource.getRow(id))
.then(row => this.clickRow(null, row));
}
selectRowByData(data: T) {
if (data === undefined) return;
this.dataSource.waitIdle()
.then(() => this.dataSource.getRows())
.then(rows => this.selectRowById(rows.find(row => this.dataEquals(row.currentData, data))?.id));
}
clickRow(event: UIEvent|undefined, row: TableElement<T>): boolean {
if (this.loading) {
// Wait while loading, and loop
......@@ -1497,6 +1512,18 @@ export abstract class AppTable<
}
}
/**
* Compare data equality (default by id)
* Can be overridden to add additional properties to compare
*
* @param t1
* @param t2
* @protected
*/
protected dataEquals(t1: T, t2: T): boolean {
return EntityUtils.equals(t1, t2, 'id');
}
/* -- private method -- */
private setLoading(value: boolean, opts?: {emitEvent?: boolean}) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment