All files / lib/pager pager.component.ts

90.73% Statements 333/367
92.15% Branches 47/51
89.47% Functions 17/19
90.73% Lines 333/367

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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 3681x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 1x 1x 1x 1x 1x 1x                         1x 1x 1x 1x 1x 9x 9x 9x 9x 1x 1x 1x 1x 1x 94x 94x 1x 1x 1x 1x 1x 74x 74x 17x 5x 5x 5x 5x 5x 5x 5x 5x 5x 74x 74x 74x 1x 1x 1x 1x 1x 928x 928x 1x 1x 1x 1x 1x 928x 928x 1x 1x 1x 1x 1x 3x 3x 3x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 5x 20x 20x 20x 1x 1x 1x 1x 1x 171x 171x 171x 171x 171x 171x 171x 171x 171x 6x 169x 169x 169x 21x 101x 101x 101x 101x 101x 101x 21x 165x 165x 150x 150x 15x 15x 15x 15x 2x 2x 2x 2x 13x 13x 13x 13x                                   13x 13x 13x 13x 13x 6x 6x 6x 6x 6x 6x 6x 6x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 171x 171x 171x 1x 1x 1x 1x 1x 405x 405x 405x 405x 405x 405x 405x 405x 405x 135x 135x 135x 135x 135x 135x 135x 405x 405x 1x 1x 1x 1x 1x 270x 270x 270x 270x 270x 270x 270x 1x 1x 1x 1x 1x 3969x 3969x 1x 1x 1x 1x 1x 613x 187x 426x 426x 613x 613x 1x 1x 1x 1x 1x 20x   20x 20x   20x 20x 20x 1x 1x 1x 1x 1x 171x 171x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterViewInit,
  ChangeDetectorRef,
  Component,
  ElementRef,
  EventEmitter,
  HostBinding,
  HostListener,
  Input,
  NgZone,
  OnChanges,
  OnDestroy,
  Output,
  QueryList,
  ViewChildren,
} from '@angular/core';
import { Subscription } from 'rxjs';
 
import { observeSize } from '../utils/internal-utils';
import { Callback, Item, WidgetKeySet } from '../utils/util.types';
import { createWidgetKeySet } from '../utils/utilities';
 
import { ComponentMapperDirective } from '../internal/component-mapper.directive';
 
interface PageItem extends Item {
  pageIndex: number;
}
 
/**
 * A paging component.
 */
@Component({
  selector: 'gc-pager',
  templateUrl: './pager.component.html',
  styleUrls: [
    './../utils/styles/reset-button.css',
    './../utils/styles/reset-list.css',
    './pager.component.css',
  ],
  standalone: false,
})
export class PagerComponent implements OnDestroy, OnChanges, AfterViewInit {
  /**
   * Number of pages to show.
   */
  @Input()
  public count = 1;
 
  /**
   * Notifies about a page change.
   */
  @Output()
  public readonly currentPageChange: EventEmitter<number> =
    new EventEmitter<number>();
 
  /**
   * Currently selected page.
   */
  @Input()
  public currentPage = 0;
 
  /** @ignore */
  @HostBinding('class.gc-vertical')
  private vertical = false;
 
  /** @ignore */
  @ViewChildren('pageButton', { read: ComponentMapperDirective })
  private pageButtons?: QueryList<ComponentMapperDirective<HTMLButtonElement>>;
 
  /** @ignore */
  protected _indicies: (number | PageItem[])[] = [];
 
  /** @ignore */
  protected readonly _widgetKeys: WidgetKeySet = createWidgetKeySet('gc-pager');
 
  /** @ignore */
  private moveFocusAfterRerender?: number;
 
  /** @ignore */
  private pageButtonsSubscription?: Subscription;
 
  /** @ignore */
  private resizeCallback: Callback;
 
  constructor(
    private elRef: ElementRef<HTMLElement>,
    zone: NgZone,
    private cdr: ChangeDetectorRef,
  ) {
    this.resizeCallback = observeSize(
      elRef,
      zone,
      this.refreshIndicies.bind(this),
    );
  }
 
  /**
   * @ignore
   */
  @HostListener('keydown', ['$event'])
  onKeyDown(event: KeyboardEvent): void {
    const key = event.key;

    if (this.pageButtons) {
      if (key === 'Home') {
        this.pageButtons.first.component.focus();
        event.preventDefault();
      } else if (key === 'End') {
        this.pageButtons.last.component.focus();
        event.preventDefault();
      }
    }
  }
 
  /**
   * @ignore
   */
  ngOnDestroy(): void {
    this.pageButtonsSubscription?.unsubscribe();
 
    this.resizeCallback();
  }
 
  /**
   * @ignore
   */
  ngOnChanges(): void {
    this.refreshIndicies();
  }
 
  /**
   * @ignore
   */
  ngAfterViewInit(): void {
    if (this.pageButtons) {
      this.pageButtonsSubscription = this.pageButtons.changes.subscribe(() => {
        if (this.moveFocusAfterRerender) {
          const focusPage = this.moveFocusAfterRerender;
          const focusButton = this.pageButtons?.find(
            i => i.key === focusPage.toString(),
          );
          if (focusButton) {
            focusButton.component.focus();
            this.moveFocusAfterRerender = undefined;
          }
        }
      });
    }
  }
 
