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

98.58% Statements 278/282
97.5% Branches 39/40
95.65% Functions 22/23
98.58% Lines 278/282

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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 2831x 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 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 565x 225x 565x 565x 256x 256x 256x 256x 256x 4716x 4716x 4716x 256x 256x 113x 5x 5x 113x 113x 256x 256x 256x 5212x 5212x 256x 256x 565x 58x 58x 565x 565x 256x 256x 5385x 5385x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 256x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x         13x 1x 1x 1x 223x 223x 223x 1x 1x 1x 129x 121x 129x 129x 1x 1x 1x 143x 143x 143x 1x 1x 1x 322x 322x 101x 101x 221x 221x 322x 322x 1x 1x 1x 50x 50x 50x 50x 50x 50x 50x 50x 43x 11x 11x 50x 50x 50x 1x 1x 1x 240x 99x 240x 240x 1x 1x 1x 221x 221x 221x 221x 221x 221x 221x 1x 1x 1x 63x 63x 57x 57x 53x 57x 57x 4x 63x 63x 63x 63x 63x 1x 1x 1x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  Component,
  ElementRef,
  EventEmitter,
  Input,
  Output,
} from '@angular/core';
 
import { playSoundIfLengthExceeded } from '../utils/internal-utils';
import {
  FocusableElementOwner,
  FormControlStatus,
  InvalidControlValue,
  Nullable,
} from '../utils/util.types';
import { SoundType, playSound } from '../utils/utilities';
 
/**
 * Base component for the DateInputComponent and TimeInputComponent.
 */
@Component({
  template: '',
  standalone: false,
})
export abstract class BaseDateTimeComponent implements FocusableElementOwner {
  /**
   * Marks the input as a required field. true === required
   */
  @Input()
  public required = false;
 
  /**
   * Label that appears inside the input field
   */
  @Input()
  public label?: string;
 
  /**
   * Decides if the control will be rendered in a readonly state.
   */
  @Input()
  public readonly = false;
 
  /**
   * Hint message that appears below the input field.
   */
  @Input()
  public hint = '';
 
  /**
   * Decides if the input will be rendered in a disabled/enabled state
   */
  @Input()
  public disabled = false;
 
  /**
   * If it has a specified value, the component will be rendered in a slot-element mode.
   * This enables the component to be used in
   * two-slot components (e.g. `gc-two-slot-label-container`) or search input fields.
   * @ignore
   */
  @Input()
  public _slotElementMode?:
    | 'two-slot-label-container'
    | 'ext-search-single-field'
    | 'ext-search-range-field';
 
  /**
   * Error message id used when `_slotElementMode` is enabled.
   * @ignore
   */
  @Input()
  public _errorMessageId?: string;
 
  /**
   * Info (hint) message id used when `_slotElementMode` is enabled.
   * @ignore
   */
  @Input()
  public _infoMessageId?: string;
 
  /**
   * Informs about the change of the picked value. Does not fire when updating the value programmatically, only on user inputs.
   */
  @Output()
  public readonly valueChange: EventEmitter<
    Nullable<string | InvalidControlValue>
  > = new EventEmitter<Nullable<string | InvalidControlValue>>();
 
  /**
   * Event fired when the control gains focus
   */
  @Output()
  public readonly onFocusEntered: EventEmitter<void> = new EventEmitter<void>();
 
  /**
   * Event fired when the control looses focus
   */
  @Output()
  public readonly onFocusLeft: EventEmitter<void> = new EventEmitter<void>();
 
  /**
   * Signals changes of the current (combined) status of the component, e.g. also reports errors triggered by internal
   * validation or input handling.
   */
  @Output()
  public readonly onStatusChange: EventEmitter<FormControlStatus> =
    new EventEmitter<FormControlStatus>(true);
 
  /**
   * Signals that the text value has changed. In contrast to "valueChange" this event is emitted immediately
   * and provides the current (unprocessed) text visible in the control
   */
  @Output()
  public readonly onInputChange: EventEmitter<string> =
    new EventEmitter<string>(true);
 
