All files / lib/checkbox checkbox.component.ts

100% Statements 208/208
100% Branches 26/26
100% Functions 10/10
100% Lines 208/208

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 2091x 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 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 1x 61183x 61183x 1x 1x 1378x 1378x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 72x 72x 72x 72x 72x 1x 1x 1x 1x 1x 183x 183x 1x 1x 1x 21307x 21307x 3904x 304x 304x 21307x 21307x 1x 1x 1x 19776x 19776x 1x 1x 1x 1531x 1531x 1531x 1531x 1531x 1531x 1x 1531x 1531x 1531x 1531x 1x 1x 1x 19776x 19776x 19776x 19776x 1x 19775x 19771x 19771x 19770x 19770x 19770x 19776x 19776x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterViewChecked,
  AfterViewInit,
  Component,
  ElementRef,
  EventEmitter,
  Input,
  NgZone,
  OnInit,
  Output,
  ViewChild,
} from '@angular/core';
 
import { FocusableElementOwner, WidgetKeySet } from '../utils/util.types';
import { createWidgetKeySet } from '../utils/utilities';
 
import { TooltipComponent } from '../internal/tooltip/tooltip.component';
 
/**
 * A Checkbox element that allows the user to mark a value as selected.
 */
@Component({
  selector: 'gc-checkbox',
  templateUrl: './checkbox.component.html',
  styleUrls: ['./checkbox.component.css'],
})
export class CheckboxComponent
  implements FocusableElementOwner, OnInit, AfterViewInit, AfterViewChecked
{
  /**
   * Custom ID to use
   */
  @Input()
  public id?: string;
 
  /**
   * Hides the label of the checkbox. May be used for embedded rendering like in tables.
   */
  @Input()
  public hideLabel = false;
 
  /**
   * @ignore
   */
  @ViewChild('input')
  public _inputElement!: ElementRef<HTMLInputElement>;
 
  /**
   * @ignore
   */
  @ViewChild('tooltip')
  public tooltip?: TooltipComponent;
 
  /**
   * Text that will show near the checkbox
   */
  @Input()
  public label = '';
 
  /**
   * Decides if the checkbox will be rendered in a disabled/enabled state
   */
  @Input()
  public disabled = false;
 
  /**
   * Decides if the checkbox will be rendered in a readonly state
   */
  @Input()
  public readonly = false;
 
  /**
   * An error message that shows under a checkbox
   */
  @Input()
  public errorMessage = '';
 
  /**
   * Hint message that appears under a checkbox
   */
  @Input()
  public hint = '';
 
  /**
   * @ignore
   * Forces the checkbox to use error styling (independent of the error message)
   */
  @Input()
  public _useErrorStyle = false;
 
  /**
   * Marks the input field as required.
   */
  @Input()
  public required = false;
 
  /**
   * Inform about the change of the selection state
   */
  @Output()
  public readonly valueChange: EventEmitter<boolean> =
    new EventEmitter<boolean>();
 
  @Input()
  public get value(): boolean {
    return this._value;
  }
 
  public set value(value: boolean) {
    this._value = value;
  }
 
  /**
   * @ignore
   */
  public _hasFocus = false;
 
  /**
   * @ignore
   */
  public readonly _widgetKeys: WidgetKeySet = createWidgetKeySet('checkbox');
 
  /**
   * @ignore
   */
  protected readonly _hintId = this._widgetKeys.baseKey + '-hint';
 
  /**
   * @ignore
   */
  private _value = false;
 
  public constructor(private readonly zone: NgZone) {}
 
  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;
  }
 
  /** @ignore */
  public ngAfterViewInit(): void {
    if (
      this.tooltip !== undefined &&
      this.tooltip.tooltipOwner !== this._inputElement
    ) {
      this.tooltip.tooltipOwner = this._inputElement;
    }
  }
 
  /** @ignore */
  public ngAfterViewChecked(): void {
    this.ngAfterViewInit();
  }
 
  /** @ignore */
  public ngOnInit(): void {
    // browser may "restore" previous values after navigating (e.g. with browser's "back" button),
    // potentially leading to an inconsistent state as no events are fired which could be used to
    // detect this (observed on Chromium) -> sync back state to the native element after it's rendered
    this.zone.runOutsideAngular(() => {
      setTimeout(() => {
        if (this.value !== this._inputElement.nativeElement.checked) {
          this._inputElement.nativeElement.checked = this.value;
        }
      });
    });
  }
 
  /** @ignore */
  protected computeAriaDescribedBy(): string | null {
    const errId =
      this.errorMessage.length > 0 ? this._widgetKeys.errorKey : null;
    const hintId = this.hint.length > 0 ? this._hintId : null;
    if (errId && hintId) {
      return hintId + ' ' + errId;
    } else if (errId) {
      return errId;
    } else if (hintId) {
      return hintId;
    } else {
      return null;
    }
  }
}