'use strict' ; import { Request } from 'express' ; import { CriteriaFilter } from '@Entities/EntityFilters/CriteriaFilter' ; import { Clamp } from '@Tools/Misc' ; import { Logger } from '@Tools/Logging' ; export class PaginationInfo extends CriteriaFilter { public PageNum : number = 1 ; public PerPage : number = 20 ; public TotalPages : number = 1 ; public TotalEntries : number = 20 ; private _currentPage : number = 1 ; private _currentItem : number = 1 ; private _doingQuery : boolean = false ; public constructor ( pPageNum = 1 , pPerPage = 1000 ) { super ( ) ; this . PageNum = Clamp ( pPageNum , 1 , 1000 ) ; this . PerPage = Clamp ( pPerPage , 1 , 1000 ) ; this . TotalPages = this . PageNum ; this . TotalEntries = 0 ; } public parametersFromRequest ( pRequest ) { if ( pRequest . query . page ) { this . PageNum = Clamp ( Number ( pRequest . query . page ) , 1 , 1000 ) ; } ; if ( pRequest . query . per_page ) { this . PerPage = Clamp ( Number ( pRequest . query . per_page ) , 1 , 1000 ) ; } ; this . TotalPages = this . PageNum ; this . TotalEntries = 0 ; } public addResponseFields ( pRequest ) { if ( pRequest . vRestResp ) { pRequest . vRestResp . addAdditionalField ( 'current_page' , this . PageNum ) ; pRequest . vRestResp . addAdditionalField ( 'per_page' , this . PerPage ) ; pRequest . vRestResp . addAdditionalField ( 'total_pages' , this . TotalPages ) ; pRequest . vRestResp . addAdditionalField ( 'total_entries' , this . TotalEntries ) ; } ; } ; public criteriaTest ( pThingy ) { if ( ! this . _doingQuery ) { this . TotalEntries ++ ; if ( this . _currentItem > this . PerPage ) { this . _currentItem = 1 ; ++ this . _currentPage ; } ; this . _currentItem ++ ; return this . PageNum === this . _currentPage ; } ; return true ; } ; public async criteriaTestAsync ( pThingy ) { return this . criteriaTest ( pThingy ) ; } ; public criteriaParameters ( ) { return { } ; } ; public sortCriteriaParameters ( ) { return null ; } ; } ;