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