Project

General

Profile

1
import {Component, Input}             from '@angular/core';
2
import {ViewChild, Output}            from '@angular/core';
3
import {EventEmitter}      from '@angular/core';
4
import {Location}                     from '@angular/common';
5
import {Router, ActivatedRoute}       from '@angular/router';
6
import {Title, Meta}                  from '@angular/platform-browser';
7

    
8
import {Filter, Value}                from './searchHelperClasses.class';
9
import {SearchFields}                 from '../../utils/properties/searchFields';
10
import {SearchUtilsClass}             from './searchUtils.class';
11
import {DOI, StringUtils}             from '../../utils/string-utils.class';
12
import {RouterHelper}                 from '../../utils/routerHelper.class';
13
import {ModalLoading}                 from '../../utils/modal/loading.component';
14
import {ErrorCodes}                   from '../../utils/properties/errorCodes';
15
import {PiwikService}                 from '../../utils/piwik/piwik.service';
16
import {EnvProperties}                from '../../utils/properties/env-properties';
17
import { SEOService }                 from '../../sharedComponents/SEO/SEO.service';
18
import {HelperFunctions} from "../../utils/HelperFunctions.class";
19

    
20
@Component({
21
    selector: 'search-page',
22
    templateUrl: 'searchPage.component.html'
23

    
24
})
25
export class SearchPageComponent {
26
  @Input() piwikSiteId = null;
27
  @Input() pageTitle = "";
28
  @Input() formPlaceholderText = "Type Keywords...";
29
  @Input() results = [];
30
  @Input() filters = [];
31
  @Input() type:string = "";
32
  @Input() entityType: string = "";
33
  @Input() searchUtils:SearchUtilsClass = new SearchUtilsClass();
34
  @Output() queryChange  = new EventEmitter();
35
  @Input() baseUrl:string = '';
36
  @Input() showResultCount:boolean = true;
37
  @Input() showRefine:boolean = true;
38
  @Input() refineFields = [];
39
  @Input() csvParams: string;
40
  @Input() csvPath: string;
41
  @Input() advancedSearchLink: string = "";
42
  @Input() advancedSearchParameters;
43
  @Input() tableViewLink: string;
44
  @Input() disableForms: boolean = false;
45
  @Input() loadPaging: boolean = true;
46
  @Input() oldTotalResults: number = 0;
47
  @Input() tableView: boolean = false;
48
  @Input() searchFormClass: string = "searchForm";
49
  @Input() openaireLink: string;
50
  @Input() connectCommunityId: string;
51
  @Input() sort: boolean = false;
52
  @Input() mapUrl: string = "";
53
  @Input() mapTooltipType: string ="content providers";
54
  @Input() newQueryButton: boolean = true;
55
  @Input() lastIndex: boolean = true;
56
  @Input() hasPrefix: boolean = true;
57
  //@Input() sortBy: string = "";
58
  @ViewChild (ModalLoading) loading : ModalLoading ;
59
  public fieldIdsMap;//:  { [key:string]:{ name:string, operator:string, type:string, indexField:string, equalityOperator:string  }};
60
  private searchFieldsHelper:SearchFields = new SearchFields();
61
  private queryParameters: Map<string, string>  = new Map<string,string>();
62
  private baseURLWithParameters:string = '';
63
  private sub: any; piwiksub: any;
64
  // public countFilters= 0;
65
  public parameterNames:string[] =[];
66
  public parameterValues:string[] =[];
67
  public showUnknownFilters:boolean = false; // when a filter exists in query but has no results, so no filters returned from the query
68
  //@ViewChild (SearchFilterModalComponent) searchFilterModal : SearchFilterModalComponent ;
69
  // public currentFilter: Filter;
70
  public csvLimit: number = 0;
71
  public pagingLimit: number = 0;
72
  public resultsPerPage: number = 10;
73
  public isPiwikEnabled;
74
  properties:EnvProperties;
75
  public routerHelper:RouterHelper = new RouterHelper();
76
  public errorCodes:ErrorCodes = new ErrorCodes();
77

    
78
  constructor (private route: ActivatedRoute,
79
               private location: Location,
80
               private _meta: Meta,
81
               private _title: Title,
82
               private _piwikService:PiwikService,
83
               private router: Router,
84
             private seoService: SEOService) {
85
   }
86

    
87
  ngOnInit() {
88

    
89
    this.route.data
90
      .subscribe((data: { envSpecific: EnvProperties }) => {
91
        this.properties = data.envSpecific;
92
        this.pagingLimit = data.envSpecific.pagingLimit;
93
        this.csvLimit = data.envSpecific.csvLimit;
94
        this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
95
        if(typeof window !== 'undefined') {
96
          this.updateUrl(data.envSpecific.baseLink+location.pathname);
97
        }
98
        if(typeof document !== 'undefined' && this.isPiwikEnabled){
99
          this.piwiksub = this._piwikService.trackView(this.properties, this.pageTitle, this.piwikSiteId).subscribe();
100
        }
101
      });
102
      HelperFunctions.scroll();
103

    
104
        // this.updateBaseUrlWithParameters(this.filters);
105
        this.updateTitle(this.pageTitle);
106
        var description = "Openaire, search, repositories, open access, type, content provider, funder, project, " + this.type + "," +this.pageTitle;
107
        this.updateDescription(description);
108
        this.seoService.createLinkForCanonicalURL(this.properties.baseLink+this.router.url,false);
109
  }
110

    
111
  ngOnDestroy() {
112
    if(this.piwiksub){
113
      this.piwiksub.unsubscribe();
114
    }
115
  }
116

    
117

    
118
  updateDescription(description:string) {
119
    this._meta.updateTag({content:description},"name='description'");
120
    this._meta.updateTag({content:description},"property='og:description'");
121
  }
122

    
123
  updateTitle(title:string) {
124
    let _title: string;
125
    if(this.hasPrefix) {
126
      let _prefix ="OpenAIRE | ";
127
      _title = _prefix + ((title.length > 50) ? title.substring(0, 50) : title);
128
    } else {
129
      _title = ((title.length > 50) ? title.substring(0, 50) : title);
130
    }
131
    this._title.setTitle(_title);
132
    this._meta.updateTag({content:_title},"property='og:title'");
133
  }
134

    
135
  updateUrl(url:string) {
136
    this._meta.updateTag({content:url},"property='og:url'");
137
  }
138

    
139
  public getQueryParamsFromUrl(params) {
140
    this.queryParameters = new Map<string,string>();
141
    for(let i=0; i< this.refineFields.length ; i++) {
142
      let filterId = this.refineFields[i];
143
      if (params[filterId] != undefined) {
144
        this.queryParameters.set(filterId, StringUtils.URIDecode(params[filterId]));
145
      }
146
    }
147
    return this.queryParameters;
148
  }
149

    
150
  public getQueryParametersFromUrl(params){ // called by ngOnInit function of search find pages
151
    // var parameters = "";
152
    var allFqs = "";
153

    
154
    this.queryParameters = new Map<string,string>();
155
    for(var i=0; i< this.refineFields.length ; i++){
156
         var filterId =  this.refineFields[i];
157

    
158
          if(params[filterId] != undefined) {
159
             this.queryParameters.set(filterId, StringUtils.URIDecode(params[filterId]));
160
             let values = (StringUtils.URIDecode(this.queryParameters.get(filterId))).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/,-1);
161
             var countvalues = 0;
162
             var fq = "";
163
             for(let value of values) {
164
               countvalues++;
165
               var paramId = this.fieldIdsMap[filterId].param;
166
              // parameters+='&' + paramId+ '='+ value;//+"&" + this.fieldIdsMap[paramId].operator + "="+((countvalues == 1)?"and":"or");
167
              fq+=(fq.length > 0 ? " " + "or" + " ":"" ) + filterId +" exact " +(value);
168
             }
169
             if(countvalues > 0){
170
               fq="&fq="+StringUtils.URIEncode(fq);
171
             }
172
             allFqs += fq;
173
          }
174
   }
175
   if(this.connectCommunityId ){
176
     allFqs+=  "&fq="+StringUtils.URIEncode("communityId exact " + StringUtils.quote((this.connectCommunityId )));
177
   }
178

    
179
   var keyword = params['keyword'];
180
   var doiQuery = "";
181
   var keywordQuery = "";
182
   if((keyword && keyword.length > 0)){
183
      if((this.type == 'publications' ||this.type == 'research data' || this.type == 'software' || this.type == 'other research products')){
184
        var DOIs:string[] = DOI.getDOIsFromString(keyword);
185
       var doisParams = "";
186

    
187
       for(var i =0 ;i < DOIs.length; i++){
188
         doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
189
       }
190
       if(doisParams.length > 0){
191
         doiQuery += "&"+doisParams;
192
       }else {
193
        keywordQuery += "&q="+StringUtils.URIEncode(keyword);
194
       }
195
     }else{
196
        keywordQuery += "&q="+StringUtils.URIEncode(keyword);
197

    
198
     }
199
   }
200
   return (doiQuery.length > 0 ? doiQuery:keywordQuery) + allFqs;
201
 }
