All files / lib/internal listbox.directive.ts

97.03% Statements 261/269
91.67% Branches 66/72
87.5% Functions 21/24
97.03% Lines 261/269

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 2701x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 382x 382x 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 26853x 1x 1x 1x 39x 39x 39x 39x 16x 17x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x 1x 1x 6x 6x 6x 6x 3x 3x 3x 3x 1x 2x 2x 2x 2x 1x 2x 2x 2x 2x       8x 8x 8x 8x 39x 39x 39x 39x 39x 39x 1x 1x 1x 1x 26787x 301x 26486x 26486x 1x 1x 1x 1x 382x 382x 1x 1x 1x   1x 1x 1x   1x 1x 1x 18x 1x 1x 1x 49x 1x 1x 1x 84x 84x 61x 23x 23x 1x 1x 1x 1x 48x 48x 20x 20x 18x 18x 18x 18x 18x 18x 18x 18x 18x 1x 1x 1x 1x 49x 49x 49x 49x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 30x 1x 1x 1x 4x 1x 1x 1x 43x 43x 43x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 1x 8x 8x 8x 25x 12x 8x 8x 8x 2x 7x 8x 8x 8x 8x 6x 1x 1x 1x 1x 52x 8x 8x 1x 1x 1x 1x 401x 401x 1298x 401x 401x 1x 1x 1x 34x 1x 1x  
import { AfterContentInit, ChangeDetectorRef, ContentChildren, Directive, ElementRef, EventEmitter, HostBinding, Input, Output, QueryList } from '@angular/core';
import { Item, Key } from '../utils/util.types';
 
import { ListBoxItemDirective } from './listbox-item.directive';
 
 
const isPrintableCharacter =  (str: string): boolean => {
  return str.length === 1 && str.match(/\S/) !== null;
};
 
@Directive({
  selector: '[listbox]'
})
export class ListBoxDirective implements AfterContentInit {
 
  @ContentChildren(ListBoxItemDirective)
  public children!: QueryList<ListBoxItemDirective>;
 
  @Output('listbox-onSelect')
  public onSelect = new EventEmitter<Key>();
 
  @Output('listbox-onCancel')
  public onCancel = new EventEmitter<void>();
 
  @Input('listbox-value')
  public value?: Key;
 
  @Input('listbox-values')
  public values?: ReadonlySet<Key>;
 
  @Input('listbox-id')
  public listboxId?: string;
 
  @Input('listbox-focus-mode')
  public focusMode: 'native' | 'virtual' = 'native';
 
  @Output('listbox-onItemFocus')
  public onItemFocus = new EventEmitter<ListBoxItemDirective>();
 
  @HostBinding('attr.role')
  readonly role = 'listbox';
 
  private itemTextStart = '';
 
  private _focused?: ListBoxItemDirective;
 
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  private clearTimer?: any;
 
  constructor(private el: ElementRef,
    private change: ChangeDetectorRef,) {}
 
  public isFocused(item: ListBoxItemDirective): boolean {
    return this._focused == item;
  }
 
  public handleKeyDown(event: KeyboardEvent) {
    const key = event.key;
    let flag = false;
 
    switch (key) {
    case 'Enter':
    case ' ': 
      {
        const focused = this.findFocusedChild();
        if (focused) {
          this.selectItem(focused.item);
        }
      }
      flag = true;
      break;
    case 'Escape':
      this.cancel();
      flag = true;
      break;
    case 'ArrowDown':
      flag = true;
      this.moveFocusToNextItem();
      break;
    case 'ArrowUp':
      this.moveFocusToPrevItem();
      flag = true;
      break;
    case 'Home':
    case 'PageUp':
      this.moveFocusToFirstItem();
      flag = true;
      break;
    case 'End':
    case 'PageDown':
      this.moveFocusToLastItem();
      flag = true;
      break;
    case 'Tab':
      this.onCancel.emit();
      break;
    default:
      if (isPrintableCharacter(key)){
        this.moveByKey(key);
        flag = true;
      }
    }
 
    if (flag) {
      event.stopPropagation();
      event.preventDefault();
    }
  }
 
  isKeySelected(key: Key): boolean {
    if (this.values) {
      return this.values.has(key);
    } else {
      return this.value === key;
    }
  }
 
  ngAfterContentInit(): void {
    this.connectMenuItems();
    this.children.changes.subscribe( () => this.connectMenuItems() );
  }
 
