2020-10-01 15:21:22 +00:00
|
|
|
import { DataSource } from '@angular/cdk/collections';
|
2020-11-23 04:55:53 +00:00
|
|
|
import { EventEmitter } from '@angular/core';
|
|
|
|
import { MatPaginator, PageEvent } from '@angular/material/paginator';
|
|
|
|
import { MatSort, Sort } from '@angular/material/sort';
|
2020-10-01 15:21:22 +00:00
|
|
|
import { merge, Observable, of as observableOf } from 'rxjs';
|
2020-10-10 03:15:05 +00:00
|
|
|
import { map } from 'rxjs/operators';
|
|
|
|
|
|
|
|
import { LedgerItem } from './ledger-item';
|
|
|
|
|
|
|
|
/** Simple sort comparator for example ID/Name columns (for client-side sorting). */
|
2020-11-23 04:55:53 +00:00
|
|
|
function compare(a: string | number, b: string | number, isAsc: boolean) {
|
2020-10-10 03:15:05 +00:00
|
|
|
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
|
|
|
|
}
|
2018-05-25 13:49:00 +00:00
|
|
|
export class LedgerDataSource extends DataSource<LedgerItem> {
|
2020-11-23 04:55:53 +00:00
|
|
|
constructor(public data: LedgerItem[], private paginator?: MatPaginator, private sort?: MatSort) {
|
2018-05-25 13:49:00 +00:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(): Observable<LedgerItem[]> {
|
2020-11-23 04:55:53 +00:00
|
|
|
const dataMutations: (
|
|
|
|
| Observable<LedgerItem[]>
|
|
|
|
| EventEmitter<PageEvent>
|
|
|
|
| EventEmitter<Sort>
|
|
|
|
)[] = [observableOf(this.data)];
|
|
|
|
if (this.paginator) dataMutations.push((this.paginator as MatPaginator).page);
|
|
|
|
if (this.sort) dataMutations.push((this.sort as MatSort).sortChange);
|
2018-05-25 13:49:00 +00:00
|
|
|
|
|
|
|
// Set the paginators length
|
2020-11-23 04:55:53 +00:00
|
|
|
if (this.paginator) this.paginator.length = this.data.length;
|
2018-05-25 13:49:00 +00:00
|
|
|
|
2020-10-01 15:21:22 +00:00
|
|
|
return merge(...dataMutations).pipe(
|
2020-10-10 03:15:05 +00:00
|
|
|
map(() => this.getPagedData(this.getSortedData([...this.data]))),
|
2020-10-01 15:21:22 +00:00
|
|
|
);
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 15:21:22 +00:00
|
|
|
disconnect() {}
|
2018-05-25 13:49:00 +00:00
|
|
|
|
|
|
|
private getPagedData(data: LedgerItem[]) {
|
2020-11-23 04:55:53 +00:00
|
|
|
if (this.paginator === undefined) return data;
|
2018-05-25 13:49:00 +00:00
|
|
|
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
|
|
|
|
return data.splice(startIndex, this.paginator.pageSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
private getSortedData(data: LedgerItem[]) {
|
2020-11-23 04:55:53 +00:00
|
|
|
if (this.sort === undefined) return data;
|
2018-05-25 13:49:00 +00:00
|
|
|
if (!this.sort.active || this.sort.direction === '') {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-11-23 04:55:53 +00:00
|
|
|
const sort = this.sort as MatSort;
|
2018-05-25 13:49:00 +00:00
|
|
|
return data.sort((a, b) => {
|
2020-11-23 04:55:53 +00:00
|
|
|
const isAsc = sort.direction === 'asc';
|
|
|
|
switch (sort.active) {
|
2018-05-25 13:49:00 +00:00
|
|
|
case 'date':
|
|
|
|
return compare(a.date, b.date, isAsc);
|
|
|
|
case 'debit':
|
|
|
|
return compare(+a.debit, +b.debit, isAsc);
|
|
|
|
case 'credit':
|
|
|
|
return compare(+a.credit, +b.credit, isAsc);
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|