2022-04-06 17:24:14 +02:00
|
|
|
import {
|
2023-05-12 16:25:14 +02:00
|
|
|
AfterViewInit,
|
2022-04-06 17:24:14 +02:00
|
|
|
ChangeDetectorRef,
|
|
|
|
Component,
|
|
|
|
ElementRef,
|
|
|
|
EventEmitter,
|
|
|
|
HostListener,
|
|
|
|
Input,
|
|
|
|
OnInit,
|
|
|
|
Output,
|
|
|
|
ViewChild
|
|
|
|
} from '@angular/core';
|
2021-01-11 16:40:51 +01:00
|
|
|
import {AbstractControl} from '@angular/forms';
|
2022-04-06 17:24:14 +02:00
|
|
|
import {InputComponent} from "../input/input.component";
|
2022-04-13 12:00:40 +02:00
|
|
|
import {ClickEvent} from "../../utils/click/click-outside-or-esc.directive";
|
2021-01-11 16:40:51 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: '[search-input]',
|
|
|
|
template: `
|
2022-04-15 14:38:41 +02:00
|
|
|
<div *ngIf="initialized" class="uk-flex uk-flex-right uk-width-1-1">
|
2022-10-12 11:23:24 +02:00
|
|
|
<div #searchInput click-outside-or-esc (clickOutside)="click($event)" class="search-input" [class.focused]="input.focused"
|
|
|
|
[class.collapsed]="hidden" [ngClass]="searchInputClass">
|
2022-04-07 00:09:49 +02:00
|
|
|
<div class="uk-flex uk-flex-middle">
|
|
|
|
<div class="uk-width-expand">
|
2022-12-07 15:56:16 +01:00
|
|
|
<div #input [class.uk-hidden]="hidden" input [formInput]="searchControl" [inputClass]="'search'+(iconPosition === 'left'?' icon-left':'')" [disabledIcon]="null"
|
2022-04-13 12:00:40 +02:00
|
|
|
[placeholder]="{label: placeholder, static: true}" [value]="value" (valueChange)="valueChange.emit($event)"
|
2022-06-09 14:25:24 +02:00
|
|
|
[disabled]="disabled" [showOptionsOnEmpty]="false" [type]="(options.length > 0?'autocomplete_soft':'text')" [options]="options"></div>
|
2022-04-07 00:09:49 +02:00
|
|
|
</div>
|
2022-04-12 16:20:51 +02:00
|
|
|
<div [class.uk-hidden]="(!searchControl?.value && !value) || disabled" class="uk-width-auto">
|
2022-04-15 10:08:12 +02:00
|
|
|
<button class="uk-close uk-icon" (click)="reset()">
|
2022-04-07 00:09:49 +02:00
|
|
|
<icon name="close" [flex]="true"></icon>
|
2022-04-15 10:08:12 +02:00
|
|
|
</button>
|
2022-04-07 00:09:49 +02:00
|
|
|
</div>
|
2022-10-12 11:23:24 +02:00
|
|
|
<div class="uk-width-auto" [class.uk-flex-first]="iconPosition === 'left'">
|
2022-04-15 10:08:12 +02:00
|
|
|
<div class="search-icon" [class.disabled]="disabled" (click)="search($event)">
|
2023-05-12 16:25:14 +02:00
|
|
|
<icon name="search" [flex]="true" [ratio]="ratio"></icon>
|
2022-04-07 00:09:49 +02:00
|
|
|
</div>
|
2022-04-06 17:24:14 +02:00
|
|
|
</div>
|
2022-12-07 15:56:16 +01:00
|
|
|
<div class="uk-width-auto filters-toggle">
|
|
|
|
<ng-content select="[filters-toggle]"></ng-content>
|
|
|
|
</div>
|
2021-01-11 16:40:51 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-04-06 17:24:14 +02:00
|
|
|
</div>
|
|
|
|
`
|
2021-01-11 16:40:51 +01:00
|
|
|
})
|
2023-05-12 16:25:14 +02:00
|
|
|
export class SearchInputComponent implements OnInit, AfterViewInit {
|
2022-04-06 17:24:14 +02:00
|
|
|
@Input() disabled: boolean = false;
|
|
|
|
@Input() searchInputClass: string = 'inner';
|
2022-10-12 11:23:24 +02:00
|
|
|
@Input() iconPosition: 'left' | 'right' = 'right';
|
2022-04-06 17:24:14 +02:00
|
|
|
@Input() searchControl: AbstractControl;
|
|
|
|
@Input() value: string;
|
2022-04-08 14:47:03 +02:00
|
|
|
@Output() valueChange = new EventEmitter<string>();
|
2022-04-12 16:20:51 +02:00
|
|
|
@Input() options: string[] = [];
|
2022-04-06 17:24:14 +02:00
|
|
|
@Input() placeholder: string;
|
|
|
|
@Input() expandable: boolean = false;
|
|
|
|
@Output() searchEmitter: EventEmitter<void> = new EventEmitter<void>();
|
|
|
|
@ViewChild('searchInput') searchInput: ElementRef;
|
|
|
|
@ViewChild('input') input: InputComponent;
|
|
|
|
public expanded: boolean = true;
|
2022-04-15 14:38:41 +02:00
|
|
|
public initialized: boolean = false;
|
2023-05-12 16:25:14 +02:00
|
|
|
public ratio: number = 1;
|
2022-04-06 17:24:14 +02:00
|
|
|
|
|
|
|
constructor(private cdr: ChangeDetectorRef) {
|
|
|
|
}
|
|
|
|
|
2022-04-17 14:25:01 +02:00
|
|
|
@HostListener('window:keydown.enter', ['$event'])
|
|
|
|
enter(event: KeyboardEvent) {
|
2022-06-09 14:25:24 +02:00
|
|
|
if(this.input.focused && !this.input.opened) {
|
2022-04-17 14:25:01 +02:00
|
|
|
event.preventDefault();
|
|
|
|
this.search(event);
|
2022-06-09 14:25:24 +02:00
|
|
|
} else {
|
|
|
|
this.input.enter(event);
|
|
|
|
event.stopPropagation();
|
|
|
|
this.search(event);
|
2022-04-07 14:12:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 12:00:40 +02:00
|
|
|
click(event: ClickEvent) {
|
|
|
|
if(this.expandable && !this.disabled) {
|
|
|
|
this.expand(!event.clicked);
|
2022-04-06 17:24:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.expanded = !this.expandable;
|
2022-04-15 14:38:41 +02:00
|
|
|
this.initialized = true;
|
2022-04-06 17:24:14 +02:00
|
|
|
}
|
|
|
|
|
2023-05-12 16:25:14 +02:00
|
|
|
ngAfterViewInit() {
|
2023-05-16 11:28:35 +02:00
|
|
|
if(typeof document !== 'undefined') {
|
|
|
|
this.ratio = Number.parseFloat(getComputedStyle(this.searchInput.nativeElement).getPropertyValue('--search-input-icon-ratio'));
|
2023-10-16 10:44:18 +02:00
|
|
|
this.cdr.detectChanges();
|
2023-05-16 11:28:35 +02:00
|
|
|
}
|
2023-05-12 16:25:14 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:24:14 +02:00
|
|
|
expand(value: boolean) {
|
|
|
|
this.expanded = value;
|
|
|
|
this.cdr.detectChanges();
|
|
|
|
if(this.expanded) {
|
|
|
|
this.input.focus(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 10:08:12 +02:00
|
|
|
public search(event) {
|
2022-04-07 00:09:49 +02:00
|
|
|
if(!this.disabled) {
|
|
|
|
this.searchEmitter.emit();
|
2022-04-15 10:08:12 +02:00
|
|
|
if(this.expandable) {
|
|
|
|
this.expand(!this.expanded);
|
|
|
|
}
|
|
|
|
event.stopPropagation();
|
2022-04-06 17:24:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public reset() {
|
2022-04-12 16:20:51 +02:00
|
|
|
if(this.searchControl){
|
|
|
|
this.searchControl.setValue('');
|
|
|
|
} else {
|
|
|
|
this.valueChange.emit('');
|
|
|
|
}
|
2022-06-09 14:25:24 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
this.input.focus(true);
|
|
|
|
}, 100)
|
2022-04-06 17:24:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get hidden(): boolean {
|
2022-04-15 10:08:12 +02:00
|
|
|
return !this.expanded && (!this.searchControl?.value && !this.value);
|
2022-04-06 17:24:14 +02:00
|
|
|
}
|
2021-01-11 16:40:51 +01:00
|
|
|
}
|