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

96.28% Statements 363/377
92.95% Branches 66/71
89.47% Functions 34/38
96.28% Lines 363/377

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 376 377 3781x 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 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 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 18432x 18432x 18432x 300x 300x 31x 31x 31x 31x 31x 31x 300x 300x 300x 444x 444x 444x 300x 300x 316x 316x 300x 300x 300x       300x 300x 42x 42x 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 1x 1x 300x 300x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 2x 2x 2x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 9x 9x 1x 1x 1x 1x 40x 40x 40x 40x 40x 40x 17x 12x 12x 40x 40x 1x 1x 1x   1x   1x 1x 1x     1x 1x 1x 1x 1x 9979x 9979x 1x 1x 1x 300x 300x 300x 300x 1x 1x 1x           1x 1x 1x 5631x 5631x 1x 1x 1x 51x 51x 51x 51x 1x 1x 1x 49x 42x 49x 49x 1x 1x 1x 21x 21x 15x 21x 21x 21x 12x 21x 21x 1x 1x 1x 45x 45x 45x 45x 45x 1x 44x 44x 45x 45x 1x 1x 1x 55x 51x 51x 51x 51x 51x 51x 51x 55x 55x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 1x 1x 1x 1x 1x 110x 110x 1x 1x 1x 3508x 3508x 3508x 3508x 3508x 1x 1x 1x 2862x 2862x 1x 1x 1x 49x 11x 49x 49x 49x 1x 1x 1x 331x 327x 327x 331x 331x 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;
 
/**
 * (Legacy) Internal Base Class to implement pickers
 * @deprecated
 */
@Component({
  template: '',
  standalone: false,
})
export abstract class LegacyBasePickerComponent<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(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;
}