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({
selector: 'advanced-search-input',
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-width-expand">
<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;
@Output() searchEmitter: EventEmitter<void> = new EventEmitter<void>();
@HostListener('window:keydown', ['$event'])
keyEvent(event: KeyboardEvent) {
@HostListener('window:keydown.enter', ['$event'])
enter(event: KeyboardEvent) {
let input: InputComponent | EntitiesSelectionComponent = this.inputs.toArray().find(input => input.focused);
if(!input) {
if (!input) {
input = this.entities.toArray().find(input => input.input.focused);
}
if(input) {
if(event.code === 'Enter') {
if(input instanceof EntitiesSelectionComponent) {
input.input.focus(false, event);
} else {
input.focus(false, event);
}
event.preventDefault();
this.searchEmitter.emit();
if (input) {
if (input instanceof EntitiesSelectionComponent) {
input.input.focus(false, event);
} else {
input.focus(false, event);
}
event.preventDefault();
this.searchEmitter.emit();
}
}

View File

@ -19,7 +19,15 @@ import {EnvProperties} from "../../utils/properties/env-properties";
import {properties} from "../../../../environments/environment";
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 {
icon?: string,
@ -62,7 +70,7 @@ declare var UIkit;
</ng-template>
<ng-template [ngIf]="type === 'textarea'">
<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 [ngIf]="type === 'select'">
<ng-container *ngIf="placeholderInfo?.static">
@ -70,7 +78,8 @@ declare var UIkit;
</ng-container>
<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">{{getLabel(formControl.value)}}</div>
<div *ngIf="getLabel(formControl.value)"
class="input uk-text-truncate">{{getLabel(formControl.value)}}</div>
</ng-container>
</ng-template>
<ng-template [ngIf]="type === 'autocomplete'">
@ -84,31 +93,36 @@ declare var UIkit;
</ng-template>
<ng-template [ngIf]="type === 'chips'">
<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">
<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"
name="close" ratio="0.7"></icon>
</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"
[attr.placeholder]="placeholderInfo?.static?placeholderInfo.label:hint"
[formControl]="searchControl" [class.uk-text-truncate]="!focused">
</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
</div>
</div>
</ng-template>
<div *ngIf="(formControl.disabled && disabledIcon) || icon || (type === 'select' && selectArrow) || type === 'autocomplete'"
class="uk-margin-left icon">
<div
*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>
<ng-template [ngIf]="formControl.enabled">
<icon *ngIf="!formControl.value && icon" [name]="icon" [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)">
<icon [flex]="true" name="close"></icon>
<button *ngIf="searchControl?.value && type === 'autocomplete'" class="uk-close uk-icon"
(click)="resetSearch($event)">
<icon [flex]="true" name="close"></icon>
</button>
</ng-template>
</div>
@ -121,7 +135,8 @@ declare var UIkit;
<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">
<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">
<a (click)="selectOption(option, $event)">{{option.label}}</a>
</li>
@ -232,7 +247,7 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
@Input()
set options(options: (Option | string) []) {
this.optionsArray = options.map(option => {
if(typeof option === 'string') {
if (typeof option === 'string') {
return {
label: option,
value: option
@ -249,27 +264,35 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
}
}
@HostListener('window:keydown', ['$event'])
keyEvent(event: KeyboardEvent) {
if(this.opened) {
if(event.code === 'ArrowUp') {
event.preventDefault();
if(this.selectedIndex > 0) {
this.selectedIndex--;
this.optionBox.nativeElement.scrollBy(0, -34);
}
} else if(event.code === 'ArrowDown') {
event.preventDefault();
if(this.selectedIndex < (this.filteredOptions.length - 1)) {
this.selectedIndex++;
this.optionBox.nativeElement.scrollBy(0, 34);
}
} else if(event.code === 'Enter') {
event.preventDefault();
if(this.filteredOptions[this.selectedIndex]) {
this.selectOption(this.filteredOptions[this.selectedIndex], event);
this.open(false);
}
@HostListener('window:keydown.arrowUp', ['$event'])
arrowUp(event: KeyboardEvent) {
if (this.opened) {
event.preventDefault();
if (this.selectedIndex > 0) {
this.selectedIndex--;
this.optionBox.nativeElement.scrollBy(0, -34);
}
}
}
@HostListener('window:keydown.arrowDown', ['$event'])
arrowDown(event: KeyboardEvent) {
if (this.opened) {
event.preventDefault();
if (this.selectedIndex < (this.filteredOptions.length - 1)) {
this.selectedIndex++;
this.optionBox.nativeElement.scrollBy(0, 34);
}
}
}
@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) {
if(this.formControl) {
if (this.formControl) {
if (changes.value) {
this.formControl.setValue(this.value);
}
if (changes.formControl || changes.validators || changes.options) {
this.reset();
}
if(changes.disabled) {
if(this.disabled) {
if (changes.disabled) {
if (this.disabled) {
this.formControl.disable();
} else {
this.formControl.enable();
@ -371,7 +394,7 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
this.required = (validator && validator.required);
}
this.subscriptions.push(this.formControl.valueChanges.subscribe(value => {
if(this.formControl.enabled) {
if (this.formControl.enabled) {
value = (value === '') ? null : value;
if (this.type === 'logoURL') {
this.secure = (!value || value.includes('https://'));
@ -388,7 +411,7 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
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);
}
}
@ -405,9 +428,9 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
}
});
}
remove(index: number, event) {
if(this.focused) {
if (this.focused) {
this.formAsArray.removeAt(index);
this.formAsArray.markAsDirty();
this.focus(true);
@ -428,7 +451,7 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
const filterValue = value.toString().toLowerCase();
options = options.filter(option => option.label.toLowerCase().indexOf(filterValue) != -1);
this.selectedIndex = options.findIndex(option => option.value === this.formControl.value);
if(this.selectedIndex === -1) {
if (this.selectedIndex === -1) {
this.selectedIndex = 0;
}
return options;
@ -491,7 +514,7 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
if (this.optionBox) {
if (this.opened) {
this.selectedIndex = this.filteredOptions.findIndex(option => option.value === this.formControl.value);
if(this.selectedIndex === -1) {
if (this.selectedIndex === -1) {
this.selectedIndex = 0;
}
UIkit.dropdown(this.optionBox.nativeElement).show();

View File

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

View File

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