2022-04-06 17:24:14 +02:00
|
|
|
import {
|
|
|
|
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';
|
|
|
|
import {MatAutocompleteTrigger} from '@angular/material/autocomplete';
|
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-04-13 12:00:40 +02:00
|
|
|
<div #searchInput click-outside-or-esc (clickOutside)="click($event)" class="search-input" [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-04-12 16:20:51 +02:00
|
|
|
<div #input [class.uk-hidden]="hidden" input [formInput]="searchControl" inputClass="search" [disabledIcon]="null"
|
2022-04-13 12:00:40 +02:00
|
|
|
[placeholder]="{label: placeholder, static: true}" [value]="value" (valueChange)="valueChange.emit($event)"
|
2022-04-11 11:27:10 +02:00
|
|
|
[disabled]="disabled" [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>
|
|
|
|
<div class="uk-width-auto">
|
2022-04-15 10:08:12 +02:00
|
|
|
<div class="search-icon" [class.disabled]="disabled" (click)="search($event)">
|
2022-04-07 00:09:49 +02:00
|
|
|
<icon name="search" [flex]="true" ratio="1.3"></icon>
|
|
|
|
</div>
|
2022-04-06 17:24:14 +02:00
|
|
|
</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
|
|
|
})
|
2022-04-06 17:24:14 +02:00
|
|
|
export class SearchInputComponent implements OnInit {
|
|
|
|
@Input() disabled: boolean = false;
|
|
|
|
@Input() searchInputClass: string = 'inner';
|
|
|
|
@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;
|
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-04-07 14:12:25 +02:00
|
|
|
if(this.input.focused) {
|
2022-04-17 14:25:01 +02:00
|
|
|
event.preventDefault();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
/** @deprecated all*/
|
2021-01-11 16:40:51 +01:00
|
|
|
@Input()
|
|
|
|
showSearch: boolean = true;
|
|
|
|
@Input()
|
|
|
|
control: AbstractControl;
|
|
|
|
@Input()
|
|
|
|
loading: boolean = false;
|
|
|
|
@Input()
|
|
|
|
selected: any;
|
|
|
|
@Input()
|
|
|
|
list: any = null;
|
|
|
|
@Input()
|
|
|
|
colorClass: string = 'portal-color';
|
2021-01-11 19:33:24 +01:00
|
|
|
@Input()
|
|
|
|
bordered: boolean = false;
|
2021-01-21 16:15:53 +01:00
|
|
|
@Input()
|
|
|
|
toggleTitle: string = 'search';
|
2021-01-11 16:40:51 +01:00
|
|
|
@ViewChild(MatAutocompleteTrigger) trigger: MatAutocompleteTrigger;
|
|
|
|
@Output()
|
|
|
|
resetEmitter: EventEmitter<any> = new EventEmitter<any>();
|
2021-02-02 13:29:18 +01:00
|
|
|
@Output()
|
|
|
|
closeEmitter: EventEmitter<any> = new EventEmitter<any>();
|
2021-01-11 16:40:51 +01:00
|
|
|
|
2022-04-06 17:24:14 +02:00
|
|
|
/** @deprecated*/
|
2021-01-11 16:40:51 +01:00
|
|
|
closeSearch() {
|
2022-04-06 17:24:14 +02:00
|
|
|
|
2021-01-11 16:40:51 +01:00
|
|
|
}
|
|
|
|
}
|