import { debounceTime, map, auditTime, filter } from 'rxjs/operators';
import { Component, OnInit, EventEmitter, Output, ElementRef,
ViewChild, OnDestroy, NgZone, Injector, AfterViewInit, Input } from '@angular/core';
import { Subscription, fromEvent } from 'rxjs';
@Component({
selector: 'find-input',
template: `
`,
styles: [
`
.f-find-input {
width: 300px; height: 40px; background: #fefefe; position: absolute; right: 0; z-index: 10; padding: 5px;
border: 1px solid #d5d5d5;
box-shadow: 0px 1px 2px 0px #989898;
}
.f-find-input .input-group-text { border-right: 1px solid #ccc; font-size: 10px }
.f-find-input .input-group-append { background: #fff; }
.f-find-input .input-group-append button { padding:1px 6px;}
`
]
})
export class FindInputComponent implements OnInit, OnDestroy, AfterViewInit {
@Input() current = 0;
@Input() count = 0;
@Input() columns = [];
@Output() close = new EventEmitter();
@Output() valueChanged = new EventEmitter();
@Output() enterHandler = new EventEmitter();
@ViewChild('input') input: ElementRef;
findField = '';
private keyupEventSub: Subscription = null;
constructor(private el: ElementRef, private ngZone: NgZone, private injector: Injector) { }
ngOnInit() {
if (this.columns.length) {
this.columns = this.columns.filter(c => !c.formatter && c.field);
this.findField = this.columns[0].field;
}
this.ngZone.runOutsideAngular(() => {
this.keyupEventSub = fromEvent(this.input.nativeElement, 'keyup').pipe(
filter((e: any) => (e.switch || e.keyCode) !== 13),
debounceTime(200),
map((e: any) => e.target.value)
).subscribe((v) => {
this.valueChanged.emit({value: v, field: this.findField});
});
});
}
ngAfterViewInit() {
this.input.nativeElement.focus();
}
ngOnDestroy() {
if (this.keyupEventSub) {
this.keyupEventSub.unsubscribe();
}
this.keyupEventSub = null;
}
onClose(event) {
this.close.emit();
return false;
}
onEnter($event, step) {
this.enterHandler.emit(step);
return false;
}
}