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

98.65% Statements 368/373
92.3% Branches 36/39
95.83% Functions 23/24
98.65% Lines 368/373

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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 3741x 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 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 478x 478x 478x 1x 1x 27x 27x 27x 1x 1x 1x 1x 1x 22x 22x 4x 4x 22x 22x 1x 1x 750x 750x 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 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 38x 28x 10x 10x 38x 38x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 12x 12x 1x 1x 1x 39x 39x 39x 39x 39x 1x 1x 1x 1x 1x 9x 9x 9x 1x 9x 9x 1x 1x 1x 1x 1x 238x 220x 238x 238x 238x 1x 1x 1x 1x 1x 38x 38x       38x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 1x 1x 1x 1x 1x 18x 18x 18x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x 18x 18x 1x 1x 1x 1x 1x 18x 18x 1x 1x 1x 1x 1x 20x 20x 1x 1x 1x 1x 1x 18x 18x 18x 18x 1x 1x 1x 1x 1x 38x 38x 38x 38x 38x 1x 1x 1x 4x 4x 3x 4x 4x 4x 4x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterViewInit,
  Component,
  ElementRef,
  EventEmitter,
  Input,
  NgZone,
  OnDestroy,
  Output,
  ViewChild,
} from '@angular/core';
 
import {
  observeSize,
  playSoundIfLengthExceeded,
  subscribeDocument,
} from '../utils/internal-utils';
import {
  Callback,
  FocusableElementOwner,
  FormControlStatus,
} from '../utils/util.types';
import { SoundType, playSound } from '../utils/utilities';
 
import { FocusScopeDirective } from '../internal/focus-scope.directive';
 
/**
 * A Text Area Input element that allows users to enter a multiple lines of text value.
 */
