All files / lib/picker base-picker.component.ts

97.06% Statements 364/375
98.5% Branches 66/67
88.23% Functions 30/34
97.06% Lines 364/375

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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 3761x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 699x 1x 1x 1x 1x 1x 1x 51625x 51625x 51625x 1x 1x 100x 100x 100x 100x 100x 100x 1x 1x 1x 444x 444x 444x 1x 1x 720x 720x 1x 1x 1x       1x 1x 102x 102x 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 699x 699x 1x 1x 1x 1x 40x 40x 40x 40x 40x 40x 11x 11x 11x 23x 5x 5x 5x 27x 6x 6x 6x 6x 6x 36x 7x 7x 7x 40x 40x 40x 40x 1x 1x 1x 1x 101x 101x 101x 101x 101x 101x 50x 46x 101x 101x 101x 1x 1x 11x 11x 11x   11x 11x 11x 11x 11x 11x 11x 1x 1x 1x 23424x 23424x 1x 1x 1x 699x 699x 699x 699x 1x 1x 1x           1x 1x 1x 14046x 14046x 1x 1x 1x 102x 102x 102x 102x 1x 1x 1x 108x 100x 108x 108x 1x 1x 1x 39x 39x 20x 39x 39x 39x 29x 39x 39x 1x 1x 1x 74x 74x 74x 74x 74x 1x 73x 73x 74x 74x 1x 1x 1x 106x 102x 102x 102x 102x 102x 106x 106x 106x 106x 1x 1x 1x     1x 1x 1x 5x 5x 1x 1x 1x 11x 11x 1x 1x 1x 7x 7x 1x 1x 1x 6x 6x 1x 1x 1x 11x 11x 1x 1x 1x 1x 1x 212x 212x 1x 1x 1x 141621x 141621x 141621x 141621x 141621x 1x 1x 1x 125352x 125352x 1x 1x 1x 93x 48x 93x 93x 93x 1x 1x 1x 799x 790x 790x 799x 799x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import {
  AfterViewInit,
  Component,
  DoCheck,
  ElementRef,
  EventEmitter,
  HostListener,
  Input,
  IterableDiffers,
  Output,
  ViewChild,
} from '@angular/core';
 
import {
  IndexedItem,
  ItemStorage,
  isOnlyAltPressedOrNothing,
} from '../utils/internal-utils';
import {
  Callback,
  Converter,
  FocusableElementOwner,
  Item,
  Key,
  hasSublabel,
} from '../utils/util.types';
 
import { FocusScopeDirective } from '../internal/focus-scope.directive';
import { AutoIdDirective } from '../internal/label-container/label-container.component';
import {
  FocusTargetFirst,
  FocusTargetLast,
  ListBoxDirective,
} from '../internal/listbox.directive';
import {
  OverlayContainerComponent,
  OverlayLocation,
} from '../internal/overlay-container/overlay-container.component';
 
import { TextInputComponent } from '../text-input-field/text-input-field.component';
 
export type KeyFocusTarget =
  | Key
  | typeof FocusTargetFirst
  | typeof FocusTargetLast;
 
/**
 * Internal Base Class to implement pickers
 */