202
 public getIndexQueryParametersFromUrl(params){  // called by ngOnInit function of search pages that request 'resources'
203
  //  var parameters = "";
204
   var allFqs = "";
205

    
206
   for(var i=0; i< this.refineFields.length ; i++){
207
        var filterId =  this.refineFields[i];
208
        var fq = "";
209
         if(params[filterId] != undefined) {
210
            this.queryParameters.set(filterId,decodeURIComponent(params[filterId]));
211
            let values = (decodeURIComponent(this.queryParameters.get(filterId))).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/,-1);
212
            var countvalues = 0
213
            for(let value of values) {
214
              countvalues++;
215
              // parameters+= ((countvalues == 1)?" and (":" or ")+ filterId+ '='+ value;
216
              fq+=(fq.length > 0 ? " " + "or" + " ":"" ) + filterId + " exact " + value;//StringUtils.quote(value);
217
            }
218
            // parameters+= " ) ";
219
            if(countvalues > 0){
220
              fq="&fq="+StringUtils.URIEncode(fq);
221
            }
222
            allFqs += fq;
223
        }
224

    
225
  }
226
  if(this.connectCommunityId ){
227
    allFqs+=  "&fq="+StringUtils.URIEncode("communityId exact " + StringUtils.quote(this.connectCommunityId ));
228
  }
