Project

General

Profile

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

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

    
11
})
12
export class SearchBarComponent {
13

    
14
  @Input() searchRoute: string = "/search/find";
15
  @Input() searchPlaceHolder: string = "Search for research results";
16
  @Input() entitiesSelection:boolean = true;
17
  @Input() properties:EnvProperties;
18
  keyword: string = "";
19
  entityType = "all";
20
  enableSearchbar:boolean = true;
21
  customFilter: SearchCustomFilter   = null;
22
  @Input() communityId;
23
  @Input() onlyresults:boolean=false;
24
  parameters = {};
25
  advancedSearchLink = null;
26
  subscriptions = [];
27
  constructor(private router: Router,
28
              private  route: ActivatedRoute ) {
29
    this.subscriptions.push(this.router.events.subscribe((e) => {
30
      if(e instanceof NavigationEnd){
31
        // console.log(e)
32
        this.initialize();
33
      }
34
    }));
35
  }
36
  ngOnDestroy() {
37
    this.subscriptions.forEach(subscription => {
38
      if (subscription instanceof Subscriber) {
39
        subscription.unsubscribe();
40
      }
41
    });
42
  }
43
  ngOnInit() {
44

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

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