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

96.29% Statements 78/81
87.8% Branches 36/41
100% Functions 20/20
96.1% Lines 74/77

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                                                                                                  1x 117x 117x 117x 117x 117x 117x 117x   117x 117x 117x 117x 117x 117x 117x 117x   117x 117x   117x 117x   117x   117x 117x   117x   121x 121x 7x     114x 114x 10x     104x   117x 16x 16x 16x 3x     46x   117x 12x 12x     50x       117x 20x         13x 4x         65x 65x   65x 53x 23x 30x     30x   53x         3x 3x 1x   2x         126x 83x   43x   29x 14x           3x       2x 2x         3x       3x 3x           7x 5x 5x     2x               2x 1x        
/**
 * Copyright (c) Siemens 2016 - 2026
 * 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 { elementCancel } from '@siemens/element-icons';
import { addIcons, 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 { InternalCriterionDefinition } from './si-filtered-search-helper';
import { CriterionValue, OptionType } from './si-filtered-search.model';
import { SiFilteredSearchDateValueComponent } from './values/date-value/si-filtered-search-date-value.component';
import { SiFilteredSearchFreeTextComponent } from './values/free-text/si-filtered-search-free-text.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,
    SiFilteredSearchFreeTextComponent,
    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<InternalCriterionDefinition>();
  readonly disabled = 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(() => {
    // Check if this is a free text criterion first
    const definition = this.definition();
    if (definition.type === 'free-text') {
      return 'free-text';
    }
 
    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.type() !== 'free-text') {
      this.edit();
    }
  }
 
  closeOverlay(): void {
    if (this.active()) {
      this.active.set(false);
    }
  }
 
  edit(field?: 'value' | 'operator'): void {
    this.active.set(true);
    this.hasPendingFocus = true;
 
    setTimeout(() => {
      if (field === 'value') {
        this.valueInput()?.focus();
      } else Iif (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 ? [] : ''
    }));
  }
 
  protected freeTextActiveChange(value: boolean): void {
    if (!value && !this.value().value) {
      this.deleteCriterion.emit({ triggerSearch: true });
    }
  }
}