CHange keydown listeners to specific key listener to optimize perfomance

This commit is contained in:
Konstantinos Triantafyllou 2022-04-17 15:25:01 +03:00
parent 0384fe87b9
commit 7e9db1d0de
4 changed files with 88 additions and 70 deletions

View File

@ -14,7 +14,8 @@ import {EntitiesSelectionComponent} from "../../searchPages/searchUtils/entities
@Component({ @Component({
selector: 'advanced-search-input', selector: 'advanced-search-input',
template: ` template: `
<div *ngIf="inputs.length > 0" class="search-input" [class.dark]="dark" [ngClass]="searchInputClass" [class.small-vertical]="smallVertical"> <div *ngIf="inputs.length > 0" class="search-input" [class.dark]="dark" [ngClass]="searchInputClass"
[class.small-vertical]="smallVertical">
<div class="uk-flex uk-flex-middle"> <div class="uk-flex uk-flex-middle">
<div class="uk-width-expand"> <div class="uk-width-expand">
<div class="uk-grid uk-grid-collapse inputs" [ngClass]="'uk-child-width-1-' + length" uk-grid> <div class="uk-grid uk-grid-collapse inputs" [ngClass]="'uk-child-width-1-' + length" uk-grid>
@ -39,22 +40,20 @@ export class AdvancedSearchInputComponent implements AfterContentInit, OnDestroy
@Input() smallVertical: boolean = false; @Input() smallVertical: boolean = false;
@Output() searchEmitter: EventEmitter<void> = new EventEmitter<void>(); @Output() searchEmitter: EventEmitter<void> = new EventEmitter<void>();
@HostListener('window:keydown', ['$event']) @HostListener('window:keydown.enter', ['$event'])
keyEvent(event: KeyboardEvent) { enter(event: KeyboardEvent) {
let input: InputComponent | EntitiesSelectionComponent = this.inputs.toArray().find(input => input.focused); let input: InputComponent | EntitiesSelectionComponent = this.inputs.toArray().find(input => input.focused);
if(!input) { if (!input) {
input = this.entities.toArray().find(input => input.input.focused); input = this.entities.toArray().find(input => input.input.focused);
} }
if(input) { if (input) {
if(event.code === 'Enter') { if (input instanceof EntitiesSelectionComponent) {
if(input instanceof EntitiesSelectionComponent) { input.input.focus(false, event);
input.input.focus(false, event); } else {
} else { input.focus(false, event);
input.focus(false, event);
}
event.preventDefault();
this.searchEmitter.emit();
} }
event.preventDefault();
this.searchEmitter.emit();
} }
} }

View File

@ -19,7 +19,15 @@ import {EnvProperties} from "../../utils/properties/env-properties";
import {properties} from "../../../../environments/environment"; import {properties} from "../../../../environments/environment";
import {ClickEvent} from "../../utils/click/click-outside-or-esc.directive"; import {ClickEvent} from "../../utils/click/click-outside-or-esc.directive";
export type InputType = 'text' | 'URL' | 'logoURL' | 'autocomplete' | 'autocomplete_soft' | 'textarea' | 'select' | 'chips'; export type InputType =
'text'
| 'URL'
| 'logoURL'
| 'autocomplete'
| 'autocomplete_soft'
| 'textarea'
| 'select'
| 'chips';
export interface Option { export interface Option {
icon?: string, icon?: string,
@ -62,7 +70,7 @@ declare var UIkit;
</ng-template> </ng-template>
<ng-template [ngIf]="type === 'textarea'"> <ng-template [ngIf]="type === 'textarea'">
<textarea #textArea class="input" [attr.placeholder]="placeholderInfo?.static?placeholderInfo.label:hint" <textarea #textArea class="input" [attr.placeholder]="placeholderInfo?.static?placeholderInfo.label:hint"
[rows]="rows" [formControl]="formAsControl"></textarea> [rows]="rows" [formControl]="formAsControl"></textarea>
</ng-template> </ng-template>
<ng-template [ngIf]="type === 'select'"> <ng-template [ngIf]="type === 'select'">
<ng-container *ngIf="placeholderInfo?.static"> <ng-container *ngIf="placeholderInfo?.static">
@ -70,7 +78,8 @@ declare var UIkit;
</ng-container> </ng-container>
<ng-container *ngIf="!placeholderInfo?.static"> <ng-container *ngIf="!placeholderInfo?.static">
<div *ngIf="!getLabel(formControl.value)" class="input uk-text-truncate">No value selected</div> <div *ngIf="!getLabel(formControl.value)" class="input uk-text-truncate">No value selected</div>
<div *ngIf="getLabel(formControl.value)" class="input uk-text-truncate">{{getLabel(formControl.value)}}</div> <div *ngIf="getLabel(formControl.value)"
class="input uk-text-truncate">{{getLabel(formControl.value)}}</div>
</ng-container> </ng-container>
</ng-template> </ng-template>
<ng-template [ngIf]="type === 'autocomplete'"> <ng-template [ngIf]="type === 'autocomplete'">
@ -84,31 +93,36 @@ declare var UIkit;
</ng-template> </ng-template>
<ng-template [ngIf]="type === 'chips'"> <ng-template [ngIf]="type === 'chips'">
<div class="uk-grid uk-grid-small uk-grid-row-collapse uk-width-expand" uk-grid> <div class="uk-grid uk-grid-small uk-grid-row-collapse uk-width-expand" uk-grid>
<div *ngFor="let chip of formAsArray.controls; let i=index" [class.uk-hidden]="!focused && i > 0" class="chip"> <div *ngFor="let chip of formAsArray.controls; let i=index" [class.uk-hidden]="!focused && i > 0"
class="chip">
<div class="uk-label uk-flex uk-flex-middle"> <div class="uk-label uk-flex uk-flex-middle">
<span class="uk-text-truncate uk-width-expand">{{getLabel(chip.value)}}</span> <span class="uk-text-truncate uk-width-expand">{{getLabel(chip.value)}}</span>
<icon (click)="remove(i, $event)" class="uk-link-text uk-margin-small-left clickable" [flex]="true" <icon (click)="remove(i, $event)" class="uk-link-text uk-margin-small-left clickable" [flex]="true"
name="close" ratio="0.7"></icon> name="close" ratio="0.7"></icon>
</div> </div>
</div> </div>
<div *ngIf="searchControl && (focused || formAsArray.length === 0)" class="uk-width-small uk-flex uk-flex-column uk-flex-center"> <div *ngIf="searchControl && (focused || formAsArray.length === 0)"
class="uk-width-small uk-flex uk-flex-column uk-flex-center">
<input #searchInput class="input" [class.search]="searchControl.value" <input #searchInput class="input" [class.search]="searchControl.value"
[attr.placeholder]="placeholderInfo?.static?placeholderInfo.label:hint" [attr.placeholder]="placeholderInfo?.static?placeholderInfo.label:hint"
[formControl]="searchControl" [class.uk-text-truncate]="!focused"> [formControl]="searchControl" [class.uk-text-truncate]="!focused">
</div> </div>
<div *ngIf="!focused && formAsArray.length > 1" class="uk-width-expand uk-flex uk-flex-column uk-flex-center more"> <div *ngIf="!focused && formAsArray.length > 1"
class="uk-width-expand uk-flex uk-flex-column uk-flex-center more">
+ {{(formAsArray.length - 1)}} more + {{(formAsArray.length - 1)}} more
</div> </div>
</div> </div>
</ng-template> </ng-template>
<div *ngIf="(formControl.disabled && disabledIcon) || icon || (type === 'select' && selectArrow) || type === 'autocomplete'" <div
class="uk-margin-left icon"> *ngIf="(formControl.disabled && disabledIcon) || icon || (type === 'select' && selectArrow) || type === 'autocomplete'"
class="uk-margin-left icon">
<icon *ngIf="formControl.disabled && disabledIcon" [name]="disabledIcon" [flex]="true"></icon> <icon *ngIf="formControl.disabled && disabledIcon" [name]="disabledIcon" [flex]="true"></icon>
<ng-template [ngIf]="formControl.enabled"> <ng-template [ngIf]="formControl.enabled">
<icon *ngIf="!formControl.value && icon" [name]="icon" [flex]="true"></icon> <icon *ngIf="!formControl.value && icon" [name]="icon" [flex]="true"></icon>
<icon *ngIf="!icon && type === 'select' && selectArrow" name="arrow_drop_down" [flex]="true"></icon> <icon *ngIf="!icon && type === 'select' && selectArrow" name="arrow_drop_down" [flex]="true"></icon>
<button *ngIf="searchControl?.value && type === 'autocomplete'" class="uk-close uk-icon" (click)="resetSearch($event)"> <button *ngIf="searchControl?.value && type === 'autocomplete'" class="uk-close uk-icon"
<icon [flex]="true" name="close"></icon> (click)="resetSearch($event)">
<icon [flex]="true" name="close"></icon>
</button> </button>
</ng-template> </ng-template>
</div> </div>
@ -121,7 +135,8 @@ declare var UIkit;
<div class="options uk-dropdown" *ngIf="filteredOptions && filteredOptions.length > 0 && opened" #optionBox <div class="options uk-dropdown" *ngIf="filteredOptions && filteredOptions.length > 0 && opened" #optionBox
uk-dropdown="pos: bottom-justify; mode: none; offset: 15; boundary-align: true;" [attr.boundary]="'#' + id"> uk-dropdown="pos: bottom-justify; mode: none; offset: 15; boundary-align: true;" [attr.boundary]="'#' + id">
<ul class="uk-nav uk-dropdown-nav"> <ul class="uk-nav uk-dropdown-nav">
<li *ngFor="let option of filteredOptions; let i=index" [class.uk-active]="(formControl.value === option.value) || selectedIndex === i" <li *ngFor="let option of filteredOptions; let i=index"
[class.uk-active]="(formControl.value === option.value) || selectedIndex === i"
[attr.uk-tooltip]="(tooltip)?('title: ' + option.label + ';'):null"> [attr.uk-tooltip]="(tooltip)?('title: ' + option.label + ';'):null">
<a (click)="selectOption(option, $event)">{{option.label}}</a> <a (click)="selectOption(option, $event)">{{option.label}}</a>
</li> </li>
@ -232,7 +247,7 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
@Input() @Input()
set options(options: (Option | string) []) { set options(options: (Option | string) []) {
this.optionsArray = options.map(option => { this.optionsArray = options.map(option => {
if(typeof option === 'string') { if (typeof option === 'string') {
return { return {
label: option, label: option,
value: option value: option
@ -249,27 +264,35 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
} }
} }
@HostListener('window:keydown', ['$event']) @HostListener('window:keydown.arrowUp', ['$event'])
keyEvent(event: KeyboardEvent) { arrowUp(event: KeyboardEvent) {
if(this.opened) { if (this.opened) {
if(event.code === 'ArrowUp') { event.preventDefault();
event.preventDefault(); if (this.selectedIndex > 0) {
if(this.selectedIndex > 0) { this.selectedIndex--;
this.selectedIndex--; this.optionBox.nativeElement.scrollBy(0, -34);
this.optionBox.nativeElement.scrollBy(0, -34); }
} }
} else if(event.code === 'ArrowDown') { }
event.preventDefault();
if(this.selectedIndex < (this.filteredOptions.length - 1)) { @HostListener('window:keydown.arrowDown', ['$event'])
this.selectedIndex++; arrowDown(event: KeyboardEvent) {
this.optionBox.nativeElement.scrollBy(0, 34); if (this.opened) {
} event.preventDefault();
} else if(event.code === 'Enter') { if (this.selectedIndex < (this.filteredOptions.length - 1)) {
event.preventDefault(); this.selectedIndex++;
if(this.filteredOptions[this.selectedIndex]) { this.optionBox.nativeElement.scrollBy(0, 34);
this.selectOption(this.filteredOptions[this.selectedIndex], event); }
this.open(false); }
} }
@HostListener('window:keydown.enter', ['$event'])
enter(event: KeyboardEvent) {
if (this.opened) {
event.preventDefault();
if (this.filteredOptions[this.selectedIndex]) {
this.selectOption(this.filteredOptions[this.selectedIndex], event);
this.open(false);
} }
} }
} }
@ -302,15 +325,15 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
} }
ngOnChanges(changes: SimpleChanges) { ngOnChanges(changes: SimpleChanges) {
if(this.formControl) { if (this.formControl) {
if (changes.value) { if (changes.value) {
this.formControl.setValue(this.value); this.formControl.setValue(this.value);
} }
if (changes.formControl || changes.validators || changes.options) { if (changes.formControl || changes.validators || changes.options) {
this.reset(); this.reset();
} }
if(changes.disabled) { if (changes.disabled) {
if(this.disabled) { if (this.disabled) {
this.formControl.disable(); this.formControl.disable();
} else { } else {
this.formControl.enable(); this.formControl.enable();
@ -371,7 +394,7 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
this.required = (validator && validator.required); this.required = (validator && validator.required);
} }
this.subscriptions.push(this.formControl.valueChanges.subscribe(value => { this.subscriptions.push(this.formControl.valueChanges.subscribe(value => {
if(this.formControl.enabled) { if (this.formControl.enabled) {
value = (value === '') ? null : value; value = (value === '') ? null : value;
if (this.type === 'logoURL') { if (this.type === 'logoURL') {
this.secure = (!value || value.includes('https://')); this.secure = (!value || value.includes('https://'));
@ -388,7 +411,7 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
this.open(true); this.open(true);
} }
} }
if((this.value && value && this.value !== value) || (!this.value && value) || this.value && !value) { if ((this.value && value && this.value !== value) || (!this.value && value) || this.value && !value) {
this.valueChange.emit(this.formControl.value); this.valueChange.emit(this.formControl.value);
} }
} }
@ -405,9 +428,9 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
} }
}); });
} }
remove(index: number, event) { remove(index: number, event) {
if(this.focused) { if (this.focused) {
this.formAsArray.removeAt(index); this.formAsArray.removeAt(index);
this.formAsArray.markAsDirty(); this.formAsArray.markAsDirty();
this.focus(true); this.focus(true);
@ -428,7 +451,7 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
const filterValue = value.toString().toLowerCase(); const filterValue = value.toString().toLowerCase();
options = options.filter(option => option.label.toLowerCase().indexOf(filterValue) != -1); options = options.filter(option => option.label.toLowerCase().indexOf(filterValue) != -1);
this.selectedIndex = options.findIndex(option => option.value === this.formControl.value); this.selectedIndex = options.findIndex(option => option.value === this.formControl.value);
if(this.selectedIndex === -1) { if (this.selectedIndex === -1) {
this.selectedIndex = 0; this.selectedIndex = 0;
} }
return options; return options;
@ -491,7 +514,7 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
if (this.optionBox) { if (this.optionBox) {
if (this.opened) { if (this.opened) {
this.selectedIndex = this.filteredOptions.findIndex(option => option.value === this.formControl.value); this.selectedIndex = this.filteredOptions.findIndex(option => option.value === this.formControl.value);
if(this.selectedIndex === -1) { if (this.selectedIndex === -1) {
this.selectedIndex = 0; this.selectedIndex = 0;
} }
UIkit.dropdown(this.optionBox.nativeElement).show(); UIkit.dropdown(this.optionBox.nativeElement).show();

View File

@ -58,13 +58,11 @@ export class SearchInputComponent implements OnInit {
constructor(private cdr: ChangeDetectorRef) { constructor(private cdr: ChangeDetectorRef) {
} }
@HostListener('window:keydown', ['$event']) @HostListener('window:keydown.enter', ['$event'])
keyEvent(event: KeyboardEvent) { enter(event: KeyboardEvent) {
if(this.input.focused) { if(this.input.focused) {
if(event.code === 'Enter') { event.preventDefault();
event.preventDefault(); this.search(event);
this.search(event);
}
} }
} }

View File

@ -13,7 +13,7 @@ export class ClickOutsideOrEsc {
constructor(private elementRef: ElementRef) {} constructor(private elementRef: ElementRef) {}
@HostListener('document:click', ['$event']) @HostListener('click', ['$event'])
click(event) { click(event) {
if(event.isTrusted) { if(event.isTrusted) {
this.clickOutside.emit({ this.clickOutside.emit({
@ -23,13 +23,11 @@ export class ClickOutsideOrEsc {
} }
} }
@HostListener('window:keydown', ['$event']) @HostListener('window:keydown.escape', ['$event'])
keyEvent(event: KeyboardEvent) { esc(event: KeyboardEvent) {
if(event.code === 'Escape') { this.clickOutside.emit({
this.clickOutside.emit({ event: event,
event: event, clicked: true
clicked: true });
});
}
} }
} }