All files / lib/ext-search/base-ext-search base-ext-search-range-field.component.ts

87.58% Statements 261/298
61.9% Branches 26/42
77.77% Functions 14/18
87.58% Lines 261/298

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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 2991x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 10644x 10644x 10644x 28x 28x 34x 34x 34x 34x 34x       34x 34x 28x 28x 28x 1306x 1306x 1306x       1306x 1306x 1306x 1306x 1306x 1306x 1306x 1306x 28x 28x 28x 1306x 1306x 1306x       1306x 1306x 1306x 1306x 1306x 1306x 1306x 1306x 28x 28x 28x 162x 162x 28x 28x 28x     28x 28x 28x     28x 28x 28x 1306x 1306x 1306x 1252x 1252x 54x 54x 1252x 1252x   1252x 1306x   1306x 1306x 1306x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 1x 1x 1x 1x 1x 1x 3986x 3986x 1x 1x 1x 2680x 2680x 2680x 2680x 1x 1x 1x             1x 1x 1x             1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 6x 6x   6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 6x   6x 6x 6x 6x 6x 6x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 34x 34x 34x 34x 34x 34x 34x   34x 34x   34x 34x 34x       34x 34x 34x       34x 34x 34x 34x 6x 6x 34x 34x 34x 34x 1x 1x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import { Directive, EventEmitter, Input, Output } from '@angular/core';
 
import {
  FocusableElementOwner,
  FormControlStatus,
  RangeInputSlots,
} from '../../utils/util.types';
 
import {
  RangeErrors,
  RangeInputSlotsFull,
  RangeInputSlotsWithStatus,
  isBothRangeErrors,
  isIndividualRangeErrors,
} from '../../base-range-input-field/base-range-input-field.component';
import { BaseExtSearchComponent } from './base-ext-search-field.component';
 
