All files / lib/search-input-field search-input-field.component.ts

94.67% Statements 302/319
93.02% Branches 40/43
88.23% Functions 15/17
94.67% Lines 302/319

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 3201x 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 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 186x 186x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 87x 87x 87x 1x 1x 1x 1x 1x 33x 33x 33x   33x 33x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 1x 1x 1x           1x 1x 1x     1x 1x 1x 6x 6x 5x 6x 6x 1x 1x 1x 6x 5x 6x 6x 1x 1x 1x 101x 101x 101x 101x 5x 5x 5x 32x       85x 20x 5x 15x 15x 20x 20x 20x 85x 85x             101x 101x 101x 101x 101x 1x 1x 1x 104x 8x 7x 7x 8x 8x 8x 8x 104x 104x 104x 1x 1x 1x 1x 1x 22x 1x 1x 1x 22x 22x 22x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterViewInit,
  Component,
  ElementRef,
  EventEmitter,
  Input,
  OnDestroy,
  Output,
  QueryList,
  ViewChild,
  ViewChildren,
} from '@angular/core';
 
import { FocusableElementOwner } from '../utils/util.types';
 
import { FocusScopeDirective } from '../internal/focus-scope.directive';
import { AutoIdDirective } from '../internal/label-container/label-container.component';
import { OverlayContainerComponent } from '../internal/overlay-container/overlay-container.component';
import { TooltipComponent } from '../internal/tooltip/tooltip.component';
 
import { AutoCompleteDirective } from '../text-input-field/auto-complete/auto-complete.directive';
 
/**
 * `gc-search-input-field` is a text field designed for searches. It triggers a passed function when the
 * search action is performed.
 */