@Component({
  template: '',
})
export abstract class BasePickerComponent<T, I extends Item>
  implements AfterViewInit, DoCheck, FocusableElementOwner
{
  /** If set to true it disables selection of items */
  @Input()
  public disabled = false;
 
  /**
   * Set of item keys that will be rendered as disabled items.
   */
  @Input()
  public disabledItems: ReadonlySet<string> = new Set();
 
  /** Fires if the picker has focus. */
  @Output()
  public readonly onFocusEntered: EventEmitter<void> = new EventEmitter<void>();
 
  /** Fires if the picker has lost focus. */
  @Output()
  public readonly onFocusLeft: EventEmitter<void> = new EventEmitter<void>();
 
  /** @ignore */
  @ViewChild('popup')
  public popup?: OverlayContainerComponent;
 
  /** @ignore */
  @ViewChild(ListBoxDirective)
  public menu?: ListBoxDirective;
 
  /** @ignore */
  @ViewChild(FocusScopeDirective)
  public focusScope?: FocusScopeDirective;
 
  /** @ignore */
  @ViewChild('button')
  public buttonElement?: ElementRef<HTMLButtonElement>;
 
  /** @ignore */
  @ViewChild('readonlyTextInput')
  public readonlyTextInput?: TextInputComponent;
 
  /** @ignore */
  @ViewChild(AutoIdDirective)
  protected autoIdDirective?: AutoIdDirective;
 
  /**
   * Decides if the picker renders in a readonly state.
   */
  @Input()
  public get readonly(): boolean {
    return this._readonly;
  }
 
  set readonly(value: boolean) {
    this._readonly = value;
    // waits a tick after re-querying the popup in order for it to be available
    setTimeout(() => {
      this.connectPopup();
    });
  }
 
  /** The items to display in the selection list */
  @Input()
  public get items(): readonly T[] {
    return this.storage.values;
  }
 
  public set items(items: readonly T[]) {
    this.storage.values = items;
  }
 
  /** Adapts the domain type T to the internal Item structure */
  @Input()
  public get itemAdapter(): Converter<T, I> | undefined {
    return this.storage.converter;
  }
 
  public set itemAdapter(itemAdapter: Converter<T, I> | undefined) {
    this.storage.converter = itemAdapter;
  }
 
  /** @ignore */
  public readonly storage: ItemStorage<T, I>;
 
  /** @ignore */
  public _impl_drawerOpen = false;
 
  /** @ignore */
  public labelId?: string;
 
  /**
   * @ignore
   */
  public _overlayLocation: OverlayLocation = 'end-bottom';
 
  /** @ignore */
  private keepFocus = false;
 
  /**
   * @ignore
   */
  private _readonly = false;
 
  /**
   * @ignore
   */
  private _impl_popupCallback: Callback | undefined;
 
  constructor(private itDiff: IterableDiffers) {
    this.storage = new ItemStorage<T, I>(itDiff);
  }
 
  /** @ignore */
  @HostListener('keydown', ['$event'])
  public _impl_onKeyDown(event: KeyboardEvent): void {
    if (this.disabled) {
      return;
    }
    const key = event.key;
    switch (key) {
      case 'Enter':
        this._impl_onEnter();
        event.preventDefault();
        break;
      case ' ':
        this._impl_onSpace();
        event.preventDefault();
        break;
      case 'ArrowUp':
        if (event.altKey) {
          this._impl_onUp();
          event.preventDefault();
        }
        break;
      case 'ArrowDown':
        if (isOnlyAltPressedOrNothing(event)) {
          this._impl_onDown();
          event.preventDefault();
        }
        break;
    }
  }
 
  /** @ignore */
  @HostListener('keyup', ['$event'])
  public _impl_onKeyUp(event: KeyboardEvent): void {
    if (this.disabled) {
      return;
    }
    const key = event.key;
    switch (key) {
      case 'Enter':
      case ' ':
        event.preventDefault();
        break;
    }
  }
 
  public focusChild(): boolean {
    if (this.disabled) {
      return false;
    } else if (this.readonly && this.readonlyTextInput) {
      return this.readonlyTextInput.focusChild();
    } else if (this.buttonElement) {
      this.buttonElement.nativeElement.focus();
      return true;
    } else {
      return false;
    }
  }
 
  /** @ignore */
  public ngDoCheck(): void {
    this.storage.onCheck();
  }
 
  /** @ignore */
  public ngAfterViewInit(): void {
    this.connectPopup();
    // Fix problem in dev-mode - see https://angular.io/errors/NG0100
    setTimeout(() => (this.labelId = this.autoIdDirective?.labelId));
  }
 
  /** @ignore */
  public isFocusWithin(): boolean {
    if (this.focusScope !== undefined) {
      return this.focusScope.focusWithin;
    }
    return false;
  }
 
  /** @ignore */
  public isButtonFocused(): boolean {
    return document.activeElement == this.buttonElement?.nativeElement;
  }
 
  /** @ignore */
  protected _impl_open(focusItem: KeyFocusTarget): void {
    focusItem = this._impl_onOpen(focusItem);
    this._impl_popupCallback = () => this.menu?.focusItemByKey(focusItem);
    this._impl_drawerOpen = true;
  }
 
  /** @ignore */
  protected _impl_onFocusEntered(): void {
    if (!this.keepFocus) {
      this.onFocusEntered.emit();
    }
  }
 
  /** @ignore */
  protected _impl_onFocusLeft(relatedTarget?: EventTarget | null): void {
    this.keepFocus = this._impl_drawerOpen && relatedTarget === null;
    if (this._impl_drawerOpen) {
      this._impl_close(this.keepFocus);
    }
 
    if (!this.keepFocus) {
      this.onFocusLeft.emit();
    }
  }
 
  /** @ignore */
  protected _impl_onButtonClick(): void {
    if (this.disabled) {
      return;
    }
 
    if (this._impl_drawerOpen) {
      this._impl_close(true);
    } else {
      this._impl_open(FocusTargetFirst);
    }
  }
 
  /** @ignore */
  protected _impl_handlePopupVisible(): void {
    if (this._impl_popupCallback) {
      // Wait a tick because the popup might not be ready to receive the focus
      setTimeout(() => {
        if (this._impl_popupCallback) {
          this._impl_popupCallback();
          this._impl_popupCallback = undefined;
        }
      });
    }
  }
 
  /** @ignore */
  protected _impl_handleHide(): void {
    this._impl_close(true);
  }
 
  /** @ignore */
  protected _impl_onSpace(): void {
    this._impl_open(FocusTargetFirst);
  }
 
  /** @ignore */
  protected _impl_onEnter(): void {
    this._impl_open(FocusTargetFirst);
  }
 
  /** @ignore */
  protected _impl_onDown(): void {
    this._impl_open(FocusTargetFirst);
  }
 
  /** @ignore */
  protected _impl_onUp(): void {
    this._impl_open(FocusTargetLast);
  }
 
  /** @ignore */
  protected _impl_onCancel(): void {
    this._impl_close(document.activeElement !== document.body);
  }
 
  /**
   * @ignore
   */
  protected _handleLocationChange(location: OverlayLocation): void {
    this._overlayLocation = location;
  }
 
  /** @ignore */
  protected _getSubLabel(item: IndexedItem<T, I>): string | undefined {
    if (hasSublabel(item.item)) {
      return item.item.sublabel;
    }
    return undefined;
  }
 
  /** @ignore */
  protected _isDisabled(key: Key): boolean {
    return this.disabledItems.has(key);
  }
 
  /** @ignore */
  protected _impl_close(keepFocus: boolean): void {
    if (keepFocus) {
      this.buttonElement?.nativeElement.focus();
    }
    this._impl_drawerOpen = false;
  }
 
  /** @ignore */
  private connectPopup(): void {
    if (this.popup) {
      this.popup.anker = this.getAnker();
      this.popup.scrollTargetElement = this.getScrollTarget();
    }
  }
 
  /** @ignore */
  public abstract getAnker(): ElementRef<HTMLElement>;
 
  /** @ignore */
  public abstract getScrollTarget(): ElementRef<HTMLElement> | undefined;
 
  /** @ignore */
  protected abstract _impl_onOpen(focusItem: KeyFocusTarget): KeyFocusTarget;
}