229
  var keyword = params['keyword'];
230
  var doiQuery = "";
231
  var keywordQuery = "";
232
  if((keyword && keyword.length > 0)){
233
    if((this.type == 'publications' ||this.type == 'research data' || this.type == 'software' || this.type == 'other research products')){
234
      var DOIs:string[] = DOI.getDOIsFromString(keyword);
235
      var doisParams = "";
236

    
237
      for(var i =0 ;i < DOIs.length; i++){
238
        doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
239
      }
240
      if(doisParams.length > 0){
241
        doiQuery += "&"+doisParams;
242
      }
243
    }else{
244
       keywordQuery += "and ("+StringUtils.quote(StringUtils.URIEncode(keyword)) +")";
245

    
246
    }
247
  }
248
  return (doiQuery.length > 0 ? doiQuery:keywordQuery) + allFqs;
249

    
250
}
251
 /*
252
 * Mark as check the new filters that are selected, when you get them from search
253
 */
254
  public checkSelectedFilters(filters:Filter[]){
255
    this.filters = filters;
256
       for(var i=0; i< filters.length ; i++){
257
            var filter:Filter = filters[i];
258
            filter.countSelectedValues = 0;
259
              if(this.queryParameters.get(filter.filterId) != undefined) {
260
                let values = (decodeURIComponent(this.queryParameters.get(filter.filterId))).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/,-1);
261
                     for(let filterValue of filter.values) {
262
                       if(values.indexOf(StringUtils.quote(filterValue.id)) > -1) {
263
                            filterValue.selected = true;
264
                            filter.countSelectedValues++;
265
                         }else{
266
                           filterValue.selected = false;
267

    
268
                         }
269
                    }
270
            }else{
271
              for(let filterValue of filter.values) {
272
                 filterValue.selected = false;
273
              }
274
            }
275
        }
