All files / filtered-search si-filtered-search-value.component.ts

96.25% Statements 77/80
83.33% Branches 30/36
100% Functions 19/19
96.05% Lines 73/76

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                                                                                          1x 113x 113x 113x 113x 113x 113x 113x 113x   113x 113x 113x 113x 113x 113x 113x 113x   113x 113x   113x 113x   113x   113x 113x   113x 116x 116x 116x 10x     106x   113x 18x 18x 18x 3x     53x   113x 12x 12x     50x       113x 22x         11x 4x         64x       64x 64x   64x 64x 27x 37x 2x   35x   64x         3x 3x 1x   2x         143x 62x   81x   81x 17x           4x       2x 2x         3x       3x 3x           5x 4x 4x     1x              
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { CdkMonitorFocus, FocusOrigin } from '@angular/cdk/a11y';
import {
  ChangeDetectionStrategy,
  Component,
  computed,
  ElementRef,
  input,
  model,
  OnInit,
  output,
  signal,
  viewChild
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { addIcons, elementCancel, SiIconComponent } from '@siemens/element-ng/icon';
import { SiTypeaheadDirective } from '@siemens/element-ng/typeahead';
import { SiTranslatePipe, TranslatableString } from '@siemens/element-translate-ng/translate';
import { Observable } from 'rxjs';
 
import { CriterionDefinition, CriterionValue, OptionType } from './si-filtered-search.model';
import { SiFilteredSearchDateValueComponent } from './values/date-value/si-filtered-search-date-value.component';
import { SiFilteredSearchMultiSelectComponent } from './values/multi-select/si-filtered-search-multi-select.component';
import { SiFilteredSearchValueBase } from './values/si-filtered-search-value.base';
import { SiFilteredSearchTypeaheadComponent } from './values/typeahead/si-filtered-search-typeahead.component';
 
@Component({
  selector: 'si-filtered-search-value',
  imports: [
    CdkMonitorFocus,
    SiTypeaheadDirective,
    FormsModule,
    SiTranslatePipe,
    SiFilteredSearchDateValueComponent,
    SiFilteredSearchTypeaheadComponent,
    SiFilteredSearchMultiSelectComponent,
    SiIconComponent
  ],
  templateUrl: './si-filtered-search-value.component.html',
  styleUrl: './si-filtered-search-value.component.scss',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SiFilteredSearchValueComponent implements OnInit {
  readonly value = model.required<CriterionValue>();
  readonly definition = input.required<CriterionDefinition>();
  readonly disabled = input.required<boolean>();
  readonly readonly = input.required<boolean>();
  readonly onlySelectValue = input.required<boolean>();
  readonly maxCriteriaOptions = input.required<number>();
  readonly optionsInScrollableView = input.required<number>();
  readonly clearButtonLabel = input.required<TranslatableString>();
  readonly lazyValueProvider =
    input<(criterionName: string, typed: string | string[]) => Observable<OptionType[]>>();
  readonly searchDebounceTime = input.required<number>();
  readonly itemCountText = input.required<TranslatableString>();
  readonly disableSelectionByColonAndSemicolon = input.required<boolean>();
  readonly searchLabel = input.required<TranslatableString>();
  readonly invalidCriterion = input.required<boolean>();
  readonly isStrictOrOnlySelectValue = input.required<boolean>();
  readonly editOnCreation = input.required<boolean>();
 
  readonly deleteCriterion = output<{ triggerSearch: boolean } | void>();
  readonly submitCriterion = output<{ freeText: string } | void>();
 
  protected readonly active = signal<boolean>(false);
  protected readonly icons = addIcons({ elementCancel });
 
  private hasPendingFocus = false;
 
  private readonly operatorInput = viewChild<ElementRef<HTMLInputElement>>('operatorInput');
  private readonly valueInput = viewChild(SiFilteredSearchValueBase);
 
  readonly type = computed(() => {
    const definition = this.definition();
    const validationType = definition.validationType;
    if (validationType === 'date' || validationType === 'date-time') {
      return 'date';
    }
    // Handle multi-select vs single-select for other validation types.
    return definition.multiSelect ? 'multi-select' : 'typeahead';
  });
  readonly selectedOperatorIndex = computed(() => {
    const operator = this.value().operator;
    const operators = this.definition().operators;
    if (!operator || !operators) {
      return -1;
    }
 
    return operators.findIndex(op => op.includes(operator));
  });
  readonly longestOperatorLength = computed(() => {
    const operators = this.definition().operators;
    Iif (!operators) {
      return 0;
    }
    return Math.max(...operators.map(a => a.length));
  });
 
  ngOnInit(): void {
    if (this.editOnCreation()) {
      this.edit();
    }
  }
 
  closeOverlay(): void {
    if (this.active()) {
      this.active.set(false);
    }
  }
 
  edit(field?: 'value' | 'operator'): void {
    Iif (this.readonly()) {
      return;
    }
 
    this.active.set(true);
    this.hasPendingFocus = true;
 
    setTimeout(() => {
      if (field === 'value') {
        this.valueInput()?.focus();
      } else if (field === 'operator') {
        this.operatorInput()?.nativeElement.focus();
      } else {
        (this.operatorInput()?.nativeElement ?? this.valueInput())?.focus();
      }
      this.hasPendingFocus = false;
    });
  }
 
  protected backspaceOverflow(): void {
    const operatorInput = this.operatorInput()?.nativeElement;
    if (operatorInput) {
      operatorInput.focus();
    } else {
      this.deleteCriterion.emit();
    }
  }
 
  protected focusChange(focusOrigin: FocusOrigin): void {
    if (this.hasPendingFocus) {
      return;
    }
    setTimeout(() => {
      // We need to wait for the overlay, that might be shown and now close as well on onside click.
      if (this.active() && !focusOrigin && !this.valueInput()?.focusInOverlay()) {
        this.active.set(false);
      }
    });
  }
 
  protected operatorUpdate(operator: string): void {
    this.value.update(value => ({ ...value, operator }));
  }
 
  protected operatorBackspace(): void {
    if (!this.operatorInput()?.nativeElement.value) {
      this.deleteCriterion.emit();
    }
  }
 
  protected operatorEnter(): void {
    this.valueInput()!.focus();
  }
 
  protected operatorBlur(): void {
    const operators = this.definition().operators;
    Iif (operators && !operators.includes(this.value().operator!)) {
      this.operatorUpdate(operators.includes('=') ? '=' : operators[0]);
    }
  }
 
  protected clear(): void {
    if (!this.active() || !this.value().value?.length) {
      this.deleteCriterion.emit({ triggerSearch: true });
      return;
    }
 
    this.value.update(v => ({
      ...v,
      dateValue: undefined,
      value: this.definition().multiSelect ? [] : ''
    }));
  }
}