  containsFocus(): boolean {
    return this.children.toArray().some(child => child.isFocused());
  }
 
  getChildByItem(item: Item): ListBoxItemDirective | undefined {
    return this.children.toArray().find((it) => it.key == item.key);
  }
 
  getChildByKey(key: string): ListBoxItemDirective | undefined {
    return this.children.toArray().find((it) => it.key == key);
  }
 
  getChildByIndex(index: number): ListBoxItemDirective | undefined {
    return this.children.toArray()[index];
  }
 
  focusItem(item: ListBoxItemDirective | undefined) {
    this._focused = item;
    if (this.focusMode === 'native') {
      item?.focusNative();
    } else {
      this.onItemFocus.emit(item);
    }
  }
 
  focusItemByKey(key: Key | undefined | 'first' | 'last' | 'prev' | 'next'): void {
    if (key == 'first' || key == undefined) {
      this.moveFocusToFirstItem();
    } else if (key == 'last') {
      this.moveFocusToLastItem();
    } else if (key == 'next') {
      this.moveFocusToNextItem();
    } else if (key == 'prev') {
      this.moveFocusToPrevItem();
    } else {
      const child = this.getChildByKey(key);
 
      child?.focus();
      child?.scrollIntoView();
    }
  }
 
  focusItemByIndex(index: number) : void {
    const child = this.getChildByIndex(index);
 
    child?.focus();
    child?.scrollIntoView();
  }
 
  moveFocusToNextItem(): void {
    const cur = this.findFocusedChild();
    if (cur == undefined) {
      this.moveFocusToFirstItem();
    } else {
      const idx = this.children.toArray().indexOf(cur);
      if (idx + 1 < this.children.toArray().length) {
        this.focusItemByIndex(idx + 1);
      }
    }
  }
 
  moveFocusToPrevItem(): void {
    const cur = this.findFocusedChild();
    if (cur == undefined) {
      this.moveFocusToFirstItem();
    } else {
      const idx = this.children.toArray().indexOf(cur);
      if (idx - 1 < this.children.toArray().length && idx - 1 >= 0) {
        this.focusItemByIndex(idx - 1);
      }
    }
  }
 
  moveFocusToFirstItem(): void {
    this.focusItemByIndex(0);
  }
 
  moveFocusToLastItem(): void {
    this.focusItemByIndex(this.children.toArray().length - 1);
  }
 
  selectItem(item: Item): void {
    this.onSelect.emit(item.key);
    this.itemTextStart='';
    this.clearSearchTimer();
  }
 
  cancel(): void {
    this.onCancel.emit();
    this.itemTextStart='';
    this.clearSearchTimer();
  }
 
  selectItemByKey(key: Key): void {
    const it = this.getChildByKey(key)?.item.item;
    if (it != undefined) {
      this.selectItem(it);
    }
  }
 
  moveByKey(char: string): void {
    const cur = this.findFocusedChild();
    const curIdx = cur == undefined ? 0 : this.children.toArray().indexOf(cur);
 
    this.itemTextStart += char;
 
    this.clearSearchTimer();
 
    this.clearTimer = setTimeout( () => {
      this.itemTextStart = '';
    }, 500 );
 
    let indx = this.children.toArray().findIndex( (itemDirective, idx) => {
      return idx > curIdx
        && itemDirective.item.label.toLocaleLowerCase().startsWith(this.itemTextStart.toLocaleLowerCase());
    } );
 
    if( indx === -1 ) {
      indx = this.children.toArray().findIndex( (itemDirective, idx) => {
        return idx <= curIdx && itemDirective.item.label.toLocaleLowerCase().startsWith(this.itemTextStart.toLocaleLowerCase());
      } );
    }
 
    if( indx !== -1 && indx !== curIdx ) {
      this.focusItemByIndex(indx);
    }
  }
 
  private clearSearchTimer() {
    if( this.clearTimer !== undefined ) {
      clearTimeout(this.clearTimer);
      this.clearTimer = undefined;
    }
  }
 
  private connectMenuItems(): void {
    const size = this.children.length;
    this.children.forEach( (e, index) => {
      e.updateItem(this, index, size);
    });
    this.change.detectChanges();
  }
 
  private findFocusedChild(): ListBoxItemDirective | undefined {
    return this.children.toArray().find((child) => child.isFocused());
  }
}