import { DataTable, Row } from "sigma-ng/datatable"; import { KeyValue } from "sigma-ts"; export class Column { public width: string; public title: string; public field: string; public classFunc: (row: any) => string; public valueFunc: (row: any) => string; public search: ColumnSearch = new ColumnSearch; public edit: ColumnEdit; public sortable: boolean = true; public visible: boolean = true; constructor(title: string, field: string, className?: string); constructor(title: string, func: ((row: any) => string), className?: string); public constructor(title: string, operation: string | ((row: any) => string), className?: string) { this.title = title; if (typeof operation == 'string') this.field = operation; else this.valueFunc = operation; } public getValue(row: any): string { try { if (this.valueFunc) return this.valueFunc(row); return row[this.field]; } catch(ex) { return 'exception'; } } public setSearchable(searchable: boolean): Column { this.search.enable = searchable; return this; } public setsortable(sortable: boolean): Column { this.sortable = sortable; return this; } public setVisibe(visible: boolean): Column { this.visible = visible; return this; } public setSearchType(type: ColumnSearchType): Column { this.search.type = type; return this; } public setClassFunc(func: (row: any) => string): Column { this.classFunc = func; return this; } public setWidth(width: string): Column { this.width = width; return this; } public setEdit(edit: ColumnEdit): Column { this.edit = edit; return this; } getHeaderClass(datatable: DataTable, index: number): string[] { let classes: string[] = []; if (this.sortable) classes.push('sortable'); if (!this.visible) classes.push('hide'); if (datatable.sort == index) { if (datatable.ascending) classes.push('asc'); else classes.push('desc'); } return classes; } getSearchClass(): string[] { let classes: string[] = []; if (!this.visible) classes.push('hide'); return classes; } getRowClass(row: any) { let classes: string[] = []; if (!this.visible) classes.push('hide'); if (this.classFunc) classes.push(this.classFunc(row)); return classes; } getSearchItems(rows:Row[]) : string[] { let array: string[] = []; for(let i = 0, length = rows.length;i < length;i++) { let value = this.getValue(rows[i].value); if(array.indexOf(value) < 0) array.push(value); } return array; } } export enum ColumnSearchType { Text = 0, Select = 1 } export class ColumnSearch { public value: string; public type: ColumnSearchType = ColumnSearchType.Text; public enable: boolean = true; } export enum ColumnEditType { Text = 0, Select = 1, Custom = 10 } export class ColumnEdit { protected _type: ColumnEditType = ColumnEditType.Text; public name: string; public enable: boolean = true; public onchange: (event:any) => void = ev => {}; public onkeydown: (event:any) => void = ev => {}; get type(): number { return +this._type; } constructor(name: string) { this.name = name; } } export class ColumnEditSelect extends ColumnEdit { public data: KeyValue[]; public compareWith: (val1:KeyValue,val2:KeyValue) => boolean = (val1,val2) => { return (!val1 && !val2) || (val1 && val2 && val1.key == val2.key); }; constructor(name: string,data: KeyValue[]) { super(name); this.name = name; this.data = data; this._type = ColumnEditType.Select; } }