import { AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, EventEmitter, HostBinding, Input, OnDestroy, Output } from '@angular/core'; import { BehaviorSubject } from 'rxjs'; import { debounceTime, distinctUntilChanged, tap } from 'rxjs/operators'; import { Monitor } from '../../../pojo/Monitor'; import { MonitorService } from '../../../service/monitor.service'; @Component({ selector: 'header-search', template: ` 监控名称: {{ option.name }} 监控Host: {{ option.host }} `, changeDetection: ChangeDetectionStrategy.OnPush }) export class HeaderSearchComponent implements AfterViewInit, OnDestroy { q = ''; qIpt: HTMLInputElement | null = null; options: Monitor[] = []; search$ = new BehaviorSubject(''); loading = false; @HostBinding('class.alain-default__search-focus') focus = false; @HostBinding('class.alain-default__search-toggled') searchToggled = false; @Input() set toggleChange(value: boolean) { if (typeof value === 'undefined') { return; } this.searchToggled = value; this.focus = value; if (value) { setTimeout(() => this.qIpt!.focus()); } } @Output() readonly toggleChangeChange = new EventEmitter(); constructor(private el: ElementRef, private cdr: ChangeDetectorRef, private monitorSvc: MonitorService) {} ngAfterViewInit(): void { this.qIpt = this.el.nativeElement.querySelector('.ant-input') as HTMLInputElement; this.search$ .pipe( debounceTime(500), distinctUntilChanged(), tap({ complete: () => { this.loading = true; } }) ) .subscribe(value => { // 远程加载搜索数据 let searchMonitors$ = this.monitorSvc.searchMonitors(null, value, 9, 0, 10).subscribe( message => { this.loading = false; searchMonitors$.unsubscribe(); if (message.code === 0) { let page = message.data; if (page.content != undefined) { this.options = page.content; } else { this.options = []; } this.cdr.detectChanges(); } else { console.warn(message.msg); } }, error => { this.loading = false; searchMonitors$.unsubscribe(); console.error(error.msg); } ); }); } qFocus(): void { this.focus = true; } qBlur(): void { this.focus = false; this.searchToggled = false; this.options = []; this.toggleChangeChange.emit(false); } search(ev: Event): void { this.search$.next((ev.target as HTMLInputElement).value); } ngOnDestroy(): void { this.search$.complete(); this.search$.unsubscribe(); } }