2021-01-05 07:32:52 +00:00
|
|
|
import { DataSource } from '@angular/cdk/collections';
|
2021-01-11 06:05:59 +00:00
|
|
|
import { EventEmitter } from '@angular/core';
|
|
|
|
import { MatPaginator, PageEvent } from '@angular/material/paginator';
|
|
|
|
import { merge, Observable, of as observableOf } from 'rxjs';
|
|
|
|
import { map, tap } from 'rxjs/operators';
|
2021-01-05 07:32:52 +00:00
|
|
|
|
|
|
|
import { Case } from '../../core/case';
|
|
|
|
|
|
|
|
export class CaseListDataSource extends DataSource<Case> {
|
2021-01-11 05:04:47 +00:00
|
|
|
private dataValues: Case[] = [];
|
2021-01-21 02:31:59 +00:00
|
|
|
private searchValues = '';
|
|
|
|
private data: Observable<Case[]>;
|
|
|
|
private search: Observable<string>;
|
|
|
|
constructor(
|
|
|
|
public d: Observable<Case[]>,
|
|
|
|
public s: Observable<string>,
|
|
|
|
private paginator?: MatPaginator,
|
|
|
|
) {
|
2021-01-05 07:32:52 +00:00
|
|
|
super();
|
2021-01-11 06:05:59 +00:00
|
|
|
this.data = d.pipe(tap((x) => (this.dataValues = x)));
|
2021-01-21 02:31:59 +00:00
|
|
|
this.search = s.pipe(tap((x) => (this.searchValues = x)));
|
2021-01-05 07:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
connect(): Observable<Case[]> {
|
2021-01-21 02:31:59 +00:00
|
|
|
const dataMutations: (Observable<Case[]> | Observable<string> | EventEmitter<PageEvent>)[] = [
|
|
|
|
this.data,
|
|
|
|
this.search,
|
|
|
|
];
|
2021-01-11 05:04:47 +00:00
|
|
|
if (this.paginator) {
|
|
|
|
dataMutations.push((this.paginator as MatPaginator).page);
|
|
|
|
}
|
|
|
|
|
|
|
|
return merge(...dataMutations)
|
|
|
|
.pipe(
|
2021-01-21 02:31:59 +00:00
|
|
|
map(() => this.getFilteredData([...this.dataValues])),
|
2021-01-11 05:04:47 +00:00
|
|
|
tap((x: Case[]) => {
|
|
|
|
if (this.paginator) {
|
|
|
|
this.paginator.length = x.length;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)
|
2021-01-21 02:31:59 +00:00
|
|
|
.pipe(map((x: Case[]) => this.getPagedData(x)));
|
2021-01-05 07:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {}
|
2021-01-11 05:04:47 +00:00
|
|
|
|
2021-01-21 02:31:59 +00:00
|
|
|
private getFilteredData(data: Case[]): Case[] {
|
|
|
|
return this.searchValues.split(' ').reduce(
|
|
|
|
(p: Case[], c: string) =>
|
|
|
|
p.filter((x) => {
|
2022-04-17 08:14:56 +00:00
|
|
|
const accountString =
|
|
|
|
`${x.caseSource.prefix}-${x.officeFileNumber} ${x.courtCaseNumber} ${x.year} ${x.title}
|
2021-01-21 02:31:59 +00:00
|
|
|
${x.docketNumber} ${x.remarks}`.toLowerCase();
|
|
|
|
return accountString.indexOf(c) !== -1;
|
|
|
|
}),
|
|
|
|
Object.assign([], data),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-11 05:04:47 +00:00
|
|
|
private getPagedData(data: Case[]) {
|
|
|
|
if (this.paginator === undefined) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
|
|
|
|
return data.slice(startIndex, startIndex + this.paginator.pageSize);
|
|
|
|
}
|
2021-01-05 07:32:52 +00:00
|
|
|
}
|