Project

General

Profile

1
import {Component, Input} from '@angular/core';
2
import {ActivatedRoute, NavigationEnd, NavigationStart, 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
    this.router.events.subscribe((e) => {
28
      if(e instanceof NavigationEnd){
29
        // console.log(e)
30
        this.initialize();
31
      }
32
    });
33
  }
34

    
35
  ngOnInit() {
36

    
37
    // this.activeRouteEnabled = false;
38
    if(this.communityId){
39
      this.customFilter = new SearchCustomFilter("Community", "communityId", this.communityId, "");
40
    }
41
    this.entityType = "all";
42
    this.route.queryParams.subscribe(params => {
43
      this.parameters = Object.assign({}, params);
44
      if(params["fv0"] && params["f0"] && params["f0"] == "q"){
45
        this.keyword =params["fv0"];
46
      }
47
      if(this.onlyresults) {
48
        if (params["type"] && params["type"].length > 0) {
49
          let types = params["type"].split(",");
50
          if (types.length == 1) {
51
            if (types.indexOf("publications") != -1) {
52
              this.entityType = "publications";
53
            } else if (types.indexOf("datasets") != -1) {
54
              this.entityType = "datasets";
55
            } else if (types.indexOf("software") != -1) {
56
              this.entityType = "software";
57
            } else if (types.indexOf("other") != -1) {
58
              this.entityType = "other";
59
            }
60
          }
61
        }
62
      }
63
    });
64
    this.initialize()
65
  }
66
  initialize(){
67
    if(!this.onlyresults){
68
      let currentRoute= this.getCurrentRoute();
69
      if(currentRoute== this.properties.searchLinkToProjects){
70
        this.entityType = "project";
71
      }else if(currentRoute== this.properties.searchLinkToDataProviders){
72
        this.entityType = "dataprovider";
73
      }else if(currentRoute== this.properties.searchLinkToOrganizations){
74
        this.entityType = "organization";
75
      }else if(currentRoute== this.properties.searchLinkToResults){
76
        this.entityType = "result";
77
      }else {
78
        // not one of the search bar search pages
79
        this.entityType = "result";
80
        this.keyword = "";
81
      }
82
    }
83
    if(this.getCurrentRoute() == this.properties.searchLinkToAdvancedResults){
84
      this.enableSearchbar =  false;
85
    }else{
86
      this.enableSearchbar = true;
87
    }
88
    this.showAdvancedLink();
89
  }
90
  showAdvancedLink(){
91
    if(this.getCurrentRoute() == this.properties.searchLinkToResults && this.entityType == "result"){
92
      this.advancedSearchLink = this.properties.searchLinkToAdvancedResults;
93
    }else{
94
      this.advancedSearchLink = null;
95
    }
96
  }
97
  isEnabled(required, enabled) {
98
    if (!required) {
99
      return true;
100
    }
101
    for (let requiredEntity of required) {
102
      if (typeof enabled[requiredEntity] === "undefined" || enabled[requiredEntity] == false) {
103
        return false;
104
      }
105
    }
106
    return true;
107
  }
108
  getCurrentRoute() {
109
    return this.router.url.split('?')[0];
110
  }
111
  entityChanged($event){
112

    
113
    this.entityType = $event.entity;
114
    this.searchRoute = $event.simpleUrl;
115
    if(!this.onlyresults && this.entityType == "result") {
116
      this.parameters["qf"] = true;
117
    }
118
    this.showAdvancedLink();
119
  }
120
  keywordChanged(){
121
   if(this.getCurrentRoute()!=this.searchRoute) {
122
      this.parameters = {};
123
    }
124
    if ( this.keyword.length > 0) {
125
      this.parameters["fv0"] = this.keyword;
126
      this.parameters["f0"] = "q";
127
    }else{
128
      delete this.parameters['fv0'];
129
      delete this.parameters['f0'];
130
    }
131
    if(this.onlyresults && this.entityType != "all"){
132
      this.parameters["type"] = this.entityType;
133
    }else{
134
      delete this.parameters['type'];
135
    }
136
    //set true only if it is not set allready
137
    if(!this.parameters["qf"]) {
138
      this.parameters["qf"] = true;
139
    }
140
    this.router.navigate([this.searchRoute], {queryParams: this.parameters} );
141
  }
142
}
(2-2/3)