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

94.57% Statements 209/221
90.24% Branches 37/41
84.61% Functions 22/26
94.57% Lines 209/221

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 2221x 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 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 843x 871x 871x 871x 871x 1x 1x 1015x 1015x 1x 1x 871x 871x 871x 1x 1x     1x 1x 45228x 45228x 1x 1x 45228x 45228x 1x 1x 865x 865x 1x 1x 1015x 1015x 84x 1015x 1015x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 843x 843x 843x 843x 843x 843x 15x 843x 843x 15x 843x 843x 843x 1x 1x 1x 15x 15x 15x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 1x 1x 1x 208x 208x   208x 208x 1x 1x 865x 865x 865x 865x 865x 865x 865x 865x 865x 1x 1x 140x 140x 140x 140x 140x 1x 1x 871x     871x 871x 871x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x     18x 18x 871x 1x 1x 72x 72x 72x 72x 72x 72x     72x 72x 72x 72x 72x 72x   72x 72x 72x 72x 72x 1x  
import {
  ContentChild,
  ContentChildren,
  Directive,
  ElementRef,
  HostBinding,
  HostListener,
  Input,
  NgZone,
  OnDestroy,
  QueryList,
} from '@angular/core';
 
import {
  IndexedItem,
  handleActive,
  observeSize,
} from '../utils/internal-utils';
import { Callback, Key } from '../utils/util.types';
 
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: '[data-gc-virtual-listbox-item]',
})
export class VirtualListBoxItemDirective implements OnDestroy {
  @ContentChildren('gcBadgeRendered')
  public renderedBadges!: QueryList<BadgeComponent>;
 
  @Input('data-gc-virtual-listbox-item')
  public item!: IndexedItem<unknown>;
 
  @HostBinding('attr.role')
  public readonly role = 'option';
 
  @HostBinding('attr.aria-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));
      }
    }
  }
}