openaire-library/sharedComponents/searchBar/searchBar.component.ts

128 lines
4.0 KiB
TypeScript

import {Component, Input} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {EnvProperties} from "../../utils/properties/env-properties";
import {SearchCustomFilter} from "../../searchPages/searchUtils/searchUtils.class";
@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;
customFilter: SearchCustomFilter = null;
@Input() communityId;
@Input() onlyresults:boolean=false;
parameters = {};
advancedSearchLink = null;
constructor(private router: Router,
private route: ActivatedRoute ) {
}
ngOnInit() {
// this.activeRouteEnabled = false;
if(this.communityId){
this.customFilter = new SearchCustomFilter("Community", "communityId", this.communityId, "");
}
this.route.queryParams.subscribe(params => {
this.parameters = Object.assign({}, params);
this.entityType = "all";
if(this.onlyresults) {
if (params["type"] && params["type"].length > 0) {
let types = params["type"].split(",");
if (types.length == 1) {
if (types.indexOf("publications") != -1) {
this.entityType = "publications";
} 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";
}
}
}
}else{
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";
}else{
this.entityType = "result";
}
}
if(this.getCurrentRoute() == this.properties.searchLinkToAdvancedResults){
this.enableSearchbar = false;
}else{
this.enableSearchbar = true;
}
if(this.getCurrentRoute() == this.properties.searchLinkToResults){
this.advancedSearchLink = this.properties.searchLinkToAdvancedResults;
}else{
this.advancedSearchLink = null;
}
});
}
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){
this.entityType = $event.entity;
this.searchRoute = $event.simpleUrl;
if(!this.onlyresults && this.entityType == "result") {
this.parameters["qf"] = true;
}
/* if(this.entityType == "result"){
this.advancedSearchLink = this.properties.searchLinkToAdvancedResults;
}else{
this.advancedSearchLink = null;
}*/
}
keywordChanged(){
if(!this.onlyresults) {
this.parameters = {};
}
if ( this.keyword.length > 0) {
this.parameters["fv0"] = this.keyword;
this.parameters["f0"] = "q";
}else{
delete this.parameters['fv0'];
delete this.parameters['f0'];
}
if(this.onlyresults && this.entityType != "all"){
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} );
}
}