  /**
   * Current value of the input.
   */
  @Input()
  public get value(): Nullable<string | InvalidControlValue> {
    return this._value;
  }
 
  public set value(value: Nullable<string | InvalidControlValue>) {
    if (this._value !== value) {
      this._value = value;
    }
  }
 
  /**
   * Error message that appears below the input field.
   */
  @Input()
  public get errorMessage(): string {
    return this._errorMessage;
  }
 
  public set errorMessage(message: string) {
    if (this._errorMessage !== message) {
      this._errorMessage = message;
      this.emitStatusChange();
    }
  }
 
  /** @ignore */
  protected get combinedErrorMessage(): string {
    return this.internalErrorMessage || this.errorMessage;
  }
 
  protected set internalErrorMessage(message: string) {
    if (message !== this._internalErrorMessage) {
      this._internalErrorMessage = message;
      this.emitStatusChange();
    }
  }
 
  protected get internalErrorMessage(): string {
    return this._internalErrorMessage;
  }
 
  /** @ignore */
  protected _focused?: boolean;
 
  /** @ignore */
  protected text = '';
 
  /** @ignore */
  private _value: Nullable<string | InvalidControlValue> = null;
 
  /** @ignore */
  private _errorMessage = '';
 
  /** @ignore */
  private _internalErrorMessage = '';
 
  /**
   * @ignore
   * Helper to access static property in an Angular binding.
   */
  protected abstract get maxLength(): number;
 
  /** @ignore */
  protected abstract get inputElement(): ElementRef<HTMLInputElement>;
 
  public focusChild(): boolean {
    if (!this.disabled) {
      this.inputElement.nativeElement.focus();
      return true;
    }
    return false;
  }
 
  /** @ignore */
  protected _handleInputChange(evt: Event): void {
    this.text = (evt.target as HTMLInputElement).value;
    this.onInputChange.emit(this.text);
  }
 
  /** @ignore */
  protected async _handleChange(): Promise<void> {
    if (await this.updateValue(this.text)) {
      this.valueChange.emit(this.value);
    }
  }
 
  /** @ignore */
  protected _handleFocusEntered(): void {
    this._focused = true;
    this.onFocusEntered.next();
  }
 
  /** @ignore */
  protected _handleBeforeInput(inputEvent: InputEvent): void {
    if (
      inputEvent.inputType === 'insertText' ||
      inputEvent.inputType === 'insertFromPaste' ||
      inputEvent.inputType === 'insertFromDrop'
    ) {
      this.playSound(inputEvent);
    }
  }
 
  /** @ignore */
  protected _handleFocusLeave(): void {
    this._focused = false;
 
    this.onFocusLeft.next();
    // We need to wait a tick so that the control is back
    // in a sync state
    setTimeout(() => {
      if (
        this.combinedErrorMessage.length > 0 ||
        this._errorMessageId !== undefined
      ) {
        void playSound(SoundType.ERROR);
      }
    });
  }
 
  /** @ignore */
  protected _onKeyUp(event: KeyboardEvent): void {
    if (event.key === 'Enter') {
      this.inputElement.nativeElement.select();
    }
  }
 
  /** @ignore */
  protected playSound(inputEvent: InputEvent) {
    playSoundIfLengthExceeded(
      this.inputElement.nativeElement,
      this.text,
      this.maxLength,
      inputEvent,
    );
  }
 
  /** @ignore */
  private emitStatusChange() {
    const status: FormControlStatus = {};
    if (this.internalErrorMessage || this.errorMessage) {
      status.error = [];
      if (this.internalErrorMessage) {
        status.error.push(this.internalErrorMessage);
      }
      if (this.errorMessage) {
        status.error.push(this.errorMessage);
      }
    }
 
    this.onStatusChange.emit(status);
  }
 
  /** @ignore */
  protected abstract updateValue(text: string): Promise<boolean>;
}