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(this.onlyresults) {
|
45
|
if (params["type"] && params["type"].length > 0) {
|
46
|
let types = params["type"].split(",");
|
47
|
if (types.length == 1) {
|
48
|
if (types.indexOf("publications") != -1) {
|
49
|
this.entityType = "publications";
|
50
|
} else if (types.indexOf("datasets") != -1) {
|
51
|
this.entityType = "datasets";
|
52
|
} else if (types.indexOf("software") != -1) {
|
53
|
this.entityType = "software";
|
54
|
} else if (types.indexOf("other") != -1) {
|
55
|
this.entityType = "other";
|
56
|
}
|
57
|
}
|
58
|
}
|
59
|
}
|
60
|
});
|
61
|
this.initialize()
|
62
|
}
|
63
|
initialize(){
|
64
|
if(!this.onlyresults){
|
65
|
let currentRoute= this.getCurrentRoute();
|
66
|
if(currentRoute== this.properties.searchLinkToProjects){
|
67
|
this.entityType = "project";
|
68
|
}else if(currentRoute== this.properties.searchLinkToDataProviders){
|
69
|
this.entityType = "dataprovider";
|
70
|
}else if(currentRoute== this.properties.searchLinkToOrganizations){
|
71
|
this.entityType = "organization";
|
72
|
}else{
|
73
|
this.entityType = "result";
|
74
|
}
|
75
|
}
|
76
|
if(this.getCurrentRoute() == this.properties.searchLinkToAdvancedResults){
|
77
|
this.enableSearchbar = false;
|
78
|
}else{
|
79
|
this.enableSearchbar = true;
|
80
|
}
|
81
|
this.showAdvancedLink();
|
82
|
}
|
83
|
showAdvancedLink(){
|
84
|
if(this.getCurrentRoute() == this.properties.searchLinkToResults && this.entityType == "result"){
|
85
|
this.advancedSearchLink = this.properties.searchLinkToAdvancedResults;
|
86
|
}else{
|
87
|
this.advancedSearchLink = null;
|
88
|
}
|
89
|
}
|
90
|
isEnabled(required, enabled) {
|
91
|
if (!required) {
|
92
|
return true;
|
93
|
}
|
94
|
for (let requiredEntity of required) {
|
95
|
if (typeof enabled[requiredEntity] === "undefined" || enabled[requiredEntity] == false) {
|
96
|
return false;
|
97
|
}
|
98
|
}
|
99
|
return true;
|
100
|
}
|
101
|
getCurrentRoute() {
|
102
|
return this.router.url.split('?')[0];
|
103
|
}
|
104
|
entityChanged($event){
|
105
|
this.entityType = $event.entity;
|
106
|
this.searchRoute = $event.simpleUrl;
|
107
|
if(!this.onlyresults && this.entityType == "result") {
|
108
|
this.parameters["qf"] = true;
|
109
|
}
|
110
|
this.showAdvancedLink();
|
111
|
}
|
112
|
keywordChanged(){
|
113
|
if(!this.onlyresults) {
|
114
|
this.parameters = {};
|
115
|
}
|
116
|
if ( this.keyword.length > 0) {
|
117
|
this.parameters["fv0"] = this.keyword;
|
118
|
this.parameters["f0"] = "q";
|
119
|
}else{
|
120
|
delete this.parameters['fv0'];
|
121
|
delete this.parameters['f0'];
|
122
|
}
|
123
|
if(this.onlyresults && this.entityType != "all"){
|
124
|
this.parameters["type"] = this.entityType;
|
125
|
}else{
|
126
|
delete this.parameters['type'];
|
127
|
}
|
128
|
//set true only if it is not set allready
|
129
|
if(!this.parameters["qf"]) {
|
130
|
this.parameters["qf"] = true;
|
131
|
}
|
132
|
this.router.navigate([this.searchRoute], {queryParams: this.parameters} );
|
133
|
}
|
134
|
}
|