All files / lib/switch switch.component.ts

94.05% Statements 95/101
94.11% Branches 16/17
88.88% Functions 8/9
94.05% Lines 95/101

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 1021x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 1x 1x 1x         1x 1x 1x 1x 1x 15x 12x 12x 12x 15x 15x 8x 5x 5x 7x 7x 7x 15x 15x 1x 1x 1x 1x 1x     1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import { Component, ElementRef, input, model, viewChild } from '@angular/core';
 
import { FocusableElementOwner } from '../utils/util.types';
 
export type SwitchContentDisplay = 'text-left' | 'text-right';
 
/**
 * Switch component that allows users to quickly activate or deactivate a function.
 */
@Component({
  selector: 'gc-switch',
  templateUrl: './switch.component.html',
  styleUrls: ['./../utils/styles/reset-button.css', './switch.component.css'],
  standalone: false,
})
export class SwitchComponent implements FocusableElementOwner {
  /**
   * @ignore
   */
  public _inputElement =
    viewChild.required<ElementRef<HTMLInputElement>>('input');
 
  /**
   * Text that will show near the switch.
   */
  public label = input.required<string>();
 
  /**
   * Decides if the switch will be rendered in a disabled/enabled state.
   */
  public disabled = input(false);
 
  /**
   * Decides if the switch will be rendered in a readonly state.
   */
  public readonly = input(false);
 
  /**
   * Decides on which side of the switch the text is shown.
   */
  public contentDisplay = input<SwitchContentDisplay>('text-right');
 
  /**
   * Enables setting of the value only when the Promise resolves with value "true".
   */
  public permitValueChange =
    input<(v: boolean) => Promise<boolean> | boolean>();
 
  /**
   * Current value of the switch.
   */
  public value = model(false);
 
  /**
   * @ignore
   */
  public _hasFocus = false;
 
  /**
   * @ignore
   */
  private _value = false;
 
  public focusChild(): boolean {
    if (!this.disabled()) {
      this._inputElement().nativeElement.focus();
      return true;
    } else {
      return false;
    }
  }
 
  /** @ignore */
  public async _handleClick(): Promise<void> {
    const doChange = () => {
      this.value.update(val => !val);
      this._inputElement().nativeElement.focus();
    };
    const permitValueChange = this.permitValueChange();
    if (permitValueChange) {
      const permit = await permitValueChange(!this.value());
      if (permit) {
        doChange();
      }
    } else {
      doChange();
    }
  }
 
  /**
   * @ignore
   */
  public _setFocus(focused: boolean) {
    this._hasFocus = focused;
  }
}