1
|
import {Component, ElementRef, EventEmitter, Input, Output} from '@angular/core';
|
2
|
import {SearchProjectsService} from '../../services/searchProjects.service';
|
3
|
import {ProjectService} from '../../landingPages/project/project.service';
|
4
|
import {ClaimEntity, ClaimProject} from './claimHelper.class';
|
5
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
6
|
import {ErrorCodes} from '../../utils/properties/errorCodes';
|
7
|
import {StringUtils} from "../../utils/string-utils.class";
|
8
|
import {Filter} from "../../searchPages/searchUtils/searchHelperClasses.class";
|
9
|
|
10
|
declare var UIkit:any;
|
11
|
|
12
|
@Component({
|
13
|
selector: 'claim-projects-search-form',
|
14
|
|
15
|
templateUrl:'claimProjectSearchForm.component.html',
|
16
|
|
17
|
})
|
18
|
export class ClaimProjectsSearchFormComponent {
|
19
|
ngOnInit() {
|
20
|
// this.getFunders();
|
21
|
// this.search(this.page, this.size);
|
22
|
}
|
23
|
// @ViewChild (ModalLoading) loading : ModalLoading ;
|
24
|
|
25
|
// @Input() public inline: boolean = false ; // for claimed started from landing pages
|
26
|
public query = '';
|
27
|
@Input() public selectedProjects:ClaimEntity[]=[] ;
|
28
|
public elementRef;
|
29
|
|
30
|
// public funders:string[];
|
31
|
// public selectedFunderId:string ="0";
|
32
|
// selectedFunderName:string ="Select funder:";
|
33
|
@Output() projectSelected = new EventEmitter();
|
34
|
@Input() public properties:EnvProperties;
|
35
|
@Input() public inlineClaim:boolean=false;
|
36
|
@Input() localStoragePrefix:string = "";
|
37
|
@Input() basketLimit;
|
38
|
|
39
|
public errorCodes:ErrorCodes = new ErrorCodes();
|
40
|
public projects:string[];
|
41
|
public warningMessage = "";
|
42
|
openaireResults:ClaimEntity[] = [];
|
43
|
openaireResultsNum: number ;
|
44
|
openaireResultsPage : number = 1;
|
45
|
openaireResultsStatus:number = this.errorCodes.NONE;
|
46
|
page : number = 1;
|
47
|
size:number = 5;
|
48
|
keyword:string = '';
|
49
|
showResults = false;
|
50
|
|
51
|
// public openaireRefineFields: string[] = ["relfunder", "relproject", "resultbestaccessright", "instancetypename", "resultlanguagename", "community"];
|
52
|
// public openaireRefineFieldsQuery: string = "&refine=true&fields=funder&fields=projectstartyear&fields=projectendyear&fields=projectecsc39";
|
53
|
public refineFields: string[] = ["funder"];
|
54
|
public refineFieldsQuery: string = "&refine=true&fields=funder";
|
55
|
filters = [];
|
56
|
prevFilters = [];
|
57
|
startYear = "";
|
58
|
endYear = "";
|
59
|
constructor(private _service: ProjectService, private _projectService: SearchProjectsService, myElement: ElementRef) {
|
60
|
this.elementRef = myElement;
|
61
|
|
62
|
}
|
63
|
|
64
|
|
65
|
search(page,size) {
|
66
|
if(this.keyword.length == 0){
|
67
|
this.showResults =false;
|
68
|
return;
|
69
|
}
|
70
|
this.showResults =true;
|
71
|
this.openaireResults = [];
|
72
|
this.openaireResultsStatus = this.errorCodes.LOADING;
|
73
|
this.prevFilters = this.filters;
|
74
|
|
75
|
//searchProjects (params: string, refineParams:string, page: number, size: number, refineFields:string[] , properties:EnvProperties ):any {
|
76
|
this._projectService.searchProjects(this.createOpenaireQueryParams(),(page==1)? this.refineFieldsQuery:null, page, size, (page==1)?this.refineFields:[], this.properties).subscribe(
|
77
|
data => {
|
78
|
if(data != null) {
|
79
|
this.openaireResultsPage=page;
|
80
|
this.openaireResultsNum = data[0];
|
81
|
this.openaireResults =ClaimProjectsSearchFormComponent.openaire2ClaimEntity(data[1]);
|
82
|
if(data[2] && data[2].length > 0){
|
83
|
this.filters = this.checkSelectedFilters( data[2], this.prevFilters);
|
84
|
}
|
85
|
|
86
|
this.openaireResultsStatus = this.errorCodes.DONE;
|
87
|
if(this.openaireResultsNum == 0){
|
88
|
this.openaireResultsStatus = this.errorCodes.NONE;
|
89
|
}
|
90
|
}else {
|
91
|
this.openaireResultsStatus = this.errorCodes.ERROR;
|
92
|
}
|
93
|
},
|
94
|
err => {
|
95
|
this.openaireResultsStatus = this.errorCodes.ERROR;
|
96
|
//console.log(err.status);
|
97
|
ClaimProjectsSearchFormComponent.handleError("Error getting projects by keyword: "+this.keyword, err);
|
98
|
}
|
99
|
);
|
100
|
}
|
101
|
// select(entity){
|
102
|
// if(this.selectedProjects.length > 50){
|
103
|
// UIkit.notification({
|
104
|
// message : 'Your basket exceeds the number of allowed projects (50)',
|
105
|
// status : 'warning',
|
106
|
// timeout : 1500,
|
107
|
// pos : 'top-center'
|
108
|
// });
|
109
|
// return;
|
110
|
// }
|
111
|
// this.query = "";
|
112
|
// // this.searchTermStream.next(this.query); //clear
|
113
|
// entity = entity.value;
|
114
|
// // var project: ClaimProject = new ClaimProject();
|
115
|
// // project.funderId = entity.funderId;
|
116
|
// // project.funderName = entity.funderName;
|
117
|
// // project.id = entity.id;
|
118
|
// // project.projectName = entity.projectName;
|
119
|
// // project.projectAcronym = entity.projectAcronym;
|
120
|
// // project.startDate = entity.startDate;
|
121
|
// // project.endDate = entity.endDate;
|
122
|
// // project.code = entity.code;
|
123
|
// // project.jurisdiction = entity.jurisdiction;
|
124
|
// // project.fundingLevel0 = entity.fundingLevel0;
|
125
|
//
|
126
|
//
|
127
|
// var index:number =this.selectedProjects.indexOf(entity);
|
128
|
// var found:boolean = false;
|
129
|
// this.warningMessage = "";
|
130
|
//
|
131
|
// for (var _i = 0; _i < this.selectedProjects.length; _i++) {
|
132
|
// let project = this.selectedProjects[_i];
|
133
|
// if(entity.id == project.id){
|
134
|
// found=true;
|
135
|
// this.warningMessage = "Project already in your basket";
|
136
|
// }
|
137
|
// }
|
138
|
//
|
139
|
// if (!found) {
|
140
|
// this.selectedProjects.push(entity);
|
141
|
// if(this.selectedProjects != null){
|
142
|
// localStorage.setItem(this.localStoragePrefix + "projects", JSON.stringify(this.selectedProjects));
|
143
|
// }
|
144
|
// this.projectSelected.emit({
|
145
|
// value: true
|
146
|
// });
|
147
|
//
|
148
|
// }
|
149
|
// }
|
150
|
/* static showItem(item):string{
|
151
|
return ((item.field[1]['@value'])?item.field[1]['@value']+" - ":"" ) + item.field[3]['@value'];
|
152
|
}*/
|
153
|
remove(item){
|
154
|
let index:number =this.selectedProjects.indexOf(item);
|
155
|
if (index > -1) {
|
156
|
this.selectedProjects.splice(index, 1);
|
157
|
}
|
158
|
|
159
|
}
|
160
|
/* handleClick(event){
|
161
|
var clickedComponent = event.target;
|
162
|
var inside = false;
|
163
|
do {
|
164
|
if (clickedComponent === this.elementRef.nativeElement) {
|
165
|
inside = true;
|
166
|
}
|
167
|
clickedComponent = clickedComponent.parentNode;
|
168
|
} while (clickedComponent);
|
169
|
|
170
|
}*/
|
171
|
static openaire2ClaimEntity(items){
|
172
|
const projects: ClaimEntity[] = [];
|
173
|
for(const item of items){
|
174
|
const entity: ClaimEntity = new ClaimEntity();
|
175
|
entity.project = new ClaimProject();
|
176
|
entity.project.funderId = item.funderId;
|
177
|
entity.project.funderName = item.funderShortname;
|
178
|
entity.id = item.id;
|
179
|
entity.title = item.title.name;
|
180
|
entity.project.acronym = item.acronym;
|
181
|
entity.project.startDate = item.startYear;
|
182
|
entity.project.endDate = item.endYear;
|
183
|
entity.project.code = item.code;
|
184
|
entity.project.jurisdiction = item.jurisdiction;
|
185
|
entity.project.fundingLevel0 = item.fundingLevel0;
|
186
|
entity.type="project";
|
187
|
projects.push(entity);
|
188
|
}
|
189
|
return projects;
|
190
|
}
|
191
|
private openaireResultsPageChange($event) {
|
192
|
this.openaireResultsPage=$event.value;
|
193
|
this.openaireResults = [];
|
194
|
this.search(this.openaireResultsPage,this.size);
|
195
|
}
|
196
|
private static handleError(message: string, error) {
|
197
|
console.error("Claim project search form (component): "+message, error);
|
198
|
}
|
199
|
|
200
|
|
201
|
createOpenaireQueryParams():string {
|
202
|
let query = "";
|
203
|
if(this.keyword.length > 0){
|
204
|
query += "q=" + StringUtils.quote(StringUtils.URIEncode(this.keyword));
|
205
|
}
|
206
|
|
207
|
if(this.startYear.length > 0 ){
|
208
|
query+='&fq=projectstartyear exact \"'+this.startYear+'\"'
|
209
|
}
|
210
|
if(this.endYear.length > 0 ){
|
211
|
query+='&fq=projectendyear exact \"'+this.endYear+'\"'
|
212
|
}
|
213
|
let allFqs = "";
|
214
|
for (let filter of this.filters){
|
215
|
if(filter.countSelectedValues > 0){
|
216
|
let count_selected=0;
|
217
|
let fq = "";
|
218
|
for (let value of filter.values){
|
219
|
if(value.selected == true){
|
220
|
count_selected++;
|
221
|
fq+=(fq.length > 0 ? " " + filter.filterOperator + " ":"" ) + filter.filterId + " exact " + (StringUtils.quote(value.id));
|
222
|
}
|
223
|
}
|
224
|
if(count_selected > 0){
|
225
|
fq="&fq="+StringUtils.URIEncode(fq);
|
226
|
allFqs += fq;
|
227
|
}
|
228
|
}
|
229
|
}
|
230
|
return query+allFqs;
|
231
|
|
232
|
}
|
233
|
public yearChanged() {
|
234
|
this.search(this.page, this.size);
|
235
|
|
236
|
}
|
237
|
|
238
|
filterChanged($event) {
|
239
|
this.search(this.page, this.size);
|
240
|
|
241
|
}
|
242
|
public checkSelectedFilters(filters:Filter[], prevFilters:Filter[]){
|
243
|
for(let i=0; i< filters.length ; i++){
|
244
|
let filter:Filter = filters[i];
|
245
|
filter.countSelectedValues = 0;
|
246
|
let prevFilterSelectedValues:string[] = [];
|
247
|
for(let j=0; j< prevFilters.length ; j++){
|
248
|
if(filters[i].filterId == prevFilters[j].filterId){
|
249
|
if(prevFilters[j].countSelectedValues >0){
|
250
|
for(let filterValue of prevFilters[j].values) {
|
251
|
if(filterValue.selected){
|
252
|
prevFilterSelectedValues.push(filterValue.id);
|
253
|
}
|
254
|
}
|
255
|
|
256
|
}
|
257
|
break;
|
258
|
}
|
259
|
}
|
260
|
for(let filterValue of filter.values) {
|
261
|
if(prevFilterSelectedValues.indexOf(filterValue.id) > -1) {
|
262
|
filterValue.selected = true;
|
263
|
filter.countSelectedValues++;
|
264
|
}
|
265
|
|
266
|
}
|
267
|
|
268
|
}
|
269
|
return filters;
|
270
|
}
|
271
|
totalPages(totalResults: number): number {
|
272
|
let totalPages:any = totalResults/(this.size);
|
273
|
if(!(Number.isInteger(totalPages))) {
|
274
|
totalPages = (parseInt(totalPages, 10) + 1);
|
275
|
}
|
276
|
return totalPages;
|
277
|
}
|
278
|
}
|