/** Angular Imports */ import { CollectionViewer , DataSource } from '@angular/cdk/collections' ; /** rxjs Imports */ import { Observable , BehaviorSubject } from 'rxjs' ; /** Custom Services */ import { GroupsService } from './groups.service' ; /**
 * Groups custom data source to implement server side filtering, pagination and sorting.
 */ export class GroupsDataSource implements DataSource < any > { /** groups behavior subject to represent loaded groups page. */ private groupsSubject = new BehaviorSubject < any [ ] > ( [ ] ) ; /** Records subject to represent total number of filtered groups records. */ private recordsSubject = new BehaviorSubject < number > ( 0 ) ; /** Records observable which can be subscribed to get the value of total number of filtered groups records. */ public records$ = this . recordsSubject . asObservable ( ) ; param { GroupsService } groupsService constructor ( private groupsService ) { } param { any } filterBy param { string } orderBy param { string } sortOrder param { number } pageIndex param { number } limit param { boolean } groupActive getGroups ( filterBy , orderBy = '' , sortOrder = '' , pageIndex = 0 , limit = 10 , groupActive = true ) { this . groupsSubject . next ( [ ] ) ; this . groupsService . getGroups ( filterBy , orderBy , sortOrder , pageIndex * limit , limit ) . subscribe ( ( groups ) => { groups . pageItems = ( groupActive ) ? ( groups . pageItems . filter ( ( group ) => group . active ) ) : groups . pageItems ; this . recordsSubject . next ( groups . totalFilteredRecords ) ; this . groupsSubject . next ( groups . pageItems ) ; } ) ; } param { CollectionViewer } collectionViewer connect ( collectionViewer ) { return this . groupsSubject . asObservable ( ) ; } param { CollectionViewer } collectionViewer disconnect ( collectionViewer ) { this . groupsSubject . complete ( ) ; this . recordsSubject . complete ( ) ; } }