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

100% Statements 280/280
100% Branches 33/33
94.73% Functions 18/19
100% Lines 280/280

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 2811x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 110x 110x 110x 238x 238x 519x 214x 519x 519x 238x 238x 238x 238x 238x 4397x 4397x 4397x 238x 238x 111x 5x 5x 111x 111x 238x 238x 238x 4882x 4882x 238x 238x 519x 57x 57x 519x 519x 238x 238x 5052x 5052x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 1x 1x 1x 176x 176x 176x 1x 1x 1x 110x 110x 110x 1x 1x 1x 125x 125x 125x 1x 1x 1x 266x 266x 92x 266x 174x 174x 266x 266x 1x 1x 1x 34x 34x 34x 34x 34x 34x 34x 34x 34x 11x 34x 34x 34x 34x 1x 1x 1x 179x 90x 179x 179x 1x 1x 1x 174x 174x 174x 174x 174x 174x 174x 1x 1x 1x 62x 62x 56x 56x 52x 56x 56x 4x 62x 62x 62x 62x 62x 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: '',
})
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 _handleChange(): void {
    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): void;
}