2021-11-27 22:21:52 +08:00
|
|
|
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';
|
2021-12-23 15:59:49 +08:00
|
|
|
|
|
|
|
|
import { Monitor } from '../../../pojo/Monitor';
|
|
|
|
|
import { MonitorService } from '../../../service/monitor.service';
|
2021-11-27 22:21:52 +08:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'header-search',
|
|
|
|
|
template: `
|
|
|
|
|
<nz-input-group [nzPrefix]="iconTpl" [nzSuffix]="loadingTpl">
|
|
|
|
|
<ng-template #iconTpl>
|
|
|
|
|
<i nz-icon [nzType]="focus ? 'arrow-down' : 'search'"></i>
|
|
|
|
|
</ng-template>
|
|
|
|
|
<ng-template #loadingTpl>
|
|
|
|
|
<i *ngIf="loading" nz-icon nzType="loading"></i>
|
|
|
|
|
</ng-template>
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
nz-input
|
|
|
|
|
[(ngModel)]="q"
|
|
|
|
|
[nzAutocomplete]="auto"
|
|
|
|
|
(input)="search($event)"
|
|
|
|
|
(focus)="qFocus()"
|
|
|
|
|
(blur)="qBlur()"
|
|
|
|
|
[attr.placeholder]="'menu.search.placeholder' | i18n"
|
|
|
|
|
/>
|
|
|
|
|
</nz-input-group>
|
2021-12-19 16:08:18 +08:00
|
|
|
<nz-autocomplete nzBackfill="false" nzDefaultActiveFirstOption #auto>
|
|
|
|
|
<nz-auto-option *ngFor="let option of options" [nzValue]="option.id" [nzLabel]="option.name">
|
|
|
|
|
<a [routerLink]="['/monitors/' + option.id]">
|
|
|
|
|
监控名称: {{ option.name }}
|
2021-12-23 15:59:49 +08:00
|
|
|
<span style="left:50% ; position: absolute;">监控Host: {{ option.host }}</span>
|
2021-12-19 16:08:18 +08:00
|
|
|
<span style="right: 10px; position: absolute;"><i nz-icon nzType="arrow-right" nzTheme="outline"></i></span>
|
|
|
|
|
</a>
|
|
|
|
|
</nz-auto-option>
|
2021-11-27 22:21:52 +08:00
|
|
|
</nz-autocomplete>
|
|
|
|
|
`,
|
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
|
|
|
|
})
|
|
|
|
|
export class HeaderSearchComponent implements AfterViewInit, OnDestroy {
|
|
|
|
|
q = '';
|
|
|
|
|
qIpt: HTMLInputElement | null = null;
|
2021-12-19 16:08:18 +08:00
|
|
|
options: Monitor[] = [];
|
2021-11-27 22:21:52 +08:00
|
|
|
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<boolean>();
|
|
|
|
|
|
2021-12-23 15:59:49 +08:00
|
|
|
constructor(private el: ElementRef<HTMLElement>, private cdr: ChangeDetectorRef, private monitorSvc: MonitorService) {}
|
2021-11-27 22:21:52 +08:00
|
|
|
|
|
|
|
|
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 => {
|
2021-12-19 16:08:18 +08:00
|
|
|
// 远程加载搜索数据
|
2022-03-15 16:21:25 +08:00
|
|
|
let searchMonitors$ = this.monitorSvc.searchMonitors(null, value, 9, 0, 10).subscribe(
|
2021-12-23 15:59:49 +08:00
|
|
|
message => {
|
2021-12-19 16:08:18 +08:00
|
|
|
this.loading = false;
|
|
|
|
|
searchMonitors$.unsubscribe();
|
|
|
|
|
if (message.code === 0) {
|
|
|
|
|
let page = message.data;
|
2022-03-15 15:00:22 +08:00
|
|
|
if (page.content != undefined) {
|
|
|
|
|
this.options = page.content;
|
|
|
|
|
} else {
|
|
|
|
|
this.options = [];
|
|
|
|
|
}
|
2021-12-19 16:08:18 +08:00
|
|
|
this.cdr.detectChanges();
|
|
|
|
|
} else {
|
|
|
|
|
console.warn(message.msg);
|
|
|
|
|
}
|
2021-12-23 15:59:49 +08:00
|
|
|
},
|
|
|
|
|
error => {
|
2021-12-19 16:08:18 +08:00
|
|
|
this.loading = false;
|
|
|
|
|
searchMonitors$.unsubscribe();
|
|
|
|
|
console.error(error.msg);
|
2021-12-23 15:59:49 +08:00
|
|
|
}
|
|
|
|
|
);
|
2021-11-27 22:21:52 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qFocus(): void {
|
|
|
|
|
this.focus = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qBlur(): void {
|
|
|
|
|
this.focus = false;
|
|
|
|
|
this.searchToggled = false;
|
2022-03-15 15:00:22 +08:00
|
|
|
this.options = [];
|
2021-11-27 22:21:52 +08:00
|
|
|
this.toggleChangeChange.emit(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
search(ev: Event): void {
|
|
|
|
|
this.search$.next((ev.target as HTMLInputElement).value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
|
this.search$.complete();
|
|
|
|
|
this.search$.unsubscribe();
|
|
|
|
|
}
|
|
|
|
|
}
|