All files / src/app/components/pager pager.component.ts

100% Statements 28/28
100% Branches 17/17
100% Functions 5/5
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 871x 1x     1x                 1x         27x                                       27x 27x 27x 27x 27x             27x   25x 17x   25x 16x 16x 16x             9x 9x 9x 4x   9x 5x   9x                 27x 26x        
import { Component, NgZone, OnDestroy, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Subscription } from 'rxjs';
import { PagerModel } from '../../models';
import { AlertService, PagerService, PageService } from '../../services';
 
/**
 * Pager Component
 */
@Component({
    selector: 'app-pager',
    templateUrl: './pager.component.html'
})
export class PagerComponent implements OnDestroy, OnInit {
    /** subscription */
    subscription: Subscription | undefined;
 
    /** pager model */
    pagerModel: PagerModel = {
        currentPageNo: 1,
        maxPageNo: 0,
        pageSize: 5,
        pagePath: '/'
    };
 
    /** next page no */
    nextPageNo?: number;
    /** previous page no */
    previousPageNo?: number;
 
    /**
     * constructor of PagerComponent
     * @param pagerService: PagerService
     * @param pageService: PageService
     * @param router: Router
     * @param ngZone: NgZone
     * @param alert: AlertService
     */
    constructor(private readonly pagerService: PagerService,
                public pageService: PageService,
                private readonly router: Router,
                private readonly ngZone: NgZone,
                private readonly alert: AlertService) {
    }
 
    /**
     * ngOnInit
     */
    ngOnInit(): void {
        this.subscription = this.pagerService.getPagerModel()
            .subscribe(pagerModel => {
                if (!pagerModel.pagePath) {
                    pagerModel.pagePath = this.pageService.getRoutePath();
                }
                if (!pagerModel.currentPageNo || pagerModel.currentPageNo < 1 || pagerModel.currentPageNo > pagerModel.maxPageNo) {
                    pagerModel.currentPageNo = !pagerModel.currentPageNo ? 1 : pagerModel.currentPageNo < 1 ? 1 : pagerModel.maxPageNo;
                    this.ngZone.run(() => {
                        this.router.navigate([pagerModel.pagePath, pagerModel.currentPageNo])
                            .catch(// istanbul ignore next
                                reason => {
                                    this.alert.error(reason);
                                });
                    });
                } else {
                    this.previousPageNo = pagerModel.currentPageNo - 1;
                    this.nextPageNo = pagerModel.currentPageNo + 1;
                    if (pagerModel.currentPageNo === 1) {
                        this.previousPageNo = undefined;
                    }
                    if (pagerModel.currentPageNo >= pagerModel.maxPageNo) {
                        this.nextPageNo = undefined;
                    }
                    this.pagerModel = pagerModel;
                }
            });
    }
 
    /**
     * ngOnDestroy
     */
    ngOnDestroy(): void {
        if (this.subscription !== undefined) {
            this.subscription.unsubscribe();
        }
    }
}