1
|
import {Component, Input, ViewChild} from '@angular/core';
|
2
|
import { ActivatedRoute} from '@angular/router';
|
3
|
import {Location} from '@angular/common';
|
4
|
|
5
|
import { Filter, Value} from '../searchUtils/searchHelperClasses.class';
|
6
|
|
7
|
import {SearchPeopleService} from '../../services/searchPeople.service';
|
8
|
import {SearchResult} from '../../utils/entities/searchResult';
|
9
|
import {OpenaireProperties, ErrorCodes} from '../../utils/properties/openaireProperties';
|
10
|
import {SearchFields} from '../../utils/properties/searchFields';
|
11
|
import {SearchPageComponent } from '../searchUtils/searchPage.component';
|
12
|
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
|
13
|
|
14
|
@Component({
|
15
|
selector: 'search-people',
|
16
|
template: `
|
17
|
|
18
|
<search-page pageTitle="Search People"
|
19
|
type="people" entityType="person" [(filters)] = "filters"
|
20
|
[(results)] = "results" [(searchUtils)] = "searchUtils"
|
21
|
[baseUrl] = "baseUrl" (queryChange)="queryChanged($event)" [showRefine]=false
|
22
|
[csvParams]="csvParams" csvPath="people" advancedSearchLink="/search/advanced/people">
|
23
|
</search-page>
|
24
|
|
25
|
`
|
26
|
|
27
|
})
|
28
|
export class SearchPeopleComponent {
|
29
|
public results =[];
|
30
|
public filters =[];
|
31
|
public baseUrl:string;
|
32
|
public searchUtils:SearchUtilsClass = new SearchUtilsClass();
|
33
|
public sub: any;
|
34
|
public _location:Location;
|
35
|
public searchFields:SearchFields = new SearchFields();
|
36
|
public refineFields = this.searchFields.PERSON_REFINE_FIELDS;
|
37
|
public csvParams: string;
|
38
|
|
39
|
@ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
|
40
|
|
41
|
constructor (private route: ActivatedRoute, private _searchPeopleService: SearchPeopleService ) {
|
42
|
//this.results =[];
|
43
|
//this.filters =[];
|
44
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
45
|
this.searchUtils.status =errorCodes.LOADING;
|
46
|
this.baseUrl = OpenaireProperties.getLinkToSearchPeople();
|
47
|
//get refine field filters from url parameters
|
48
|
// if(!this.searchPage){
|
49
|
// this.searchPage = new SearchPageComponent(this._location);
|
50
|
// }
|
51
|
}
|
52
|
|
53
|
public ngOnInit() {
|
54
|
this.searchPage.refineFields = this.refineFields; //TODO make it work as a directive
|
55
|
this.sub = this.route.queryParams.subscribe(params => {
|
56
|
this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
|
57
|
this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
|
58
|
|
59
|
this.getResults(this.searchUtils.keyword, false, this.searchUtils.page, this.searchUtils.size);
|
60
|
|
61
|
|
62
|
});
|
63
|
}
|
64
|
|
65
|
public ngOnDestroy() {
|
66
|
this.sub.unsubscribe();
|
67
|
}
|
68
|
|
69
|
public getResults(keyword:string,refine:boolean, page: number, size: number){
|
70
|
var parameters = "";
|
71
|
if(keyword.length > 0){
|
72
|
parameters = "q="+ keyword;
|
73
|
}
|
74
|
this._getResults(parameters,refine,page,this.searchUtils.size);
|
75
|
}
|
76
|
private _getResults(parameters:string,refine:boolean, page: number, size: number){
|
77
|
this.csvParams = parameters;
|
78
|
|
79
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
80
|
this.searchUtils.status = errorCodes.LOADING;
|
81
|
this.searchPage.openLoading();
|
82
|
|
83
|
this._searchPeopleService.searchPeople(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields()).subscribe(
|
84
|
data => {
|
85
|
this.searchUtils.totalResults = data[0];
|
86
|
console.info("search People: [Parameters:"+parameters+" ] [total results:"+this.searchUtils.totalResults+"]");
|
87
|
this.results = data[1];
|
88
|
this.filters = this.searchPage.checkSelectedFilters(data[2]);
|
89
|
this.searchPage.updateBaseUrlWithParameters(this.filters);
|
90
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
91
|
this.searchUtils.status = errorCodes.DONE;
|
92
|
if(this.searchUtils.totalResults == 0 ){
|
93
|
this.searchUtils.status = errorCodes.NONE;
|
94
|
}
|
95
|
this.searchPage.closeLoading();
|
96
|
},
|
97
|
err => {
|
98
|
console.log(err);
|
99
|
//TODO check erros (service not available, bad request)
|
100
|
// if( ){
|
101
|
// this.searchUtils.status = ErrorCodes.ERROR;
|
102
|
// }
|
103
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
104
|
this.searchUtils.status = errorCodes.ERROR;
|
105
|
this.searchPage.closeLoading();
|
106
|
}
|
107
|
);
|
108
|
}
|
109
|
|
110
|
private setFilters(){
|
111
|
//TODO set filters from
|
112
|
}
|
113
|
|
114
|
public queryChanged($event) {
|
115
|
var parameters = $event.value;
|
116
|
console.info("queryChanged: Execute search query "+parameters);
|
117
|
this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
|
118
|
}
|
119
|
|
120
|
|
121
|
}
|