All files / lib/table table.component.ts

93.46% Statements 1030/1102
91.66% Branches 187/204
91.17% Functions 62/68
93.46% Lines 1030/1102

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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 11031x 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 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 392x 392x 1x 1x           1x 1x 2214x 2214x 2214x 2214x 2214x 2214x 2214x 2214x 2214x 2214x 2214x 2214x 2214x 2214x 2214x 2214x 2214x 2214x 26116x 26116x 2908x 23208x 23208x 26116x 2214x 2214x 26116x 26116x 2214x 2214x 6565x 6565x 6565x 6565x 6565x 6565x 6565x 6565x 6565x 6565x 2214x 2214x 2214x 1x 1x 1x 784x 784x 784x 784x 784x 784x 784x 784x 784x 784x 784x 10420x 10420x 741x 9679x 9679x 10420x 784x 784x 9693x 9693x 784x 784x 5235x 5235x 5235x 5235x 5235x 5235x 5235x 5235x 784x 784x 784x 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 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 56x 56x 1x 1x 1x 1x 1x 1x 1x 1x 20492x 20492x 20492x 1x 1x 116x 116x 116x 1x 1x 1x 1506x 1506x 1506x 11x 11x 1506x 1506x 1506x 1506x 1506x 1506x 1506x 1506x 1x 1x 1x 1x 1x 1x 42876x 42876x 42876x 1x 1x 137x 137x 137x 6x 16x 6x 137x 137x 137x 1x 1x 1x 2988x 8x 2980x 2980x 1466x 1514x   1514x 1514x 2988x 2988x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 112x 112x 112x 1x 1x 3x 3x 3x 3x 3x 3x 3x       3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 26116x 26116x 26116x 26116x 26116x 26116x 1x 1x 1x 10420x 10420x 10420x 10420x 10420x 5828x 10420x 10420x 1x 1x 1x 42x 42x 42x 42x 42x 42x 42x 1x 1x 1x 1x 30735x 30735x 30735x 30735x 30735x 28390x 30735x 30735x 30735x 1x 1x 1x 10031x 10031x 1x 1x 1x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 106x 106x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 124x 112x 112x 112x 112x 112x 112x 1x 1x 1x 1506x 12x 1494x 112x 112x 112x 112x 1506x 1506x 1506x 1x 1x 1x                                 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 2x 3x 3x 3x 3x 3x         3x 3x 3x 1x 1x 1x 26116x 540x 25576x 25576x 26116x 1x 1x 1x 37x 37x 1x 1x 1x 39444x 39444x 1x 1x 1506x 1506x   1506x 1506x 1506x 1506x 1506x 1506x 10528x 10528x 10528x 10528x 2259x 8269x 2259x 6010x 3765x 2245x 2245x 1506x 1506x 1506x 1506x 1x 1x 432x 48x 48x 384x 384x 384x 432x 1x 1x 1x 106x 106x 106x 16x 16x 16x 16x 60x 12x 12x 12x 12x 70x 32x 32x 32x 32x 72x 12x 12x 12x 12x 74x 1x 1x 1x 1x 1x 1x 1x 1x 106x 106x 106x 1x 1x 6024x   6024x 6024x 6024x 1x 1x 120x 120x 8x 120x 120x 120x 116x 177x 177x 116x 116x 116x 116x 116x 116x 116x 116x 4x 120x 120x 1x 1x 297x 293x 293x 293x 4x 4x 4x 297x 297x 1x 1x 3x 3x 3x 3x         3x 3x 3x   3x 3x 3x       3x 3x 3x 3x 3x 1x 1x 1x 2x 2x 1x 1x 1x 141x 141x 1x 1x 1x 253x 253x 253x 1x 1x 1x 32x 32x 32x 32x 32x 32x   32x 32x 32x 28x 32x 32x 32x 1x 1x 1x 12x 12x 12x 12x 8x 12x 12x 12x 1x 1x 1x 16x 16x 16x 10x 16x 16x 16x 1x 1x 1x 12x 12x 12x 12x 12x 12x   10x 10x 10x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 68x 68x 68x 124x 2x 2x 2x   2x 2x     2x 2x 2x 124x 124x 124x 124x 1x 1x 1x 124x 124x 807x 68x 68x 499x 124x 124x 124x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 124x 1x 1x 1x 1x 1x 1x 1x 1x 1x 60x 60x 60x 60x 60x 30x 30x 30x 30x 30x 30x 8x 8x 8x 8x 8x 8x 8x 8x 30x 30x 22x 22x 16x 6x 3x 3x 3x 22x 22x 22x 22x 22x   22x 22x 22x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 16x 14x 14x 30x 30x 30x 30x 30x 30x 30x 30x 60x 1x 1x 1x 1x 1x 1x 1x 32x 32x 32x 32x 32x 32x 32x 32x 32x 12x 12x 12x 12x 26x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 26x 26x 32x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 10x 12x 12x 12x 12x 12x                         12x 12x 12x 12x 1x 1x 1x 124x 2515x 66x 124x 124x 124x 124x 124x 1x 1x 1x 253x 253x 253x 253x 253x 253x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterViewChecked,
  AfterViewInit,
  Component,
  ContentChildren,
  Directive,
  ElementRef,
  EventEmitter,
  HostBinding,
  Input,
  NgZone,
  OnDestroy,
  Output,
  QueryList,
  TemplateRef,
  ViewChild,
  ViewChildren,
  inject,
} from '@angular/core';
 