@Component({
  selector: 'gc-search-input-field',
  templateUrl: './search-input-field.component.html',
  styleUrls: [
    './../utils/styles/reset-button.css',
    './search-input-field.component.css',
    './../text-input-field/auto-complete/auto-complete.style.css',
  ],
})
export class SearchInputComponent
  extends AutoCompleteDirective
  implements FocusableElementOwner, OnDestroy, AfterViewInit
{
  /**
   * Custom ID to use.
   */
  @Input()
  public id?: string;
 
  /**
   * The label that shows on the input. Also used as placeholder value.
   */
  @Input()
  public label = '';
 
  /**
   * 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 input.
   */
  @Input()
  public disabled = false;
 
  /**
   * Function that will be fired on value submission.
   */
  @Output()
  public readonly valueChange: EventEmitter<string> =
    new EventEmitter<string>();
 
  /**
   * Function that will be fired when an item from the auto-complete list is selected
   */
  @Output()
  public readonly onAutoCompleteSelected: EventEmitter<string> =
    new EventEmitter<string>();
 
  /**
   * Currently typed text in the input field.
   */
  @Input()
  public value = '';
 
  /**
   * Hint message that appears below the input field.
   */
  @Input()
  public hint = '';
 
  /**
   * If set the value of the search input will be emitted after the passed number of milliseconds after the last key press.
   */
  @Input()
  public autoEmitDelay?: number;
 
  /**
   * Highlight auto-complete results only when prefix matches
   * @default true
   */
  @Input()
  public autoCompletePrefixOnly = true;
 
  /**
   * If true the value of the search field doesn't get updated when item from auto complete list gets selected
   */
  @Input()
  public autoCompletePreventUpdate = false;
 
  /** @ignore */
  @ViewChild(FocusScopeDirective)
  protected focusScope?: FocusScopeDirective;
 
  /** @ignore */
  @ViewChild(AutoIdDirective)
  protected autoId!: AutoIdDirective;
 
  /** @ignore */
  @ViewChild('popup')
  protected popup?: OverlayContainerComponent;
 
  /** @ignore */
  @ViewChildren('autocompleteItem')
  protected autocompleteItems?: QueryList<ElementRef<HTMLElement>>;
 
  /** @ignore */
  @ViewChild('searchInput')
  private searchInput!: ElementRef<HTMLInputElement>;
 
  /** @ignore */
  @ViewChild('startSearchTooltip')
  private startSearchTooltip?: TooltipComponent;
 
  /** @ignore */
  @ViewChild('startSearchButton')
  private startSearchButton?: ElementRef<HTMLButtonElement>;
 
  /** @ignore */
  @ViewChild('clearInputTooltip')
  private clearInputTooltip?: TooltipComponent;
 
  /** @ignore */
  @ViewChild('clearInputButton')
  private clearInputButton?: ElementRef<HTMLButtonElement>;
 
  /** @ignore */
  protected override get popupAnker(): undefined | ElementRef<HTMLElement> {
    return this.searchInput;
  }
 
  /** @ignore */
  protected override get popupScrollTarget():
    | undefined
    | ElementRef<HTMLElement> {
    return undefined;
  }
 
  /**
   * @ignore
   */
  public _focused = false;
 
  /**
   * @ignore
   */
  private inputTimeout?: ReturnType<typeof setTimeout>;
 
  public focusChild(): boolean {
    if (!this.disabled) {
      this.searchInput.nativeElement.focus();
      return true;
    } else {
      return false;
    }
  }
 
  /**
   * @ignore
   */
  public async _onInput(event: Event): Promise<void> {
    this.value = (event.target as HTMLInputElement).value;
    await this._autoCompleteOnInput(this.value);
  }
 
  /**
   * @ignore
   */
  public _hasFocus(val: boolean): void {
    this._focused = val;
 
    if (!val && this._impl_drawerOpen) {
      this._hideCompletions();
    }
  }
 
  /**
   * @ignore
   */
  public _onClear(): void {
    this.value = '';
    this.searchInput.nativeElement.focus();
    this._hideCompletions(true);
    this.scheduleAutoEmit();
  }
 
  /**
   * @ignore
   */
  public _handleStartSearch(): void {
    this._updateValue();
    this.searchInput.nativeElement.focus();
  }
 
  /** @ignore */
  public override ngAfterViewInit(): void {
    if (
      this.startSearchTooltip &&
      this.startSearchButton &&
      this.startSearchTooltip.tooltipOwner !== this.startSearchButton
    ) {
      this.startSearchTooltip.tooltipOwner = this.startSearchButton;
    }
    if (
      this.clearInputTooltip &&
      this.clearInputButton &&
      this.clearInputTooltip.tooltipOwner !== this.clearInputButton
    ) {
      this.clearInputTooltip.tooltipOwner = this.clearInputButton;
    }
    super.ngAfterViewInit();
  }
 
  /** @ignore */
  public ngOnDestroy(): void {
    if (this.inputTimeout) {
      clearTimeout(this.inputTimeout);
      this.inputTimeout = undefined;
    }
  }
 
  /** @ignore */
  protected override focusInputField(): void {
    this.focusChild();
  }
 
  /** @ignore */
  protected override _handleAutoCompleteSelection(text: string) {
    this.onAutoCompleteSelected.emit(text);
    if (!this.autoCompletePreventUpdate) {
      this.value = text;
    }
  }
 
  /** @ignore */
  protected _handleChange() {
    if (!this.autoCompletePreventUpdate) {
      this._updateValue();
    }
  }
 
  /** @ignore */
  protected _handleInputKeyDown(evt: KeyboardEvent): void {
    this.scheduleAutoEmit();
 
    switch (evt.key) {
      case 'ArrowDown':
        this._popup_focusNext();
        evt.preventDefault();
        break;
      case 'ArrowUp':
        this._popup_focusPrev();
        evt.preventDefault();
        break;
      case 'Enter':
        if (this._impl_drawerOpen && this._popup_focused) {
          this._popup_applyFocused();
        } else {
          this._updateValue();
        }
        evt.preventDefault();
        break;
 
      case 'Escape':
        if (this._impl_drawerOpen) {
          this._hideCompletions();
        } else {
          this.value = '';
        }
        evt.preventDefault();
        break;
      default:
        break;
    }
  }
 
  /** @ignore */
  private scheduleAutoEmit(): void {
    if (this.autoEmitDelay) {
      if (this.inputTimeout !== undefined) {
        clearTimeout(this.inputTimeout);
        this.inputTimeout = undefined;
      }
      this.inputTimeout = setTimeout(
        this._updateValue.bind(this),
        this.autoEmitDelay,
      );
    }
  }
 
  /**
   * @ignore
   */
  private _updateValue(): void {
    if (this.inputTimeout !== undefined) {
      // cancel pending auto-submit to avoid repeated events after submitting via "Enter" or search button
      clearTimeout(this.inputTimeout);
      this.inputTimeout = undefined;
    }
    this.valueChange.emit(this.value);
  }
}