@Directive()
export abstract class BaseExtSearchRangeComponent
  extends BaseExtSearchComponent
  implements FocusableElementOwner
{
  /**
   * Sets the labels of the slots.
   * - "start": sets the inner label of the start slot
   * - "end": sets the inner label of the end slot
   * - "both": sets the outer label of both slots (whole component)
   */
  @Input({ required: true })
  public label: Record<RangeInputSlotsFull, string> = {
    both: '',
    start: '',
    end: '',
  };
 
  /**
   * 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<RangeInputSlotsWithStatus> =
    new EventEmitter<RangeInputSlotsWithStatus>();
 
  /**
   * Event fired when a slot gains focus. Outputs information about focused slot.
   */
  @Output()
  public readonly onSlotFocusEntered: EventEmitter<RangeInputSlots> =
    new EventEmitter<RangeInputSlots>();
 
  /**
   * Event fired when a slot loses focus. Outputs information about the slot that lost focus.
   */
  @Output()
  public readonly onSlotFocusLeft: EventEmitter<RangeInputSlots> =
    new EventEmitter<RangeInputSlots>();
 
  /**
   * Optional error message for the slots.
   *  First option:
   *  - "start": error message that refers to the start slot
   *  - "end": error message that refers to the end slot
   *  Second option:
   *  - "both": error message that refers to both slots
   */
  @Input()
  public get errorMessage(): RangeErrors {
    return this._errors;
  }
 
  public set errorMessage(value: RangeErrors) {
    this._errors = value;
    this.onStatusChange.emit(this.combineAllErrorStatuses());
    if (this._isBothRangeErrors(value)) {
      this.startSlotHasExternalError = value.both.length > 0;
      this.endSlotHasExternalError = value.both.length > 0;
    } else {
      this.startSlotHasExternalError = value.start.length > 0;
      this.endSlotHasExternalError = value.end.length > 0;
    }
  }
 
  /** @ignore */
  protected get startSlotErrorMessageId(): string | undefined {
    if (this._isBothRangeErrors(this.errorMessage)) {
      if (this.errorMessage.both.length > 0) {
        return this.bothSlotErrorsMessageIdString;
      }
    } else if (this.errorMessage.start.length > 0) {
      return this.startErrorMessageIdString;
    }
 
    if (this.startSlotHasInternalError) {
      return this.startErrorMessageIdString;
    }
 
    return undefined;
  }
 
  /** @ignore */
  protected get endSlotErrorMessageId(): string | undefined {
    if (this._isBothRangeErrors(this.errorMessage)) {
      if (this.errorMessage.both.length > 0) {
        return this.bothSlotErrorsMessageIdString;
      }
    } else if (this.errorMessage.end.length > 0) {
      return this.endErrorMessageIdString;
    }
 
    if (this.endSlotHasInternalError) {
      return this.endErrorMessageIdString;
    }
 
    return undefined;
  }
 
  /** @ignore */
  protected get bothSlotErrorsMessageIdString(): string {
    return this._widgetKeys.errorKey + '-both';
  }
 
  /** @ignore */
  protected get startErrorMessageIdString(): string {
    return this._widgetKeys.errorKey + '-start';
  }
 
  /** @ignore */
  protected get endErrorMessageIdString(): string {
    return this._widgetKeys.errorKey + '-end';
  }
 
  /** @ignore */
  protected get actualErrorSlots(): RangeInputSlotsFull | undefined {
    if (
      (this.startSlotHasInternalError && this.endSlotHasInternalError) ||
      (this.startSlotHasExternalError && this.endSlotHasExternalError) ||
      (this.startSlotHasExternalError && this.endSlotHasInternalError) ||
      (this.startSlotHasInternalError && this.endSlotHasExternalError)
    ) {
      return 'both';
    }
    if (this.startSlotHasInternalError || this.startSlotHasExternalError) {
      return 'start';
    }
    if (this.endSlotHasInternalError || this.endSlotHasExternalError) {
      return 'end';
    }
    return undefined;
  }
 
  /** @ignore */
  protected startSlotInternalError = '';
 
  /** @ignore */
  protected endSlotInternalError = '';
 
  /** @ignore */
  protected startSlotHasInternalError = false;
 
  /** @ignore */
  protected endSlotHasInternalError = false;
 
  /** @ignore */
  protected focusedSlot?: 'start' | 'end';
 
  /** @ignore */
  private _errors: RangeErrors = { start: '', end: '', both: '' };
 
  /** @ignore */
  private startSlotHasExternalError = false;
 
  /** @ignore */
  private endSlotHasExternalError = false;
 
  /** @ignore */
  private lastFocusedSlot?: RangeInputSlots;
 
  /** @ignore */
  protected _isBothRangeErrors(val: RangeErrors): val is { both: string } {
    return isBothRangeErrors(val);
  }
 
  /** @ignore */
  protected _isIndividualRangeErrors(
    val: RangeErrors,
  ): val is { start: string; end: string } {
    return isIndividualRangeErrors(val);
  }
 
  /** @ignore */
  protected startSlotStatusHandler(s: FormControlStatus) {
    this.startSlotInternalError = s.error ? s.error[0] : '';
    this.startSlotHasInternalError = s.error !== undefined;

    const errors: RangeInputSlotsWithStatus = this.combineAllErrorStatuses();
    this.onStatusChange.emit(errors);
  }
 
  /** @ignore */
  protected endSlotStatusHandler(s: FormControlStatus) {
    this.endSlotInternalError = s.error ? s.error[0] : '';
    this.endSlotHasInternalError = s.error !== undefined;

    const errors: RangeInputSlotsWithStatus = this.combineAllErrorStatuses();
    this.onStatusChange.emit(errors);
  }
 
  /** @ignore */
  protected handleStartSlotFocusEntered(): void {
    if (this.lastFocusedSlot === undefined) {
      this.onFocusEntered.emit();
    }
 
    this.focusedSlot = 'start';
    this.lastFocusedSlot = 'start';
    this.onSlotFocusEntered.emit('start');
  }
 
  /** @ignore */
  protected handleStartSlotFocusLeft(): void {
    setTimeout(() => {
      if (this.focusedSlot === undefined) {
        this.lastFocusedSlot = undefined;
        this.onFocusLeft.emit();
      }
    });
 
    this.focusedSlot = undefined;
    this.onSlotFocusLeft.emit('start');
  }
 
  /** @ignore */
  protected handleEndSlotFocusEntered(): void {
    if (this.lastFocusedSlot === undefined) {
      this.onFocusEntered.emit();
    }
 
    this.focusedSlot = 'end';
    this.lastFocusedSlot = 'end';
    this.onSlotFocusEntered.emit('end');
  }
 
  /** @ignore */
  protected handleEndSlotFocusLeft(): void {
    setTimeout(() => {
      if (this.focusedSlot === undefined) {
        this.onFocusLeft.emit();
        this.lastFocusedSlot = undefined;
      }
    });
 
    this.focusedSlot = undefined;
    this.onSlotFocusLeft.emit('end');
  }
 
  /** @ignore */
  private combineAllErrorStatuses(): RangeInputSlotsWithStatus {
    const errors: RangeInputSlotsWithStatus = {
      start: { error: [] },
      end: { error: [] },
      both: { error: [] },
    };
 
    if (this.startSlotInternalError.length > 0) {
      errors.start.error?.push(this.startSlotInternalError);
    }
    if (this.endSlotInternalError.length > 0) {
      errors.end.error?.push(this.endSlotInternalError);
    }
    if (
      this._isIndividualRangeErrors(this.errorMessage) &&
      this.errorMessage.start.length > 0
    ) {
      errors.start.error?.push(this.errorMessage.start);
    }
    if (
      this._isIndividualRangeErrors(this.errorMessage) &&
      this.errorMessage.end.length > 0
    ) {
      errors.end.error?.push(this.errorMessage.end);
    }
    if (
      this._isBothRangeErrors(this.errorMessage) &&
      this.errorMessage.both.length > 0
    ) {
      errors.both.error?.push(this.errorMessage.both);
    }
 
    return errors;
  }
 
  public abstract focusRangeSlot(slot: 'start' | 'end'): boolean;
}