1
|
import {Component, Input, ViewChild} from '@angular/core';
|
2
|
import {Output, EventEmitter} from '@angular/core';
|
3
|
import {Location} from '@angular/common';
|
4
|
import {ActivatedRoute, Router} from '@angular/router';
|
5
|
import {Title, Meta} from '@angular/platform-browser';
|
6
|
|
7
|
import {Observable} from 'rxjs/Observable';
|
8
|
|
9
|
import {Filter, Value, AdvancedField} from '../searchUtils/searchHelperClasses.class';
|
10
|
import {SearchResult} from '../../utils/entities/searchResult';
|
11
|
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
|
12
|
import {SearchUtilsClass} from './searchUtils.class';
|
13
|
import {ModalLoading} from '../../utils/modal/loading.component';
|
14
|
import {StringUtils, Dates} from '../../utils/string-utils.class';
|
15
|
import {ErrorCodes} from '../../utils/properties/errorCodes';
|
16
|
|
17
|
import {PiwikService} from '../../utils/piwik/piwik.service';
|
18
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
19
|
import { SEOService } from '../../sharedComponents/SEO/SEO.service';
|
20
|
|
21
|
@Component({
|
22
|
selector: 'advanced-search-page',
|
23
|
templateUrl: 'advancedSearchPage.component.html'
|
24
|
})
|
25
|
export class AdvancedSearchPageComponent {
|
26
|
@Input() piwikSiteId = null;
|
27
|
@Input() pageTitle = "";
|
28
|
@Input() results = [];
|
29
|
@Input() type;
|
30
|
@Input() entityType;
|
31
|
@Input() searchUtils:SearchUtilsClass = new SearchUtilsClass();
|
32
|
@Input() fieldIds: string[];
|
33
|
@Input() fieldIdsMap;//:{ [key:string]:{ name:string, operator:string, type:string, indexField:string, equalityOperator:string }} ;
|
34
|
@Input() selectedFields:AdvancedField[];
|
35
|
@Input() simpleSearchUrl: string;
|
36
|
@ViewChild (ModalLoading) loading : ModalLoading ;
|
37
|
@Input() csvParams: string;
|
38
|
@Input() csvPath: string;
|
39
|
@Input() simpleSearchLink: string = "";
|
40
|
@Input() disableForms:boolean = false;
|
41
|
@Input() loadPaging: boolean = true;
|
42
|
@Input() oldTotalResults: number = 0;
|
43
|
@Input() openaireLink: string;
|
44
|
@Input() connectCommunityId: string;
|
45
|
@Input() sort: boolean = false;
|
46
|
@Input() searchFormClass: string = "searchForm";
|
47
|
piwiksub: any;
|
48
|
public parameterNames:string[] =[];
|
49
|
public parameterValues:string[] =[];
|
50
|
|
51
|
public baseURLWithParameters:string = '';
|
52
|
|
53
|
public csvLimit: number = 0;
|
54
|
public pagingLimit: number = 0;
|
55
|
public resultsPerPage: number = 0;
|
56
|
isPiwikEnabled = false;
|
57
|
properties:EnvProperties;
|
58
|
public errorCodes:ErrorCodes = new ErrorCodes();
|
59
|
|
60
|
url = null;
|
61
|
@Output() queryChange = new EventEmitter();
|
62
|
constructor (private route: ActivatedRoute,
|
63
|
private location: Location,
|
64
|
private _meta: Meta,
|
65
|
private _title: Title,
|
66
|
private _piwikService:PiwikService,
|
67
|
private router: Router,
|
68
|
private seoService: SEOService) {
|
69
|
}
|
70
|
|
71
|
ngOnInit() {
|
72
|
this.route.data
|
73
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
74
|
this.properties = data.envSpecific;
|
75
|
this.pagingLimit = data.envSpecific.pagingLimit;
|
76
|
this.resultsPerPage =data.envSpecific.resultsPerPage;
|
77
|
this.csvLimit = data.envSpecific.csvLimit;
|
78
|
this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
|
79
|
if(typeof window !== 'undefined') {
|
80
|
this.updateUrl(data.envSpecific.baseLink+location.pathname);
|
81
|
this.url = data.envSpecific.baseLink+location.pathname
|
82
|
}
|
83
|
if(typeof document !== 'undefined' && this.isPiwikEnabled){
|
84
|
this.piwiksub = this._piwikService.trackView(this.properties, this.pageTitle, this.piwikSiteId).subscribe();
|
85
|
}
|
86
|
});
|
87
|
|
88
|
var title = "Advanced search "+this.pageTitle;
|
89
|
var description = "Openaire, search, repositories, open access, type, content provider, funder, project, "+ this.pageTitle;
|
90
|
|
91
|
this.updateTitle(title);
|
92
|
this.updateDescription(description);
|
93
|
|
94
|
this.searchUtils.baseUrl = "/" + this.searchUtils.baseUrl;
|
95
|
|
96
|
this.updateBaseUrlWithParameters();
|
97
|
this.seoService.createLinkForCanonicalURL(this.properties.baseLink+this.router.url,false);
|
98
|
|
99
|
|
100
|
}
|
101
|
|
102
|
ngOnDestroy() {
|
103
|
if(this.piwiksub){
|
104
|
this.piwiksub.unsubscribe();
|
105
|
}
|
106
|
}
|
107
|
updateDescription(description:string) {
|
108
|
this._meta.updateTag({content:description},"name='description'");
|
109
|
this._meta.updateTag({content:description},"property='og:description'");
|
110
|
}
|
111
|
updateTitle(title:string) {
|
112
|
var _prefix ="OpenAIRE | ";
|
113
|
var _title = _prefix + ((title.length> 50 ) ?title.substring(0,50):title);
|
114
|
this._title.setTitle(_title);
|
115
|
this._meta.updateTag({content:_title},"property='og:title'");
|
116
|
}
|
117
|
updateUrl(url:string) {
|
118
|
this._meta.updateTag({content:url},"property='og:url'");
|
119
|
}
|
120
|
|
121
|
public getSelectedFiltersFromUrl(params) {
|
122
|
for(var i=0; i< this.fieldIds.length ; i++){
|
123
|
|
124
|
var fieldId = this.fieldIds[i];
|
125
|
var fieldparam = (this.fieldIdsMap[fieldId])?this.fieldIdsMap[fieldId].param:"";
|
126
|
if(!this.fieldIdsMap[fieldId]){
|
127
|
|
128
|
console.error("Field: "+fieldId +" not found in fieldIds map");
|
129
|
}
|
130
|
|
131
|
var operatorId = this.getOperatorParameter(fieldparam);
|
132
|
if(params[fieldparam] != undefined && params[operatorId] != undefined) {
|
133
|
var values:string [] = StringUtils.URIDecode(params[fieldparam]).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/,-1);
|
134
|
var operators:string [] = (StringUtils.URIDecode(params[operatorId])).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/,-1);
|
135
|
if(values.length == operators.length){
|
136
|
for(var j=0; j< values.length ; j++){
|
137
|
if(this.fieldIdsMap[fieldId].type == "date"){
|
138
|
var value:string =StringUtils.unquote(values[j]);
|
139
|
var validDates:boolean = true;
|
140
|
var dateField:AdvancedField = new AdvancedField(fieldId,fieldparam,this.fieldIdsMap[fieldId].name,this.fieldIdsMap[fieldId].type,value,operators[j]) ;
|
141
|
if(value.indexOf("range") != -1){
|
142
|
dateField.dateValue.type="range";
|
143
|
if(value.length < 26 ){
|
144
|
validDates =false;
|
145
|
}else{
|
146
|
if(!Dates.isValidDate(value.substring(5,15)) || !Dates.isValidDate(value.substring(16,26))){
|
147
|
validDates =false;
|
148
|
}else {
|
149
|
dateField.dateValue.from = Dates.getDateFromString(value.substring(5,15));
|
150
|
dateField.dateValue.to = Dates.getDateFromString(value.substring(16,26));
|
151
|
}
|
152
|
}
|
153
|
// "rangeYYYY-MM-DD:YYYY-MM-DD"
|
154
|
}else{
|
155
|
dateField.dateValue.setDatesByType(value);
|
156
|
}
|
157
|
if(validDates){
|
158
|
this.selectedFields.push(dateField);
|
159
|
}
|
160
|
|
161
|
}
|
162
|
else{
|
163
|
this.selectedFields.push(new AdvancedField(fieldId,fieldparam,this.fieldIdsMap[fieldId].name,this.fieldIdsMap[fieldId].type,StringUtils.unquote(values[j]),operators[j]) );
|
164
|
}
|
165
|
}
|
166
|
}
|
167
|
}
|
168
|
}
|
169
|
if(this.selectedFields.length == 0){
|
170
|
this.selectedFields.push(new AdvancedField(this.fieldIds[0],fieldparam,this.fieldIdsMap[this.fieldIds[0]].name,this.fieldIdsMap[this.fieldIds[0]].type,"","and"));
|
171
|
}
|
172
|
}
|
173
|
private createUrlParameters(includePage:boolean){
|
174
|
var params="";
|
175
|
this.parameterNames.splice(0,this.parameterNames.length);
|
176
|
this.parameterValues.splice(0,this.parameterValues.length);
|
177
|
var fields: { [key:string]:{ values:string[], operators:string[] }}={};
|
178
|
for(var i = 0; i< this.selectedFields.length; i++){
|
179
|
if(this.fieldIdsMap[this.selectedFields[i].id] != undefined && (this.selectedFields[i].value.length > 0 || this.selectedFields[i].type == "date" )){
|
180
|
if(!fields[this.selectedFields[i].id]){
|
181
|
fields[this.selectedFields[i].id] = {values:[], operators:[]};
|
182
|
fields[this.selectedFields[i].id].values =[];
|
183
|
fields[this.selectedFields[i].id].operators =[];
|
184
|
}
|
185
|
if(this.selectedFields[i].type == "date"){
|
186
|
if(this.selectedFields[i].dateValue.type == "range"){
|
187
|
fields[this.selectedFields[i].id].values.push(StringUtils.quote(StringUtils.URIEncode("range"+Dates.getDateToString(this.selectedFields[i].dateValue.from)+":"+Dates.getDateToString(this.selectedFields[i].dateValue.to))));
|
188
|
}else{
|
189
|
fields[this.selectedFields[i].id].values.push(StringUtils.quote(StringUtils.URIEncode(this.selectedFields[i].dateValue.type)));
|
190
|
}
|
191
|
}else{
|
192
|
fields[this.selectedFields[i].id].values.push(StringUtils.quote(StringUtils.URIEncode(this.selectedFields[i].value)));
|
193
|
}
|
194
|
fields[this.selectedFields[i].id].operators.push(this.selectedFields[i].operatorId);
|
195
|
|
196
|
}
|
197
|
}
|
198
|
for(var i = 0; i< this.fieldIds.length; i++){
|
199
|
if(fields[this.fieldIds[i]]){
|
200
|
|
201
|
params+="&"+this.fieldIdsMap[this.fieldIds[i]].param+"="+fields[this.fieldIds[i]].values.join()+
|
202
|
"&"+ this.fieldIdsMap[this.fieldIds[i]].operator+"="+fields[this.fieldIds[i]].operators.join()
|
203
|
this.parameterNames.push(this.fieldIdsMap[this.fieldIds[i]].param);
|
204
|
this.parameterValues.push(fields[this.fieldIds[i]].values.join());
|
205
|
this.parameterNames.push( this.fieldIdsMap[this.fieldIds[i]].operator);
|
206
|
this.parameterValues.push(fields[this.fieldIds[i]].operators.join());
|
207
|
}
|
208
|
}
|
209
|
if(includePage && this.searchUtils.page != 1){
|
210
|
params += "&page="+this.searchUtils.page;
|
211
|
}
|
212
|
|
213
|
if(this.searchUtils.size != 10) {
|
214
|
params+=((params.length == 0)?'':'&') + 'size=' + this.searchUtils.size;
|
215
|
this.parameterNames.push("size");
|
216
|
this.parameterValues.push(""+this.searchUtils.size);
|
217
|
}
|
218
|
|
219
|
if(this.sort && this.searchUtils.sortBy) {
|
220
|
params+=((params.length == 0)?'':'&') + 'sortBy=' + this.searchUtils.sortBy;
|
221
|
this.parameterNames.push("sortBy");
|
222
|
this.parameterValues.push(this.searchUtils.sortBy);
|
223
|
}
|
224
|
|
225
|
return '?'+params;
|
226
|
}
|
227
|
public createQueryParameters(){
|
228
|
var params="";
|
229
|
var countParams = 0;
|
230
|
for(var i = 0; i< this.selectedFields.length; i++){
|
231
|
if(this.fieldIdsMap[this.selectedFields[i].id] != undefined && (this.selectedFields[i].value != "" ||this.selectedFields[i].type == "date")){
|
232
|
console.log("createQueryParameters::"+this.selectedFields[i].type);
|
233
|
if(this.selectedFields[i].type == "date"){
|
234
|
if(this.selectedFields[i].dateValue.type != "any"){
|
235
|
params += (countParams == 0 ? "" : this.selectedFields[i].operatorId) + " " + this.selectedFields[i].id + this.fieldIdsMap[this.selectedFields[i].id].equalityOperator+ '"' + StringUtils.URIEncode(Dates.getDateToString(this.selectedFields[i].dateValue.from)) + " "
|
236
|
+ StringUtils.URIEncode(Dates.getDateToString(this.selectedFields[i].dateValue.to)) + '"' + " ";
|
237
|
}
|
238
|
}else{
|
239
|
if(this.selectedFields[i].id == "q"){
|
240
|
var op = "";
|
241
|
// if()
|
242
|
params += (countParams == 0 ? "" : this.selectedFields[i].operatorId) + " " + '"' + StringUtils.URIEncode(this.selectedFields[i].value) + '"' + " ";
|
243
|
}else if(countParams == 0 && this.selectedFields[i].operatorId == "not"){
|
244
|
params += " "+ this.selectedFields[i].id + " <> "+'"' + StringUtils.URIEncode(this.selectedFields[i].value) +'"' + " ";
|
245
|
}else{
|
246
|
params += (countParams == 0 ? "" : this.selectedFields[i].operatorId + " " ) + this.selectedFields[i].id + this.fieldIdsMap[this.selectedFields[i].id].equalityOperator+'"' + encodeURIComponent(this.selectedFields[i].value) +'"' + " ";
|
247
|
|
248
|
}
|
249
|
}
|
250
|
countParams++;
|
251
|
}
|
252
|
}
|
253
|
|
254
|
//community
|
255
|
if(this.connectCommunityId ){
|
256
|
params += (countParams == 0 ? "" : " and " ) + "communityId exact "+'"' + this.connectCommunityId +'"' + " ";
|
257
|
}
|
258
|
return params;
|
259
|
|
260
|
}
|
261
|
clearFilters(){
|
262
|
}
|
263
|
|
264
|
goTo(page:number = 1){
|
265
|
this.searchUtils.page = page;
|
266
|
var urlParameters = this.createUrlParameters(true);
|
267
|
var queryParameters = this.createQueryParameters();
|
268
|
this.location.go(location.pathname,urlParameters);
|
269
|
this.queryChange.emit({
|
270
|
value: queryParameters
|
271
|
});
|
272
|
/* Code For Piwik*/
|
273
|
if (typeof localStorage !== 'undefined') {
|
274
|
console.log("In PreviousRouteRecorder : "+this.router.url );
|
275
|
localStorage.setItem('previousRoute', this.router.url);
|
276
|
}
|
277
|
if(this.isPiwikEnabled && (typeof document !== 'undefined')){
|
278
|
this.piwiksub = this._piwikService.trackView(this.properties, this.pageTitle, this.piwikSiteId).subscribe();
|
279
|
}
|
280
|
/* End Piwik Code */
|
281
|
}
|
282
|
|
283
|
queryChanged($event) {
|
284
|
|
285
|
this.goTo(1);
|
286
|
}
|
287
|
pageChanged($event) {
|
288
|
this.searchUtils.page = +$event.value;
|
289
|
this.goTo(this.searchUtils.page);
|
290
|
}
|
291
|
sizeChanged($event) {
|
292
|
this.searchUtils.size = $event.value;
|
293
|
this.goTo(1);
|
294
|
}
|
295
|
sortByChanged($event) {
|
296
|
this.searchUtils.sortBy = $event.value;
|
297
|
this.goTo(1);
|
298
|
}
|
299
|
|
300
|
/*
|
301
|
* Update the url with proper parameters. This is used as base url in Paging Component
|
302
|
*/
|
303
|
public updateBaseUrlWithParameters(){
|
304
|
this.baseURLWithParameters = this.searchUtils.baseUrl + this.createUrlParameters(false);
|
305
|
}
|
306
|
getOperatorParameter(parameter:string):string{
|
307
|
for (let id of this.fieldIds) {
|
308
|
if(this.fieldIdsMap[id]["param"] == parameter){
|
309
|
return this.fieldIdsMap[id]["operator"];
|
310
|
}
|
311
|
}
|
312
|
}
|
313
|
// for loading
|
314
|
public openLoading(){
|
315
|
this.loading.open();
|
316
|
}
|
317
|
public closeLoading(){
|
318
|
this.loading.close();
|
319
|
}
|
320
|
}
|