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

98.3% Statements 232/236
95.83% Branches 46/48
100% Functions 19/19
98.3% Lines 232/236

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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 2371x 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 8x 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 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 64x 64x 64x 90x 90x 207x 207x 207x 207x 22x 22x 185x 113x 113x 72x 72x 72x 207x 207x 207x 72x 207x 207x 90x 90x 90x 90x 90x 90x 1700x 1700x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 1x 1x 1x 1402x 1402x 1x 1x 1x 2804x 2804x 1x 1x 1x 1402x 1402x 1x 1x 1x 764x         764x 764x 764x 764x 764x 1x 1x 1x 33x 33x 33x 2x 32x 33x 6x 6x 3x 3x 3x 3x 3x 3x 3x 3x 25x 25x 33x 33x 1x 1x 1x 33x 33x 33x 33x 33x 33x 27x 27x 22x 5x 5x 5x 5x 5x 27x 27x 27x 6x 6x 6x 6x 33x 33x 33x 33x 1x 1x 33x 33x 33x 33x 33x 33x 15x 18x 33x 33x 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 = 'Keine gültige Uhrzeit';
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],
  },
];
 
type ValueType = Nullable<string | InvalidControlValue>;
 
/**
 * 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',
  ],
  standalone: false,
})
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 = '';
 
  /**
   * Enables setting of the value only when the Promise resolves with value "true".
   */
  @Input()
  public permitValueChange?: (v: ValueType) => Promise<boolean> | boolean;
 
  /** @ignore */
  @ViewChild('input')
  protected _inputElement!: ElementRef<HTMLInputElement>;
 
  /**
   * Current value of the time input, in HH:mm format.
   */
  @Input()
  public override get value(): ValueType {
    return super.value;
  }
 
  public override set value(value: ValueType) {
    super.value = value;
    const currentText = this.text;
 
    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 = '';
    }
 
    if (this.text !== currentText) {
      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 async updateValue(text: string): Promise<boolean> {
    const value = this.computeValueFromInput(text);
    if (value === this.value) {
      this.value = value; // ensure (re-)formatting of entered text
      return false;
    }
    if (this.permitValueChange) {
      if (await this.permitValueChange(value)) {
        this.value = value;
        return true;
      } else {
        const oldValue = this.value;
        // call the setter to restore the old visual state
        this.value = oldValue;
        return false;
      }
    } else {
      this.value = value;
      return true;
    }
  }
 
  /** @ignore */
  private computeValueFromInput(text: string): ValueType {
    if (text === '') {
      return 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)) {
          return result;
        } else {
          return {
            kind: 'InvalidControlInput',
            rawValue: result,
            errorMessage: this.generateErrorMsg(result),
          };
        }
      } else {
        return {
          kind: 'InvalidControlInput',
          rawValue: text,
          errorMessage: this.generateErrorMsg(text),
        };
      }
    }
  }
 
  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;
  }
}