import {
  getKeyboardFocusableElements,
  observeSize,
  relativeTo,
  subscribeOutsideZone,
  subscribeWindow,
  waitForRender,
} from '../utils/internal-utils';
import {
  Callback,
  FocusableElementOwner,
  Item,
  ItemGroup,
} from '../utils/util.types';
import { isItem } from '../utils/utilities';
 
import { TextHighlightService } from '../internal/text-highlight/text-highlight.component';
 
import {
  TableCellContext,
  TableColumn,
  TableHeaderCellContext,
} from './table.common';
 
export * from './table.common';
 
@Directive({
  selector: 'ng-template[td-template-for]', // eslint-disable-line @angular-eslint/directive-selector -- old occurence, change requires API breakage
})
export class TableTdTemplateDirective<T> {
  @Input('td-template-for')
  public templateFor!: string;
  constructor(public readonly template: TemplateRef<TableCellContext<T>>) {}
 
  static ngTemplateContextGuard<T>(
    dir: TableTdTemplateDirective<T>,
    ctx: unknown,
  ): ctx is TableCellContext<T> {
    return true;
  }
}
 
interface TmpCell<T> {
  column: TableColumn<T>;
  item?: T;
  label: string;
  rowIndex: number;
  colIndex: number;
}
 
@Directive({
  selector: 'ng-template[th-template-for]', // eslint-disable-line @angular-eslint/directive-selector -- old occurence, change requires API breakage
})
export class TableThTemplateDirective<T> {
  @Input('th-template-for')
  public templateFor!: string;
  constructor(
    public readonly template: TemplateRef<TableHeaderCellContext<T>>,
  ) {}
 
  static ngTemplateContextGuard<T>(
    dir: TableThTemplateDirective<T>,
    ctx: unknown,
  ): ctx is TableHeaderCellContext<T> {
    return true;
  }
}
 
@Directive({
  selector: '[data-gc-int-td]',
  exportAs: 'tdCtx',
})
export class TableCellDirective<T> implements TableCellContext<T> {
  @Input('col')
  public col!: TableColumn<T>;
 
  @Input('row')
  public row!: T;
 
  @Input('colIdx')
  public colIdx!: number;
 
  @Input('rowIdx')
  public rowIdx!: number;
 
  @HostBinding('style.text-align')
  get alignment(): string {
    if (this.col.alignment == 'right-decimal') {
      return 'right';
    }
    return this.col.alignment;
  }
 
  public get $implicit(): TableCellContext<T> {
    return this;
  }
 
  public get cell(): TmpCell<T> {
    const label = this.col.label ? this.col.label(this.row) : 'null';
    return {
      column: this.col,
      item: this.row,
      rowIndex: this.rowIdx,
      colIndex: this.colIdx,
 
      label,
    };
  }
 
  public readonly el: ElementRef<HTMLElement> = inject<ElementRef<HTMLElement>>(
    ElementRef<HTMLElement>,
  );
}
 
@Directive({
  selector: 'th[data-gc-table-int-th]',
  exportAs: 'thCtx',
})
export class TableHeaderDirective<T> implements TableHeaderCellContext<T> {
  @Input('col')
  public col!: TableColumn<T>;
  @Input('colIdx')
  public colIdx!: number;
 
