Project

General

Profile

1
import {Component, Input} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3
import {EnvProperties} from "../../utils/properties/env-properties";
4
import {SearchCustomFilter} from "../../searchPages/searchUtils/searchUtils.class";
5

    
6
@Component({
7
  selector: 'search-bar',
8
  templateUrl: 'searchBar.component.html',
9

    
10
})
11
export class SearchBarComponent {
12

    
13
  @Input() searchRoute: string = "/search/find";
14
  @Input() searchPlaceHolder: string = "Search for research results";
15
  @Input() entitiesSelection:boolean = true;
16
  @Input() properties:EnvProperties;
17
  keyword: string = "";
18
  entityType = "all";
19
  enableSearchbar:boolean = true;
20
  customFilter: SearchCustomFilter   = null;
21
  @Input() communityId;
22
  @Input() onlyresults:boolean=false;
23
  parameters = {};
24
  advancedSearchLink = null;
25
  constructor(private router: Router,
26
              private  route: ActivatedRoute ) {
27
  }
28

    
29
  ngOnInit() {
30

    
31
    // this.activeRouteEnabled = false;
32
    if(this.communityId){
33
      this.customFilter = new SearchCustomFilter("Community", "communityId", this.communityId, "");
34
    }
35
    this.route.queryParams.subscribe(params => {
36
      this.parameters = Object.assign({}, params);
37
      this.entityType = "all";
38
      if(this.onlyresults) {
39
        if (params["type"] && params["type"].length > 0) {
40
          let types = params["type"].split(",");
41
          if (types.length == 1) {
42
            if (types.indexOf("publications") != -1) {
43
              this.entityType = "publications";
44
            } else if (types.indexOf("datasets") != -1) {
45
              this.entityType = "datasets";
46
            } else if (types.indexOf("software") != -1) {
47
              this.entityType = "software";
48
            } else if (types.indexOf("other") != -1) {
49
              this.entityType = "other";
50
            }
51
          }
52
        }
53
      }else{
54
        let currentRoute= this.getCurrentRoute();
55
        if(currentRoute== this.properties.searchLinkToProjects){
56
          this.entityType = "project";
57
        }else if(currentRoute== this.properties.searchLinkToDataProviders){
58
          this.entityType = "dataprovider";
59
        }else if(currentRoute== this.properties.searchLinkToOrganizations){
60
          this.entityType = "organization";
61
        }else{
62
          this.entityType = "result";
63
        }
64
      }
65
      if(this.getCurrentRoute() == this.properties.searchLinkToAdvancedResults){
66
        this.enableSearchbar =  false;
67
      }else{
68
        this.enableSearchbar = true;
69
      }
70
      if(this.entityType == "result"){
71
        this.advancedSearchLink = this.properties.searchLinkToAdvancedResults;
72
      }else{
73
        this.advancedSearchLink = null;
74
      }
75
    });
76

    
77
  }
78

    
79
  isEnabled(required, enabled) {
80
    if (!required) {
81
      return true;
82
    }
83
    for (let requiredEntity of required) {
84
      if (typeof enabled[requiredEntity] === "undefined" || enabled[requiredEntity] == false) {
85
        return false;
86
      }
87
    }
88
    return true;
89
  }
90
  getCurrentRoute() {
91
    return this.router.url.split('?')[0];
92
  }
93
  entityChanged($event){
94
    this.entityType = $event.entity;
95
    this.searchRoute = $event.simpleUrl;
96
    if(!this.onlyresults && this.entityType == "result") {
97
      this.parameters["qf"] = true;
98
    }
99
    if(this.entityType == "result"){
100
      this.advancedSearchLink = this.properties.searchLinkToAdvancedResults;
101
    }else{
102
      this.advancedSearchLink = null;
103
    }
104
  }
105
  keywordChanged(){
106
    if(!this.onlyresults) {
107
      this.parameters = {};
108
    }
109
    if ( this.keyword.length > 0) {
110
      this.parameters["fv0"] = this.keyword;
111
      this.parameters["f0"] = "q";
112
    }else{
113
      delete this.parameters['fv0'];
114
      delete this.parameters['f0'];
115
    }
116
    if(this.onlyresults && this.entityType != "all"){
117
      this.parameters["type"] = this.entityType;
118
    }else{
119
      delete this.parameters['type'];
120
    }
121
    //set true only if it is not set allready
122
    if(!this.parameters["qf"]) {
123
      this.parameters["qf"] = true;
124
    }
125
    this.router.navigate([this.searchRoute], {queryParams: this.parameters} );
126
  }
127
}
(2-2/3)