All files / lib/switch switch.component.ts

95.2% Statements 119/125
94.11% Branches 16/17
88.88% Functions 8/9
95.2% Lines 119/125

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 1261x 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 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 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 {
  ChangeDetectionStrategy,
  Component,
  ElementRef,
  input,
  model,
  viewChild,
} from '@angular/core';
 
import { FocusableElementOwner } from '../utils/util.types';
import { createWidgetKeySet } from '../utils/utilities';
 
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'],
  changeDetection: ChangeDetectionStrategy.Default,
  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>();
 
  /**
   * Hint message that appears under a switch.
   */
  public readonly hint = input('');
 
  /**
   * 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
   */
  protected readonly _widgetKeys = createWidgetKeySet('gc-switch');
 
  /**
   * @ignore
   */
  protected readonly _hintId = this._widgetKeys.baseKey + '-hint';
 
  /**
   * @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;
  }
}