import { Defaults, Column, Options, Menu, Button, Row, Cell } from './index'; import { Service } from 'sigma-ng/datatable/models/service'; import { Filter } from 'sigma-ng/datatable/models/filter'; import { isNumber, isArray } from "util"; export class DataTable { defaults: Defaults = new Defaults private _page: number = this.defaults.page; private _sort: number = this.defaults.sort; private _ascending: boolean = this.defaults.ascending; private _limit: number = this.defaults.limit; private _message: string; private _rows: Row[] = []; private _rowsFilter: Row[] = []; private _rowsResult: Row[] = []; private _checkedAll : boolean; public name: string; public service: Service; public columns: Column[] = []; public options: Options = new Options; public limits: number[] = this.defaults.limits; public menus: Menu[] = []; public buttons: Button[] = []; public progress: boolean = false; constructor() { this.name = (location.pathname + location.hash).replace(/\//g, '.'); } get message(): string { return this._message; } get page(): number { const count = this.pageCount; if(count && this._page > count) this._page = count; return this._page; } get limit(): number { if(this.limits.indexOf(this._limit) < 0) this._limit = this.limits[0]; return this._limit; } get sort(): number { return this._sort; } get ascending(): boolean { return this._ascending; } get pageCount(): number { return (this.rowsFilterCount ? Math.ceil(this.rowsFilterCount / this.limit) : 0); } get rowIndexFrom(): number { return (this.page - 1) * this.limit + 1; } get rowIndexTo(): number { let to = this.page * this.limit, count = this.rowsFilterCount; return (count < to ? count : to); } get rowsResultCount(): number { return this._rowsResult.length; } get rowsFilterCount(): number { return this._rowsFilter.length; } get rowsTotalCount(): number { return (this.service ? this.service.count : this.rows.length); } get rows(): Row[] { return this._rows; } get rowsFilter(): Row[] { return this._rowsFilter; } get rowsSelected(): Row[] { let array: any[] = []; let rows = this.rowsFilter; for (let i = 0,length = rows.length; i < length; i++) if (rows[i].selected) array.push(rows[i]); return array; } get rowsResult(): Row[] { let rows = this.rows; rows = Filter.search(rows, this.columns); rows = Filter.sort(rows, this.sort, this.ascending); this._rowsFilter = rows; if (this.options.paging) rows = Filter.paging(rows, this.rowIndexFrom, this.limit); this._checkedAll = true; for(let i = 0,length = rows.length;i < length;i++) if(!rows[i].selected) { this._checkedAll = false; break; } return this._rowsResult = rows; } get hasFilter(): boolean { for(let i = 0,length = this.columns.length;i < length;i++) if(this.columns[i].search.value) return true; return false; } get hasSavedOptions(): boolean { return (localStorage.getItem(this.name) != null); } set checkedAll(checked: boolean) { this._checkedAll = checked; for(let i = 0,length = this.rowsResult.length;i < length;i++) this.rowsResult[i].selected = checked; } set rows(rows: Row[]) { this._rows = rows; } set limit(limit: number) { if (this.limits.indexOf(limit) > -1) this._limit = limit; } set sortType(ascending: boolean) { this._ascending = ascending; } set sort(sort: number) { if (sort >= 0 && sort < this.columns.length) { if (this.sort == sort) this._ascending = !this._ascending; else { this._sort = sort; this._ascending = true; } } } set page(page: number) { if (page >= 1 && page <= this.pageCount) this._page = page; } set dataSource(data: any[]) { this.rows = []; for (let i = 0; i < data.length; i++) { let row = new Row; row.value = data[i]; for (let j = 0; j < this.columns.length; j++) { let cell = new Cell; cell.row = row; cell.column = this.columns[j]; row.cells.push(cell); } this.rows.push(row); } } set message(value: string) { this._message = value; setTimeout(() => this._message = '',this.options.messageTime); } firstPage() { this.page = 1; } prevPage() { if (this.page > 1) this.page--; } nextPage() { if (this.page < this.pageCount) this.page++; } lastPage() { this.page = this.pageCount; } saveOptions() { let columns: any[] = []; for (let i = 0,length = this.columns.length; i < length; i++) { columns.push({ visible: this.columns[i].visible, search: this.columns[i].search.value }); } let obj = { limit: this.limit, sort: this.sort, ascending: this.ascending, columns: columns }; localStorage.setItem(this.name, JSON.stringify(obj)); } loadOptions() { let obj = JSON.parse(localStorage.getItem(this.name)); if (obj) { if (this.limits.indexOf(+obj.limit) > -1) this.limit = +obj.limit; this.sort = obj.sort; this._ascending = obj.ascending; if (isArray(obj.columns)) { for (let i = 0,length = this.columns.length; i < length; i++) { let column = obj.columns[i]; if(column) { this.columns[i].visible = (column.visible ? true : false); if(column.search) this.columns[i].search.value = column.search; } } } } } deleteOptions() { localStorage.removeItem(this.name); this._limit = this.defaults.limit; this._sort = this.defaults.sort; this._ascending = this.defaults.ascending; this.clearFilters(); } clearFilters() { for(let i = 0,length = this.columns.length;i < length;i++) { this.columns[i].search.value = undefined; } } replaceRow(func:(row: Row) => boolean,value: any) { for(let i = 0,length = this._rows.length;i < length;i++) if(func(this._rows[i])) this._rows[i].value = value; } removeRow(func:(row: Row) => boolean) { for(let i = 0,length = this._rows.length;i < length;i++) if(func(this._rows[i])) this._rows.splice(i,1); } }