  /**
   * @ignore
   */
  isPrevEnabled(): boolean {
    return this.currentPage != 0;
  }
 
  /**
   * @ignore
   */
  isNextEnabled(): boolean {
    return this.currentPage != this.count - 1;
  }
 
  /**
   * @ignore
   */
  next(): void {
    this.select(this.currentPage + 1, false);
    this.refreshIndicies();
  }
 
  /**
   * @ignore
   */
  prev(): void {
    this.select(this.currentPage - 1, false);
    this.refreshIndicies();
  }
 
  /**
   * @ignore
   */
  select(num: number, setFocusAfterRerender: boolean): void {
    if (num !== this.currentPage) {
      this.currentPage = this.clamp(num);
      this.currentPageChange.emit(this.currentPage);
 
      if (setFocusAfterRerender) {
        this.moveFocusAfterRerender = num;
      }
    }
  }
 
  /**
   * @ignore
   */
  computeIndices(): (number | PageItem[])[] {
    const availableWidth = this.elRef.nativeElement.clientWidth - 2 * 40;
    const preferredWidth = this.count * 40;
    const widthFor7 = 7 * 40;
    const widthFor5 = 5 * 40;
    const widthFor3 = 3 * 40;
 
    // if less then 4 we always just render all indices
    if (this.count < 4) {
      this.vertical = availableWidth < preferredWidth;
      return Array.from({ length: this.count }, (_, i) => i);
    }
 
    const createMenuItems = (firstPage: number, lastPage: number): PageItem[] =>
      Array.from({ length: lastPage - firstPage + 1 }, (_, i) => {
        const page = firstPage + i;
        return {
          label: (page + 1).toString(),
          key: page.toString(),
          pageIndex: page,
        };
      });
 
    if (availableWidth >= preferredWidth && this.count <= 7) {
      this.vertical = false;
      return Array.from({ length: this.count }, (_, i) => i);
    }
 
    if (
      availableWidth <= widthFor5 &&
      (this.currentPage === 0 || this.currentPage === this.count - 1)
    ) {
      this.vertical = availableWidth < widthFor3;
      return [0, createMenuItems(1, this.count - 2), this.count - 1];
    }
 
    this.vertical = availableWidth < widthFor5;
    if (availableWidth <= widthFor7) {
      if (this.currentPage <= 1) {
        return [0, 1, 2, createMenuItems(3, this.count - 2), this.count - 1];
      } else if (this.currentPage >= this.count - 2) {
        return [
          0,
          createMenuItems(1, this.count - 4),
          this.count - 3,
          this.count - 2,
          this.count - 1,
        ];
      } else {
        return [
          0,
          createMenuItems(1, this.currentPage - 1),
          this.currentPage,
          createMenuItems(this.currentPage + 1, this.count - 2),
          this.count - 1,
        ];
      }
    }
 
    if (this.currentPage <= 3) {
      return [
        0,
        1,
        2,
        3,
        4,
        createMenuItems(5, this.count - 2),
        this.count - 1,
      ];
    } else if (this.currentPage >= this.count - 4) {
      return [
        0,
        createMenuItems(1, this.count - 6),
        this.count - 5,
        this.count - 4,
        this.count - 3,
        this.count - 2,
        this.count - 1,
      ];
    } else {
      return [
        0,
        createMenuItems(1, this.currentPage - 2),
        this.currentPage - 1,
        this.currentPage,
        this.currentPage + 1,
        createMenuItems(this.currentPage + 2, this.count - 2),
        this.count - 1,
      ];
    }
  }
 
  /**
   * @ignore
   */
  protected pageMenuLabel(
    menu: PageItem[],
    variant: 'label-only' | 'pageinfo-only' | 'label-and-pageinfo',
  ): string {
    const label = 'Ausgeblendete Seiten anzeigen';
    const first = menu[0].label;
    const last = menu[menu.length - 1].label;
 
    switch (variant) {
      case 'label-only':
        return label;
 
      case 'pageinfo-only':
        return '(' + first + ' bis ' + last + ')';
 
      case 'label-and-pageinfo':
        return '**' + label + '** (' + first + '-' + last + ')';
    }
  }
 
  /**
   * @ignore
   */
  protected getPageInfoId(menu: PageItem[]): string {
    return (
      this._widgetKeys.widgetKey +
      menu[0].label +
      '-' +
      menu[menu.length - 1].label
    );
  }
 
  /**
   * @ignore
   */
  protected isPageMenuArray(item: number | PageItem[]): item is PageItem[] {
    return Array.isArray(item);
  }
 
  /**
   * @ignore
   */
  protected trackByItem(index: number, item: number | PageItem[]) {
    if (Array.isArray(item)) {
      return item.map(a => a.key).toString();
    } else {
      return item;
    }
  }
 
  /**
   * @ignore
   */
  private clamp(value: number): number {
    if (value >= this.count) {
      value = this.count - 1;
    }
    if (value < 0) {
      value = 0;
    }
    return value;
  }
 
  /**
   * @ignore
   */
  private refreshIndicies(): void {
    this._indicies = this.computeIndices();
  }
}