@Component({
  selector: 'gc-text-area',
  templateUrl: './text-area.component.html',
  styleUrls: ['./text-area.component.css'],
})
export class TextAreaComponent
  implements AfterViewInit, OnDestroy, FocusableElementOwner
{
  /**
   * Custom ID to use.
   */
  @Input()
  public id?: string;
 
  /**
   * The label that shows on the text area input.
   */
  @Input()
  public label = '';
 
  /**
   * Decides if the control will be rendered in a readonly state.
   */
  @Input()
  public readonly = false;
 
  /**
   * If set to true the state of the input is set to disabled which means that it is no longer possible to type text inside the text area input.
   */
  @Input()
  public disabled = false;
 
  /**
   * Marks the text area input field as required.
   */
  @Input()
  public required = false;
 
  /**
   * Hint message that appears below the text area input field.
   */
  @Input()
  public hint = '';
 
  /**
   * Placeholder text that appears inside of the text area field.
   */
  @Input()
  public placeholder = '';
 
  /**
   * Max number of chars allowed inside that text area input field.
   */
  @Input()
  public maxLength?: number;
 
  /**
   * Inform about the change of the value.
   */
  @Output()
  readonly valueChange: EventEmitter<string> = new EventEmitter<string>();
 
  /**
   * Signals changes of the current status of the component, e.g. also reports errors triggered by input handling.
   */
  @Output()
  public readonly onStatusChange: EventEmitter<FormControlStatus> =
    new EventEmitter<FormControlStatus>(true);
 
  /**
   * @ignore
   */
  @ViewChild(FocusScopeDirective)
  public focusScope?: FocusScopeDirective;
 
  /**
   * @ignore
   */
  @ViewChild('mirrorNode')
  public mirrorNode!: ElementRef<HTMLParagraphElement>;
 
  /**
   * @ignore
   */
  @ViewChild('textarea')
  public textareaElement!: ElementRef<HTMLInputElement>;
 
  /**
   * Event fired when the control looses focus
   */
  @Output()
  public readonly onFocusLeft: EventEmitter<void> = new EventEmitter<void>();
 
  /**
   * The value of the text area.
   */
  @Input()
  get value(): string {
    return this._value;
  }
 
  set value(text: string) {
    this._value = text;
    this.updateMirrorValue(text);
  }
 
  /**
   * Error message that appears below the text area input field.
   */
  @Input()
  public set errorMessage(message: string) {
    if (this._errorMessage !== message) {
      this._errorMessage = message;
      this.emitStatusChange();
    }
  }
 
  public get errorMessage(): string {
    return this._errorMessage;
  }
 
  /**
   * @ignore
   */
  _value = '';
 
  /**
   * @ignore
   */
  _focused = false;
 
  /**
   * @ignore
   */
  _mirrorValue = '';
 
  /** @ignore */
  private _errorMessage = '';
 
  /** @ignore */
  private eventListenerCleanup: Callback[] = [];
 
  /**
   * @ignore
   */
  private lastPressedKey?: string;
 
  constructor(private zone: NgZone) {}
 
  /**
   * @ignore
   */
  ngAfterViewInit(): void {
    this.updateMirrorNodeWidth();
    this.eventListenerCleanup.push(
      observeSize(
        this.mirrorNode,
        this.zone,
        this.onMirrorNodeResizeCallback.bind(this),
      ),
      observeSize(
        this.textareaElement,
        this.zone,
        this.onTextAreaResizeCallback.bind(this),
      ),
    );
    this.initializeClickListener();
    this.initializeKeyDownListener();
  }
 
  /**
   * @ignore
   */
  ngOnDestroy(): void {
    this.eventListenerCleanup.forEach(cleanup => cleanup());
  }
 
  focusChild(): boolean {
    if (!this.disabled) {
      this.textareaElement.nativeElement.focus();
      return true;
    } else {
      return false;
    }
  }
 
  /**
   * @ignore
   */
  _length(): number {
    if (this._mirrorValue) {
      return this._mirrorValue.length;
    } else {
      return 0;
    }
  }
 
  /**
   * @ignore
   */
  public _onChange(event: Event): void {
    this.value = (event.target as HTMLTextAreaElement).value;
    this.valueChange.emit(this.value);
  }
 
  /** @ignore */
  public _onInput(event: Event): void {
    this.updateMirrorValue((event.target as HTMLTextAreaElement).value);
  }
 
  /** @ignore */
  public updateMirrorValue(text: string) {
    // if there's a line end on the text text-area already shows
    // the newline but the mirror node not, adding an extra character
    // makes the mirror node take this empty line into account
    this._mirrorValue = text.endsWith('\n') ? text + 'n' : text;
  }
 
  /**
   * @ignore
   */
  public _setFocus(focused: boolean): void {
    this._focused = focused;
 
    if (this.lastPressedKey === 'Tab' && focused) {
      this.textareaElement.nativeElement.select();
    }
  }
 
  /**
   * @ignore
   */
  public isFocusWithin(): boolean {
    if (this.focusScope !== undefined) {
      return this.focusScope.focusWithin;
    }
    return false;
  }
 
  /**
   * @ignore
   */
  public maxLengthAriaText(): string | null {
    if (this.maxLength !== undefined && this.maxLength !== 0) {
      return 'maximal ' + this.maxLength.toString() + ' Zeichen';
    }

    return null;
  }
 
  /**
   * @ignore
   */
  public _handleFocusLeave(): void {
    this.onFocusLeft.next();
    // We need to wait a tick so that the control is back
    // in a sync state
    setTimeout(() => {
      if (this.errorMessage.length > 0) {
        void playSound(SoundType.ERROR);
      }
    });
  }
 
  /**
   * @ignore
   */
  public _handleBeforeInput(evt: InputEvent): void {
    playSoundIfLengthExceeded(
      this.textareaElement.nativeElement,
      this._mirrorValue,
      this.maxLength,
      evt,
    );
  }
 
  /**
   * @ignore
   */
  private recomputeHeight(): void {
    this.textareaElement.nativeElement.style.height =
      (this.mirrorNode.nativeElement.clientHeight - 32).toString() + 'px'; // 32 = 20 + 12 see css
  }
 
  /**
   * @ignore
   */
  private initializeKeyDownListener(): void {
    this.eventListenerCleanup.push(
      subscribeDocument(
        'keydown',
        (ev: KeyboardEvent) => (this.lastPressedKey = ev.key),
      ),
    );
  }
 
  /**
   * @ignore
   */
  private onMirrorNodeResizeCallback(): void {
    this.recomputeHeight();
  }
 
  /**
   * @ignore
   */
  private onTextAreaResizeCallback(): void {
    this.updateMirrorNodeWidth();
  }
 
  /**
   * @ignore
   */
  private initializeClickListener(): void {
    this.eventListenerCleanup.push(
      subscribeDocument('mousedown', () => (this.lastPressedKey = 'click')),
    );
  }
 
  /**
   * @ignore
   */
  private updateMirrorNodeWidth(): void {
    this.mirrorNode.nativeElement.style.width =
      (
        this.textareaElement.nativeElement.getBoundingClientRect().width - 24
      ).toString() + 'px'; // padding-left: 12px, padding-right: 12px
  }
 
  /** @ignore */
  private emitStatusChange() {
    const status: FormControlStatus = {};
    if (this.errorMessage) {
      status.error = [this.errorMessage];
    }
 
    this.onStatusChange.emit(status);
  }
}