1
|
import { Component, OnDestroy, OnInit } from '@angular/core';
|
2
|
import { SearchRequest } from '../../../model/SearchRequest';
|
3
|
import { QueryService } from '../../../services/query/query.service';
|
4
|
import { Router } from '@angular/router';
|
5
|
import { DataHolderService } from '../../../services/data-holder/data-holder.service';
|
6
|
import { Subscription } from 'rxjs';
|
7
|
|
8
|
@Component({
|
9
|
selector: 'app-search-box',
|
10
|
templateUrl: './search-box.component.html',
|
11
|
styleUrls: ['./search-box.component.css']
|
12
|
})
|
13
|
export class SearchBoxComponent implements OnInit, OnDestroy {
|
14
|
enableSpellCheck: boolean;
|
15
|
expression: string;
|
16
|
private queryServiceSubscription$: Subscription;
|
17
|
|
18
|
constructor(private queryService: QueryService, private router: Router, private dataHolderService: DataHolderService) { }
|
19
|
|
20
|
ngOnInit(): void {
|
21
|
this.enableSpellCheck = false;
|
22
|
}
|
23
|
|
24
|
onToggle() {
|
25
|
this.enableSpellCheck = !this.enableSpellCheck;
|
26
|
}
|
27
|
|
28
|
onSubmit(): void {
|
29
|
if (!this.expression) { return; }
|
30
|
|
31
|
const searchRequest: SearchRequest = new SearchRequest(this.expression, new Date(), this.enableSpellCheck);
|
32
|
|
33
|
this.queryServiceSubscription$ = this.queryService.searchPost(searchRequest).subscribe (data => {
|
34
|
this.dataHolderService.setData(data);
|
35
|
if (this.router.url === '/') {
|
36
|
this.router.navigate(['/search']);
|
37
|
}
|
38
|
});
|
39
|
}
|
40
|
|
41
|
ngOnDestroy(): void {
|
42
|
if (this.queryServiceSubscription$ !== undefined) {
|
43
|
this.queryServiceSubscription$.unsubscribe();
|
44
|
}
|
45
|
}
|
46
|
|
47
|
setClasses() {
|
48
|
let classes;
|
49
|
|
50
|
if (this.router.url === '/') {
|
51
|
classes = {
|
52
|
'is-not-visible': true,
|
53
|
'is-visible': false
|
54
|
};
|
55
|
} else {
|
56
|
classes = {
|
57
|
'is-not-visible': false,
|
58
|
'is-visible': true
|
59
|
};
|
60
|
}
|
61
|
return classes;
|
62
|
}
|
63
|
}
|