276
        this.filterFilterValues(this.filters);
277
        return filters;
278
  }
279
  /*
280
  * For Funder filters - if funder selected
281
  */
282
   public filterFilterValues(filters:Filter[]){
283
     var funders = [];
284
     var funder_prefix = [];
285
        for(var i=0; i< filters.length ; i++){
286

    
287
             var filter:Filter = filters[i];
288
            //  console.log(filter.filterId);
289
             if(filter.filterId.indexOf("funder")!=-1 && this.queryParameters.get(filter.filterId) != undefined) {
290
                 let funders = (decodeURIComponent(this.queryParameters.get(filter.filterId))).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/,-1);
291
                      for(let funder of funders) {
292
                        funder_prefix.push(StringUtils.unquote(funder).split("____")[0]);
293
                     }
294
                    //  console.log(funder_prefix );
295
             }else if(filter.filterId.indexOf("funding")!=-1){
296
              //  console.log(" funding: "+filter.filterId );
297
               var filteredValues = []
298
               for(let filterValue of filter.values) {
299
                 var value_prefix = filterValue.id.split("____")[0];
300
                //  console.log("Value prefix: "+value_prefix );
301
                 if(funder_prefix.indexOf(value_prefix)!=-1){
302
                  //  console.log("here" + value_prefix);
303
                   filteredValues.push(filterValue);
304
                 }
305

    
306
               }
307
               if(filteredValues.length > 0){
308
                 filter.values = filteredValues;
309
               }
310
             }
311
        //      else if(this.connectCommunityId && filter.filterId.indexOf("community")!=-1 ){
312
        //        var filteredValues = [];
313
        //        for(let filterValue of filter.values) {
314
        //          if(this.connectCommunityId != filterValue.id){
315
        //              filteredValues.push(filterValue);
316
        //          }else{
317
        //            filter.countSelectedValues-- ;
318
        //          }
319
        //        }
320
        //        if(filteredValues.length > 0){
321
        //          filter.values = filteredValues;
322
        //        }
323
        //  }
324
       }
325

    
326
         return filters;
327
   }
328
  /*
329
  * Update the url with proper parameters. This is used as base url in Paging Component
330
  */
331
  public updateBaseUrlWithParameters(filters:Filter[]){
332
    this.baseURLWithParameters = this.baseUrl + this.createUrlParameters(filters,false);
333

    
334
  }
335

    
336
  /*
337
  *Get The filters and create url parameters
338
  */
