Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AAE-22900] Update ObjectDataTableAdapter to support server and clien… #10418

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 48 additions & 39 deletions lib/core/src/lib/datatable/data/object-datatable-adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { ObjectDataRow } from './object-datarow.model';
import { ObjectDataColumn } from './object-datacolumn.model';

describe('ObjectDataTableAdapter', () => {

it('should init with empty row collection', () => {
const adapter = new ObjectDataTableAdapter(null, []);
expect(adapter.getRows()).toBeDefined();
Expand All @@ -46,10 +45,7 @@ describe('ObjectDataTableAdapter', () => {
});

it('should map columns without rows', () => {
const adapter = new ObjectDataTableAdapter(null, [
{} as DataColumn,
{} as DataColumn
]);
const adapter = new ObjectDataTableAdapter(null, [{} as DataColumn, {} as DataColumn]);
const columns = adapter.getColumns();

expect(columns.length).toBe(2);
Expand Down Expand Up @@ -99,7 +95,7 @@ describe('ObjectDataTableAdapter', () => {

it('should apply new columns array', () => {
const adapter = new ObjectDataTableAdapter([], []);
const columns = [{},{}] as DataColumn[];
const columns = [{}, {}] as DataColumn[];

adapter.setColumns(columns);
expect(adapter.getColumns()).toBe(columns);
Expand All @@ -116,10 +112,7 @@ describe('ObjectDataTableAdapter', () => {
});

it('should reset columns by null value', () => {
const adapter = new ObjectDataTableAdapter([], [
{} as DataColumn,
{} as DataColumn
]);
const adapter = new ObjectDataTableAdapter([], [{} as DataColumn, {} as DataColumn]);
expect(adapter.getColumns()).toBeDefined();
expect(adapter.getColumns().length).toBe(2);

Expand Down Expand Up @@ -188,9 +181,7 @@ describe('ObjectDataTableAdapter', () => {
{ id: 2, name: 'abs' },
{ id: 1, name: 'xyz' }
],
[
new ObjectDataColumn({ key: 'id', sortable: true })
]
[new ObjectDataColumn({ key: 'id', sortable: true })]
);

const rows = adapter.getRows();
Expand All @@ -199,10 +190,7 @@ describe('ObjectDataTableAdapter', () => {
});

it('should take first sortable column by default', () => {
const adapter = new ObjectDataTableAdapter([], [
{ key: 'icon' } as DataColumn,
new ObjectDataColumn({ key: 'id', sortable: true })
]);
const adapter = new ObjectDataTableAdapter([], [{ key: 'icon' } as DataColumn, new ObjectDataColumn({ key: 'id', sortable: true })]);

expect(adapter.getSorting()).toEqual(
jasmine.objectContaining({
Expand All @@ -221,10 +209,7 @@ describe('ObjectDataTableAdapter', () => {
{ id: 1, created: new Date(2016, 7, 6, 15, 7, 2) },
{ id: 2, created: new Date(2016, 7, 6, 15, 7, 1) }
],
[
{ key: 'id' } as DataColumn,
{ key: 'created' } as DataColumn
]
[{ key: 'id' } as DataColumn, { key: 'created' } as DataColumn]
);

adapter.setSorting(new DataSorting('created', 'asc', { numeric: true }));
Expand All @@ -235,11 +220,7 @@ describe('ObjectDataTableAdapter', () => {
});

it('should sort by numbers', () => {
const adapter = new ObjectDataTableAdapter([
{ id: 123 },
{ id: 38 },
{ id: 50 }
],[{key: 'id'} as DataColumn]);
const adapter = new ObjectDataTableAdapter([{ id: 123 }, { id: 38 }, { id: 50 }], [{ key: 'id' } as DataColumn]);

adapter.setSorting(new DataSorting('id', 'asc', { numeric: true }));

Expand All @@ -255,10 +236,7 @@ describe('ObjectDataTableAdapter', () => {
{ id: 2, name: 'abs' },
{ id: 1, name: 'xyz' }
],
[
new ObjectDataColumn({ key: 'id' }),
new ObjectDataColumn({ key: 'name' })
]
[new ObjectDataColumn({ key: 'id' }), new ObjectDataColumn({ key: 'name' })]
);

expect(adapter.getSorting()).toBeUndefined();
Expand All @@ -270,9 +248,7 @@ describe('ObjectDataTableAdapter', () => {
{ id: 2, name: 'abs' },
{ id: 1, name: 'xyz' }
],
[
new ObjectDataColumn({ key: 'id', sortable: true })
]
[new ObjectDataColumn({ key: 'id', sortable: true })]
);

adapter.setSorting(new DataSorting('id', 'asc', { numeric: true }));
Expand Down Expand Up @@ -313,10 +289,43 @@ describe('ObjectDataTableAdapter', () => {
})
);
});

it('should not sort rows ascending when in server sorting mode', () => {
const adapter = new ObjectDataTableAdapter(
[
{ id: 2, name: 'abs' },
{ id: 1, name: 'xyz' }
],
[new ObjectDataColumn({ key: 'id', sortable: true })],
'server'
);

adapter.setSorting(new DataSorting('id', 'asc', { numeric: true }));

const rows = adapter.getRows();
expect(rows[0].getValue('id')).toBe(2);
expect(rows[1].getValue('id')).toBe(1);
});

it('should not sort rows descending when in server sorting mode', () => {
const adapter = new ObjectDataTableAdapter(
[
{ id: 1, name: 'xyz' },
{ id: 2, name: 'abs' }
],
[new ObjectDataColumn({ key: 'id', sortable: true })],
'server'
);

adapter.setSorting(new DataSorting('id ', 'desc', { numeric: true }));

const rows = adapter.getRows();
expect(rows[0].getValue('id')).toBe(1);
expect(rows[1].getValue('id')).toBe(2);
});
});

describe('ObjectDataRow', () => {

it('should require object source', () => {
expect(() => new ObjectDataRow(null)).toThrowError('Object source not found');
});
Expand All @@ -335,10 +344,10 @@ describe('ObjectDataRow', () => {

it('should get nested property value', () => {
const row = new ObjectDataRow({
name: {
firstName: 'John',
lastName: 'Doe'
}
name: {
firstName: 'John',
lastName: 'Doe'
}
});

expect(row.getValue('name.lastName')).toBe('Doe');
Expand Down Expand Up @@ -370,7 +379,7 @@ describe('ObjectDataRow', () => {
});

it('should generateSchema generate a schema from data', () => {
const data = [
const data = [
{ id: 2, name: 'abs' },
{ id: 1, name: 'xyz' }
];
Expand Down
13 changes: 12 additions & 1 deletion lib/core/src/lib/datatable/data/object-datatable-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ import { DataSorting } from './data-sorting.model';
import { DataTableAdapter } from './datatable-adapter';
import { Subject } from 'rxjs';

export type SortingMode = 'client' | 'server';

// Simple implementation of the DataTableAdapter interface.
export class ObjectDataTableAdapter implements DataTableAdapter {
private _sorting: DataSorting;
private _rows: DataRow[];
private _columns: DataColumn[];
private readonly _sortingMode: SortingMode;

selectedRow: DataRow;
rowsChanged: Subject<Array<DataRow>>;
Expand All @@ -54,10 +57,14 @@ export class ObjectDataTableAdapter implements DataTableAdapter {
return schema;
}

constructor(data: any[] = [], schema: DataColumn[] = []) {
constructor(data: any[] = [], schema: DataColumn[] = [], sortingMode: SortingMode = 'client') {
this._rows = [];
this._columns = [];

if (!sortingMode) {
sortingMode = 'client';
}

if (data && data.length > 0) {
this._rows = data.map((item) => new ObjectDataRow(item));
}
Expand Down Expand Up @@ -119,6 +126,10 @@ export class ObjectDataTableAdapter implements DataTableAdapter {
setSorting(sorting: DataSorting): void {
this._sorting = sorting;

if (this._sortingMode === 'server') {
return;
}

if (sorting?.key) {
this._rows.sort((a: DataRow, b: DataRow) => {
let left = a.getValue(sorting.key) ?? '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ describe('ProcessListDatatableAdapter', () => {

expect(adapter.getColumnType(row, column)).toBe('number');
});

it('should initialize constructor with server sortingMode', () => {
const adapter = new ProcessListDatatableAdapter([], []);
expect(adapter['_sortingMode']).toBe('server');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { ProcessInstanceCloudListViewModel } from '../models/perocess-instance-c

export class ProcessListDatatableAdapter extends ObjectDataTableAdapter {
constructor(data: ProcessInstanceCloudListViewModel[], schema: DataColumn<ProcessListDataColumnCustomData>[]) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DenysVuika @eromano Do we want to provide the ability to set the sortingMode in these extending classes so we can utilize feature flags in the hxp-frontend-apps projects? Not sure if we want a feature flag for this but if we did, we'd need to be able to provide the sort option here.

Based on the information I've received, we don't want pages using these adapters to use client sorting so the constructor parameter would be redundant. Also, we would have to remove the constructor parameter once the feature flag is removed so it seems architecturally redundant.

super(data, schema);
super(data, schema, 'server');
}

getColumnType(row: DataRow, col: DataColumn<ProcessListDataColumnCustomData>): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ describe('TasksListDatatableAdapter', () => {

expect(adapter.getColumnType(row, column)).toBe('number');
});

it('should initialize constructor with server sortingMode', () => {
const adapter = new TasksListDatatableAdapter([], []);
expect(adapter['_sortingMode']).toBe('server');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { TaskInstanceCloudListViewModel } from '../models/task-cloud-view.model'

export class TasksListDatatableAdapter extends ObjectDataTableAdapter {
constructor(data: TaskInstanceCloudListViewModel[], schema: DataColumn<ProcessListDataColumnCustomData>[]) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DenysVuika @eromano Same question for this as the other adapter class comment.

super(data, schema);
super(data, schema, 'server');
}

getColumnType(row: DataRow, col: DataColumn<ProcessListDataColumnCustomData>): string {
Expand Down
Loading