mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
30
lib/core/datatable/data/data-column.model.ts
Normal file
30
lib/core/datatable/data/data-column.model.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { TemplateRef } from '@angular/core';
|
||||
|
||||
export interface DataColumn {
|
||||
key: string;
|
||||
type: string; // text|image|date
|
||||
format?: string;
|
||||
sortable?: boolean;
|
||||
title?: string;
|
||||
srTitle?: string;
|
||||
cssClass?: string;
|
||||
template?: TemplateRef<any>;
|
||||
formatTooltip?: Function;
|
||||
}
|
32
lib/core/datatable/data/data-row-event.model.ts
Normal file
32
lib/core/datatable/data/data-row-event.model.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { BaseUIEvent } from '../../events';
|
||||
import { DataRow } from './data-row.model';
|
||||
|
||||
export class DataRowEvent extends BaseUIEvent<DataRow> {
|
||||
|
||||
sender: any;
|
||||
|
||||
constructor(value: DataRow, domEvent: Event, sender?: any) {
|
||||
super();
|
||||
this.value = value;
|
||||
this.event = domEvent;
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
}
|
24
lib/core/datatable/data/data-row.model.ts
Normal file
24
lib/core/datatable/data/data-row.model.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export interface DataRow {
|
||||
isSelected: boolean;
|
||||
isDropTarget?: boolean;
|
||||
cssClass?: string;
|
||||
hasValue(key: string): boolean;
|
||||
getValue(key: string): any;
|
||||
}
|
23
lib/core/datatable/data/data-sorting.model.ts
Normal file
23
lib/core/datatable/data/data-sorting.model.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export class DataSorting {
|
||||
constructor(
|
||||
public key?: string,
|
||||
public direction?: string) {
|
||||
}
|
||||
}
|
32
lib/core/datatable/data/datatable-adapter.ts
Normal file
32
lib/core/datatable/data/datatable-adapter.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { DataColumn } from './data-column.model';
|
||||
import { DataRow } from './data-row.model';
|
||||
import { DataSorting } from './data-sorting.model';
|
||||
|
||||
export interface DataTableAdapter {
|
||||
selectedRow: DataRow;
|
||||
getRows(): Array<DataRow>;
|
||||
setRows(rows: Array<DataRow>): void;
|
||||
getColumns(): Array<DataColumn>;
|
||||
setColumns(columns: Array<DataColumn>): void;
|
||||
getValue(row: DataRow, col: DataColumn): any;
|
||||
getSorting(): DataSorting;
|
||||
setSorting(sorting: DataSorting): void;
|
||||
sort(key?: string, direction?: string): void;
|
||||
}
|
23
lib/core/datatable/data/index.ts
Normal file
23
lib/core/datatable/data/index.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export { DataTableAdapter } from './datatable-adapter';
|
||||
export { ObjectDataColumn, ObjectDataRow, ObjectDataTableAdapter } from './object-datatable-adapter';
|
||||
export { DataRow } from './data-row.model';
|
||||
export { DataRowEvent } from './data-row-event.model';
|
||||
export { DataColumn } from './data-column.model';
|
||||
export { DataSorting } from './data-sorting.model';
|
366
lib/core/datatable/data/object-datatable-adapter.spec.ts
Normal file
366
lib/core/datatable/data/object-datatable-adapter.spec.ts
Normal file
@@ -0,0 +1,366 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { DataColumn } from './data-column.model';
|
||||
import { DataRow } from './data-row.model';
|
||||
import { DataSorting } from './data-sorting.model';
|
||||
import { ObjectDataColumn, ObjectDataRow, ObjectDataTableAdapter } from './object-datatable-adapter';
|
||||
|
||||
describe('ObjectDataTableAdapter', () => {
|
||||
|
||||
it('should init with empty row collection', () => {
|
||||
let adapter = new ObjectDataTableAdapter(null, []);
|
||||
expect(adapter.getRows()).toBeDefined();
|
||||
expect(adapter.getRows().length).toBe(0);
|
||||
});
|
||||
|
||||
it('should init with empty column collection', () => {
|
||||
let adapter = new ObjectDataTableAdapter([], null);
|
||||
expect(adapter.getColumns()).toBeDefined();
|
||||
expect(adapter.getColumns().length).toBeDefined();
|
||||
});
|
||||
|
||||
it('should map rows', () => {
|
||||
let adapter = new ObjectDataTableAdapter([{}, {}], null);
|
||||
let rows = adapter.getRows();
|
||||
|
||||
expect(rows.length).toBe(2);
|
||||
expect(rows[0] instanceof ObjectDataRow).toBe(true);
|
||||
expect(rows[1] instanceof ObjectDataRow).toBe(true);
|
||||
});
|
||||
|
||||
it('should map columns without rows', () => {
|
||||
let adapter = new ObjectDataTableAdapter(null, [
|
||||
<DataColumn> {},
|
||||
<DataColumn> {}
|
||||
]);
|
||||
let columns = adapter.getColumns();
|
||||
|
||||
expect(columns.length).toBe(2);
|
||||
expect(columns[0] instanceof ObjectDataColumn).toBe(true);
|
||||
expect(columns[1] instanceof ObjectDataColumn).toBe(true);
|
||||
});
|
||||
|
||||
it('should sort by first column if column is available', () => {
|
||||
let adapter = new ObjectDataTableAdapter(null, null);
|
||||
expect(adapter.getSorting()).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should apply new rows array', () => {
|
||||
let adapter = new ObjectDataTableAdapter([], []);
|
||||
let newRows = [
|
||||
<DataRow> {},
|
||||
<DataRow> {}
|
||||
];
|
||||
|
||||
adapter.setRows(newRows);
|
||||
expect(adapter.getRows()).toBe(newRows);
|
||||
});
|
||||
|
||||
it('should accept null for new rows array', () => {
|
||||
let adapter = new ObjectDataTableAdapter([], []);
|
||||
expect(adapter.getRows()).toBeDefined();
|
||||
expect(adapter.getRows().length).toBe(0);
|
||||
|
||||
adapter.setRows(null);
|
||||
expect(adapter.getRows()).toBeDefined();
|
||||
expect(adapter.getRows().length).toBe(0);
|
||||
});
|
||||
|
||||
it('should reset rows by null value', () => {
|
||||
let adapter = new ObjectDataTableAdapter([{}, {}], []);
|
||||
expect(adapter.getRows()).toBeDefined();
|
||||
expect(adapter.getRows().length).toBe(2);
|
||||
|
||||
adapter.setRows(null);
|
||||
expect(adapter.getRows()).toBeDefined();
|
||||
expect(adapter.getRows().length).toBe(0);
|
||||
});
|
||||
|
||||
it('should sort new row collection', () => {
|
||||
let adapter = new ObjectDataTableAdapter([], []);
|
||||
spyOn(adapter, 'sort').and.callThrough();
|
||||
adapter.setRows([]);
|
||||
expect(adapter.sort).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should apply new columns array', () => {
|
||||
let adapter = new ObjectDataTableAdapter([], []);
|
||||
let columns = [
|
||||
<DataColumn> {},
|
||||
<DataColumn> {}
|
||||
];
|
||||
|
||||
adapter.setColumns(columns);
|
||||
expect(adapter.getColumns()).toBe(columns);
|
||||
});
|
||||
|
||||
it('should accept null for new columns array', () => {
|
||||
let adapter = new ObjectDataTableAdapter([], []);
|
||||
expect(adapter.getColumns()).toBeDefined();
|
||||
expect(adapter.getColumns().length).toBe(0);
|
||||
|
||||
adapter.setColumns(null);
|
||||
expect(adapter.getColumns()).toBeDefined();
|
||||
expect(adapter.getColumns().length).toBe(0);
|
||||
});
|
||||
|
||||
it('should reset columns by null value', () => {
|
||||
let adapter = new ObjectDataTableAdapter([], [
|
||||
<DataColumn> {},
|
||||
<DataColumn> {}
|
||||
]);
|
||||
expect(adapter.getColumns()).toBeDefined();
|
||||
expect(adapter.getColumns().length).toBe(2);
|
||||
|
||||
adapter.setColumns(null);
|
||||
expect(adapter.getColumns()).toBeDefined();
|
||||
expect(adapter.getColumns().length).toBe(0);
|
||||
});
|
||||
|
||||
it('should fail getting value with row not defined', () => {
|
||||
let adapter = new ObjectDataTableAdapter([], []);
|
||||
expect(() => { adapter.getValue(null, null); }).toThrowError('Row not found');
|
||||
});
|
||||
|
||||
it('should fail getting value with column not defined', () => {
|
||||
let adapter = new ObjectDataTableAdapter([], []);
|
||||
expect(() => { adapter.getValue(<DataRow> {}, null); }).toThrowError('Column not found');
|
||||
});
|
||||
|
||||
it('should get value from row with column key', () => {
|
||||
let value = 'hello world';
|
||||
|
||||
let row = jasmine.createSpyObj('row', ['getValue']);
|
||||
row.getValue.and.returnValue(value);
|
||||
|
||||
let adapter = new ObjectDataTableAdapter([], []);
|
||||
let result = adapter.getValue(row, <DataColumn> { key: 'col1' });
|
||||
|
||||
expect(row.getValue).toHaveBeenCalledWith('col1');
|
||||
expect(result).toBe(value);
|
||||
});
|
||||
|
||||
it('should set new sorting', () => {
|
||||
let sorting = new DataSorting('key', 'direction');
|
||||
let adapter = new ObjectDataTableAdapter([], []);
|
||||
|
||||
adapter.setSorting(sorting);
|
||||
expect(adapter.getSorting()).toBe(sorting);
|
||||
|
||||
adapter.setSorting(null);
|
||||
expect(adapter.getSorting()).toBe(null);
|
||||
});
|
||||
|
||||
it('should sort rows with new sorting value', () => {
|
||||
let adapter = new ObjectDataTableAdapter([{}, {}], []);
|
||||
spyOn(adapter.getRows(), 'sort').and.stub();
|
||||
|
||||
adapter.setSorting(new DataSorting('key', 'direction'));
|
||||
expect(adapter.getRows().sort).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should sort rows only when sorting key provided', () => {
|
||||
let adapter = new ObjectDataTableAdapter([{}, {}], []);
|
||||
spyOn(adapter.getRows(), 'sort').and.stub();
|
||||
|
||||
adapter.setSorting(new DataSorting());
|
||||
expect(adapter.getRows().sort).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should sort by first column by default', () => {
|
||||
let adapter = new ObjectDataTableAdapter(
|
||||
[
|
||||
{ id: 2, name: 'abs' },
|
||||
{ id: 1, name: 'xyz' }
|
||||
],
|
||||
[
|
||||
new ObjectDataColumn({ key: 'id', sortable: true })
|
||||
]
|
||||
);
|
||||
|
||||
let rows = adapter.getRows();
|
||||
expect(rows[0].getValue('id')).toBe(1);
|
||||
expect(rows[1].getValue('id')).toBe(2);
|
||||
});
|
||||
|
||||
it('should take first sortable column by default', () => {
|
||||
let adapter = new ObjectDataTableAdapter([], [
|
||||
<DataColumn> { key: 'icon' },
|
||||
new ObjectDataColumn({ key: 'id', sortable: true })
|
||||
]);
|
||||
|
||||
expect(adapter.getSorting()).toEqual(
|
||||
jasmine.objectContaining({
|
||||
key: 'id',
|
||||
direction: 'asc'
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('should sort by dates', () => {
|
||||
let adapter = new ObjectDataTableAdapter(
|
||||
[
|
||||
{ id: 1, created: new Date(2016, 7, 6, 15, 7, 2) },
|
||||
{ id: 2, created: new Date(2016, 7, 6, 15, 7, 1) }
|
||||
],
|
||||
[
|
||||
<DataColumn> { key: 'id' },
|
||||
<DataColumn> { key: 'created' }
|
||||
]
|
||||
);
|
||||
|
||||
adapter.setSorting(new DataSorting('created', 'asc'));
|
||||
|
||||
let rows = adapter.getRows();
|
||||
expect(rows[0].getValue('id')).toBe(2);
|
||||
expect(rows[1].getValue('id')).toBe(1);
|
||||
});
|
||||
|
||||
it('should be sorting undefined if no sortable found', () => {
|
||||
let adapter = new ObjectDataTableAdapter(
|
||||
[
|
||||
{ id: 2, name: 'abs' },
|
||||
{ id: 1, name: 'xyz' }
|
||||
],
|
||||
[
|
||||
new ObjectDataColumn({ key: 'id' }),
|
||||
new ObjectDataColumn({ key: 'name' })
|
||||
]
|
||||
);
|
||||
|
||||
expect(adapter.getSorting()).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should sort asc and desc', () => {
|
||||
let adapter = new ObjectDataTableAdapter(
|
||||
[
|
||||
{ id: 2, name: 'abs' },
|
||||
{ id: 1, name: 'xyz' }
|
||||
],
|
||||
[
|
||||
new ObjectDataColumn({ key: 'id', sortable: true })
|
||||
]
|
||||
);
|
||||
|
||||
adapter.setSorting(new DataSorting('id', 'asc'));
|
||||
expect(adapter.getRows()[0].getValue('id')).toBe(1);
|
||||
expect(adapter.getRows()[1].getValue('id')).toBe(2);
|
||||
|
||||
adapter.setSorting(new DataSorting('id', 'desc'));
|
||||
expect(adapter.getRows()[0].getValue('id')).toBe(2);
|
||||
expect(adapter.getRows()[1].getValue('id')).toBe(1);
|
||||
});
|
||||
|
||||
it('should use asc for sort command by default', () => {
|
||||
let adapter = new ObjectDataTableAdapter([], []);
|
||||
adapter.setSorting(null);
|
||||
expect(adapter.getSorting()).toBe(null);
|
||||
|
||||
adapter.sort('id', null);
|
||||
expect(adapter.getSorting()).toEqual(
|
||||
jasmine.objectContaining({
|
||||
key: 'id',
|
||||
direction: 'asc'
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('should use direction for sort command', () => {
|
||||
let adapter = new ObjectDataTableAdapter([], []);
|
||||
adapter.setSorting(null);
|
||||
expect(adapter.getSorting()).toBe(null);
|
||||
|
||||
adapter.sort('id', 'desc');
|
||||
expect(adapter.getSorting()).toEqual(
|
||||
jasmine.objectContaining({
|
||||
key: 'id',
|
||||
direction: 'desc'
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('ObjectDataRow', () => {
|
||||
|
||||
it('should require object source', () => {
|
||||
expect(() => { return new ObjectDataRow(null); }).toThrowError('Object source not found');
|
||||
});
|
||||
|
||||
it('should get top level property value', () => {
|
||||
let row = new ObjectDataRow({
|
||||
id: 1
|
||||
});
|
||||
expect(row.getValue('id')).toBe(1);
|
||||
});
|
||||
|
||||
it('should not get top level property value', () => {
|
||||
let row = new ObjectDataRow({});
|
||||
expect(row.getValue('missing')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should get nested property value', () => {
|
||||
let row = new ObjectDataRow({
|
||||
name: {
|
||||
firstName: 'John',
|
||||
lastName: 'Doe'
|
||||
}
|
||||
});
|
||||
|
||||
expect(row.getValue('name.lastName')).toBe('Doe');
|
||||
});
|
||||
|
||||
it('should not get nested property value', () => {
|
||||
let row = new ObjectDataRow({});
|
||||
expect(row.getValue('some.missing.property')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should check top level value exists', () => {
|
||||
let row = new ObjectDataRow({ id: 1 });
|
||||
|
||||
expect(row.hasValue('id')).toBeTruthy();
|
||||
expect(row.hasValue('other')).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should check nested value exists', () => {
|
||||
let row = new ObjectDataRow({
|
||||
name: {
|
||||
firstName: 'John',
|
||||
lastName: 'Doe'
|
||||
}
|
||||
});
|
||||
|
||||
expect(row.hasValue('name')).toBeTruthy();
|
||||
expect(row.hasValue('name.firstName')).toBeTruthy();
|
||||
expect(row.hasValue('some.other.prop')).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should generateSchema generate a schema from data', () => {
|
||||
let data = [
|
||||
{ id: 2, name: 'abs' },
|
||||
{ id: 1, name: 'xyz' }
|
||||
];
|
||||
|
||||
let schema = ObjectDataTableAdapter.generateSchema(data);
|
||||
|
||||
expect(schema.length).toBe(2);
|
||||
expect(schema[0].title).toBe('id');
|
||||
expect(schema[1].title).toBe('name');
|
||||
});
|
||||
|
||||
});
|
222
lib/core/datatable/data/object-datatable-adapter.ts
Normal file
222
lib/core/datatable/data/object-datatable-adapter.ts
Normal file
@@ -0,0 +1,222 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { DatePipe } from '@angular/common';
|
||||
import { TemplateRef } from '@angular/core';
|
||||
|
||||
import { TimeAgoPipe } from '../../pipes';
|
||||
import { ObjectUtils } from '../../utils';
|
||||
import { DataColumn } from './data-column.model';
|
||||
import { DataRow } from './data-row.model';
|
||||
import { DataSorting } from './data-sorting.model';
|
||||
import { DataTableAdapter } from './datatable-adapter';
|
||||
|
||||
// Simple implementation of the DataRow interface.
|
||||
export class ObjectDataRow implements DataRow {
|
||||
|
||||
constructor(private obj: any, public isSelected: boolean = false) {
|
||||
if (!obj) {
|
||||
throw new Error('Object source not found');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
getValue(key: string): any {
|
||||
return ObjectUtils.getValue(this.obj, key);
|
||||
}
|
||||
|
||||
hasValue(key: string): boolean {
|
||||
return this.getValue(key) !== undefined;
|
||||
}
|
||||
}
|
||||
|
||||
// Simple implementation of the DataTableAdapter interface.
|
||||
export class ObjectDataTableAdapter implements DataTableAdapter {
|
||||
|
||||
private _sorting: DataSorting;
|
||||
private _rows: DataRow[];
|
||||
private _columns: DataColumn[];
|
||||
|
||||
selectedRow: DataRow;
|
||||
|
||||
static generateSchema(data: any[]) {
|
||||
let schema = [];
|
||||
|
||||
if (data && data.length) {
|
||||
let rowToExaminate = data[0];
|
||||
|
||||
if (typeof rowToExaminate === 'object') {
|
||||
for (let key in rowToExaminate) {
|
||||
if (rowToExaminate.hasOwnProperty(key)) {
|
||||
schema.push({
|
||||
type: 'text',
|
||||
key: key,
|
||||
title: key,
|
||||
sortable: false
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return schema;
|
||||
}
|
||||
|
||||
constructor(data: any[] = [], schema: DataColumn[] = []) {
|
||||
this._rows = [];
|
||||
this._columns = [];
|
||||
|
||||
if (data && data.length > 0) {
|
||||
this._rows = data.map(item => {
|
||||
return new ObjectDataRow(item);
|
||||
});
|
||||
}
|
||||
|
||||
if (schema && schema.length > 0) {
|
||||
this._columns = schema.map(item => {
|
||||
return new ObjectDataColumn(item);
|
||||
});
|
||||
|
||||
// Sort by first sortable or just first column
|
||||
let sortable = this._columns.filter(c => c.sortable);
|
||||
if (sortable.length > 0) {
|
||||
this.sort(sortable[0].key, 'asc');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getRows(): Array<DataRow> {
|
||||
return this._rows;
|
||||
}
|
||||
|
||||
setRows(rows: Array<DataRow>) {
|
||||
this._rows = rows || [];
|
||||
this.sort();
|
||||
}
|
||||
|
||||
getColumns(): Array<DataColumn> {
|
||||
return this._columns;
|
||||
}
|
||||
|
||||
setColumns(columns: Array<DataColumn>) {
|
||||
this._columns = columns || [];
|
||||
}
|
||||
|
||||
getValue(row: DataRow, col: DataColumn): any {
|
||||
if (!row) {
|
||||
throw new Error('Row not found');
|
||||
}
|
||||
if (!col) {
|
||||
throw new Error('Column not found');
|
||||
}
|
||||
|
||||
let value = row.getValue(col.key);
|
||||
|
||||
if (col.type === 'date') {
|
||||
try {
|
||||
return this.formatDate(col, value);
|
||||
} catch (err) {
|
||||
console.error(`Error parsing date ${value} to format ${col.format}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (col.type === 'icon') {
|
||||
const icon = row.getValue(col.key);
|
||||
return icon;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
formatDate(col: DataColumn, value: any): string {
|
||||
if (col.type === 'date') {
|
||||
const format = col.format || 'medium';
|
||||
if (format === 'timeAgo') {
|
||||
const timeAgoPipe = new TimeAgoPipe();
|
||||
return timeAgoPipe.transform(value);
|
||||
} else {
|
||||
const datePipe = new DatePipe('en-US');
|
||||
return datePipe.transform(value, format);
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
getSorting(): DataSorting {
|
||||
return this._sorting;
|
||||
}
|
||||
|
||||
setSorting(sorting: DataSorting): void {
|
||||
this._sorting = sorting;
|
||||
|
||||
if (sorting && sorting.key) {
|
||||
this._rows.sort((a: DataRow, b: DataRow) => {
|
||||
let left = a.getValue(sorting.key);
|
||||
if (left) {
|
||||
left = (left instanceof Date) ? left.valueOf().toString() : left.toString();
|
||||
} else {
|
||||
left = '';
|
||||
}
|
||||
|
||||
let right = b.getValue(sorting.key);
|
||||
if (right) {
|
||||
right = (right instanceof Date) ? right.valueOf().toString() : right.toString();
|
||||
} else {
|
||||
right = '';
|
||||
}
|
||||
|
||||
return sorting.direction === 'asc'
|
||||
? left.localeCompare(right)
|
||||
: right.localeCompare(left);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
sort(key?: string, direction?: string): void {
|
||||
let sorting = this._sorting || new DataSorting();
|
||||
if (key) {
|
||||
sorting.key = key;
|
||||
sorting.direction = direction || 'asc';
|
||||
}
|
||||
this.setSorting(sorting);
|
||||
}
|
||||
}
|
||||
|
||||
// Simple implementation of the DataColumn interface.
|
||||
export class ObjectDataColumn implements DataColumn {
|
||||
|
||||
key: string;
|
||||
type: string; // text|image
|
||||
format: string;
|
||||
sortable: boolean;
|
||||
title: string;
|
||||
srTitle: string;
|
||||
cssClass: string;
|
||||
template?: TemplateRef<any>;
|
||||
|
||||
constructor(obj: any) {
|
||||
this.key = obj.key;
|
||||
this.type = obj.type || 'text';
|
||||
this.format = obj.format;
|
||||
this.sortable = obj.sortable;
|
||||
this.title = obj.title;
|
||||
this.srTitle = obj.srTitle;
|
||||
this.cssClass = obj.cssClass;
|
||||
this.template = obj.template;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user