339
  private createUrlParameters(filters:Filter[], includePage:boolean){
340
    var allLimits="";//location.search.slice(1);
341
    this.queryParameters = new Map<string,string>();
342
    this.parameterNames.splice(0,this.parameterNames.length);
343
    this.parameterValues.splice(0,this.parameterValues.length);
344

    
345
    for (let filter of filters){
346
      var filterLimits="";
347
      if(filter.countSelectedValues > 0 ){//|| (this.connectCommunityId && filter.filterId.indexOf("community")!=-1)){
348
        for (let value of filter.values){
349
          if(value.selected == true){
350
            filterLimits+=((filterLimits.length == 0)?'':',') +'"'+ (value.id)+'"';
351
          }
352
        }
353
        // if(this.connectCommunityId && filter.filterId.indexOf("community")!=-1 ){
354
        //   filterLimits+=((filterLimits.length == 0)?'':',') +'"'+ StringUtils.URIEncode(this.connectCommunityId)+'"';
355
        // }
356
        this.queryParameters.set(filter.filterId,filterLimits);
357
        if(filterLimits.length > 0){
358
          this.parameterNames.push(filter.filterId);
359
          this.parameterValues.push(filterLimits);
360
        }
361
        allLimits+=(allLimits.length==0?"?":"&")+((filterLimits.length == 0 )?'':filter.filterId + '='+ filterLimits) ;
362
      }
363
    }
364
    if(this.searchUtils.keyword.length > 0 ){
365
       allLimits+=(allLimits.length==0?"?":"&")+'keyword=' + this.searchUtils.keyword;
366
      this.parameterNames.push("keyword");
367
      this.parameterValues.push(this.searchUtils.keyword);
368
     }
369
    if(this.searchUtils.page != 1 && includePage){
370
       allLimits+=((allLimits.length == 0)?'?':'&') + 'page=' + this.searchUtils.page;
371
    }
372
    if(this.searchUtils.size != this.resultsPerPage) {
373
      allLimits+=((allLimits.length == 0)?'?':'&') + 'size=' + this.searchUtils.size;
374
      this.parameterNames.push("size");
375
      this.parameterValues.push(""+this.searchUtils.size);
376
    }
377
    if(this.sort && this.searchUtils.sortBy) {
378
      allLimits+=((allLimits.length == 0)?'?':'&') + 'sortBy=' + this.searchUtils.sortBy;
379
      this.parameterNames.push("sortBy");
380
      this.parameterValues.push(this.searchUtils.sortBy);
381
    } /*else {
382
      let index: number = this.parameterNames.findIndex(function(element) {
383
        return element == "sortBy";
384
      });
385
      console.info("index of sortby: "+index);
386
      if(index >= 0) {
387
        console.info("------remove sortBy------");
388
        this.parameterNames.splice(index, 1);
389
        this.parameterValues.splice(index, 1);
390
      }
391
    }*/
392

    
393
    return allLimits;
394
  }
395
  /*
396
  *
397
  */
398
  private createSearchQueryParameters(filters:Filter[]){   // called by goTo, result emited to and used by search find pages
399
    var allFqs = "";
400
    for (let filter of filters){
401
      if(filter.countSelectedValues > 0 ){//|| (this.connectCommunityId && filter.filterId.indexOf("community")!=-1)){
402
        var fq = "";
403
        var count_selected=0;
404
        for (let value of filter.values){
405
          if(value.selected == true){
406
              count_selected++;
407
              fq+=(fq.length > 0 ? " " + filter.filterOperator + " ":"" ) + filter.filterId +  " exact " + StringUtils.quote((value.id));
408
           }
409
        }
410
        // if(this.connectCommunityId && filter.filterId.indexOf("community")!=-1){
411
        //   count_selected++;
412
        //   fq+=(fq.length > 0 ? " " + filter.filterOperator + " ":"" ) + filter.filterId +  " exact " + StringUtils.quote(StringUtils.URIEncode(this.connectCommunityId));
413
        // }
414
        fq="&fq="+StringUtils.URIEncode(fq);
415
        allFqs += fq;
416
      }
417
    }
418
    if(this.connectCommunityId ){
419
      allFqs+=  "&fq="+StringUtils.URIEncode("communityId exact " + StringUtils.quote(this.connectCommunityId ));
420
    }
421
      //TODO --
422
    var doiQuery = "";
423
    var keywordQuery = "";
424
    if((this.searchUtils.keyword && this.searchUtils.keyword.length > 0)){
425
      if((this.type == 'publications' ||this.type == 'research data' || this.type == 'software' || this.type == 'other research products')){
426
        var DOIs:string[] = DOI.getDOIsFromString(this.searchUtils.keyword);
427
        var doisParams = "";
428

    
429
        for(var i =0 ;i < DOIs.length; i++){
430
          doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
431
        }
432
        if(doisParams.length > 0){
433
          doiQuery += "&"+doisParams;
434
        }else{
435
          keywordQuery += "&q="+StringUtils.URIEncode(this.searchUtils.keyword);
436
        }
437
      }else{
438
           keywordQuery += "&q="+StringUtils.URIEncode(this.searchUtils.keyword);
439
      }
440
    }
441
    return (doiQuery.length > 0 ? doiQuery:keywordQuery) + allFqs;
442

    
443
  }
