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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { AfterViewInit, ChangeDetectionStrategy, Component, EventEmitter, HostBinding, inject, Output, ViewChild } from '@angular/core';
import { FormControl } from '@angular/forms';
import { MatInput } from '@angular/material/input';
import { isActive, IsActiveMatchOptions, Router } from '@angular/router';
import {
BehaviorSubject,
combineLatest,
debounceTime,
defer,
distinctUntilChanged,
forkJoin,
from,
map,
of,
startWith,
switchMap,
} from 'rxjs';
import { AppSearchService, IParsedRoute } from '../../services/search/search.service';
interface ISearchOption {
name: string;
description: string;
icon?: string;
value: string;
routerLink: string;
match: boolean;
isActive: ReturnType<typeof isActive>;
children: Array<Omit<ISearchOption, 'children'>>;
}
type TChild = Record<string, string | Pick<IsActiveMatchOptions, 'paths'>>;
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: false,
})
export class AppSearchComponent implements AfterViewInit {
readonly #config: { dedounceTime: number } = { dedounceTime: 175 };
readonly #router = inject(Router);
readonly #search = inject(AppSearchService);
@HostBinding('class.density-3') public density = true;
/** The autocomplete input. */
@ViewChild(MatInput) public matInput?: MatInput;
/** Event emitter to notify the parent component that an option has been selected. */
@Output() public readonly optionSelected = new EventEmitter<void>();
/** The autocomplete input form control. */
public control = new FormControl('', { nonNullable: true });
/** The search options state. */
private readonly optionsSubject = new BehaviorSubject<ISearchOption[]>([]);
/** The options value for the autocomplete. */
public readonly filteredOptions = combineLatest([
this.control.valueChanges.pipe(startWith(''), debounceTime(this.#config.dedounceTime), distinctUntilChanged()),
this.optionsSubject.asObservable(),
]).pipe(
map(([value, options]) => {
const searchTerm = value.toLowerCase();
if (searchTerm.length === 0) {
return options;
}
return options
.filter(item => {
const match = item.name.toLowerCase().includes(searchTerm) || item.description.toLowerCase().includes(searchTerm);
const childMatch = item.children.some(
child => child.name.toLowerCase().includes(searchTerm) || child.description.toLowerCase().includes(searchTerm),
);
return match || childMatch;
})
.map(item => ({
...item,
match: item.name.toLowerCase().includes(searchTerm) || item.description.toLowerCase().includes(searchTerm),
children: item.children.map(child => ({
...child,
match: child.name.toLowerCase().includes(searchTerm) || child.description.toLowerCase().includes(searchTerm),
})),
}));
}),
);
constructor() {
void this.getOptions().subscribe();
}
/**
* Search option mapper.
* @param name Option name.
* @param description Option description.
* @param path Router path.
* @param icon Option icon.
* @param rlaMatchOptions Router link active options.
*/
#mapSearchOption(
name: string,
description: string,
path: string,
icon?: string,
rlaMatchOptions?: Pick<IsActiveMatchOptions, 'paths'>,
): Omit<ISearchOption, 'children'> {
const routerLink = path.replace(/\s/g, '/');
const option: Omit<ISearchOption, 'children'> = {
name,
description,
value: path,
icon: icon,
routerLink,
match: true,
isActive: isActive(routerLink, this.#router, {
matrixParams: 'ignored',
queryParams: 'ignored',
paths: rlaMatchOptions?.paths ?? 'exact',
fragment: 'ignored',
}),
};
return option;
}
/**
* Feature value parser.
* @param route Parsed application route.
*/
#featureValue(route: IParsedRoute): string {
const feature = typeof route.data?.['feature'] === 'string' && route.data['feature'].length > 0 ? route.data['feature'] : route.path;
return typeof route.data?.['title'] === 'string' && route.data['title'].length > 0
? route.data['title']
: `${feature.slice(0, 1).toUpperCase()}${feature.slice(1, feature.length)}`;
}
/**
* Description value parser.
* @param route Parsed application route.
*/
#descriptionValue(route: IParsedRoute): string {
return typeof route.data?.['description'] === 'string' && route.data['description'].length > 0 ? route.data['description'] : '';
}
/**
* Children value parser.
* @param route Parsed application route.
*/
#childrenValue(route: IParsedRoute): TChild[] {
return typeof route.data?.['children'] === 'string' && Array.isArray(route.data['children']) ? route.data['children'] : [];
}
/**
* Child feature value parser.
* @param route Parsed application route.
*/
#childFeatureValue(child: TChild): string | undefined {
return typeof child['feature'] === 'string' && child['feature'].length > 0 ? child['feature'] : void 0;
}
/**
* Child icon value parser.
* @param route Parsed application route.
*/
#childIconValue(child: TChild): string | undefined {
return typeof child['icon'] === 'string' && child['icon'].length > 0 ? child['icon'] : void 0;
}
/**
* Child path value parser.
* @param route Parsed application route.
*/
#childPathValue(routePath: string, child: TChild): string | undefined {
return typeof child['path'] === 'string' && child['path'].length > 0 ? `${routePath}/${child['path']}` : void 0;
}
/**
* Child route link active match options value parser.
* @param route Parsed application route.
*/
#childRlaMatchOptionsValue(child: TChild): Pick<IsActiveMatchOptions, 'paths'> | undefined {
return typeof child['rlaMatchOptions'] === 'object' && Object.keys(child['rlaMatchOptions']).length > 0
? child['rlaMatchOptions']
: void 0;
}
/**
* Child skip search value parser.
* @param route Parsed application route.
*/
#childSkipSearchValue(child: TChild): boolean {
return 'skipSearch' in child && typeof child['skipSearch'] === 'boolean' ? child['skipSearch'] : false;
}
/**
* Search options getter.
* @returns search options
*/
private getOptions() {
return of(this.#router.config).pipe(
switchMap(routes => {
const r = routes.flatMap(item => (typeof item.outlet !== 'undefined' ? of([]) : defer(() => from(this.#search.parseRoute(item)))));
return forkJoin(r);
}),
map(routes => {
const options = routes
.flat(1)
.filter(route => this.#search.routeFilter(route))
.map(route => {
const feature = this.#featureValue(route);
const description = this.#descriptionValue(route);
const children = this.#childrenValue(route);
return {
...this.#mapSearchOption(feature, description, route.path, route.data?.['icon'], route.data?.['rlaMatchOptions']),
children: children.reduce((accumulator: Array<Omit<ISearchOption, 'children'>>, child: TChild) => {
const childFeature = this.#childFeatureValue(child);
const childIcon = this.#childIconValue(child);
const childPath = this.#childPathValue(route.path, child);
const childRlaMatchOptions = this.#childRlaMatchOptionsValue(child);
const skipSearch = this.#childSkipSearchValue(child);
if (
typeof childFeature !== 'undefined' &&
typeof childIcon !== 'undefined' &&
typeof childPath !== 'undefined' &&
!skipSearch
) {
const childOption = this.#mapSearchOption(childFeature, '', childPath, childIcon, childRlaMatchOptions);
accumulator.push(childOption);
}
return accumulator;
}, []),
};
});
options.sort((x, y) => x.name.localeCompare(y.name));
this.optionsSubject.next(options);
return options;
}),
);
}
/**
* Select option handler.
* @param routerLink the option's router link
*/
public selectOption(routerLink: string) {
this.optionSelected.emit();
void this.#router.navigate([routerLink]);
}
public ngAfterViewInit(): void {
setTimeout(() => {
const matInput = this.matInput;
Iif (typeof matInput !== 'undefined') {
matInput.focus();
}
});
}
}
|