2020-04-22 15:00:13 +02:00
|
|
|
import {Component, Input} from '@angular/core';
|
2020-11-11 15:43:13 +01:00
|
|
|
import {ActivatedRoute, NavigationEnd, Router} from '@angular/router';
|
2020-04-22 15:00:13 +02:00
|
|
|
import {EnvProperties} from "../../utils/properties/env-properties";
|
|
|
|
import {SearchCustomFilter} from "../../searchPages/searchUtils/searchUtils.class";
|
2020-11-11 15:43:13 +01:00
|
|
|
import {Subscriber} from "rxjs";
|
2020-04-22 15:00:13 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'search-bar',
|
|
|
|
templateUrl: 'searchBar.component.html',
|
|
|
|
|
|
|
|
})
|
|
|
|
export class SearchBarComponent {
|
|
|
|
|
|
|
|
@Input() searchRoute: string = "/search/find";
|
|
|
|
@Input() searchPlaceHolder: string = "Search for research results";
|
|
|
|
@Input() entitiesSelection:boolean = true;
|
|
|
|
@Input() properties:EnvProperties;
|
|
|
|
keyword: string = "";
|
|
|
|
entityType = "all";
|
|
|
|
enableSearchbar:boolean = true;
|
2020-04-27 10:14:05 +02:00
|
|
|
customFilter: SearchCustomFilter = null;
|
|
|
|
@Input() communityId;
|
|
|
|
@Input() onlyresults:boolean=false;
|
2020-04-22 15:00:13 +02:00
|
|
|
parameters = {};
|
2020-04-27 10:14:05 +02:00
|
|
|
advancedSearchLink = null;
|
2020-11-11 15:43:13 +01:00
|
|
|
subscriptions = [];
|
2020-04-22 15:00:13 +02:00
|
|
|
constructor(private router: Router,
|
|
|
|
private route: ActivatedRoute ) {
|
2020-11-11 15:43:13 +01:00
|
|
|
this.subscriptions.push(this.router.events.subscribe((e) => {
|
2020-04-30 11:23:30 +02:00
|
|
|
if(e instanceof NavigationEnd){
|
|
|
|
// console.log(e)
|
|
|
|
this.initialize();
|
|
|
|
}
|
2020-11-11 15:43:13 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.subscriptions.forEach(subscription => {
|
|
|
|
if (subscription instanceof Subscriber) {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
}
|
2020-04-30 11:23:30 +02:00
|
|
|
});
|
2020-04-22 15:00:13 +02:00
|
|
|
}
|
|
|
|
ngOnInit() {
|
|
|
|
|
|
|
|
// this.activeRouteEnabled = false;
|
2020-04-27 10:14:05 +02:00
|
|
|
if(this.communityId){
|
|
|
|
this.customFilter = new SearchCustomFilter("Community", "communityId", this.communityId, "");
|
|
|
|
}
|
2020-04-30 11:23:30 +02:00
|
|
|
this.entityType = "all";
|
2020-11-11 15:43:13 +01:00
|
|
|
this.subscriptions.push(this.route.queryParams.subscribe(params => {
|
2020-04-22 15:00:13 +02:00
|
|
|
this.parameters = Object.assign({}, params);
|
2020-05-05 18:21:31 +02:00
|
|
|
if(params["fv0"] && params["f0"] && params["f0"] == "q"){
|
|
|
|
this.keyword =params["fv0"];
|
|
|
|
}
|
2020-04-27 10:14:05 +02:00
|
|
|
if(this.onlyresults) {
|
|
|
|
if (params["type"] && params["type"].length > 0) {
|
|
|
|
let types = params["type"].split(",");
|
|
|
|
if (types.length == 1) {
|
|
|
|
if (types.indexOf("publications") != -1) {
|
2020-04-22 15:00:13 +02:00
|
|
|
this.entityType = "publications";
|
2020-04-27 10:14:05 +02:00
|
|
|
} else if (types.indexOf("datasets") != -1) {
|
|
|
|
this.entityType = "datasets";
|
|
|
|
} else if (types.indexOf("software") != -1) {
|
|
|
|
this.entityType = "software";
|
|
|
|
} else if (types.indexOf("other") != -1) {
|
|
|
|
this.entityType = "other";
|
|
|
|
}
|
2020-04-22 15:00:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-11 15:43:13 +01:00
|
|
|
}));
|
2020-04-30 11:23:30 +02:00
|
|
|
this.initialize()
|
|
|
|
}
|
|
|
|
initialize(){
|
|
|
|
if(!this.onlyresults){
|
|
|
|
let currentRoute= this.getCurrentRoute();
|
|
|
|
if(currentRoute== this.properties.searchLinkToProjects){
|
|
|
|
this.entityType = "project";
|
|
|
|
}else if(currentRoute== this.properties.searchLinkToDataProviders){
|
|
|
|
this.entityType = "dataprovider";
|
|
|
|
}else if(currentRoute== this.properties.searchLinkToOrganizations){
|
|
|
|
this.entityType = "organization";
|
2020-05-05 18:21:31 +02:00
|
|
|
}else if(currentRoute== this.properties.searchLinkToResults){
|
|
|
|
this.entityType = "result";
|
|
|
|
}else {
|
|
|
|
// not one of the search bar search pages
|
2020-04-30 11:23:30 +02:00
|
|
|
this.entityType = "result";
|
2020-05-05 18:21:31 +02:00
|
|
|
this.keyword = "";
|
2020-04-27 10:14:05 +02:00
|
|
|
}
|
2020-04-30 11:23:30 +02:00
|
|
|
}
|
|
|
|
if(this.getCurrentRoute() == this.properties.searchLinkToAdvancedResults){
|
|
|
|
this.enableSearchbar = false;
|
|
|
|
}else{
|
|
|
|
this.enableSearchbar = true;
|
|
|
|
}
|
|
|
|
this.showAdvancedLink();
|
|
|
|
}
|
|
|
|
showAdvancedLink(){
|
|
|
|
if(this.getCurrentRoute() == this.properties.searchLinkToResults && this.entityType == "result"){
|
|
|
|
this.advancedSearchLink = this.properties.searchLinkToAdvancedResults;
|
|
|
|
}else{
|
|
|
|
this.advancedSearchLink = null;
|
|
|
|
}
|
2020-04-22 15:00:13 +02:00
|
|
|
}
|
|
|
|
isEnabled(required, enabled) {
|
|
|
|
if (!required) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (let requiredEntity of required) {
|
|
|
|
if (typeof enabled[requiredEntity] === "undefined" || enabled[requiredEntity] == false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
getCurrentRoute() {
|
|
|
|
return this.router.url.split('?')[0];
|
|
|
|
}
|
|
|
|
entityChanged($event){
|
2020-05-05 18:21:31 +02:00
|
|
|
|
2020-04-22 15:00:13 +02:00
|
|
|
this.entityType = $event.entity;
|
|
|
|
this.searchRoute = $event.simpleUrl;
|
2020-04-27 10:14:05 +02:00
|
|
|
if(!this.onlyresults && this.entityType == "result") {
|
|
|
|
this.parameters["qf"] = true;
|
|
|
|
}
|
2020-04-30 11:23:30 +02:00
|
|
|
this.showAdvancedLink();
|
2020-04-22 15:00:13 +02:00
|
|
|
}
|
|
|
|
keywordChanged(){
|
2020-05-05 18:21:31 +02:00
|
|
|
if(this.getCurrentRoute()!=this.searchRoute) {
|
2020-04-27 10:14:05 +02:00
|
|
|
this.parameters = {};
|
|
|
|
}
|
2020-04-22 15:00:13 +02:00
|
|
|
if ( this.keyword.length > 0) {
|
|
|
|
this.parameters["fv0"] = this.keyword;
|
|
|
|
this.parameters["f0"] = "q";
|
|
|
|
}else{
|
|
|
|
delete this.parameters['fv0'];
|
|
|
|
delete this.parameters['f0'];
|
|
|
|
}
|
2020-04-27 10:14:05 +02:00
|
|
|
if(this.onlyresults && this.entityType != "all"){
|
2020-04-22 15:00:13 +02:00
|
|
|
this.parameters["type"] = this.entityType;
|
|
|
|
}else{
|
|
|
|
delete this.parameters['type'];
|
|
|
|
}
|
|
|
|
//set true only if it is not set allready
|
|
|
|
if(!this.parameters["qf"]) {
|
|
|
|
this.parameters["qf"] = true;
|
|
|
|
}
|
|
|
|
this.router.navigate([this.searchRoute], {queryParams: this.parameters} );
|
|
|
|
}
|
|
|
|
}
|