444
  private createIndexQueryParameters(filters:Filter[]){   // called by goTo, result emited to and used by search pages that request 'resources'
445
    var allFqs = "";
446
    for (let filter of filters){
447
      if(filter.countSelectedValues > 0){
448
        var count_selected=0;
449
        var fq = "";
450
        for (let value of filter.values){
451
          if(value.selected == true){
452
              count_selected++;
453
               fq+=(fq.length > 0 ? " " + filter.filterOperator + " ":"" ) + filter.filterId + " exact " + (StringUtils.quote(value.id));
454
           }
455
        }
456
        if(count_selected > 0){
457
          fq="&fq="+StringUtils.URIEncode(fq);
458
          allFqs += fq;
459
        }
460
      }
461
    }
462
    var doiQuery = "";
463
    var keywordQuery = "";
464
    if((this.searchUtils.keyword && this.searchUtils.keyword.length > 0)){
465
      if((this.type == 'publications' ||this.type == 'research data' || this.type == 'software' || this.type == 'other research products')){
466
        var DOIs:string[] = DOI.getDOIsFromString(this.searchUtils.keyword);
467
        var doisParams = "";
468
        for(var i =0 ;i < DOIs.length; i++){
469
          doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
470
        }
471
        if(doisParams.length > 0){
472
          doiQuery += "&"+doisParams;
473
        }
474
      }else{
475
         keywordQuery += " and ("+StringUtils.quote(StringUtils.URIEncode(this.searchUtils.keyword)) +")"
476

    
477
      }
478
    }
479
    return (doiQuery.length > 0 ? doiQuery:keywordQuery) + allFqs;
480

    
481
  }
482
  public countFilters():number{
483
    var filters=0;
484
    this.showUnknownFilters = false;
485
    for (let filter of this.filters){
486
       if(filter.countSelectedValues > 0){
487
           filters+=filter.countSelectedValues;
488
       }
489
     }
490
    if(this.searchUtils.keyword.length > 0 ){
491
      filters++;
492
    }
493
    var errorCodes:ErrorCodes = new ErrorCodes();
494
    if(this.queryParameters.keys() && this.searchUtils.totalResults == 0 && this.searchUtils.status !=errorCodes.LOADING ){
495
      if(this.newQueryButton) {
496
        this.showUnknownFilters = true;
497
      }
498
    }
499
    return filters;
500
  }
501
  private clearKeywords(){
502
    if(this.searchUtils.keyword.length > 0 ){
503
      this.searchUtils.keyword ='';
504
    }
505
    this.goTo(1);
506
  }
507
  private clearFilters(){
508
    for (var i =0 ; i <  this.filters.length; i++){
509
         for (var j=0; j <  this.filters[i].countSelectedValues; j++){
510
          if(this.filters[i].values[j].selected){
511
            this.filters[i].values[j].selected = false;
512
           }
513
        this.filters[i].countSelectedValues = 0;
514
      }
515
    }
516
    this.clearKeywords();
517

    
518
  }
519
  private removeFilter(value:Value,filter:Filter){
520
    filter.countSelectedValues--;
521
    if(value.selected == true){
522
      value.selected = false;
523
     }
524
    this.goTo(1);
525

    
526
  }
527
  goTo(page:number = 1){
528
    this.searchUtils.page = page;
529
    var urlParameters = this.createUrlParameters(this.filters,true);
530
    //console.info("urlParams : "+urlParameters);
531
    this.updateBaseUrlWithParameters(this.filters);
532
    var queryParameters = this.createSearchQueryParameters(this.filters);
533
    //console.info("queryParams : "+queryParameters);
534
    var indexQuery = this.createIndexQueryParameters(this.filters);
535

    
536
    //this.location.go(location.pathname,urlParameters);
537
    this.router.navigate( [this.baseUrl], { queryParams: this.routerHelper.createQueryParams(this.parameterNames, this.parameterValues) } );
538
    
539
/* Code For Piwik*/
540
    if (typeof localStorage !== 'undefined') {
541
      localStorage.setItem('previousRoute', this.router.url);
542
    }
543
    if(this.isPiwikEnabled && (typeof document !== 'undefined')){
544
      this.piwiksub = this._piwikService.trackView(this.properties, this.pageTitle, this.piwikSiteId).subscribe();
545
    }
546
    /* End Piwik Code */
547
    this.queryChange.emit({
548
        value: queryParameters,
549
        index:indexQuery,
550
        params: this.queryParameters
551
    });
552
    HelperFunctions.scroll();
553
  }
