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">
|
|
|
|
<div *ngIf="control" class="uk-width-expand" [class.bordered]="bordered && showSearch">
|
2021-01-11 16:40:51 +01:00
|
|
|
<form (ngSubmit)="search()">
|
|
|
|
<input #input type="text" class="uk-width-1-1"
|
2021-01-11 19:33:24 +01:00
|
|
|
[class.uk-animation-slide-right-medium]="showSearch"
|
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-02-24 15:35:02 +01:00
|
|
|
<div *ngIf="selected && !showSearch" class="uk-flex uk-flex-left selected">
|
|
|
|
<span class="uk-flex uk-flex-middle clickable">
|
|
|
|
<span (click)="toggle()">{{selected}}</span>
|
|
|
|
<span class="uk-icon space" uk-icon="icon: close; ratio: 0.8" (click)="resetEmitter.emit()">
|
2021-01-11 16:40:51 +01:00
|
|
|
<svg width="16" height="16" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" data-svg="close"><path
|
|
|
|
fill="none" stroke="#000" stroke-width="1.06" d="M16,16 L4,4"></path>
|
|
|
|
<path fill="none" stroke="#000" stroke-width="1.06" d="M16,4 L4,16"></path>
|
|
|
|
</svg>
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<button [disabled]="loading" class="search uk-flex uk-flex-middle uk-margin-medium-left uk-visible@m"
|
|
|
|
(mousedown)="$event.preventDefault()" (click)="toggle()">
|
|
|
|
<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"
|
|
|
|
(mousedown)="$event.preventDefault()" (click)="search()">
|
|
|
|
<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);
|
|
|
|
}
|
|
|
|
}
|