Newer
Older
import { Component, OnDestroy, OnInit, Output, EventEmitter } from '@angular/core';
import { Subject } from 'rxjs';
import { ActivatedRoute, Router } from '@angular/router';
import { QueryParam, QueryParamBuilder, QueryParamGroup } from '@ngqp/core';
import { takeUntil } from 'rxjs/operators';
import { SearchInput } from 'app/shared/model/search-input.model';
@Component({
selector: 'jhi-search-input',
templateUrl: './search-input.component.html',
styleUrls: ['./search-input.component.scss'],
})
export class SearchInputComponent implements OnInit, OnDestroy {
private static DEBOUNCE_TIME = 200;
@Output() searchInputEvent = new EventEmitter<SearchInput>();
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public pageSize = 4;
public paramGroup: QueryParamGroup;
private componentDestroyed$ = new Subject<void>();
public searchInput: SearchInput;
constructor(protected activatedRoute: ActivatedRoute, private router: Router, private qpb: QueryParamBuilder) {
this.searchInput = new SearchInput();
this.paramGroup = qpb.group({
searchText: qpb.stringParam('q', {
debounceTime: SearchInputComponent.DEBOUNCE_TIME,
emptyOn: '',
}),
page: qpb.numberParam('page', {
emptyOn: 1,
}),
programmingLanguage: qpb.stringParam('pl', {
debounceTime: SearchInputComponent.DEBOUNCE_TIME,
emptyOn: '',
}),
keyword: qpb.stringParam('kw', {
debounceTime: SearchInputComponent.DEBOUNCE_TIME,
emptyOn: '',
}),
author: qpb.stringParam('a', {
debounceTime: SearchInputComponent.DEBOUNCE_TIME,
emptyOn: '',
}),
license: qpb.stringParam('l', {
debounceTime: SearchInputComponent.DEBOUNCE_TIME,
emptyOn: '',
}),
});
this.paramGroup.valueChanges.pipe(takeUntil(this.componentDestroyed$)).subscribe(value => {
this.searchInput.setValues(value);
this.searchInputEvent.emit(this.searchInput);
ngOnDestroy(): void {
this.componentDestroyed$.next();
this.componentDestroyed$.complete();
}
public get pageParam(): QueryParam<number> {
return this.paramGroup.get('page') as QueryParam<number>;
}
public onPageChange(page: number): void {
this.pageParam.setValue(page);
}