554
  filterChanged($event){
555
       this.goTo(1);
556
  }
557
  keywordChanged($event) {
558
       this.searchUtils.keyword = $event.value;
559
       this.goTo(1);
560
  }
561

    
562
  sizeChanged($event) {
563
       this.searchUtils.size = $event.value;
564
       this.goTo(1);
565
  }
566

    
567
  sortByChanged($event) {
568
       this.searchUtils.sortBy = $event.value;
569
       this.goTo(1);
570
  }
571

    
572
  /*
573
  * Get A sub-array of this.refineFields array, which contains the ids of the selected filters
574
  */
575
  public getSelectedFilters():string[] {
576
    var selected:string[] = [];
577
    for(var i=0; i <  this.filters.length; i++){
578
      var filter:Filter = this.filters[i];
579
      if(filter.countSelectedValues > 0){
580
          selected.push(filter.filterId);
581
      }
582
    }
583
    return selected;
584
  }
585
  /*
586
  * Get A sub-array of this.refineFields array, which contains the ids of the selected parameters
587
  */
588
  private getSelectedParameters():string[] {
589
    var selected:string[] = [];
590
    var params:string[] = Object.keys(this.queryParameters);
591

    
592
    for(var i=0; i <  this.refineFields.length; i++){
593
       if(this.queryParameters.get(this.refineFields[i])!= undefined ){
594
          selected.push(this.refineFields[i]);
595
      }
596
    }
597
    return selected;
598
  }
599
  /*
600
  * Get A sub-array of this.refineFields array, which hides hidden fields (e.g Funding level 0,1,2,..), and contains those that depend on another fields (e.g  Funding level 0 if Funder is selected )
601
  */
602
  public getFields():string[] {
603
    var selected_filters:string[] = this.getSelectedFilters();
604
    if(selected_filters.length == 0){
605
      selected_filters = this.getSelectedParameters();
606
    }
607
    var fields:string[] = [];
608
    for(var i =0 ; i < this.refineFields.length;i++){
609
      var dependentTo = this.searchFieldsHelper.DEPENDENT_FIELDS[this.refineFields[i]];
610

    
611
      //if filter is not marked as hidden OR it is hidden but it is dependent to a field that it IS selected
612
      if(this.searchFieldsHelper.HIDDEN_FIELDS.indexOf(this.refineFields[i]) == -1 || (selected_filters.indexOf(dependentTo) != -1) || (selected_filters.indexOf(this.refineFields[i]) != -1) ){
613
          fields.push(this.refineFields[i]);
614
       }
615
    }
616
    return fields;
617
  }
618
  /*
619
  * Get a query  string of all fields, that want to get from search (e.g. &fields=funderid&fields=projectstartyear&...))
620
  */
621
  public getRefineFieldsQuery():string{
622

    
623
    var fields:string[] = this.getFields();
624
    var fieldsStr = ""
625
    for(var i =0 ; i < fields.length  ;i++){
626
        fieldsStr+="&fields="+fields[i];
627
    }
628
    return "&refine=true"+fieldsStr;
629
  }
630

    
631
  // for loading
632
  public openLoading(){
633
    if(this.loading){
634
      this.loading.open();
635
    }
636
  }
637
  public closeLoading(){
638
    if(this.loading){
639
      this.loading.close();
640
    }
641
  }
642
  getSelectedValues(filter):any{
643
    var selected = [];
644
    if(filter.countSelectedValues >0){
645
      for (var i=0; i < filter.values.length; i++){
646
        if(filter.values[i].selected){
647
          selected.push(filter.values[i]);
648
        }
649
      }
650
    }
651
    return selected;
652

    
653
  }
654
}
(28-28/45)