All files / pagination si-pagination.component.ts

100% Statements 50/50
58.33% Branches 7/12
100% Functions 9/9
100% Lines 42/42

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128                            1x 1x           6x           6x         6x         6x                   6x                   6x                   6x   6x 5x     7x   6x 7x     6x 7x 7x 7x   7x 4x 16x   3x   1x 5x   1x 1x 2x   1x 1x 1x 5x       1x 1x 1x 3x   1x 1x     7x     6x     2x       2x 2x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { ChangeDetectionStrategy, Component, computed, input, model } from '@angular/core';
import { addIcons, elementLeft3, elementRight3, SiIconComponent } from '@siemens/element-ng/icon';
import { SiTranslatePipe, t } from '@siemens/element-translate-ng/translate';
 
@Component({
  selector: 'si-pagination',
  imports: [SiIconComponent, SiTranslatePipe],
  templateUrl: './si-pagination.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SiPaginationComponent {
  private static maxItems = 7;
 
  /**
   * The total number of pages.
   * Alternatively use `itemsPerPage` and `totalItems`
   */
  readonly totalPages = input<number>();
  /**
   * The current active page, counting starts from 1
   *
   * @defaultValue 1
   */
  readonly currentPage = model<number>(1);
  /**
   * The number of items per page.
   * Used to calculate the total number of page if `totalPages` is not available
   */
  readonly pageSize = input<number>();
  /**
   * The total number of items.
   * Used to calculate the total number of page if `totalPages` is not available
   */
  readonly totalRowCount = input<number>();
 
  /**
   * The text of the back button for pagination. Required for a11y.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_PAGINATION.BACK:Back`)
   * ```
   */
  readonly backButtonText = input(t(() => $localize`:@@SI_PAGINATION.BACK:Back`));
 
  /**
   * The text of the forward button for pagination. Required for a11y.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_PAGINATION.FORWARD:Forward`)
   * ```
   */
  readonly forwardButtonText = input(t(() => $localize`:@@SI_PAGINATION.FORWARD:Forward`));
  /**
   * When multiple paginations are used on the same page each pagination needs a distinct aria label.
   * Required for a11y.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_PAGINATION.NAV_LABEL:Pagination`)
   * ```
   */
  readonly navAriaLabel = input(t(() => $localize`:@@SI_PAGINATION.NAV_LABEL:Pagination`));
 
  private readonly calculatedTotalPages = computed(
    () => this.totalPages() ?? Math.ceil((this.totalRowCount() ?? 0) / (this.pageSize() ?? 1))
  );
 
  protected readonly prevDisabled = computed(() => this.currentPage() === 1);
 
  protected readonly nextDisabled = computed(
    () => this.currentPage() === this.calculatedTotalPages()
  );
 
  protected readonly pageButtons = computed(() => {
    const pageButtons: { page: number; sep: boolean }[] = [];
    const totalPages = this.calculatedTotalPages();
    const currentPage = this.currentPage();
 
    if (totalPages <= SiPaginationComponent.maxItems) {
      for (let i = 0; i < totalPages; i++) {
        pageButtons.push({ page: i + 1, sep: false });
      }
    } else if (currentPage < SiPaginationComponent.maxItems - 2) {
      // separator only on the right (in LTR)
      for (let i = 0; i < SiPaginationComponent.maxItems - 2; i++) {
        pageButtons.push({ page: i + 1, sep: false });
      }
      pageButtons.push({ page: 0, sep: true });
      pageButtons.push({ page: totalPages, sep: false });
    } else if (currentPage >= totalPages - 3) {
      // separator only on the left (in LTR)
      pageButtons.push({ page: 1, sep: false });
      pageButtons.push({ page: 0, sep: true });
      for (let i = totalPages - SiPaginationComponent.maxItems + 2; i < totalPages; i++) {
        pageButtons.push({ page: i + 1, sep: false });
      }
    } else {
      // separator on both sides
      pageButtons.push({ page: 1, sep: false });
      pageButtons.push({ page: 0, sep: true });
      for (let i = -1; i <= 1; i++) {
        pageButtons.push({ page: currentPage + i, sep: false });
      }
      pageButtons.push({ page: 0, sep: true });
      pageButtons.push({ page: totalPages, sep: false });
    }
 
    return pageButtons;
  });
 
  protected readonly icons = addIcons({ elementLeft3, elementRight3 });
 
  protected direction(event: Event, delta: number): void {
    this.setPage(event, this.currentPage() + delta);
  }
 
  protected setPage(event: Event, page: number): void {
    (event.currentTarget as HTMLElement).blur();
    this.currentPage.set(page);
  }
}