2021-01-11 16:40:51 +01:00
|
|
|
import {Component, ElementRef, EventEmitter, Input, Output, ViewChild} from '@angular/core';
|
|
|
|
import {AbstractControl} from '@angular/forms';
|
|
|
|
import {MatAutocompleteTrigger} from '@angular/material/autocomplete';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: '[search-input]',
|
|
|
|
styleUrls: ['search-input.component.css'],
|
|
|
|
template: `
|
2021-02-02 13:29:18 +01:00
|
|
|
<div class="uk-flex uk-flex-middle uk-flex-center search-input" [ngClass]="colorClass">
|
2021-03-23 23:03:10 +01:00
|
|
|
<div *ngIf="control" class="uk-width-expand" [class.bordered]="bordered && (showSearch || selected)" [class.focused]="showSearch">
|
2021-01-11 16:40:51 +01:00
|
|
|
<form (ngSubmit)="search()">
|
|
|
|
<input #input type="text" class="uk-width-1-1"
|
2021-07-14 13:19:57 +02:00
|
|
|
[class.uk-animation-slide-right-medium]="showSearch && !selected"
|
2021-01-11 16:40:51 +01:00
|
|
|
[class.uk-hidden@m]="!showSearch"
|
|
|
|
[placeholder]="placeholder"
|
|
|
|
(blur)="closeSearch()"
|
|
|
|
[formControl]="control"
|
|
|
|
[matAutocomplete]="auto">
|
|
|
|
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="search()">
|
|
|
|
<mat-option *ngFor="let option of list | async" [value]="option">
|
|
|
|
{{option}}
|
|
|
|
</mat-option>
|
|
|
|
</mat-autocomplete>
|
|
|
|
</form>
|
2021-03-22 16:10:18 +01:00
|
|
|
<div *ngIf="selected && !showSearch" class="uk-flex selected uk-padding-small uk-visible@m">
|
2021-03-23 17:28:38 +01:00
|
|
|
<span class="uk-flex uk-flex-middle clickable" style="max-width: 100%" [class.uk-width-1-1]="bordered">
|
2021-03-22 16:10:18 +01:00
|
|
|
<span class="uk-width-expand uk-text-truncate" (click)="toggle()">{{selected}}</span>
|
|
|
|
<icon name="close" class="space" [ratio]="0.8" (click)="resetEmitter.emit()"></icon>
|
2021-01-11 16:40:51 +01:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<button [disabled]="loading" class="search uk-flex uk-flex-middle uk-margin-medium-left uk-visible@m"
|
2021-03-23 23:03:10 +01:00
|
|
|
(mousedown)="$event.preventDefault()" (click)="toggle()"
|
|
|
|
[attr.uk-tooltip]="'title: '+placeholder+'; cls: uk-padding-small uk-active'">
|
2021-01-11 16:40:51 +01:00
|
|
|
<span [ngClass]="colorClass" class="icon-bg">
|
|
|
|
<icon class="uk-position-center" name="search"></icon>
|
|
|
|
</span>
|
2021-01-21 16:15:53 +01:00
|
|
|
<span class="uk-text-uppercase overlay">{{toggleTitle}}</span>
|
2021-01-11 16:40:51 +01:00
|
|
|
</button>
|
|
|
|
<button [disabled]="loading" class="search uk-flex uk-flex-middle uk-hidden@m"
|
2021-03-23 23:03:10 +01:00
|
|
|
(mousedown)="$event.preventDefault()" (click)="search()"
|
|
|
|
[attr.uk-tooltip]="'title: '+placeholder+'; cls: uk-padding-small uk-active'">
|
2021-01-11 16:40:51 +01:00
|
|
|
<span [ngClass]="colorClass" class="icon-bg">
|
|
|
|
<icon class="uk-position-center" name="search"></icon>
|
|
|
|
</span>
|
2021-01-21 16:15:53 +01:00
|
|
|
<span class="uk-text-uppercase overlay">{{toggleTitle}}</span>
|
2021-01-11 16:40:51 +01:00
|
|
|
</button>
|
|
|
|
</div>`
|
|
|
|
})
|
|
|
|
export class SearchInputComponent {
|
|
|
|
@Input()
|
|
|
|
showSearch: boolean = true;
|
|
|
|
@Input()
|
|
|
|
control: AbstractControl;
|
|
|
|
@Input()
|
|
|
|
placeholder: string;
|
|
|
|
@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('input') input: ElementRef;
|
|
|
|
@ViewChild(MatAutocompleteTrigger) trigger: MatAutocompleteTrigger;
|
|
|
|
@Output()
|
|
|
|
searchEmitter: EventEmitter<boolean> = new EventEmitter<boolean>();
|
|
|
|
@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
|
|
|
|
|
|
|
toggle() {
|
2021-02-24 15:35:02 +01:00
|
|
|
this.showSearch = !this.showSearch;
|
|
|
|
if (this.showSearch) {
|
|
|
|
setTimeout(() => { // this will make the execution after the above boolean has changed
|
|
|
|
this.input.nativeElement.focus();
|
|
|
|
}, 0);
|
2021-01-11 16:40:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closeSearch() {
|
|
|
|
this.showSearch = false;
|
2021-02-02 13:29:18 +01:00
|
|
|
this.closeEmitter.emit();
|
2021-01-11 16:40:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public search(emit = true) {
|
|
|
|
this.trigger.closePanel();
|
|
|
|
this.searchEmitter.emit(emit);
|
|
|
|
}
|
|
|
|
|
|
|
|
public reset() {
|
2021-02-24 15:35:02 +01:00
|
|
|
this.control.setValue('');
|
|
|
|
this.input.nativeElement.value = '';
|
2021-01-11 16:40:51 +01:00
|
|
|
this.showSearch = true;
|
|
|
|
setTimeout(() => {
|
|
|
|
this.input.nativeElement.focus();
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
}
|