/** Angular Imports */ import { CollectionViewer , DataSource } from '@angular/cdk/collections' ; /** rxjs Imports */ import { Observable , BehaviorSubject } from 'rxjs' ; /** Custom Services */ import { ClientsService } from './clients.service' ; /**
 * Clients custom data source to implement server side filtering, pagination and sorting.
 */ export class ClientsDataSource implements DataSource < any > { /** clients behavior subject to represent loaded journal clients page. */ private clientsSubject = new BehaviorSubject < any [ ] > ( [ ] ) ; /** Records subject to represent total number of filtered clients records. */ private recordsSubject = new BehaviorSubject < number > ( 0 ) ; /** Records observable which can be subscribed to get the value of total number of filtered clients records. */ public records$ = this . recordsSubject . asObservable ( ) ; param { ClientsService } clientsService constructor ( private clientsService ) { } param { any } filterBy param { string } orderBy param { string } sortOrder param { number } pageIndex param { number } limit getClients ( orderBy = '' , sortOrder = '' , pageIndex = 0 , limit = 10 , clientActive = true ) { this . clientsSubject . next ( [ ] ) ; this . clientsService . getClients ( orderBy , sortOrder , pageIndex * limit , limit ) . subscribe ( ( clients ) => { clients . pageItems = ( clientActive ) ? ( clients . pageItems . filter ( ( client ) => client . active ) ) : ( clients . pageItems . filter ( ( client ) => ! client . active ) ) ; this . recordsSubject . next ( clients . totalFilteredRecords ) ; this . clientsSubject . next ( clients . pageItems ) ; } ) ; } param { CollectionViewer } collectionViewer connect ( collectionViewer ) { return this . clientsSubject . asObservable ( ) ; } param { CollectionViewer } collectionViewer disconnect ( collectionViewer ) { this . clientsSubject . complete ( ) ; this . recordsSubject . complete ( ) ; } param { string } filterValue param { string } orderBy param { string } sortOrder param { number } pageIndex param { number } limit filterClients ( filter , orderBy = '' , sortOrder = '' , pageIndex = 0 , limit = 10 , clientActive = true ) { this . clientsSubject . next ( [ ] ) ; this . clientsService . getClients ( orderBy , sortOrder , pageIndex * limit , limit ) . subscribe ( ( clients ) => { clients . pageItems = clients . pageItems . filter ( ( client ) => client . active === clientActive && client . displayName . toLowerCase ( ) . includes ( filter ) ) ; this . recordsSubject . next ( clients . totalFilteredRecords ) ; this . clientsSubject . next ( clients . pageItems ) ; } ) ; } }