All files / lib/internal listbox-item.directive.ts

98.58% Statements 139/141
94.73% Branches 36/38
94.11% Functions 16/17
98.58% Lines 139/141

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 1421x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 53x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 545x 545x 1x 1x 13877x 13877x 1x 1x 13255x 13255x 1x 1x     1x 1x 13255x 13255x 13255x 11004x 2251x 2251x 13255x 13255x 1x 1x 13255x 13255x 13255x 1x 1x 13255x 13255x 13255x 1x 1x 1x 1x 522x 522x 522x 1x 1x 1x 53x 46x 53x 53x 53x 53x 1x 1x 1x 53x 46x 53x 53x 1x 1x 1x 17x 17x 1x 1x 545x 545x 534x 11x 11x 11x 11x 545x 545x 545x 545x 545x 1x 1x 13305x 12783x 522x 522x 13305x 13305x 1x 1x 116x 116x 1x 1x 35x 35x 1x 1x 70x 70x 70x 70x 70x 1x  
import {
  Directive,
  ElementRef,
  HostBinding,
  HostListener,
  Input,
} from '@angular/core';
 
import { BaseIndexedItem } from '../utils/internal-utils';
import { Key } from '../utils/util.types';
 
import { ListBoxDirective } from './listbox.directive';
 
@Directive({
  selector: '[data-gc-listbox-item]',
  standalone: false,
})
export class ListBoxItemDirective {
  @Input('data-gc-listbox-item')
  public item!: BaseIndexedItem;
 
  @HostBinding('attr.id')
  public itemId?: string;
 
  @HostBinding('attr.role')
  public readonly role = 'option';
 
  // Does not work on JAWS
  // some roles where this is used currently handle aria-selected manually, check usages of the directive before (re-)enabling this in the future
  //  @HostBinding('attr.aria-selected')
  //  get ariaSelected(): boolean {
  //    return this.selected;
  //  }
 
  @HostBinding('attr.aria-posinset')
  public ariaPosInSet = 1;
 
  @HostBinding('attr.aria-setsize')
  public ariaSetSize = 1;
 
  get index(): number {
    return this.item.index;
  }
 
  get key(): Key | symbol {
    return this.item.key;
  }
 
  get selected(): boolean {
    return this._listbox ? this._listbox.isKeySelected(this.key) : false;
  }
 
  get label(): string {
    return this.item.label;
  }
 
  @HostBinding('attr.tabindex')
  public get tabindex(): number | null {
    // a tabindex of -1 allows the node to get the native focus
    if (this._listbox?.focusMode == 'native') {
      return -1;
    } else {
      return null;
    }
  }
 
  @HostBinding('class.gc-selected')
  get classSelected(): boolean {
    return this.selected;
  }
 
  @HostBinding('class.gc-focused')
  get classFocused(): boolean {
    return this.isFocused();
  }
 
  private _listbox?: ListBoxDirective;
 
  constructor(
    private el: ElementRef<HTMLElement>,
    /*Fails in storybook hence done in MenuDirective  private menu: MenuDirective<T>*/
  ) {}
 
  @HostListener('click', ['$event'])
  _handleMenuItemClicked(event: MouseEvent): void {
    if (this._listbox?.focusMode === 'virtual') {
      this.focus();
    }
    this._listbox?.selectItem(this.item.key, 'space');
    event.preventDefault();
  }
 
  @HostListener('mousedown', ['$event'])
  _handleMenuItemMouseDown(event: MouseEvent): void {
    if (this._listbox?.focusMode === 'virtual') {
      event.preventDefault();
    }
  }
 
  @HostListener('keydown', ['$event'])
  _handleMenuItemKeyDown(event: KeyboardEvent): void {
    this._listbox?.handleKeyDown(event);
  }
 
  updateItem(listbox: ListBoxDirective, index: number, size: number): void {
    this._listbox = listbox;
    if (typeof this.key === 'string') {
      this.itemId = `${listbox.listboxId ?? ''}_k_${this.key}`;
    } else {
      this.itemId = `${listbox.listboxId ?? ''}_s_${
        this.key.description ?? String(this.key)
      }`;
    }
 
    this.ariaPosInSet = this.index + 1;
    this.ariaSetSize = size;
  }
 
  isFocused(): boolean {
    if (this._listbox) {
      return this._listbox.isFocused(this);
    } else {
      return false;
    }
  }
 
  focus(): void {
    this._listbox?.focusItem(this);
  }
 
  focusNative(): void {
    this.el.nativeElement.focus();
  }
 
  scrollIntoView(): void {
    this.el.nativeElement.scrollIntoView({
      block: 'nearest',
      inline: 'nearest',
    });
  }
}