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 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | 1x 1x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 291x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 806x 214x 139x 214x 281x 439x 281x 1x 280x 214x 10x 10x 44x 44x 214x 519x 229x 229x 229x 229x 404x 229x 21x 229x 163x 66x 290x 288x 214x 214x 214x 168x 46x 59x 59x 572x 572x 572x 572x 345x 345x 345x 345x 345x 2x 1x 1x 9x 2x 2x 2x 2x 2x 214x 214x 214x 163x 124x 124x 163x 39x 124x 124x 124x 806x 483x 35x 35x 29x 29x 29x 35x 35x 35x 29x 356x 48x 48x 356x 356x 309x 31x 31x 229x | /**
* Copyright (c) Siemens 2016 - 2025
* SPDX-License-Identifier: MIT
*/
import { ConnectionPositionPair, Overlay, OverlayRef } from '@angular/cdk/overlay';
import { ComponentPortal } from '@angular/cdk/portal';
import {
booleanAttribute,
ComponentRef,
computed,
Directive,
effect,
ElementRef,
HostListener,
inject,
Injector,
input,
numberAttribute,
OnChanges,
OnDestroy,
output,
signal,
SimpleChanges,
TemplateRef
} from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { SiAutocompleteDirective } from '@siemens/element-ng/autocomplete';
import { t } from '@siemens/element-translate-ng/translate';
import { isObservable, ReplaySubject, Subscription } from 'rxjs';
import { map } from 'rxjs/operators';
import { SiTypeaheadComponent } from './si-typeahead.component';
import {
Typeahead,
TypeaheadArray,
TypeaheadMatch,
TypeaheadOption,
TypeaheadOptionItemContext
} from './si-typeahead.model';
import { typeaheadSearch } from './si-typeahead.search';
import { SiTypeaheadSorting } from './si-typeahead.sorting';
@Directive({
selector: '[siTypeahead]',
host: {
class: 'si-typeahead'
},
hostDirectives: [SiAutocompleteDirective],
exportAs: 'si-typeahead'
})
export class SiTypeaheadDirective implements OnChanges, OnDestroy {
protected static readonly overlayPositions: ConnectionPositionPair[] = [
{
overlayX: 'start',
overlayY: 'top',
originX: 'start',
originY: 'bottom',
offsetY: 2
},
{
overlayX: 'start',
overlayY: 'bottom',
originX: 'start',
originY: 'top',
offsetY: -4
},
{
overlayX: 'end',
overlayY: 'top',
originX: 'end',
originY: 'bottom',
offsetY: 2
},
{
overlayX: 'end',
overlayY: 'bottom',
originX: 'end',
originY: 'top',
offsetY: -4
}
];
/**
* Set the options of the typeahead.
* Has to be either an Array or an Observable of an Array
* of options (string or object)
*/
readonly siTypeahead = input.required<Typeahead>();
/**
* Turns on/off the processing (searching and sorting) of the typeahead options.
* Is used when searching and sorting is done externally.
*
* @defaultValue true
*/
readonly typeaheadProcess = input(true, {
transform: booleanAttribute
});
/**
* Makes the typeahead scrollable and sets its height.
* Uses {@link typeaheadOptionsInScrollableView} and {@link typeaheadScrollableAdditionalHeight}.
*
* @defaultValue false
*/
readonly typeaheadScrollable = input(false, { transform: booleanAttribute });
/**
* If {@link typeaheadScrollable} is `true`, defines the number of items visible at once.
*
* @defaultValue 10
*/
readonly typeaheadOptionsInScrollableView = input(10);
/**
* Defines the maximum number of items added into the DOM. Default is 20 and 0 means unlimited.
*
* @defaultValue 20
*/
readonly typeaheadOptionsLimit = input(20);
/**
* If {@link typeaheadScrollable} is `true`, defines the number of additional pixels
* to be added the the bottom of the typeahead to show users that it is scrollable.
*
* @defaultValue 13
*/
readonly typeaheadScrollableAdditionalHeight = input(13);
/**
* Defines the index of the item which should automatically be selected.
*
* @defaultValue 0
*/
readonly typeaheadAutoSelectIndex = input(0, { transform: numberAttribute });
/**
* Defines whether the typeahead can be closed using escape.
*
* @defaultValue true
*/
readonly typeaheadCloseOnEsc = input(true, { transform: booleanAttribute });
/**
* Defines whether the host value should be cleared when a value is selected.
*
* @defaultValue false
*/
readonly typeaheadClearValueOnSelect = input(false, { transform: booleanAttribute });
/**
* Defines the number of milliseconds to wait before displaying a typeahead after the host was
* focused or a value inputted.
*
* @defaultValue 0
*/
readonly typeaheadWaitMs = input(0);
/**
* Defines the number of characters the value of the host needs to be before a typeahead is displayed.
* Use `0` to have it display when focussing the host (clicking or tabbing into it).
*
* @defaultValue 1
*/
readonly typeaheadMinLength = input(1);
/**
* Defines the name of the field/property the option string is in when the typeahead options are objects.
*
* @defaultValue 'name'
*/
readonly typeaheadOptionField = input('name');
/**
* Defines whether multiselection of typeahead is possible with checkboxes.
*
* @defaultValue false
*/
readonly typeaheadMultiSelect = input(false, { transform: booleanAttribute });
/**
* Defines whether to tokenize the search or match the whole search.
*
* @defaultValue true
*/
readonly typeaheadTokenize = input(true, { transform: booleanAttribute });
/**
* Defines whether and how to require to match with all the tokens if {@link typeaheadTokenize} is enabled.
* - `no` does not require all of the tokens to match.
* - `once` requires all of the parts to be found in the search.
* - `separately` requires all of the parts to be found in the search where there is not an overlapping different result.
* - `independently` requires all of the parts to be found in the search where there is not an overlapping or adjacent different result.
* ('independently' also slightly changes sorting behavior in the same way.)
*
* @defaultValue 'separately'
*/
readonly typeaheadMatchAllTokens = input<'no' | 'once' | 'separately' | 'independently'>(
'separately'
);
/**
* Defines an optional template to use as the typeahead match item instead of the one built in.
* Gets the {@link TypeaheadOptionItemContext} passed to it.
*/
readonly typeaheadItemTemplate = input<TemplateRef<TypeaheadOptionItemContext>>();
/**
* Skip the sorting of matches.
* If the value is `true`, the matches are sorted according to {@link SiTypeaheadSorting}.
*
* @defaultValue false
*/
readonly typeaheadSkipSortingMatches = input(false, { transform: booleanAttribute });
/**
* Screen reader only label for the autocomplete list.
*
* @defaultValue
* ```
* t(() => $localize`:@@SI_TYPEAHEAD.AUTOCOMPLETE_LIST_LABEL:Suggestions`)
* ```
*/
readonly typeaheadAutocompleteListLabel = input(
t(() => $localize`:@@SI_TYPEAHEAD.AUTOCOMPLETE_LIST_LABEL:Suggestions`)
);
/**
* If set, the typeahead will at minium have the width of the connected input field.
*
* @defaultValue false
*/
readonly typeaheadFullWidth = input(false, { transform: booleanAttribute });
/**
* Emits an Event when the input field is changed.
*/
readonly typeaheadOnInput = output<string>();
/**
* Emits an Event when a typeahead match is selected.
* The event is a {@link TypeaheadMatch}
*/
readonly typeaheadOnSelect = output<TypeaheadMatch>();
/**
* Emits an Event when a typeahead full match exists. A full match occurs when the entered text
* is equal to one of the typeahead options.
* The event is a {@link TypeaheadMatch}
*/
readonly typeaheadOnFullMatch = output<TypeaheadMatch>();
/** Emits whenever the typeahead overlay is opened or closed. */
readonly typeaheadOpenChange = output<boolean>();
/** @internal */
readonly foundMatches = computed(() =>
this.typeaheadProcess() ? this.processedSearch() : this.unprocessedSearch()
);
/** @internal */
readonly query = signal('');
/**
* Indicates whether the typeahead is shown.
*/
get typeaheadOpen(): boolean {
return !!this.componentRef;
}
private overlay = inject(Overlay);
private elementRef = inject<ElementRef<HTMLInputElement>>(ElementRef);
private injector = inject(Injector);
private autoComplete = inject<SiAutocompleteDirective<TypeaheadMatch>>(SiAutocompleteDirective);
private $typeahead = new ReplaySubject<TypeaheadArray>(1);
private componentRef?: ComponentRef<SiTypeaheadComponent>;
private inputTimer: any;
private sourceSubscription?: Subscription;
private matchSorter = new SiTypeaheadSorting();
private overlayRef?: OverlayRef;
/**
* Indicates that the typeahead can be potentially open.
* This signal is typically `true` when the input is focussed.
* It may be overridden and set to `false` when escape is pressed
* or when an option was selected.
*/
private readonly canBeOpen = signal(false);
private readonly selectionCounter = signal(0);
private readonly typeaheadOptions = toSignal(
this.$typeahead.pipe(
map(options =>
options.map(option => ({
text: this.getOptionValue(option),
option
}))
)
),
{ initialValue: [] }
);
private readonly typeaheadSearch = typeaheadSearch(
this.typeaheadOptions,
this.query,
computed(() => ({
matchAllTokens: this.typeaheadMatchAllTokens(),
disableTokenizing: !this.typeaheadTokenize(),
skipProcessing: !this.typeaheadProcess()
}))
);
private readonly processedSearch = computed(() => {
this.selectionCounter(); // This is a workaround for the multi-select which needs to trigger a change detection in the typeahead component.
const matches = this.typeaheadSearch().map(match => ({
...match,
itemSelected: this.typeaheadMultiSelect()
? (match.option as Record<string, any>).selected
: false,
iconClass: this.getOptionField(match.option, 'iconClass')
}));
if (this.typeaheadSkipSortingMatches()) {
return matches;
} else {
return this.matchSorter.sortMatches(matches);
}
});
private readonly unprocessedSearch = computed(() => {
this.selectionCounter(); // This is a workaround for the multi-select which needs to trigger a change detection in the typeahead component.
return this.typeaheadOptions().map(option => {
const itemSelected = this.typeaheadMultiSelect()
? (option as Record<string, any>).selected
: false;
return {
option,
text: option.text,
result: option.text
? [{ text: option.text, isMatching: false, matches: 0, uniqueMatches: 0 }]
: [],
itemSelected,
iconClass: this.getOptionField(option.option, 'iconClass'),
stringMatch: false,
atBeginning: false,
matches: 0,
uniqueMatches: 0,
uniqueSeparateMatches: 0,
matchesEntireQuery: false,
matchesAllParts: false,
matchesAllPartsSeparately: false,
active: false
};
});
});
constructor() {
effect(() => {
// The value needs to fulfil the minimum length requirement set.
if (this.canBeOpen() && this.query().length >= this.typeaheadMinLength()) {
const matches = this.foundMatches();
const escapedQuery = this.escapeRegex(this.query());
const equalsExp = new RegExp(`^${escapedQuery}$`, 'i');
const fullMatches = matches.filter(
match => match.result.length === 1 && equalsExp.test(match.text)
);
if (fullMatches.length > 0) {
this.typeaheadOnFullMatch.emit(fullMatches[0]);
}
if (matches.length) {
this.loadComponent();
} else {
this.removeComponent();
}
} else {
this.removeComponent();
}
});
}
// Every time the main input changes, detect whether it is async and if it is not make an observable out of the array.
ngOnChanges(changes: SimpleChanges): void {
if (changes.siTypeahead) {
this.sourceSubscription?.unsubscribe();
const typeahead = this.siTypeahead();
if (isObservable(typeahead)) {
this.sourceSubscription = typeahead.subscribe(this.$typeahead);
} else {
this.$typeahead.next(typeahead);
}
}
}
// Clear the current input timeout (if set) and remove the component when the focus of the host is lost.
@HostListener('focusout')
protected onBlur(): void {
this.clearTimer();
this.canBeOpen.set(false);
}
// Start the input timeout to display the typeahead when the host is focussed or a value is inputted into it.
@HostListener('focusin', ['$event'])
@HostListener('input', ['$event'])
protected onInput(event: Event): void {
const target = event.target as HTMLInputElement;
Iif (!target) {
return;
}
// Get the value or otherwise textContent of the host element now, because later it could be reset.
const firstValue = target.value || target.textContent;
this.inputTimer ??= setTimeout(() => {
this.inputTimer = undefined;
const value = (target.value || target.textContent) ?? firstValue ?? '';
this.query.set(value);
this.typeaheadOnInput.emit(value ?? '');
this.canBeOpen.set(true);
}, this.typeaheadWaitMs());
}
@HostListener('keydown.escape')
protected onKeydownEscape(): void {
if (this.typeaheadCloseOnEsc()) {
this.clearTimer();
this.canBeOpen.set(false);
}
}
@HostListener('keydown.space', ['$event'])
protected onKeydownSpace(event: Event): void {
if (this.typeaheadMultiSelect()) {
// Avoid space character to be inserted into the input field
event.preventDefault();
const value = this.autoComplete.active?.value();
if (value) {
this.selectMatch(value);
// this forces change detection in the typeahead component.
this.selectionCounter.update(v => v + 1);
}
}
}
ngOnDestroy(): void {
this.clearTimer();
this.sourceSubscription?.unsubscribe();
this.overlayRef?.dispose();
}
// Dynamically create the typeahead component and then set the matches and the query.
private loadComponent(): void {
if (!this.overlayRef?.hasAttached()) {
this.overlayRef?.dispose();
this.overlayRef = this.overlay.create({
positionStrategy: this.overlay
.position()
.flexibleConnectedTo(this.elementRef.nativeElement)
.withPositions(SiTypeaheadDirective.overlayPositions),
minWidth: this.typeaheadFullWidth()
? this.elementRef.nativeElement.getBoundingClientRect().width + 2 // 2px border
: 0
});
}
if (this.overlayRef.hasAttached()) {
return;
}
const typeaheadPortal = new ComponentPortal(SiTypeaheadComponent, null, this.injector);
this.componentRef = this.overlayRef.attach(typeaheadPortal);
this.typeaheadOpenChange.emit(true);
}
/**
* Extracts the display value from a typeahead option.
*
* For string options, returns the string value directly.
* For object options, returns the value of the field specified by {@link typeaheadOptionField}
* (defaults to 'name'), or an empty string if the field doesn't exist.
*
* @param option - The typeahead option to extract the value from
* @returns The string representation of the option for display purposes
*/
private getOptionValue(option: TypeaheadOption): string {
return typeof option !== 'object'
? option.toString()
: (option[this.typeaheadOptionField()] ?? '');
}
/**
* Extracts a specific field value from a typeahead option.
*
* This method is used to access additional properties of object-type options,
* such as 'selected' for multi-select functionality or 'iconClass' for displaying icons.
*
* @param option - The typeahead option to extract the field from
* @param field - The name of the field to extract
* @returns The field value as a string if the option is an object and the field exists,
* otherwise undefined
*/
private getOptionField(option: TypeaheadOption, field: string): string | undefined {
return typeof option !== 'object' ? undefined : option[field];
}
// Select a match, either gets called due to a enter keypress or from the component due to a click.
/** @internal */
selectMatch(match: TypeaheadMatch): void {
match.itemSelected = !match.itemSelected;
if (!this.typeaheadMultiSelect()) {
const inputElement =
this.elementRef.nativeElement.querySelector('input')! ?? this.elementRef.nativeElement;
inputElement.value = this.typeaheadClearValueOnSelect() ? '' : match.text;
inputElement.dispatchEvent(new Event('input'));
}
// Clear the current input timeout (if set) and remove the typeahead.
this.clearTimer();
this.typeaheadOnSelect.emit(match);
if (!this.typeaheadMultiSelect()) {
this.canBeOpen.set(false);
}
}
// Remove the component by clearing the viewContainerRef
private removeComponent(): void {
if (this.overlayRef?.hasAttached()) {
this.overlayRef?.detach();
this.typeaheadOpenChange.emit(false);
}
this.componentRef?.destroy();
this.componentRef = undefined;
}
private clearTimer(): void {
if (this.inputTimer) {
clearTimeout(this.inputTimer);
this.inputTimer = undefined;
}
}
private escapeRegex(query: string): string {
return query.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
}
}
|