Advertisement
deni-parvanov

newpagdir

May 7th, 2020
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {
  2.     AfterViewInit,
  3.     Directive,
  4.     Host,
  5.     Optional,
  6.     Renderer2,
  7.     Self,
  8.     ViewContainerRef,
  9.     Input
  10. } from "@angular/core";
  11.  
  12. import { MatPaginator, PageEvent } from '@angular/material/paginator';
  13. import { MatButton } from '@angular/material/button';
  14.  
  15. @Directive({
  16.     selector: "[appCustomPaginator]"
  17. })
  18. export class CustomPaginatorDirective implements AfterViewInit {
  19.    
  20.     private currentPage: number;
  21.    
  22.     private totalPages: number;
  23.    
  24.     private pageButtons: MatButton[];
  25.  
  26.     private enableSkipForwardButton: boolean;
  27.  
  28.     private enableSkipBackwardButton: boolean;
  29.  
  30.     @Input() totalViewablePages: number;
  31.  
  32.     constructor(
  33.         @Host() @Self() @Optional() private readonly matPaginator: MatPaginator,
  34.         private viewRef: ViewContainerRef,
  35.         private renderer: Renderer2) {
  36.             this.currentPage = 0;
  37.             this.pageButtons = [];
  38.  
  39.             this.enableSkipForwardButton = false;
  40.             this.enableSkipBackwardButton = false;
  41.  
  42.             this.matPaginator.page.subscribe((v: { pageIndex: number; }) => this.changePage(v.pageIndex));
  43.     }
  44.    
  45.     public ngAfterViewInit() {
  46.         let interval = setInterval(() => {
  47.             if (this.matPaginator.getNumberOfPages() !== 0) {
  48.                 this.totalPages = this.matPaginator.getNumberOfPages();
  49.                 this.renderPageButtons();
  50.  
  51.                 clearInterval(interval);
  52.             }
  53.         }, 250);
  54.     }
  55.  
  56.     private changePage(page: number): void {
  57.         this.currentPage = page;
  58.         this.matPaginator.pageIndex = page;
  59.  
  60.         this.renderPageButtons();
  61.     }
  62.  
  63.     private renderPageButtons() {
  64.         this.applyPagingDirectionChange();
  65.  
  66.         const matPaginatorActionsElem = this.viewRef.element.nativeElement
  67.                                             .querySelector("div.mat-paginator-range-actions");
  68.                                    
  69.         const matPaginatorNextPageElem = this.viewRef.element.nativeElement
  70.                                             .querySelector("button.mat-paginator-navigation-next");
  71.            
  72.         this.pageButtons.forEach(button => { this.renderer.removeChild(matPaginatorActionsElem, button) });
  73.              
  74.         if (this.enableSkipBackwardButton) {
  75.             this.renderer.insertBefore(
  76.                 matPaginatorActionsElem, this.createPageButton(0, this.matPaginator.pageIndex), matPaginatorNextPageElem);
  77.                
  78.             if (this.currentPage - this.totalViewablePages === 0) {
  79.                 this.renderer.insertBefore(
  80.                     matPaginatorActionsElem, this.createPageButton(1, this.matPaginator.pageIndex), matPaginatorNextPageElem);
  81.             } else {
  82.                 this.renderer.insertBefore(
  83.                     matPaginatorActionsElem, this.createSkipButton(false), matPaginatorNextPageElem);
  84.             }
  85.  
  86.         }
  87.  
  88.         let start = Math.min(this.currentPage, this.totalPages - this.totalViewablePages - 1);
  89.         start = Math.max(0, start);
  90.         let end   = Math.min(this.totalPages, this.currentPage + this.totalViewablePages);
  91.  
  92.         for (let page = start; page < end; page++) {
  93.             this.renderer.insertBefore(
  94.                 matPaginatorActionsElem, this.createPageButton(page, this.matPaginator.pageIndex), matPaginatorNextPageElem);
  95.         }
  96.  
  97.         if (this.enableSkipForwardButton) {
  98.             if (this.currentPage + this.totalViewablePages < this.totalPages - 1) {
  99.                 this.renderer.insertBefore(
  100.                     matPaginatorActionsElem, this.createSkipButton(true), matPaginatorNextPageElem);
  101.             }
  102.  
  103.             this.renderer.insertBefore(
  104.                 matPaginatorActionsElem, this.createPageButton(this.totalPages - 1, this.matPaginator.pageIndex), matPaginatorNextPageElem);
  105.         }
  106.     }
  107.    
  108.     private createPageButton(pageBtnContent: number, currentPageIndex: number): any {
  109.         const pageBtn = this.createButton(pageBtnContent + 1 + "");
  110.        
  111.         if (pageBtnContent === currentPageIndex) {
  112.             this.renderer.setAttribute(pageBtn, "disabled", "disabled");
  113.         } else {
  114.             this.renderer.listen(pageBtn, "click", () => {
  115.                 this.matPaginator.page.emit({
  116.                     pageIndex: pageBtnContent,
  117.                     pageSize: this.matPaginator.pageSize,
  118.                     length: this.matPaginator.getNumberOfPages()
  119.                 });
  120.             });
  121.         }
  122.        
  123.         return pageBtn;
  124.     }
  125.  
  126.     private createSkipButton(incrementOnClick: boolean): any {
  127.         const pageBtn = this.createButton("...");
  128.  
  129.         let nextPageIndex: number;
  130.         if (incrementOnClick) {
  131.             nextPageIndex = this.currentPage + this.totalViewablePages;
  132.         } else {
  133.             nextPageIndex = this.currentPage - this.totalViewablePages;
  134.         }
  135.  
  136.         this.renderer.listen(pageBtn, "click", () => {
  137.             this.matPaginator.page.emit({
  138.                 pageIndex: nextPageIndex,
  139.                 pageSize: this.matPaginator.pageSize,
  140.                 length: this.matPaginator.getNumberOfPages()
  141.             });
  142.         });
  143.  
  144.         return pageBtn;
  145.     }
  146.  
  147.     private createButton(btnContent: string): any {
  148.         const pageBtn = this.renderer.createElement("mat-button");
  149.        
  150.         this.renderer.addClass(pageBtn, "mat-mini-fab");
  151.         this.renderer.addClass(pageBtn, "mat-custom-page");
  152.        
  153.         this.renderer.appendChild(pageBtn, this.renderer.createText(btnContent));
  154.        
  155.         this.pageButtons.push(pageBtn);
  156.  
  157.         return pageBtn;
  158.     }
  159.  
  160.     private applyPagingDirectionChange(): void {
  161.         if (this.totalPages - this.totalViewablePages > this.totalViewablePages) {
  162.             this.enableSkipForwardButton  = (this.totalPages - this.currentPage > this.totalViewablePages);
  163.        
  164.             this.enableSkipBackwardButton = !(this.currentPage < this.totalViewablePages);
  165.         }
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement