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 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 8x 8x 8x 8x 8x 8x 7x 8x 8x 8x 3x 8x 8x 2x 8x 8x 8x | import { CommonModule, DOCUMENT } from '@angular/common'; import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef, HostListener, Inject, Input, OnDestroy, OnInit, ViewChild, } from '@angular/core'; import { Router } from '@angular/router'; import { IconName } from '@fortawesome/fontawesome-svg-core'; import { TranslateModule } from '@ngx-translate/core'; import { Observable, ReplaySubject, map, shareReplay } from 'rxjs'; import { ShoppingFacade } from 'ish-core/facades/shopping.facade'; import { IconModule } from 'ish-core/icon.module'; import { SearchBoxConfiguration } from 'ish-core/models/search-box-configuration/search-box-configuration.model'; import { Suggestions } from 'ish-core/models/suggestions/suggestions.model'; import { DeviceType } from 'ish-core/models/viewtype/viewtype.types'; import { PipesModule } from 'ish-core/pipes.module'; import { SuggestBrandsComponent } from 'ish-shared/components/search/suggest-brands/suggest-brands.component'; import { SuggestCategoriesComponent } from 'ish-shared/components/search/suggest-categories/suggest-categories.component'; import { SuggestKeywordsComponent } from 'ish-shared/components/search/suggest-keywords/suggest-keywords.component'; import { SuggestProductsComponent } from 'ish-shared/components/search/suggest-products/suggest-products.component'; import { SuggestSearchTermsComponent } from 'ish-shared/components/search/suggest-search-terms/suggest-search-terms.component'; /** * The SearchBoxComponent is responsible for handling the search box functionality, * including managing the search input, handling focus and blur events, and interacting * with the shopping facade to fetch search suggestions and results. * * @remarks * This component uses Angular's lifecycle hooks to initialize and manage the search box. * It also listens to various events such as transition end and window scroll to handle * the search box's behavior appropriately. * * @example * <app-search-box [configuration]="searchBoxConfig"></app-search-box> * * @publicApi */ @Component({ selector: 'ish-search-box', templateUrl: './search-box.component.html', standalone: true, imports: [ CommonModule, IconModule, PipesModule, TranslateModule, SuggestBrandsComponent, SuggestCategoriesComponent, SuggestKeywordsComponent, SuggestProductsComponent, SuggestSearchTermsComponent, ], changeDetection: ChangeDetectionStrategy.OnPush, }) export class SearchBoxComponent implements OnInit, AfterViewInit, OnDestroy { /** * the search box configuration for this component */ @Input() configuration: SearchBoxConfiguration; @Input() deviceType: DeviceType; @ViewChild('searchBox') searchBox: ElementRef; @ViewChild('searchInput') searchInput: ElementRef; searchSuggestLoading$: Observable<boolean>; inputSearchTerms$ = new ReplaySubject<string>(1); suggestions$: Observable<Suggestions>; // check if suggest has results to show the suggest layer searchBoxResults$: Observable<boolean>; // check if there are recent searched terms searchedTerms$: Observable<boolean>; // check if the input search term has more than 3 characters hasMinimumCharCount$: Observable<boolean>; // searchbox focus handling searchBoxFocus = false; searchBoxScaledUp = false; private searchBoxInitialWidth: number; // search suggest layer height private resizeTimeout: ReturnType<typeof setTimeout>; constructor( private shoppingFacade: ShoppingFacade, private router: Router, @Inject(DOCUMENT) private document: Document ) {} get usedIcon(): IconName { return this.configuration?.icon || 'search'; } ngOnInit() { this.searchSuggestLoading$ = this.shoppingFacade.searchSuggestLoading$; // suggests are triggered solely via stream this.suggestions$ = this.shoppingFacade .suggestResults$(this.inputSearchTerms$) .pipe(shareReplay(1)) as Observable<Suggestions>; this.searchBoxResults$ = this.suggestions$.pipe( map( results => !!( results && (results.keywords?.length || results.categories?.length || results.brands?.length || results.products?.length) ) ), shareReplay(1) ); this.searchedTerms$ = this.shoppingFacade.recentSearchTerms$.pipe( map(terms => terms.length > 0), shareReplay(1) ); this.hasMinimumCharCount$ = this.inputSearchTerms$.pipe( map(value => value.length > 2), shareReplay(1) ); } ngAfterViewInit() { this.searchBoxInitialWidth = this.searchBox.nativeElement.offsetWidth; } ngOnDestroy() { clearTimeout(this.resizeTimeout); } // add event listener for transition end to check if search box has scaled up // to show the suggest layer only if the input has scaled up @HostListener('transitionend', ['$event']) onTransitionEnd(event: TransitionEvent) { Iif (event.propertyName === 'width' && event.target === this.searchBox.nativeElement) { const newWidth = this.searchInput.nativeElement.offsetWidth; if (newWidth > this.searchBoxInitialWidth) { // check if search box has scaled up this.searchBoxScaledUp = true; } else { this.searchBoxScaledUp = false; } } } // if searchbox has focus - scale down and remove focus when scrolling the document @HostListener('window:scroll', []) onWindowScroll() { Iif (this.searchBoxFocus) { this.blur(); } } // reset input when ESC key is pressed and element is focused within the search box @HostListener('document:keydown.escape', ['$event']) onEscape(event: KeyboardEvent) { Iif (this.searchBox.nativeElement.contains(event.target)) { event.preventDefault(); // Optional: Prevent default behavior this.resetInput(); } } // remove focus when clicking outside the search box @HostListener('document:click', ['$event.target']) onClick(targetElement: undefined): void { this.blurIfOutside(targetElement); } // remove focus when focused outside the search box @HostListener('document:focusin', ['$event.target']) onFocusIn(targetElement: undefined): void { this.blurIfOutside(targetElement); } // check if the target element is outside the search box private blurIfOutside(targetElement: undefined): void { const clickedOrFocusedInside = this.searchBox.nativeElement.contains(targetElement); Iif (!clickedOrFocusedInside) { this.blur(); } } // simple blur method to remove focus from search input private blur() { this.handleFocus(false); this.searchInput.nativeElement.blur(); } // handle focus status of search box handleFocus(scaleUp: boolean) { this.updateMobileSuggestLayerHeight(); if (scaleUp) { this.searchBoxFocus = true; // this.searchBoxScaledUp is set using transitionend event } else { this.searchBoxFocus = false; this.searchBoxScaledUp = false; } } // manually set focus on search input // the explicit function call in the component is needed to get the focus working in iOS devices setFocusOnSearchInput() { this.searchInput.nativeElement.focus(); } // handle the user input handleInput(source: EventTarget) { const inputValue = (source as HTMLInputElement).value; this.inputSearchTerms$.next(inputValue); Iif (inputValue === '') { // clear suggestions in state when input is set to empty this.shoppingFacade.clearSuggestSearchSuggestions(); } } // reset all and blur the input resetInput() { this.blur(); this.inputSearchTerms$.next(''); this.shoppingFacade.clearSuggestSearchSuggestions(); } // handle the reset button handleResetButton(event: Event) { event.stopPropagation(); // important to prevent any other event listeners from firing this.inputSearchTerms$.next(''); this.shoppingFacade.clearSuggestSearchSuggestions(); } // submit the search form submitSearch(suggestedTerm: string) { Iif (!suggestedTerm) { this.setFocusOnSearchInput(); return false; } // add the suggested term to the input field this.inputSearchTerms$.next(suggestedTerm); this.router.navigate(['/search', suggestedTerm]); this.blur(); return false; // prevent form submission } // set CSS variable for suggest layer height on mobile devices to prevent keyboard overlay issues private updateMobileSuggestLayerHeight = () => { Iif (!SSR && this.deviceType === 'mobile') { clearTimeout(this.resizeTimeout); // timeout to wait for keyboard animation to finish this.resizeTimeout = setTimeout(() => { const remainingHeight = window.visualViewport?.height || window.innerHeight; this.document.documentElement.style.setProperty('--viewport-remaining-height', `${remainingHeight}px`); }, 300); } }; } |