brewman/overlord/src/app/user/user-list/user-list-datasource.ts
tanshu 6be1dd5a3a Moved to Angular 6.0
----

Pending
* Table width for the points column in incentive
* Linting
* keyboard navigation where it was used earlier
* can remove the unused totals calculated serverside in productledger
* spinner and loading bars
* Activate Guard for Employee Function tabs
* Progress for Fingerprint uploads
* deleted reconcile and receipe features as they were not being used
* focus the right control on component load
2018-06-09 17:05:11 +05:30

59 lines
1.5 KiB
TypeScript

import {DataSource} from '@angular/cdk/collections';
import {MatPaginator, MatSort} from '@angular/material';
import {map} from 'rxjs/operators';
import {merge, Observable, of as observableOf} from 'rxjs';
import {User} from '../user';
export class UserListDataSource extends DataSource<User> {
constructor(private paginator: MatPaginator, private sort: MatSort, public data: User[]) {
super();
}
connect(): Observable<User[]> {
const dataMutations = [
observableOf(this.data),
this.paginator.page,
this.sort.sortChange
];
// Set the paginators length
this.paginator.length = this.data.length;
return merge(...dataMutations).pipe(map(() => {
return this.getPagedData(this.getSortedData([...this.data]));
}));
}
disconnect() {
}
private getPagedData(data: User[]) {
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
return data.splice(startIndex, this.paginator.pageSize);
}
private getSortedData(data: User[]) {
if (!this.sort.active || this.sort.direction === '') {
return data;
}
return data.sort((a, b) => {
const isAsc = this.sort.direction === 'asc';
switch (this.sort.active) {
case 'name':
return compare(a.name, b.name, isAsc);
case 'id':
return compare(+a.id, +b.id, isAsc);
default:
return 0;
}
});
}
}
/** Simple sort comparator for example ID/Name columns (for user-side sorting). */
function compare(a, b, isAsc) {
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}