All files / lib/switch switch.component.ts

98.18% Statements 108/110
100% Branches 11/11
87.5% Functions 7/8
98.18% Lines 108/110

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 1111x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 154x 154x 154x 8x 8x 19x 19x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x     1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  Component,
  ElementRef,
  EventEmitter,
  Input,
  Output,
  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'],
})
export class SwitchComponent implements FocusableElementOwner {
  /**
   * @ignore
   */
  @ViewChild('input')
  public _inputElement!: ElementRef<HTMLInputElement>;
 
  /**
   * Text that will show near the switch.
   */
  @Input()
  public label = '';
 
  /**
   * Decides if the switch will be rendered in a disabled/enabled state.
   */
  @Input()
  public disabled = false;
 
  /**
   * Decides if the switch will be rendered in a readonly state.
   */
  @Input()
  public readonly = false;
 
  /**
   * Decides on which side of the switch the text is shown.
   */
  @Input()
  public contentDisplay: SwitchContentDisplay = 'text-right';
 
  /**
   * Inform about the change of the selection state.
   */
  @Output()
  public readonly valueChange: EventEmitter<boolean> =
    new EventEmitter<boolean>();
 
  /**
   * Current value of the switch.
   */
  @Input()
  get value(): boolean {
    return this._value;
  }
 
  set value(value: boolean) {
    this._value = value;
  }
 
  /**
   * @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 _handleClick(): void {
    this._inputElement.nativeElement.focus();
 
    this.value = !this.value;
    this.valueChange.emit(this.value);
  }
 
  /**
   * @ignore
   */
  public _setFocus(focused: boolean) {
    this._hasFocus = focused;
  }
}