  @HostBinding('style.text-align')
  get alignment(): string {
    if (this.col.alignment === 'right-decimal') {
      return 'right';
    }
    return this.col.alignment;
  }
 
  public get $implicit(): TableHeaderCellContext<T> {
    return this;
  }
 
  public get cell(): TmpCell<T> {
    return {
      column: this.col,
      item: undefined,
      rowIndex: 0,
      colIndex: this.colIdx,
      label: this.col.title,
    };
  }
 
  public readonly el: ElementRef<HTMLElement> = inject<ElementRef<HTMLElement>>(
    ElementRef<HTMLElement>,
  );
}
 
/** A group of table items. */
export interface TableItemGroup<T> extends ItemGroup<T> {
  /**
   * Override the total number of entries within the group, if not set the current number of items is used.
   * Count may be different from the number of current items in case of pagination.
   */
  entryCount?: {
    /** Total number of items in this group. */
    totalEntryCount: number;
    /** Index of the first item in the current list within the complete group. */
    offset: number;
  };
}
 
interface TableState {
  loading?: false | 'default';
  search?: boolean;
}
 
/**
 * A Table component.
 */
@Component({
  selector: 'gc-table',
  templateUrl: './table.component.html',
  styleUrls: ['./table.component.css'],
  providers: [TextHighlightService],
})
export class TableComponent<T>
  implements FocusableElementOwner, AfterViewInit, AfterViewChecked, OnDestroy
{
  /** Title of the table. Visibility depends on the `showLabel` input. */
  @Input()
  public label = '';
 
  /**
   * If `true`, the label will be visible as heading above the table. Otherwise, the information will
   * be visually hidden and only available for accessibility utilities.
   */
  @Input()
  public showLabel = false;
 
  /**
   * Items displayed in the settings menu (menu-button is not visible, when `settingsMenuItems` is undefined or empty)
   */
  @Input()
  public settingsMenuItems?: readonly Item[];
 
  /** The columns of the table */
  @Input()
  public columns: readonly TableColumn<T>[] = [];
 
  /** Pager template slot*/
  @Input()
  public pagerTemplate: TemplateRef<Record<string, never>> | null = null;
 
  /**
   * show the entry count
   * @deprecated - property no longer needs to be an input and will be removed in future as such. (will remain a static property)
   */
  @Input()
  public showEntryCount = true;
 
  /**
   * Override the total number of entries, if not set the current number of items is used.
   * Count may be different from the number of current items in case of pagination.
   */
  @Input()
  public totalEntryCount?: number;
 
  /** If set to `true` the header columns are sticky and will remain visible when the table content scrolls. */
  @Input()
  @HostBinding('class.gc-with-sticky-header')
  public stickyHeaderColumns = false;
 
  /**
   * Set a fixed height on the content area of the table, specified as a number of rows to always reserve space for.
   * Only used when data is present, does not affect the height of empty tables or related states. When the space needed
   * to display the provided data exceeds the fixed height, the table content may scroll.
   * @see stickyHeaderColumns
   */
  @Input()
  public fixedContentHeight?: number;
 
  /** Marks the table as required. The table should contain selectable option when this is used. */
  @Input()
  @HostBinding('class.gc-state-required')
  public required = false;
 
  /** Hint message that appears within the table caption. */
  @Input()
  public hint = '';
 
  /** Error message which is part of the table caption. */
  @Input()
  public errorMessage = '';
 
  /** Used to display a confirmation-banner on top of the table. */
  @Input()
  public confirmationBannerTemplate: TemplateRef<Record<string, never>> | null =
    null;
 
  /**
   * Action that is fired when clicked on item in table-settings-menu
   */
  @Output()
  public readonly onSettingsMenuAction: EventEmitter<Item> =
    new EventEmitter<Item>();
 
  /** @ignore */
  @ViewChildren(TableHeaderDirective)
  public thElements?: QueryList<TableHeaderDirective<T>>;
 
  /** @ignore */
  @ViewChildren(TableCellDirective)
  public tdElements?: QueryList<TableCellDirective<T>>;
 
  /** @ignore */
  @ContentChildren(TableThTemplateDirective)
  public thTemplates!: QueryList<TableThTemplateDirective<T>>;
 
  /** @ignore */
  @ContentChildren(TableTdTemplateDirective)
  public tdTemplates!: QueryList<TableTdTemplateDirective<T>>;
 
  /** @ignore */
  @ViewChild('thDefaultTemplate')
  public thDefaultTemplate!: TemplateRef<TableHeaderCellContext<T>>;
 
  /** @ignore */
  @ViewChild('tdDefaultTemplate')
  public tdDefaultTemplate!: TemplateRef<TableCellContext<T>>;
 
  /** @ignore */
  @ViewChild('table')
  public table!: ElementRef<HTMLTableElement>;
 
  /** @ignore */
  @ViewChild('header')
  public header?: ElementRef<HTMLElement>;
 
  /** The text which should be highlighted through the table. */
  @Input()
  public get highlightString(): string | undefined {
    return this.textHighlightService.highlightString;
  }
 
  public set highlightString(val: string | undefined) {
    this.textHighlightService.highlightString = val;
  }
 
  /**
   * Changes the state of the table.
   * Possible optional states are:
   * - loading: indicates that the table is in a loading state.
   * - search: indicates that the table is in an ongoing search context/state.
   */
  @Input()
  public get state(): TableState {
    return this._state;
  }
 
  public set state(val: TableState) {
    this._state = val;
    this.recomputeShowEntryCount();
  }
 
  /** @ignore */
  @HostBinding('style.--gc-table-fixed-content-height.px')
  public get _fixedContentHeight(): number | undefined {
    if (
      this.fixedContentHeight === undefined ||
      this.isEmpty() ||
      this.state.loading
    ) {
      return undefined;
    } else {
      const headerHeight = 42;
      const rowHeight = 73;
      return headerHeight + rowHeight * this.fixedContentHeight;
    }
  }
 
  /**
   * The items of the table. If group items are supplied, items of each group are displayed in separate blocks
   * within the table.
   */
  @Input()
  public get items(): readonly T[] | readonly TableItemGroup<T>[] {
    return this._items;
  }
 
  public set items(value: readonly T[] | readonly TableItemGroup<T>[]) {
    this._items = value;
 
    if (this._isTableItemGroup(this._items)) {
      this.allRowsFlattened = this._items.reduce<T[]>((acc, curr) => {
        return [...acc, ...curr.items];
      }, []);
    }
    this.recomputeShowEntryCount();
  }
 
  /** @ignore */
  public get _totalEntryCount(): number {
    if (this.state.loading) {
      return 0;
    }
    if (this.totalEntryCount !== undefined) {
      return this.totalEntryCount;
    } else if (this._isTableItemGroup(this.items)) {
      return this.items.reduce((sum, group) => sum + group.items.length, 0);
    } else {
      return this.items.length;
    }
  }
 
  /**
   * Set to true if the data is currently loading.
   * @deprecated - use new *state* input instead.
   */
  @Input()
  public get isLoading(): boolean {
    return this._isLoading;
  }
 
  public set isLoading(value: boolean) {
    this._isLoading = value;
    this.state = { ...this.state, loading: value ? 'default' : false };
  }
 
  /** @ignore */
  smallActions = false;
 
  /** @ignore */
  _stickyRowHeaders = true;
 
  /** @ignore */
  protected _showEntryCount = true;
 
  /** @ignore */
  private _isLoading = false;
 
  /** @ignore */
  private resizeObserver?: Callback;
 
  /** @ignore */
  private screenResizeObserver?: Callback;
 
  /** @ignore */
  private resizeObserverHeaderCleanup?: Callback;
 
  /** @ignore */
  private headerHeightObserver?: Callback;
 
  /**
   * Helper object for tracking the last focused cell and it's contained elements information.
   * @prop { index } - index of the last focused element within the cell
   * @prop { cell }  - coordinates of the last focused cell
   * @ignore
   * */
  private lastFocusedCell?: {
    index: number;
    cell: { colIdx: number; rowIdx: number };
  };
 
  /** @ignore */
  private _items: readonly T[] | readonly TableItemGroup<T>[] = [];
 
  /** @ignore */
  private allRowsFlattened: readonly T[] = [];
 
  /** @ignore */
  private eventCallbacks: Callback[] = [];
 
  /** @ignore */
  private _state: Readonly<TableState> = {
    loading: false,
    search: false,
  };
 
  constructor(
    private textHighlightService: TextHighlightService,
    private zone: NgZone,
  ) {}
 
  focusChild(): boolean {
    const firstFocusableChildren = getKeyboardFocusableElements(
      this.table.nativeElement,
    );
 
    const children =
      firstFocusableChildren.length > 0
        ? firstFocusableChildren
        : Array.from(
            this.table.nativeElement.querySelectorAll<HTMLElement>(
              '[tabindex="-1"]:not([disabled]):not([aria-hidden])',
            ),
          );
    if (children.length > 0) {
      children[0].focus();
      return true;
    } else {
      return false;
    }
  }
 
  /** @ignore */
  public findTdTemplate(columnId: string): TemplateRef<TableCellContext<T>> {
    let templ = this.tdDefaultTemplate;
    templ =
      this.tdTemplates.find(template => template.templateFor === columnId)
        ?.template ?? templ;
    return templ;
  }
 
  /** @ignore */
  public findThTemplate(
    columnId: string,
  ): TemplateRef<TableHeaderCellContext<T>> {
    let templ = this.thDefaultTemplate;
    templ =
      this.thTemplates.find(template => template.templateFor === columnId)
        ?.template ?? templ;
    return templ;
  }
 
  /** @ignore */
  public findCell(
    colIndex: number,
    rowIndex: number,
  ): TableCellDirective<T> | undefined {
    return this.tdElements?.find(
      tdEl => tdEl.cell.colIndex == colIndex && tdEl.cell.rowIndex == rowIndex,
    );
  }
 
  // type guard written as class member to allow using it within the template
  /** @ignore */
  public _isTableItemGroup(
    items: readonly T[] | readonly TableItemGroup<T>[],
  ): items is readonly TableItemGroup<T>[] {
    if (items.length === 0) {
      return false;
    } else {
      const item = items[0];
      return isItem(item) && Array.isArray(item.items);
    }
  }
 
  /** @ignore */
  isEmpty(): boolean {
    return this.items.length === 0;
  }
 
  /** @ignore */
  ngAfterViewInit(): void {
    this.resizeObserver = observeSize(
      this.table,
      this.zone,
      this.resizeObserverCallback.bind(this),
    );
    this.screenResizeObserver = subscribeWindow(
      'resize',
      this.windowResizeObserverCallback.bind(this),
    );
    this.thElements?.changes.subscribe(this.updateTh.bind(this));
 
    this.eventCallbacks.push(
      subscribeOutsideZone(
        this.zone,
        this.table.nativeElement,
        'keydown',
        e => {
          if (e.target instanceof HTMLElement) {
            this.onKeyDown(e);
          }
        },
      ),
    );
    this.eventCallbacks.push(
      subscribeOutsideZone(
        this.zone,
        this.table.nativeElement,
        'focusin',
        e => {
          void this.onFocusableNodeFocus(e);
        },
      ),
    );
 
    this.updateTh();
  }
 
  /** @ignore */
  ngAfterViewChecked(): void {
    if (this.resizeObserverHeaderCleanup && !this.header) {
      this.resizeObserverHeaderCleanup();
    } else if (this.header && !this.resizeObserverHeaderCleanup) {
      this.resizeObserverHeaderCleanup = observeSize(
        this.header,
        this.zone,
        () => this.checkScrolling(),
      );
    }
  }
 
  /** @ignore */
  ngOnDestroy(): void {
    if (this.resizeObserver) {
      this.resizeObserver();
    }
    if (this.resizeObserverHeaderCleanup) {
      this.resizeObserverHeaderCleanup();
    }
    if (this.screenResizeObserver) {
      this.screenResizeObserver();
    }
    if (this.headerHeightObserver) {
      this.headerHeightObserver();
    }

    // clear focus listeners
    this.eventCallbacks.forEach(fc => fc());
  }
 
  /**
   * Focus a cell in an item
   *
   * @param item the item representing the row
   * @param column optional column or index of the column to move the focus to
   * @param defer defer the focus to the next render cycle
   * @returns `true` if the focus could be transfer otherwise `false` - if you passed `defer=true` it is always true
   */
  public focusItem(
    item: T,
    column?: TableColumn<T> | number,
    defer?: boolean,
  ): boolean {
    const block = () => {
      const target = this.computeFocusTarget(item, column);
      if (target) {
        return this.focusCell(target.rowIdx, target.colIdx, 'first');
      }
 
      return false;
    };
    if (defer) {
      this.zone.runOutsideAngular(() => {
        setTimeout(block);
      });
      return true;
    }
    return block();
  }
 
  /** @ignore */
  protected getGlobalRowIndex(item: T, index: number) {
    if (this._isTableItemGroup(this.items)) {
      return this.allRowsFlattened.indexOf(item);
    }
    return index;
  }
 
  /** @ignore */
  protected trackByGroup(index: number, group: TableItemGroup<T>) {
    return group.key;
  }
 
  /** @ignore */
  protected trackByIndex(index: number) {
    return index;
  }
 
  protected computeGridStyles() {
    const fillColumn =
      this.columns.find(c => c.fillWidth && c.fillWidth > 0) === undefined
        ? this.columns.find(c => c.rowHeader) ?? this.columns[0]
        : undefined;
 
    return (
      'grid-template-columns: ' +
      this.columns
        .map(c => {
          const fillWidth = fillColumn === c ? 1 : c.fillWidth;
          const minWidth = c.minWidth;
 
          if (minWidth && fillWidth) {
            return `minmax(${this.minWidthValue(minWidth)}, ${fillWidth.toFixed(0)}fr)`;
          } else if (fillWidth) {
            return `${fillWidth.toFixed(0)}fr`;
          } else if (minWidth) {
            return `minmax(${this.minWidthValue(minWidth)}, auto)`;
          }
          return 'auto';
        })
        .join(' ')
    );
  }
 
  protected computeGroupStyles(column: TableColumn<T>, colIdx: number) {
    if (column.rowHeader) {
      // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- number is known to be an integer
      return `grid-column: 1/${this.columns.length + 1};`;
    }
    // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- number is known to be an integer
    return `grid-column: ${colIdx + 1}; border-bottom: unset;`;
  }
 
  /** @ignore */
  private onKeyDown(event: KeyboardEvent): void {
    const key = event.key;
    switch (key) {
      case 'ArrowUp':
        this.handleArrowUp();
        event.preventDefault();
        event.stopPropagation();
        break;
      case 'ArrowDown':
        this.handleArrowDown();
        event.preventDefault();
        event.stopPropagation();
        break;
      case 'ArrowRight':
        this.handleArrowRight();
        event.preventDefault();
        event.stopPropagation();
        break;
      case 'ArrowLeft':
        this.handleArrowLeft();
        event.preventDefault();
        event.stopPropagation();
        break;
      case 'Home':
        this.handleHomeKey();
        event.preventDefault();
        event.stopPropagation();
        break;
      case 'End':
        this.handleEndKey();
        event.preventDefault();
        event.stopPropagation();
        break;
    }
  }
 
  private minWidthValue(value: number) {
    if (value <= 0) {
      return 'min-content';
    }
    return `${value.toFixed(0)}px`;
  }
 
  private updateTh() {
    const first = this.thElements?.first;
    if (this.headerHeightObserver) {
      this.headerHeightObserver();
    }
 
    if (first) {
      const r = new ResizeObserver(() => {
        this.updateHeaderStyle(
          Math.max(first.el.nativeElement.getBoundingClientRect().height, 41),
        );
      });
      r.observe(first.el.nativeElement);
      this.headerHeightObserver = () => r.disconnect();
      this.updateHeaderStyle(
        Math.max(first.el.nativeElement.getBoundingClientRect().height, 41),
      );
    } else {
      this.updateHeaderStyle(undefined);
    }
  }
 
  private updateHeaderStyle(height: number | undefined) {
    if (height) {
      this.table.nativeElement.style.setProperty(
        '--gc-table-header-height',
        `${height.toFixed(0)}px`,
      );
    } else {
      this.table.nativeElement.style.removeProperty('--gc-table-header-height');
    }
  }
 
  private computeFocusTarget(item: T, column?: TableColumn<T> | number) {
    if (column !== undefined) {
      if (typeof column === 'number') {
        return this.tdElements?.find(
          cell => cell.row === item && cell.colIdx === column,
        );
      } else {
        return this.tdElements?.find(
          cell => cell.row === item && cell.col.id === column.id,
        );
      }
    } else {
      const rowHeaderCell = this.tdElements?.find(
        cell => cell.row === item && cell.col.rowHeader === true,
      );
      if (rowHeaderCell) {
        return rowHeaderCell;
      } else {
        return this.tdElements?.find(
          cell => cell.row === item && cell.colIdx === 0,
        );
      }
    }
  }
 
  /** @ignore */
  private windowResizeObserverCallback(): void {
    this._stickyRowHeaders = window.innerHeight >= 400;
  }
 
  /** @ignore */
  private resizeObserverCallback(): void {
    this.checkScrolling();
  }
 
  /** @ignore */
  private checkScrolling() {
    const table = this.table.nativeElement.clientWidth;
    this.smallActions = table < 650;
  }
 
  /** @ignore */
  private handleArrowRight(): void {
    if (this.lastFocusedCell) {
      const nextCol = this.lastFocusedCell.cell.colIdx + 1;
      const totalNumberOfCols =
        this.tdElements?.reduce(
          (acc, curr) => (curr.colIdx > acc ? curr.colIdx : acc),
          0,
        ) ?? 0;
 
      const nextElementWithinFocused = this.focusNextElementInCell();
      if (nextCol <= totalNumberOfCols && !nextElementWithinFocused) {
        this.focusCell(this.lastFocusedCell.cell.rowIdx, nextCol, 'first');
      }
    }
  }
 
  /** @ignore */
  private handleArrowLeft(): void {
    if (this.lastFocusedCell) {
      const previousElementWithinFocused = this.focusPreviousElementInCell();
      const nextCol = this.lastFocusedCell.cell.colIdx - 1;
      if (nextCol >= 0 && !previousElementWithinFocused) {
        this.focusCell(this.lastFocusedCell.cell.rowIdx, nextCol, 'last');
      }
    }
  }
 
  /** @ignore */
  private handleArrowUp(): void {
    if (this.lastFocusedCell) {
      const nextRow = this.lastFocusedCell.cell.rowIdx - 1;
      if (nextRow >= -1) {
        this.focusCell(nextRow, this.lastFocusedCell.cell.colIdx, 'index');
      }
    }
  }
 
  /** @ignore */
  private handleArrowDown(): void {
    if (this.lastFocusedCell) {
      const previousRow = this.lastFocusedCell.cell.rowIdx + 1;
      const totalNumberOfRows =
        this.tdElements?.reduce(
          (acc, curr) => (curr.rowIdx > acc ? curr.rowIdx : acc),
          0,
        ) ?? 0;
 
      if (previousRow <= totalNumberOfRows) {
        this.focusCell(previousRow, this.lastFocusedCell.cell.colIdx, 'index');
      }
    }
  }
 
  /** @ignore */
  private handleEndKey(): void {
    const lastCell = this.tdElements?.last;
 
    if (lastCell) {
      this.focusCell(lastCell.rowIdx, lastCell.colIdx, 'last');
    }
  }
 
  /** @ignore */
  private handleHomeKey(): void {
    const firstCell = this.tdElements?.first;
 
    if (firstCell) {
      this.focusCell(0, 0, 'first');
    }
  }
 
  /** @ignore */
  private async onFocusableNodeFocus(event: FocusEvent): Promise<void> {
    const findTD = (el: HTMLElement): HTMLElement | null => {
      let current = el as HTMLElement | null;
      while (current) {
        if (current.tagName == 'TD') {
          break;
        }
        current = current.parentElement;
      }
      return current;
    };
 
    this.updateLastFocusedCell();
 
    const target = event.target as HTMLElement;
    const tr = findTD(target);
 
    if (tr) {
      // Firefox does not scroll the focus element intoView if more than
      // 50% is visible already
      tr.scrollIntoView({ block: 'nearest', inline: 'nearest' });
      if (this.stickyHeaderColumns) {
        await waitForRender();
        const relative = relativeTo(tr, this.table.nativeElement);
        const stickyHeaderHeight = this._isTableItemGroup(this.items)
          ? 42 * 2
          : 42;
        if (relative < stickyHeaderHeight) {
          this.table.nativeElement.scrollBy({
            top: relative - stickyHeaderHeight,
          });
        }
      }
    }
 
    this.scrollToActiveCell();
  }
 
  /** @ignore */
  private updateLastFocusedCell() {
    const focusedCell =
      this.thElements?.find(i =>
        i.el.nativeElement.contains(document.activeElement),
      ) ??
      this.tdElements?.find(i =>
        i.el.nativeElement.contains(document.activeElement),
      );
 
    if (focusedCell) {
      const cell: { rowIdx: number; colIdx: number } = {
        colIdx: focusedCell.colIdx,
        rowIdx:
          focusedCell instanceof TableCellDirective ? focusedCell.rowIdx : -1,
      };
 
      const focusableChildren = getKeyboardFocusableElements(
        focusedCell.el.nativeElement,
      );
 
      const index = focusableChildren.indexOf(
        document.activeElement as HTMLElement,
      );
      this.lastFocusedCell = { index, cell };
    }
  }
 
  /**
   * Focuses the cell within the table that corresponds to the passed row and column parameters.
   * @param rowIdx index of the row to be focused
   * @param colIdx index of the column to be focused
   * @returns true if the cell exists otherwise returns false
   * @ignore
   */
  private focusCell(
    rowIdx: number,
    colIdx: number,
    restoreIndexInCellStrategy: 'index' | 'first' | 'last',
  ): boolean {
    if (rowIdx >= 0) {
      const cell = this.findCell(colIdx, rowIdx);
      if (cell) {
        const focusableElements = getKeyboardFocusableElements(
          cell.el.nativeElement,
        );
        if (focusableElements.length === 0) {
          const programmticallyFocusable = Array.from(
            cell.el.nativeElement.querySelectorAll<HTMLElement>(
              '*[tabindex="-1"]:not([disabled]):not([aria-hidden])',
            ),
          );
          if (programmticallyFocusable.length > 0) {
            programmticallyFocusable[0].focus();
            return true;
          }
        } else {
          let targetIndex;
          if (restoreIndexInCellStrategy === 'index') {
            targetIndex = Math.max(0, this.lastFocusedCell?.index ?? 0);
          } else if (restoreIndexInCellStrategy === 'first') {
            targetIndex = 0;
          } else {
            targetIndex = focusableElements.length - 1;
          }
 
          focusableElements[
            targetIndex < focusableElements.length
              ? targetIndex
              : focusableElements.length - 1
          ].focus();
          return true;
        }
      }
    } else {
      const headerElement = this.thElements?.find(
        item => item.colIdx === colIdx,
      );
      if (headerElement) {
        const focusableElements = getKeyboardFocusableElements(
          headerElement.el.nativeElement,
        );
        const focusTarget =
          focusableElements.length > 0
            ? focusableElements[0]
            : headerElement.el.nativeElement.querySelector<HTMLElement>(
                '*[tabindex="-1"]',
              );
        if (focusTarget) {
          focusTarget.focus();
        }
      }
    }
 
    return false;
  }
 
  /**
   * Focuses the next focusable element within the currently focused cell that contains focusable elements.
   * @returns true if a next element exists within the current cell otherwise returns false
   * @ignore
   */
  private focusNextElementInCell(): boolean {
    if (!this.lastFocusedCell) {
      return false;
    }
 
    if (this.lastFocusedCell.index === -1) {
      return false;
    }
 
    const cell = this.findCell(
      this.lastFocusedCell.cell.colIdx,
      this.lastFocusedCell.cell.rowIdx,
    );
 
    if (cell) {
      const focusableChildren = getKeyboardFocusableElements(
        cell.el.nativeElement,
      );
 
      const nextIdx = this.lastFocusedCell.index + 1;
 
      if (nextIdx < focusableChildren.length) {
        focusableChildren[nextIdx].focus();
        return true;
      }
    }
 
    return false;
  }
 
  /**
   * Focuses the previous focusable element within the currently focused cell that contains focusable elements.
   * @returns true if a previous element exists within the current cell otherwise returns false
   * @ignore
   */
  private focusPreviousElementInCell(): boolean {
    if (!this.lastFocusedCell) {
      return false;
    }
 
    // This means the cell itself is focused
    if (
      this.lastFocusedCell.index === -1 ||
      this.lastFocusedCell.index - 1 < 0
    ) {
      return false;
    }
 
    const cell = this.findCell(
      this.lastFocusedCell.cell.colIdx,
      this.lastFocusedCell.cell.rowIdx,
    );

    if (cell) {
      const focusableChildren = getKeyboardFocusableElements(
        cell.el.nativeElement,
      );
      const prevElement = focusableChildren[this.lastFocusedCell.index - 1];
      prevElement.focus();

      return true;
    }
 
    return false;
  }
 
  /** @ignore */
  private scrollToActiveCell(): void {
    const focusedCell = this.tdElements?.find(i =>
      i.el.nativeElement.contains(document.activeElement),
    );
    focusedCell?.el.nativeElement.scrollIntoView({
      block: 'nearest',
      inline: 'nearest',
    });
  }
 
  /** @ignore */
  private recomputeShowEntryCount() {
    if (this.isEmpty() && !this.state.search) {
      this._showEntryCount = false;
    } else {
      this._showEntryCount = true;
    }
  }
}