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

98.96% Statements 190/192
95.56% Branches 43/45
94.44% Functions 17/18
98.96% Lines 190/192

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 1931x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 1x 1x 1x 1x 1x 46x 46x 46x 46x 7606x 7606x 7606x 7606x 40x 40x 40x 40x 40x 40x 40x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 30x 1x 1x 1x 7x 7x 1x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 3x 3x 3x 3x 3x 2x 2x 2x 2x 1x 1x 1x 1x 6x 6x 6x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 3x 3x 3x 1x 1x 1x 1x 1x 6x   6x 6x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 30x 30x 3x 3x 3x 3x 1x 1x 1x 1x 1x 14x 1x 1x 1x 12x 12x 4x 1x 1x 1x 1x 46x 37x 37x 37x 46x 46x 46x 46x 46x 46x 46x 43x 43x 3x 3x 1x 1x 1x 1x 33x 33x 322x 322x 33x 33x 1x 1x  
import { AfterContentInit, ChangeDetectorRef, ContentChildren, Directive, EventEmitter, HostBinding, HostListener, Input, OnDestroy, Output, QueryList } from '@angular/core';
import { Subscription } from 'rxjs';
 
import { Converter, Item, Key } from '../utils/util.types';
import { VirtualListBoxItemDirective } from './virtual-listbox-item.directive';
 
/**
 * Aria Support for ListBox using a virtual focus with aria-activedescendant
 */
@Directive({
  selector: '[virtual-listbox]'
})
export class VirtualListBoxDirective implements AfterContentInit, OnDestroy {
 
  @Output()
  public onSelect = new EventEmitter<Key>();
 
  @Output()
  public onOpen = new EventEmitter<Key>();
 
  @HostBinding('attr.role')
  readonly role = 'listbox';
 
  @HostBinding('tabindex')
  readonly tabindex = 0;
 
  @ContentChildren(VirtualListBoxItemDirective)
  public children!: QueryList<VirtualListBoxItemDirective>;
 
  @HostBinding('attr.aria-activedescendant')
  public get activeDescendant(): string | null {
    return this.hasFocus ? this._activeDescendant : null; // Only render active descendant if it has focus otherwise FF & NVDA thinks the list has the focus
  }
 
  public set activeDescendant(value: string | null) {
    this._activeDescendant = value;
  }
 
  public get value(): Key | undefined {
    return this._value;
  }
 
  @Input()
  @Output()
  public set value(value: Key | undefined) {
    this._value = value;
    if( this.children ) {
      this.children.find( c => c.item.key === value)?.scrollIntoView();
    }
  }
 
  public _value?: Key;
 
  private focusedListIndex = -1;
 
  private childrenChangeSubscription?: Subscription;
 
  private _activeDescendant: string | null = null;
 
  private hasFocus = false;
 
  constructor(private change: ChangeDetectorRef) {}
 
  @Input()
  public itemIdComputer: Converter<Item, string > = i => 'item-'+i.key;
 
  @HostListener('focus', [ '$event' ])
  public _impl_onFocus(): void {
    this.hasFocus = true;
    if( this.focusedListIndex === -1 && this.children.length > 0 ) {
      this.updateFocusedIndex(0);
    }
 
    // Do not sync with selection - eg windows explorer does not do it either
    if( this.focusedListIndex !== -1 ) {
      this.children.get(this.focusedListIndex)?.scrollIntoView();
    }
  }
 
  @HostListener('blur', [ '$event' ])
  public _impl_onBlur(): void {
    this.hasFocus = false;
  }
 
  /** @ignore */
  @HostListener('keydown', [ '$event' ])
  public _impl_onKeyDown(event: KeyboardEvent): void {
    const key = event.key;
    switch (key) {
    case 'Enter':
      this._impl_onEnter();
      event.preventDefault();
      break;
    case ' ':
      this._impl_onSpace();
      event.preventDefault();
      break;
    case 'ArrowUp':
      this._impl_onUp();
      event.preventDefault();
      break;
    case 'ArrowDown':
      this._impl_onDown();
      event.preventDefault();
      break;
    }
  }
 
  /** @ignore */
  public _impl_onSpace(): void {
    const el = this.children.get(this.focusedListIndex);
    if( el ) {
      this.selectItem(el.item.item, false);
    }
  }
 
  /** @ignore */
  public _impl_onEnter(): void {
    const el = this.children.get(this.focusedListIndex);
    if( el ) {
      this.selectItem(el.item.item, true);
    }
  }
 
  /** @ignore */
  public _impl_onDown(): void {
    if( this.focusedListIndex == -1 ) {
      this.updateFocusedIndex(0);
    } else if( this.focusedListIndex + 1 < this.children.length ) {
      this.updateFocusedIndex(this.focusedListIndex + 1);
    }
  }
 
  /** @ignore */
  public _impl_onUp(): void {
    if( this.focusedListIndex == -1 ) {
      this.updateFocusedIndex(this.children.length - 1);
    } else if( this.focusedListIndex - 1 >= 0 ) {
      this.updateFocusedIndex(this.focusedListIndex - 1);
    }
  }
 
  ngAfterContentInit(): void {
    this.connectMenuItems();
    this.childrenChangeSubscription = this.children.changes.subscribe( () => {
      this.connectMenuItems();
      // Fix dev exception value change while ...
      setTimeout( () => {
        this.updateFocusedIndex(this.children.toArray().findIndex( c => c.item.key === this.value));
      });
    } );
  }
 
  ngOnDestroy(): void {
    this.childrenChangeSubscription?.unsubscribe();
  }
 
  selectItem(item: Item, open: boolean): void {
    this.onSelect.emit(item.key);
    if( open ) {
      this.onOpen.emit(item.key);
    }
  }
 
  updateFocusedIndex(idx: number) {
    if( this.focusedListIndex != -1 ) {
      const prevItem = this.children.get(this.focusedListIndex);
      if( prevItem ) {
        prevItem.focused = false;
      }
    }
 
    this.focusedListIndex = idx;
 
    const item = this.children.get(idx);
    if( item ) {
      item.focused = true;
      this.activeDescendant = this.itemIdComputer(item.item);
    } else {
      this.activeDescendant = null;
    }
  }
 
  private connectMenuItems(): void {
    const size = this.children.length;
    this.children.forEach( (e, index) => {
      e.updateItem(this, index, size);
      e.focused = false;
    });
    this.change.detectChanges();
  }
}