All files / lib/time-input time-input.component.ts

98% Statements 196/200
94.87% Branches 37/39
100% Functions 18/18
98% Lines 196/200

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 2011x 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 3x 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 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 22x 22x 22x 80x 80x 180x 180x 22x 22x 158x 99x 99x 59x 59x 59x 180x 180x 180x 80x 80x 80x 80x 80x 80x 1439x 1439x 80x 80x 80x 64x 64x 80x 80x 80x 80x 80x 80x 80x 80x 80x 1x 1x 1x 1166x 1166x 1x 1x 1x 2332x 2332x 1x 1x 1x 1166x 1166x 1x 1x 1x 830x         830x 830x 830x 830x 830x 1x 1x 1x 22x 22x 22x 22x 22x 22x 16x 16x 11x 5x 5x 5x 5x 5x 16x 16x 16x 6x 6x 6x 6x 22x 22x 22x 22x 22x 1x 1x 22x 22x 22x 22x 22x 22x 10x 12x 22x 22x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import { Component, ElementRef, Input, ViewChild } from '@angular/core';
 
import { computeErrorTextPrefix } from '../utils/internal-utils';
import {
  InvalidControlValue,
  Nullable,
  WidgetKeySet,
} from '../utils/util.types';
import { createWidgetKeySet, isInvalidControlValue } from '../utils/utilities';
 
import { BaseDateTimeComponent } from './base-date-time.component';
 
const TIME_NONEXISTENT_ERROR_TEXT = 'Uhrzeit existiert nicht';
const TIME_INVALID_FORMAT_ERROR_TEXT = 'Falsches Eingabeformat';
 
//check if time is written in the correct format but is not an exsisant time. for example 25:61
const VALID_FORMAT = /^\d{2}:\d{2}$/;
const VALID_RANGE_FORMAT = /^(0\d|1\d|2[0-3]):[0-5]\d$/;
 
const SUPPORTED_FORMATS = [
  {
    format: 'hh:mm',
    regex: /^\d{2}:\d{2}$/,
    toFormattedTime: (text: string) => text,
  },
  {
    format: 'hhmm',
    regex: /^\d{4}$/,
    toFormattedTime: (text: string) =>
      text[0] + text[1] + ':' + text[2] + text[3],
  },
  {
    format: 'h:mm',
    regex: /^\d:\d{2}$/,
    toFormattedTime: (text: string) => '0' + text,
  },
  {
    format: 'h',
    regex: /^\d$/,
    toFormattedTime: (text: string) => '0' + text + ':' + '00',
  },
  {
    format: 'hh',
    regex: /^\d{2}$/,
    toFormattedTime: (text: string) => text + ':' + '00',
  },
  {
    format: 'hmm',
    regex: /^\d{3}$/,
    toFormattedTime: (text: string) => '0' + text[0] + ':' + text[1] + text[2],
  },
];
 
/**
 * A Time input component allows users to enter a specific time value.
 */
@Component({
  selector: 'gc-time-input',
  templateUrl: './time-input.component.html',
  styleUrls: [
    '../base-range-input-field/base-slot-mode.css',
    './time-input.component.css',
  ],
})
export class TimeInputComponent extends BaseDateTimeComponent {
  /**
   * @ignore
   * maximum input length - equals the length of a fully formatted (local) time
   */
  private static readonly INPUT_MAX_LENGTH = 5;
 
  /**
   * Overrides the default text which is shown when the control is readonly and empty.
   */
  @Input()
  public readonlyEmptyValue = '';
 
  /** @ignore */
  @ViewChild('input')
  protected _inputElement!: ElementRef<HTMLInputElement>;
 
  /**
   * Current value of the time input, in HH:mm format.
   */
  @Input()
  public override get value(): Nullable<string | InvalidControlValue> {
    return super.value;
  }
 
  public override set value(value: Nullable<string | InvalidControlValue>) {
    super.value = value;
    if (isInvalidControlValue(value)) {
      this.text = value.rawValue;
      this.internalErrorMessage = this.generateErrorMsg(value.rawValue);
    } else if (value !== null) {
      this.text = value;
      this.internalErrorMessage = '';
    } else {
      this.text = '';
      this.internalErrorMessage = '';
    }
    this.onInputChange.emit(this.text);
  }
 
  /**
   * @ignore
   * Helper to access static property in an Angular binding.
   */
  protected get maxLength() {
    return TimeInputComponent.INPUT_MAX_LENGTH;
  }
 
  /** @ignore */
  protected get inputElement(): ElementRef<HTMLInputElement> {
    return this._inputElement;
  }
 
  /** @ignore */
  protected readonly _widgetKeys: WidgetKeySet = createWidgetKeySet('gc-time');
 
  /**
   * displayed on readonly when readonlyEmptyValue is empty
   * @ignore
   */
  protected readonly READONLY_DEFAULT_EMPTY_VALUE = '\u2013';
 
  /** @ignore */
  protected getLabelId(): string {
    return this._widgetKeys.labelKey;
  }
 
  /** @ignore */
  protected getInputId(): string {
    return this._widgetKeys.widgetKey;
  }
 
  /** @ignore */
  protected computeAriaDescribedBy(): string | null {
    return this._infoMessageId ?? null;
  }
 
  /** @ignore */
  protected sizeHelperText(): string {
    if (this.readonly && this.text.length === 0) {
      if (this.readonlyEmptyValue === '') {
        return this.READONLY_DEFAULT_EMPTY_VALUE;
      } else {
        return this.readonlyEmptyValue;
      }
    } else {
      return this.text;
    }
  }
 
  /** @ignore */
  protected updateValue(text: string): void {
    if (text === '') {
      this.value = null;
    } else {
      const timeFormat = SUPPORTED_FORMATS.find(i => i.regex.exec(text)?.input);
 
      if (timeFormat) {
        const result = timeFormat.toFormattedTime(text);
        if (VALID_RANGE_FORMAT.test(result)) {
          this.value = result;
        } else {
          this.value = {
            kind: 'InvalidControlInput',
            rawValue: result,
            errorMessage: TIME_NONEXISTENT_ERROR_TEXT,
          };
        }
      } else {
        this.value = {
          kind: 'InvalidControlInput',
          rawValue: text,
          errorMessage: TIME_INVALID_FORMAT_ERROR_TEXT,
        };
        // error message updated automatically in 'value' setter
      }
    }
  }
 
  private generateErrorMsg(inputValue: string): string {
    const errorPrefix = computeErrorTextPrefix(
      this.label,
      this._slotElementMode,
      'Wert',
    );
    const errorMessage = VALID_FORMAT.test(inputValue)
      ? TIME_NONEXISTENT_ERROR_TEXT
      : TIME_INVALID_FORMAT_ERROR_TEXT;
    return errorPrefix + errorMessage;
  }
}