All files / datatable si-datatable-interaction.directive.ts

95.12% Statements 39/41
85.71% Branches 24/28
100% Functions 7/7
95.12% Lines 39/41

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                                          1x 5x             5x   5x   5x         5x       6x   3x         3x 3x 3x   3x   3x         3x 3x 3x             1x         1x         49x 49x       49x     49x 49x         2x     2x     2x 2x     49x 42x   42x             42x 4x     38x             38x 2x               5x 5x 5x         5x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import {
  booleanAttribute,
  Directive,
  ElementRef,
  HostBinding,
  HostListener,
  inject,
  input,
  OnDestroy,
  OnInit
} from '@angular/core';
import { DatatableComponent } from '@siemens/ngx-datatable';
 
@Directive({
  selector: 'ngx-datatable[siDatatableInteraction]',
  exportAs: 'si-datatable-interaction'
})
export class SiDatatableInteractionDirective implements OnDestroy, OnInit {
  private table = inject(DatatableComponent, { self: true });
  /**
   * Automatically select every row or cell that is navigated trough.
   * Is ignored unless `selectionType` is `single` or `cell`.
   *
   * @defaultValue false
   */
  readonly datatableInteractionAutoSelect = input(false, { transform: booleanAttribute });
 
  @HostBinding('attr.tabindex') protected tabIndex = '0';
 
  private element: HTMLElement = inject(ElementRef).nativeElement;
  private tableBody?: HTMLElement;
 
  private autoSelectTimeout: any;
 
  private isMousedown = false;
 
  @HostListener('keydown', ['$event'])
  protected onKeydown(event: KeyboardEvent): void {
    if (event.key === 'ArrowDown') {
      const first =
        this.table.selectionType === 'cell'
          ? this.element.querySelector(
              '.datatable-row-wrapper > .datatable-body-row .datatable-body-cell'
            )
          : this.element.querySelector('.datatable-row-wrapper > .datatable-body-row');
      if (first) {
        (first as HTMLElement).focus();
        event.preventDefault();
      }
    } else if (event.key === 'ArrowUp') {
      const last =
        this.table.selectionType === 'cell'
          ? this.element.querySelector(
              '.datatable-row-wrapper:last-child > .datatable-body-row .datatable-body-cell'
            )
          : this.element.querySelector('.datatable-row-wrapper:last-child > .datatable-body-row');
      if (last) {
        (last as HTMLElement).focus();
        event.preventDefault();
      }
    }
  }
 
  @HostListener('mousedown', ['$event'])
  protected onMousedown(event: MouseEvent): void {
    this.isMousedown = true;
  }
 
  @HostListener('mouseup', ['$event'])
  protected onMouseup(event: MouseEvent): void {
    this.isMousedown = false;
  }
 
  @HostListener('focusin', ['$event'])
  protected onFocusin(event: FocusEvent): void {
    const target = event.target as HTMLElement;
    Iif (!target) {
      return;
    }
 
    clearTimeout(this.autoSelectTimeout);
    // Re-select on every element
 
    const selectionType = this.table.selectionType;
    if (
      !this.isMousedown &&
      this.datatableInteractionAutoSelect() &&
      (selectionType === 'single' || selectionType === 'cell')
    ) {
      const rowOrCell = target.closest(
        selectionType === 'cell' ? 'datatable-body-cell' : 'datatable-body-row'
      );
      Iif (!rowOrCell) {
        return;
      }
      this.autoSelectTimeout = setTimeout(() => {
        rowOrCell.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', keyCode: 13 }));
      }, 100);
    }
    if (this.table.virtualization) {
      if (this.tableBody) {
        const lastList =
          selectionType === 'cell'
            ? this.tableBody.querySelectorAll(
                '.datatable-row-wrapper:last-child > .datatable-body-row .datatable-body-cell'
              )
            : this.tableBody.querySelectorAll(
                '.datatable-row-wrapper:last-child > .datatable-body-row'
              );
        if (Array.from(lastList).includes(target)) {
          this.tableBody.scrollTop = this.tableBody.scrollTop + lastList[0].clientHeight;
        } else {
          const firstList =
            selectionType === 'cell'
              ? this.tableBody.querySelectorAll(
                  '.datatable-row-wrapper:first-child > .datatable-body-row .datatable-body-cell'
                )
              : this.tableBody.querySelectorAll(
                  '.datatable-row-wrapper:first-child > .datatable-body-row'
                );
          if (Array.from(firstList).includes(target)) {
            this.tableBody.scrollTop = this.tableBody.scrollTop - firstList[0].clientHeight;
          }
        }
      }
    }
  }
 
  ngOnInit(): void {
    this.tableBody = this.element.querySelector('datatable-body') as HTMLElement;
    if (this.tableBody) {
      this.tableBody.tabIndex = -1;
    }
  }
 
  ngOnDestroy(): void {
    clearTimeout(this.autoSelectTimeout);
  }
}