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

[ACA-3368] Update datatable columns when presetColumn schema changes #5732

Merged
merged 4 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,21 @@ describe('DataTable', () => {
expect(dataTable.data.getRows().length).toEqual(2);
expect(dataTable.resolverFn).toHaveBeenCalledTimes(4);
});

it('should update data columns when columns input changes', () => {
const existingDataColumnsSchema = [new ObjectDataColumn({ key: 'id' })];
const existingData = [{ id: 'fake-data' }];
dataTable.data = new ObjectDataTableAdapter(
existingData,
existingDataColumnsSchema
);

const newDataColumnsSchema = { key: 'new-column'};
const columnsChange = new SimpleChange(null, [newDataColumnsSchema], false);
dataTable.ngOnChanges({ 'columns': columnsChange });
const expectedNewDataColumns = [new ObjectDataColumn(newDataColumnsSchema)];
expect(dataTable.data.getColumns()).toEqual(expectedNewDataColumns);
});
});

describe('Accesibility', () => {
Expand Down
40 changes: 28 additions & 12 deletions lib/core/datatable/components/datatable/datatable.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { DataTableAdapter } from '../../data/datatable-adapter';
import { DataTableRowComponent } from './datatable-row.component';

import { ObjectDataRow } from '../../data/object-datarow.model';
import { ObjectDataColumn } from '../../data/object-datacolumn.model';
import { ObjectDataTableAdapter } from '../../data/object-datatable-adapter';
import { DataCellEvent } from './data-cell.event';
import { DataRowActionEvent } from './data-row-action.event';
Expand Down Expand Up @@ -225,21 +226,23 @@ export class DataTableComponent implements AfterContentInit, OnChanges, DoCheck,

ngOnChanges(changes: SimpleChanges) {
this.initAndSubscribeClickStream();
if (this.isPropertyChanged(changes['data'])) {
if (this.isTableEmpty()) {
this.initTable();
} else {
this.data = changes['data'].currentValue;
this.resetSelection();
}
return;
}

if (this.isPropertyChanged(changes['rows'])) {
const dataChanges = changes['data'];
const rowChanges = changes['rows'];
const columnChanges = changes['columns'];

if (this.isPropertyChanged(dataChanges) || this.isPropertyChanged(rowChanges) || this.isPropertyChanged(columnChanges)) {
if (this.isTableEmpty()) {
this.initTable();
} else {
this.setTableRows(changes['rows'].currentValue);
if (dataChanges) {
this.data = changes['data'].currentValue;
this.resetSelection();
} else if (rowChanges) {
this.setTableRows(changes['rows'].currentValue);
} else {
this.setTableColumns(changes['columns'].currentValue);
}
}
return;
}
Expand Down Expand Up @@ -280,6 +283,10 @@ export class DataTableComponent implements AfterContentInit, OnChanges, DoCheck,
return rows.map((row) => new ObjectDataRow(row, row.isSelected));
}

convertToColumnsData(columns: any []): ObjectDataColumn[] {
return columns.map((column) => new ObjectDataColumn(column));
}

convertToDataSorting(sorting: any[]): DataSorting | null {
if (sorting && sorting.length > 0) {
return new DataSorting(sorting[0], sorting[1]);
Expand Down Expand Up @@ -366,7 +373,16 @@ export class DataTableComponent implements AfterContentInit, OnChanges, DoCheck,
private setTableRows(rows: any[]) {
if (this.data) {
this.resetSelection();
this.data.setRows(this.convertToRowsData(rows));
const rowsData = this.convertToRowsData(rows);
this.data.setRows(rowsData);
}
}

private setTableColumns(columns: any[]) {
if (this.data) {
this.resetSelection();
const columnsData = this.convertToColumnsData(columns);
this.data.setColumns(columnsData);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
* limitations under the License.
*/

import { ObjectDataColumn } from '@alfresco/adf-core';

export let fakeProcessInstance = {
size: 2,
total: 2,
Expand Down Expand Up @@ -133,28 +131,42 @@ export let fakeProcessInstancesEmpty = {
};

export let fakeProcessCustomSchema = [
new ObjectDataColumn({
{
key: 'fakeName',
type: 'text',
title: 'ADF_PROCESS_LIST.PROPERTIES.FAKE',
sortable: true
}),
new ObjectDataColumn({
},
{
key: 'fakeProcessName',
type: 'text',
title: 'ADF_PROCESS_LIST.PROPERTIES.PROCESS_FAKE',
sortable: true
})
}
];

export let fakeProcessColumnSchema = {
default: [
{
key: 'name',
key: 'default-name',
type: 'text',
title: 'ADF_PROCESS_LIST.PROPERTIES.NAME',
sortable: true
}
],
fakeRunningProcessSchema: [
{
key: 'process-id',
type: 'text',
title: 'ADF_PROCESS_LIST.PROPERTIES.FAKE',
sortable: true
},
{
key: 'process-name',
type: 'text',
title: 'ADF_PROCESS_LIST.PROPERTIES.PROCESS_FAKE',
sortable: true
}
],
fakeProcessCustomSchema
};
26 changes: 19 additions & 7 deletions lib/process-services/src/lib/mock/task/task-list.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
* limitations under the License.
*/

import { ObjectDataColumn } from '@alfresco/adf-core';

export const fakeGlobalTask = {
size: 2,
start: 0,
Expand Down Expand Up @@ -79,29 +77,43 @@ export const fakeGlobalTask = {
};

export let fakeCustomSchema = [
new ObjectDataColumn({
{
key: 'fakeName',
type: 'text',
title: 'ADF_TASK_LIST.PROPERTIES.FAKE',
sortable: true
}),
new ObjectDataColumn({
},
{
key: 'fakeTaskName',
type: 'text',
title: 'ADF_TASK_LIST.PROPERTIES.TASK_FAKE',
sortable: true
})
}
];

export let fakeColumnSchema = {
default: [
{
key: 'name',
key: 'default-name',
type: 'text',
title: 'ADF_TASK_LIST.PROPERTIES.NAME',
sortable: true
}
],
fakeMyTasksSchema: [
{
key: 'task-id',
type: 'text',
title: 'ADF_TASK_LIST.PROPERTIES.FAKE',
sortable: true
},
{
key: 'task-name',
type: 'text',
title: 'ADF_TASK_LIST.PROPERTIES.PROCESS_FAKE',
sortable: true
}
],
fakeCustomSchema
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ import { By } from '@angular/platform-browser';
import { ProcessInstanceListComponent } from './process-list.component';
import {
AppConfigService, setupTestBed, DataRow, DataColumn,
DataRowEvent, ObjectDataRow, ObjectDataTableAdapter, DataCellEvent
DataRowEvent, ObjectDataRow, ObjectDataTableAdapter, DataCellEvent, ObjectDataColumn
} from '@alfresco/adf-core';
import { fakeProcessInstance, fakeProcessInstancesWithNoName, fakeProcessInstancesEmpty, fakeProcessCustomSchema } from '../../mock';
import {
fakeProcessInstance,
fakeProcessInstancesWithNoName,
fakeProcessInstancesEmpty,
fakeProcessColumnSchema
} from '../../mock';
import { ProcessService } from '../services/process.service';
import { ProcessTestingModule } from '../../testing/process.testing.module';
import { TranslateModule } from '@ngx-translate/core';
Expand Down Expand Up @@ -60,22 +65,7 @@ describe('ProcessInstanceListComponent', () => {

getProcessInstancesSpy = spyOn(service, 'getProcessInstances').and.returnValue(of(fakeProcessInstance));
appConfig.config['adf-process-list'] = {
'presets': {
'fakeProcessCustomSchema': [
{
'key': 'fakeName',
'type': 'text',
'title': 'ADF_PROCESS_LIST.PROPERTIES.FAKE',
'sortable': true
},
{
'key': 'fakeProcessName',
'type': 'text',
'title': 'ADF_PROCESS_LIST.PROPERTIES.PROCESS_FAKE',
'sortable': true
}
]
}
'presets': fakeProcessColumnSchema
};
}));

Expand All @@ -89,7 +79,8 @@ describe('ProcessInstanceListComponent', () => {
it('should use the default schemaColumn as default', () => {
component.ngAfterContentInit();
expect(component.columns).toBeDefined();
expect(component.columns.length).toEqual(2);
expect(component.columns.length).toEqual(1);
expect(component.columns[0]).toEqual(new ObjectDataColumn(fakeProcessColumnSchema.default[0]));
});

it('should use the schemaColumn passed in input', () => {
Expand All @@ -109,7 +100,9 @@ describe('ProcessInstanceListComponent', () => {
component.presetColumn = 'fakeProcessCustomSchema';
component.ngAfterContentInit();
fixture.detectChanges();
expect(component.columns).toEqual(fakeProcessCustomSchema);
expect(component.columns.length).toEqual(2);
expect(component.columns[0]).toEqual(new ObjectDataColumn(fakeProcessColumnSchema.fakeProcessCustomSchema[0]));
expect(component.columns[1]).toEqual(new ObjectDataColumn(fakeProcessColumnSchema.fakeProcessCustomSchema[1]));
});

it('should fetch custom schemaColumn when the input presetColumn is defined', () => {
Expand Down Expand Up @@ -437,6 +430,27 @@ describe('ProcessInstanceListComponent', () => {

component.ngOnChanges({'processInstanceId': change});
});

it('should update the columns when presetColumn schema changes', () => {
component.presetColumn = 'fakeProcessCustomSchema';
component.ngAfterContentInit();
const initialColumnSchema = component.mergeJsonAndHtmlSchema();
expect(component.columns).toEqual(initialColumnSchema);

component.presetColumn = 'fakeRunningProcessSchema';
const presetColumnChange = new SimpleChange(null, 'fakeRunningProcessSchema', false);
component.ngOnChanges({ 'presetColumn': presetColumnChange });

const newColumnSchema = component.mergeJsonAndHtmlSchema();
const expectedColumn1 = new ObjectDataColumn(fakeProcessColumnSchema.fakeRunningProcessSchema[0]);
const expectedColumn2 = new ObjectDataColumn(fakeProcessColumnSchema.fakeRunningProcessSchema[1]);

expect(component.columns).toEqual(newColumnSchema);
expect(initialColumnSchema).not.toEqual(newColumnSchema);
expect(component.columns.length).toEqual(2);
expect(component.columns[0]).toEqual(expectedColumn1);
expect(component.columns[1]).toEqual(expectedColumn2);
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ export class ProcessInstanceListComponent extends DataTableSchema implements OnC
}
this.reload();
}

const presetColumnChanges = changes['presetColumn'];
if (presetColumnChanges && !presetColumnChanges.firstChange) {
this.columns = this.mergeJsonAndHtmlSchema();
}
}

private isSortChanged(changes: SimpleChanges): boolean {
Expand Down