126 lines
3.4 KiB
TypeScript
126 lines
3.4 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { FormControl } from '@angular/forms';
|
|
import { PageEvent } from '@angular/material/paginator';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import { Subscription } from 'rxjs';
|
|
import { debounceTime } from 'rxjs/operators';
|
|
import { SearchService } from 'src/app/services/search.service';
|
|
import { SettingsService } from 'src/app/services/settings.service';
|
|
|
|
@Component({
|
|
selector: 'page-search',
|
|
templateUrl: './search.page.html',
|
|
styleUrls: ['./search.page.scss']
|
|
})
|
|
export class PageSearch implements OnInit {
|
|
|
|
searchFormControl = new FormControl();
|
|
pageSizeOptions: number[] = [1, 2, 3, 4, 5, 10, 15, 30, 50, 100];
|
|
results: any;
|
|
asc: boolean = false;
|
|
byDate: boolean = false;
|
|
settings: any;
|
|
settingsSubscription: Subscription;
|
|
boundRefresh: Function;
|
|
types: string[] = ['all', 'entry', 'comment'];
|
|
searchType: string = this.types[1];
|
|
|
|
constructor(
|
|
private searchService: SearchService,
|
|
private settingsService: SettingsService,
|
|
private router: Router,
|
|
private route: ActivatedRoute) { }
|
|
|
|
ngOnInit(): void {
|
|
this.boundRefresh = this.refresh.bind(this);
|
|
|
|
this.route.queryParams.subscribe({
|
|
next: (params) => {
|
|
this.asc = params['asc'] == 'true';
|
|
this.byDate = params['byDate'] == 'true';
|
|
this.searchType = params['type'] || this.types[1];
|
|
this.searchFormControl.setValue(params['q']);
|
|
}
|
|
})
|
|
|
|
this.searchFormControl.valueChanges.pipe(
|
|
debounceTime(300)).subscribe({
|
|
next: (value: string) => {
|
|
this.refresh();
|
|
}
|
|
})
|
|
|
|
this.settingsSubscription = this.settingsService.settings.subscribe({
|
|
next: (settings) => {
|
|
this.settings = settings;
|
|
this.refresh();
|
|
}
|
|
})
|
|
}
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
this.settingsSubscription.unsubscribe();
|
|
}
|
|
|
|
refresh(): void {
|
|
if (!this.results) {
|
|
this.results = {};
|
|
}
|
|
|
|
const params: any = { q: null, p: null };
|
|
params.q = this.searchFormControl.value;
|
|
params.asc = this.asc ? 'true' : undefined;
|
|
params.byDate = this.byDate ? 'true' : undefined;
|
|
params.type = this.searchType != 'entry' ? this.searchType : undefined;
|
|
|
|
this.router.navigate(
|
|
[],
|
|
{
|
|
relativeTo: this.route,
|
|
queryParams: params,
|
|
queryParamsHandling: 'merge'
|
|
});
|
|
|
|
this.results.content = null;
|
|
this.searchService.search(this.searchFormControl.value || " ", this.searchType, 0, this.results.size || this.settings.pageSize, this.asc, this.byDate).subscribe({
|
|
next: (data: any) => {
|
|
this.results = data;
|
|
},
|
|
error: (error) => {
|
|
this.results = { error: error };
|
|
}
|
|
})
|
|
}
|
|
|
|
update(event: PageEvent) {
|
|
this.results.content = null;
|
|
const params: any = { p: null, s: null };
|
|
|
|
if (event.pageIndex != 0) {
|
|
params.p = event.pageIndex + 1;
|
|
}
|
|
|
|
if (event.pageSize != this.settings.pageSize) {
|
|
params.s = event.pageSize;
|
|
}
|
|
|
|
this.router.navigate(
|
|
[],
|
|
{
|
|
relativeTo: this.route,
|
|
queryParams: params,
|
|
queryParamsHandling: 'merge'
|
|
});
|
|
|
|
this.searchService.search(this.searchFormControl.value, this.searchType, event.pageIndex, event.pageSize, this.asc, this.byDate).subscribe({
|
|
next: (data: any) => {
|
|
this.results = data;
|
|
},
|
|
error: (error) => {
|
|
this.results = { error: error };
|
|
}
|
|
})
|
|
}
|
|
}
|