All files / lib/tab-folder tab-picker.component.ts

91.47% Statements 118/129
90.47% Branches 19/21
100% Functions 11/11
91.47% Lines 118/129

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 1301x 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 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 1x 1x 1x 18x 18x 18x 1x 1x 165x 165x 1x 1x 135x 135x 1x 1x 135x 135x 1x 1x 1x 6x 6x 6x 6x 6x 1x 1x 1x 6x 6x 6x 5x 5x 5x 5x 5x 6x 6x                       6x 5x 6x 6x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 363x 363x 1x  
import {
  Component,
  ElementRef,
  EventEmitter,
  Input,
  Output,
  TemplateRef,
  ViewChild,
} from '@angular/core';
 
import { Item, Key } from '../utils/util.types';
import { itemTrack } from '../utils/utilities';
 
import { SelectionEvent } from '../internal/listbox.directive';
 
import {
  KeyFocusTarget,
  LegacyBasePickerComponent,
} from '../picker/legacy-base-picker.component';
 
@Component({
  selector: 'gc-tab-picker',
  templateUrl: './tab-picker.component.html',
  styleUrls: ['./tab-picker.component.css'],
  standalone: false,
})
export class TabPickerComponent<
  T extends Item,
> extends LegacyBasePickerComponent<T, Item> {
  @ViewChild('drawerSlot')
  public drawerSlot!: ElementRef;
 
  @ViewChild('gcElementRoot')
  public elementRoot!: ElementRef;
 
  /** Inform about the change of the selection state */
  @Output()
  public readonly valueChange: EventEmitter<T> = new EventEmitter<T>();
 
  /**
   * @ignore
   */
  @Input()
  public iconProvider?: TemplateRef<unknown>;
 
  /**
   * Label for the control.
   */
  @Input()
  public label = '';
 
  /**
   * Value change will be only performed when the callback evaluates with value `true`. Leaving this
   * empty (`undefined`) is equivalent to always resolving to `true`.
   */
  @Input()
  public permitValueChange?: (key: Key | symbol) => Promise<boolean> | boolean;
 
  /** The value represents the selected value. */
  @Input()
  public get value(): T | undefined {
    return this.storage.selected;
  }
 
  public set value(value: T | undefined) {
    this.storage.selected = value;
  }
 
  public getAnker(): ElementRef {
    return this.drawerSlot;
  }
 
  public getScrollTarget(): ElementRef<HTMLElement> | undefined {
    return this.elementRoot;
  }
 
  /** @ignore */
  public async _impl_onSelect(event: SelectionEvent): Promise<void> {
    if (typeof event.key === 'string' && !this._isDisabled(event.key)) {
      await this._impl_handleChange(event.key);
      this._impl_close(true);
    }
  }
 
  /** @ignore */
  public async _impl_handleChange(key: Key): Promise<void> {
    const value = this.storage.getValueByKey(key);
 
    const doChange = () => {
      if (value !== undefined) {
        this.value = value;
        this.valueChange.emit(value);
      }
    };
 
    if (this.permitValueChange !== undefined && this.value !== value) {
      const permit = this.permitValueChange(key);
      if (typeof permit !== 'boolean') {
        // permit is of type Promise<boolean>
        const proceed = await permit;
        if (proceed) {
          doChange();
        }
      } else if (permit) {
        // permit is of type boolean
        doChange();
      }
    } else if (this.permitValueChange === undefined && this.value !== value) {
      doChange();
    }
  }
 
  /** @ignore */
  protected _impl_onOpen(focusItem: KeyFocusTarget): KeyFocusTarget {
    // always open selected value if set
    if (this.value != undefined) {
      const key = this.storage.getItemByValue(this.value)?.key;
      if (key != undefined) {
        focusItem = key;
      }
    }
    return focusItem;
  }
 
  /** @ignore */
  protected _itemTrackBy(item: Item): string {
    return itemTrack(item);
  }
}