All files / lib/replaceable-text replaceable-text.component.ts

100% Statements 91/91
100% Branches 11/11
100% Functions 4/4
100% Lines 91/91

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 921x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 2x 2x 2x 2x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  Component,
  EventEmitter,
  HostBinding,
  Input,
  NgZone,
  Output,
  ViewChild,
} from '@angular/core';
 
import { FocusableElementOwner } from '../utils/util.types';
 
import { TextInputComponent } from '../text-input-field/text-input-field.component';
 
/**
 * Displays a read-only value and provides the option to enter an alternative override value.
 * The input component to enter an override is supplied in the component's content.
 *
 * This component does not move focus when the input field becomes visible, such functionality
 * must be implemented by the user of the component if needed (the input is simply projected by this
 * component, hence it is not aware of any details about the inserted input structure).
 */
@Component({
  selector: 'gc-replaceable-text',
  templateUrl: './replaceable-text.component.html',
  styleUrls: ['./replaceable-text.component.css'],
})
export class ReplaceableTextComponent implements FocusableElementOwner {
  /** The (readonly) value */
  @Input()
  public value = '';
 
  /** Label shown for the (readonly) value. */
  @Input()
  public label = '';
 
  /**
   * Indicates if an override is present, if `true` the input field is shown.
   */
  @Input()
  @HostBinding('class.gc-replaceable-has-override')
  public hasOverride = false;
 
  /**
   * Label shown for the override value.
   */
  @Input()
  public overrideLabel = '';
 
  /**
   * Informs about changes of the `hasOverride` property, i.e. when the user
   * adds or removes the override value.
   */
  @Output()
  public readonly hasOverrideChange: EventEmitter<boolean> =
    new EventEmitter<boolean>();
 
  /** @ignore */
  @ViewChild('readOnlyInput')
  private readOnlyInputField?: TextInputComponent;
 
  public constructor(private readonly ngZone: NgZone) {}
 
  /** @ignore */
  public _internalChangeHasOverride(hasOverride: boolean) {
    this.hasOverride = hasOverride;
    this.hasOverrideChange.emit(hasOverride);
 
    if (!hasOverride) {
      this.ngZone.runOutsideAngular(() => {
        setTimeout(() => {
          this.readOnlyInputField?.focusChild();
        });
      });
    }
  }
 
  public focusChild(): boolean {
    if (this.readOnlyInputField) {
      this.readOnlyInputField.focusChild();
      return true;
    } else {
      return false;
    }
  }
}