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

90.71% Statements 166/183
89.74% Branches 35/39
83.33% Functions 20/24
90.71% Lines 166/183

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 1841x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 320x 320x 320x 360x 360x 360x 360x 310x 310x 310x 310x 310x         8484x 8484x 8484x 8484x 8484x 8484x 8484x 8484x 322x 322x 322x 322x 402x 402x 402x 402x 402x 43x           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 300x 6x 300x 6x 1x 1x 1x 1x 1x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 1x 1x 1x 1x 148x 148x   1x 1x 1x 1x 322x 322x 322x 322x 1x 1x 1x 60x 1x 1x 1x 320x     320x 320x 320x 10x 10x 10x     1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x     20x 20x 20x 20x 20x 20x   20x 20x 1x 1x 1x 1x  
import { ContentChild, ContentChildren, Directive, ElementRef, HostBinding, HostListener, Input, NgZone, OnDestroy, QueryList } from '@angular/core';
 
import { Callback, Key } from '../utils/util.types';
import { IndexedItem, handleActive, observeSize } from '../utils/internal-utils';
import { BadgeComponent } from '../badge/badge.component';
import { VirtualListBoxDirective } from './virtual-listbox.directive';
 
/**
 * Aria Support for ListBox using a virtual focus with aria-activedescendant
 */
@Directive({
  selector: '[virtual-listbox-item]'
})
export class VirtualListBoxItemDirective implements OnDestroy {
  @ContentChildren('gcBadgeRendered')
  public renderedBadges!: QueryList<BadgeComponent>;
 
  @Input('virtual-listbox-item')
  public item!: IndexedItem<unknown>;
 
  @HostBinding('attr.role')
  public readonly role = 'option';
 
  @HostBinding('attr.area-posinset')
  public ariaPosInSet = 1;
 
  @HostBinding('attr.aria-setsize')
  public ariaSetSize = 1;
 
  @HostBinding('class.gc-list-item-focused')
  public _focused = false;
 
  @HostBinding('class.gc-list-item-active')
  public _active = false;
 
  @HostBinding('class.gc-selected')
  get classSelected(): boolean {
    return this.selected;
  }
 
  @HostBinding('class.surface-contrast')
  get classSurfaceSelected(): boolean {
    return this.selected;
  }
 
  @HostBinding('attr.aria-selected')
  get ariaSelected(): boolean {
    return this.selected;
  }
 
  @ContentChild('gcBadge', { read: ElementRef })
  public set badge( badge: ElementRef<HTMLElement> | undefined) {
    this._badge = badge;
    this.handleBadgeUpdate();
  }
 
  public get badge() {
    return this._badge;
  }
 
  @ContentChild('gcIcon')
  public set icon(icon: ElementRef<HTMLElement> | undefined) {
    this._icon = icon;
  }

  public get icon(): ElementRef<HTMLElement> | undefined {
    return this._icon;
  }
 
  get key(): Key {
    return this.item.key;
  }
 
  get selected(): boolean {
    return this.key == this._listbox?.value;
  }
 
  get index(): number {
    return this.item.index;
  }
 
  set focused(value: boolean) {
    this._focused = value;
    if( value ) {
      this.scrollIntoView();
    }
  }

  get focused() {
    return this._focused;
  }
 
  private _listbox!: VirtualListBoxDirective;
 
  private _activeSubscription: Callback;
 
  private _sizeSubscription?: Callback;
 
  private _badge?: ElementRef<HTMLElement>;
 
  private _icon?: ElementRef<HTMLElement>;
 
  constructor(private el: ElementRef<HTMLElement>, private zone: NgZone) {
    this._activeSubscription = handleActive(el.nativeElement, () => {
      this._active = true;
    }, () => {
      this._active = false;
    });
  }
 
  @HostListener('click')
  public _handleClick() {
    this._listbox.selectItem(this.item.item, false);
    this._listbox.updateFocusedIndex(this.item.index);
  }
 
  @HostListener('dblclick')
  public _handleDoubleClick() {
    this._listbox.selectItem(this.item.item, true);
  }
 
  @HostListener('mousedown')
  public _handleMouseDown() {
    this._listbox.updateFocusedIndex(this.item.index);
  }
 
  /** @ignore */
  ngOnDestroy(): void {
    this._activeSubscription();
    if( this._sizeSubscription ) {
      this._sizeSubscription();
    }
  }
 
  updateItem(listbox: VirtualListBoxDirective, index: number, size: number): void {
    this._listbox = listbox;
 
    this.ariaPosInSet = this.index + 1;
    this.ariaSetSize = size;
  }
 
  scrollIntoView() {
    this.el.nativeElement.scrollIntoView({ block: 'nearest', inline: 'nearest' });
  }
 
  private handleBadgeUpdate() {
    if( this._sizeSubscription ) {
      this._sizeSubscription();
      this._sizeSubscription = undefined;
    }
 
    if( this.badge ) {
      const listResize = observeSize(this.el, this.zone, this.handleResize.bind(this));
      const badgeResize = observeSize(this.el, this.zone, this.handleResize.bind(this));
      this._sizeSubscription = () => {
        listResize();
        badgeResize();
      };
    }
  }
 
  private handleResize() {
    if( this.badge ) {
      const badgeWidth = this.badge.nativeElement.clientWidth;
      const itemWidth = this.el.nativeElement.clientWidth;
 
      let availableWidth = itemWidth - 16 * 2; // remove the padding
      if( this._icon ) {
        availableWidth -= 40; // Bounding Box for Icons
        availableWidth -= 12; // Space between icon and text
      }
 
      availableWidth -= badgeWidth;
      availableWidth -= 20; // Margin of the badge
 
      if( availableWidth < 160 ) {
        this.renderedBadges.forEach( e => e.allowTextWrap = true );
      } else {
        this.renderedBadges.forEach( e => e.allowTextWrap = false );
      }
    }
  }
}