import {ChangeDetectorRef, Component, Input} from '@angular/core'; import {ChangeDetectionStrategy} from '@angular/core'; import {ViewEncapsulation} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import {Location} from '@angular/common'; import {Title, Meta} from '@angular/platform-browser'; import {FetchResearchResults} from '../../utils/fetchEntitiesClasses/fetchResearchResults.class'; import {FetchDataproviders} from '../../utils/fetchEntitiesClasses/fetchDataproviders.class'; import {FetchProjects} from '../../utils/fetchEntitiesClasses/fetchProjects.class'; import {FetchOrganizations} from '../../utils/fetchEntitiesClasses/fetchOrganizations.class'; import {SearchResearchResultsService} from '../../services/searchResearchResults.service'; import {SearchDataprovidersService} from '../../services/searchDataproviders.service'; import {SearchProjectsService} from '../../services/searchProjects.service'; import {SearchOrganizationsService} from '../../services/searchOrganizations.service'; import {SearchFields} from '../../utils/properties/searchFields'; import {ErrorCodes} from '../../utils/properties/errorCodes'; import {RouterHelper} from '../../utils/routerHelper.class'; import {RefineFieldResultsService} from '../../services/refineFieldResults.service'; import {PiwikService} from '../../utils/piwik/piwik.service'; import {ConfigurationService} from '../../utils/configuration/configuration.service'; import {EnvProperties} from '../../utils/properties/env-properties'; import {SEOService} from '../../sharedComponents/SEO/SEO.service'; import {StringUtils} from '../../utils/string-utils.class'; import {SearchCustomFilter} from "../searchUtils/searchUtils.class"; import {Observable} from "rxjs"; import {AdvancedField, Filter} from "../searchUtils/searchHelperClasses.class"; @Component({ changeDetection: ChangeDetectionStrategy.Default, encapsulation: ViewEncapsulation.Emulated, selector: 'search-all', templateUrl: 'searchAll.component.html' }) export class SearchAllComponent { public sub: any; piwiksub: any; // public reloadResults: boolean; // public reloadPublications: boolean; // public reloadDatasets: boolean; // public reloadSoftware: boolean; // public reloadOrps: boolean; // public reloadProjects: boolean; // public reloadDataproviders: boolean; // public reloadOrganizations: boolean; reload:{result:boolean, projects:boolean, datasources: boolean, organizations:boolean} = {result:true, projects:true, datasources: true, organizations:true}; public pageTitle = "Search in OpenAIRE" public keyword: string = ""; public publications: string[]; public datasets: string[]; public software: string[]; public orps: string[]; public projectsTab: string[]; public dataproviders: string[]; public organizations: string[]; public activeEntity = null; public linkToSearchPublications = ""; public linkToSearchProjects = ""; public linkToSearchDataproviders = ""; public linkToSearchDatasets = ""; public linkToSearchSoftware = ""; public linkToSearchOrps = ""; public linkToSearchOrganizations = ""; public fetchPublications: FetchResearchResults; public fetchDataproviders: FetchDataproviders; public fetchProjects: FetchProjects; public fetchDatasets: FetchResearchResults; public fetchSoftware: FetchResearchResults; public fetchOrps: FetchResearchResults; public fetchOrganizations: FetchOrganizations; public searchFields: SearchFields = new SearchFields(); public errorCodes: ErrorCodes = new ErrorCodes(); public routerHelper: RouterHelper = new RouterHelper(); public publicationsSize: any = null; public datasetsSize: any = null; public softwareSize: any = null; public oprsSize: any = null; public fundersSize: any = null; public projectsSize: any = null; public datasourcesSize: any = null; showPublications: boolean = false; showDatasets: boolean = false; showSoftware: boolean = false; showOrps: boolean = false; showProjects: boolean = false; showDataProviders: boolean = false; showOrganizations: boolean = false; advancedSearchLink: string = "/search/advanced/publications"; properties: EnvProperties; @Input() logoURL; @Input() name; @Input() customFilter: SearchCustomFilter = null; @Input() piwikSiteId = null; @Input() formPlaceholderText = "Search for research results, projects, content providers & organizations in OpenAIRE"; public subPub; public subData; public subSoftware; public subOrps; public subProjects; public subOrg; public subDataPr; quickFilter: { filter: Filter, selected: boolean, filterId: string, value: string } = { filter: null, selected: true, filterId: "resultbestaccessright", value: "Open Access" }; resultTypes = {publication: true, dataset: true, software: true, other: true}; //adv Search Form public fieldIds: string[] = this.searchFields.RESULT_ADVANCED_FIELDS; public fieldIdsMap = this.searchFields.RESULT_FIELDS; public selectedFields: AdvancedField[] = []; //new parameters = {}; disableForms: boolean = true; constructor(private route: ActivatedRoute, private _router: Router, private _searchResearchResultsService: SearchResearchResultsService, private _searchDataprovidersService: SearchDataprovidersService, private _searchProjectsService: SearchProjectsService, private _searchOrganizationsService: SearchOrganizationsService, private _refineFieldResultsService: RefineFieldResultsService, private location: Location, private _meta: Meta, private _title: Title, private _piwikService: PiwikService, private config: ConfigurationService, private seoService: SEOService, private router: Router, private cdr:ChangeDetectorRef) { this.fetchPublications = new FetchResearchResults(this._searchResearchResultsService); this.fetchDataproviders = new FetchDataproviders(this._searchDataprovidersService); this.fetchProjects = new FetchProjects(this._searchProjectsService); this.fetchDatasets = new FetchResearchResults(this._searchResearchResultsService); this.fetchSoftware = new FetchResearchResults(this._searchResearchResultsService); this.fetchOrps = new FetchResearchResults(this._searchResearchResultsService); this.fetchOrganizations = new FetchOrganizations(this._searchOrganizationsService); this.selectedFields.push(new AdvancedField(this.fieldIds[0], this.fieldIdsMap[this.fieldIds[0]].param, this.fieldIdsMap[this.fieldIds[0]].name, this.fieldIdsMap[this.fieldIds[0]].type, '', "and")); } public ngOnInit() { this.route.data .subscribe((data: { envSpecific: EnvProperties }) => { var description = "open access, research, scientific publication, European Commission, EC, FP7, ERC, Horizon 2020, H2020, search, projects "; var title = "OpenAIRE | Search publications, research data, projects... | OpenAIRE"; this.properties = data.envSpecific; var url = data.envSpecific.baseLink + this._router.url; this._title.setTitle(title); this._meta.updateTag({content: description}, "name='description'"); this._meta.updateTag({content: description}, "property='og:description'"); this._meta.updateTag({content: title}, "property='og:title'"); this._meta.updateTag({content: url}, "property='og:url'"); this.seoService.createLinkForCanonicalURL(this.properties.baseLink + this._router.url, false); if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) { this.piwiksub = this._piwikService.trackView(this.properties, "OpenAIRE |Search publications, research data, projects...", this.piwikSiteId).subscribe(); } if ((this.customFilter && this.customFilter.queryFieldName == "communitId") || this.properties.adminToolsCommunity) { this.config.getCommunityInformation(this.properties, (this.customFilter && this.customFilter.queryFieldName == "communitId") ? this.customFilter.valueId : this.properties.adminToolsCommunity).subscribe(data => { var showEntity = {}; for (var i = 0; i < data['entities'].length; i++) { showEntity["" + data['entities'][i]["pid"] + ""] = data['entities'][i]["isEnabled"]; } this.showPublications = showEntity["publication"]; this.showDatasets = showEntity["dataset"]; this.showProjects = showEntity["project"]; this.showOrganizations = showEntity["organization"]; this.showDataProviders = showEntity["datasource"]; this.showSoftware = showEntity["software"]; this.showOrps = showEntity["orp"]; if (this.customFilter && this.customFilter.queryFieldName == "communityId") { this.showProjects = false; this.showOrganizations = false; this.showDataProviders = false; } this.loadAll(); }); } else { if ((this.customFilter && this.customFilter.queryFieldName == "country")) { this.showPublications = true; this.showDatasets = true; this.showDataProviders = true; this.showSoftware = true; this.showOrps = true; this.showProjects = true; this.showOrganizations = true; this.showDataProviders = true; } } this.loadAll(); }); } loadAll() { this.reloadTabs(); this.sub = this.route.queryParams.subscribe(params => { this.parameters = Object.assign({}, params); this.keyword = (params['keyword']) ? params['keyword'] : (params["q"] ? params["q"] : ""); this.selectedFields[0].value = this.keyword; this.quickFilter.selected = ((params['qf']== undefined || params["qf"] == "true") == true); if (params["type"] && params["type"].length > 0) { this.resultTypes['publication'] = (params["type"].split(",").indexOf("publications") != -1); this.resultTypes['dataset'] = (params["type"].split(",").indexOf("datasets") != -1); this.resultTypes['software'] = (params["type"].split(",").indexOf("software") != -1); this.resultTypes['other'] = (params["type"].split(",").indexOf("other") != -1); } if(this.activeEntity == null && (!params["active"] || params["active"].length ==0)){ this.activeEntity = this.getDefaultEntityToShow(); }else if(params["active"] && params["active"].length >0 ){ this.activeEntity = ((["result","projects","organizations","datasources"]).indexOf(params["active"])!= -1)?params["active"]:this.getDefaultEntityToShow(); }else if (this.activeEntity !=null && (!params["active"] || params["active"].length ==0)){ this.parameters["active"]=this.activeEntity; if(location.search && location.search.indexOf("active=") == -1){ this.location.go(location.pathname, ((location.search)?(location.search+"&"):("?")) +"active=" + this.activeEntity); } } if (this.activeEntity == "result") { this.searchResults(); } else if (this.activeEntity == "projects") { this.searchProjects(); } else if (this.activeEntity == "datasources") { this.searchDataProviders(); } else if (this.activeEntity == "organizations") { this.searchOrganizations(); } this.count(); }); } getDefaultEntityToShow(){ if (this.showPublications || this.showDatasets || this.showSoftware || this.showOrps) { return "result"; } else if (this.showProjects) { return "projects"; } else if (this.showDataProviders) { return "content providers"; } else if (this.showOrganizations) { return "organizations"; } } public ngOnDestroy() { if (this.sub) { this.sub.unsubscribe(); } if (this.piwiksub) { this.piwiksub.unsubscribe(); } if (this.keyword != null && this.keyword.length > 0) { if (this.subPub) { this.subPub.unsubscribe(); } if (this.subData) { this.subData.unsubscribe(); } if (this.subSoftware) { this.subSoftware.unsubscribe(); } if (this.subOrps) { this.subOrps.unsubscribe(); } if (this.subProjects) { this.subProjects.unsubscribe(); } if (this.subOrg) { this.subOrg.unsubscribe(); } if (this.subDataPr) { this.subDataPr.unsubscribe(); } } } public searchResults() { this.advancedSearchLink = this.properties.searchLinkToAdvancedPublications;//"/search/advanced/publications"; if (this.reload[this.activeEntity] && this.fetchPublications.searchUtils.status != this.errorCodes.NONE) { this.reload[this.activeEntity] = false; this.linkToSearchPublications = this.properties.searchLinkToPublications;// + "?keyword=" + this.keyword; } } public searchProjects() { this.advancedSearchLink = this.properties.searchLinkToAdvancedProjects;//"/search/advanced/projects"; if (this.reload[this.activeEntity] && this.fetchProjects.searchUtils.status != this.errorCodes.NONE ) { this.reload[this.activeEntity] = false; this.linkToSearchProjects = this.properties.searchLinkToProjects;// + "?keyword=" + this.keyword; } } public searchDataProviders() { this.advancedSearchLink = this.properties.searchLinkToAdvancedDataProviders;//"/search/advanced/dataproviders"; if ( this.reload[this.activeEntity] && this.fetchDataproviders.searchUtils.status != this.errorCodes.NONE) { this.reload[this.activeEntity] = false; this.linkToSearchDataproviders = this.properties.searchLinkToDataProviders;// + "?keyword=" + this.keyword; } } public searchOrganizations() { this.advancedSearchLink = this.properties.searchLinkToAdvancedOrganizations;//"/search/advanced/organizations"; if (this.reload[this.activeEntity] && this.fetchOrganizations.searchUtils.status != this.errorCodes.NONE) { this.reload[this.activeEntity] = false; this.linkToSearchOrganizations = this.properties.searchLinkToOrganizations;// + "?keyword=" + this.keyword; } } private prepareKeywordParam(keyword){ if (this.parameters["q"] && keyword.length == 0) { delete this.parameters['q']; delete this.parameters['op']; } else { this.parameters["q"] = keyword; this.parameters["op"] = "and"; } } private prepareResultParameters() { //quickSelections if (this.resultTypes && this.activeEntity == "result") { let values = []; if (this.resultTypes.publication) { values.push("publications"); } if (this.resultTypes.dataset) { values.push("datasets"); } if (this.resultTypes.software) { values.push("software"); } if (this.resultTypes.other) { values.push("other"); } if (values.length > 0) { this.parameters["type"] = values.join(","); } } if (this.quickFilter && this.activeEntity == "result") { this.parameters["qf"] = this.quickFilter.selected; } } public quickSelectionsChanged() { this.prepareResultParameters(); this.reload[this.activeEntity] = true; this.router.navigate([location.pathname], {queryParams: this.parameters}); } public keywordChanged($event) { this.prepareKeywordParam(this.selectedFields[0].value); this.reloadTabs(); this.router.navigate([location.pathname], {queryParams: this.parameters}); } private count() { var refineParams = null; if (this.customFilter) { refineParams = (refineParams ? (refineParams + '&') : '') + "&fq=" + StringUtils.URIEncode(this.customFilter.queryFieldName + " exact " + StringUtils.quote((this.customFilter.valueId))); } if (this.activeEntity != "result" && this.reload["result"] && (this.showPublications || this.showSoftware || this.datasets || this.showOrps)) { this.fetchPublications.searchUtils.status = this.errorCodes.LOADING; this.reload["result"] = false; this.fetchPublications.results = []; this.subPub = this.numOfSearchResults(this.fetchPublications, refineParams); } if (this.activeEntity != "projects" && this.reload["projects"] && this.showProjects) { this.fetchProjects.searchUtils.status = this.errorCodes.LOADING; this.fetchProjects.results = []; this.reload["projects"] = false; this.subProjects = this._searchProjectsService.numOfSearchProjects(this.keyword, this.properties, refineParams).subscribe( data => { this.fetchProjects.searchUtils.totalResults = data; this.fetchProjects.searchUtils.status = this.errorCodes.DONE; if (this.fetchProjects.searchUtils.totalResults == 0) { this.fetchProjects.searchUtils.status = this.errorCodes.NONE; } }, err => { //console.log(err); this.handleError("Error getting number of Projects", err); this.fetchProjects.searchUtils.status = this.errorCodes.ERROR; } ); } if (this.activeEntity != "datasources" && this.reload["datasources"] && this.showDataProviders) { this.fetchDataproviders.results = []; this.reload["datasources"] = false; this.fetchDataproviders.getNumForSearch(this.keyword, this.properties, refineParams); } if (this.activeEntity != "organizations" && this.reload["organizations"] && this.showOrganizations) { this.fetchOrganizations.searchUtils.status = this.errorCodes.LOADING; this.fetchOrganizations.results = []; this.reload["organizations"] = false; this.subOrg = this._searchOrganizationsService.numOfSearchOrganizations(this.keyword, this.properties, refineParams).subscribe( data => { this.fetchOrganizations.searchUtils.totalResults = data; this.fetchOrganizations.searchUtils.status = this.errorCodes.DONE; if (this.fetchOrganizations.searchUtils.totalResults == 0) { this.fetchOrganizations.searchUtils.status = this.errorCodes.NONE; } }, err => { //console.log(err); this.handleError("Error getting number of Organizations", err); this.fetchOrganizations.searchUtils.status = this.errorCodes.ERROR; } ); } } private numOfSearchResults(fetchClass: FetchResearchResults, refineParams): Observable { return this._searchResearchResultsService.numOfResearchOutcomes(this.keyword, this.properties, refineParams).subscribe( data => { fetchClass.searchUtils.totalResults = data; fetchClass.searchUtils.status = this.errorCodes.DONE; if (fetchClass.searchUtils.totalResults == 0) { fetchClass.searchUtils.status = this.errorCodes.NONE; } }, err => { this.handleError("Error getting number of research results", err); fetchClass.searchUtils.status = this.errorCodes.ERROR; } ); } private reloadTabs() { this.reload = {result:true, projects:true, datasources: true, organizations:true}; this.fetchOrganizations.searchUtils.status = this.errorCodes.LOADING; this.fetchDataproviders.searchUtils.status = this.errorCodes.LOADING; this.fetchProjects.searchUtils.status = this.errorCodes.LOADING; this.fetchPublications.searchUtils.status = this.errorCodes.LOADING; this.fetchDatasets.searchUtils.status = this.errorCodes.LOADING; this.fetchSoftware.searchUtils.status = this.errorCodes.LOADING; this.fetchOrps.searchUtils.status = this.errorCodes.LOADING; } private handleError(message: string, error) { console.error("General Search Page: " + message, error); } private getEntityName(entityType: string, plural: boolean, full: boolean): string { if (entityType == "publication") { return "publication" + (plural ? "s" : ""); } else if (entityType == "dataset") { return (full ? "research data" : ("dataset" + (plural ? "s" : ""))); } else if (entityType == "software") { return "software"; } else if (entityType == "other") { return (full ? ("other research product" + (plural ? "s" : "")) : "other"); } } activeEntityUpdate($event) { this.disableForms = $event.disableForms; let updated = true; if (this.activeEntity == "result") { if($event.searchUtils.status!=this.errorCodes.LOADING) { this.fetchPublications.searchUtils.totalResults = $event.searchUtils.totalResults; } this.fetchPublications.searchUtils.status = $event.searchUtils.status; }else if (this.activeEntity == "projects"){ if($event.searchUtils.status!=this.errorCodes.LOADING) { this.fetchProjects.searchUtils.totalResults = $event.searchUtils.totalResults; } this.fetchProjects.searchUtils.status = $event.searchUtils.status; }else if (this.activeEntity == "datasources"){ if($event.searchUtils.status!=this.errorCodes.LOADING) { this.fetchDataproviders.searchUtils.totalResults = $event.searchUtils.totalResults; } this.fetchDataproviders.searchUtils.status = $event.searchUtils.status; }else if (this.activeEntity == "organizations") { if($event.searchUtils.status!=this.errorCodes.LOADING) { this.fetchOrganizations.searchUtils.totalResults = $event.searchUtils.totalResults; } this.fetchOrganizations.searchUtils.status = $event.searchUtils.status; }else{ updated = false; } if(updated) { this.cdr.detectChanges(); } } entityChanged(entity){ this.activeEntity = entity; this.parameters = {}; this.reload[entity]= true; this.parameters["active"] = entity; if ( this.keyword.length > 0) { this.parameters["q"] = this.keyword; this.parameters["op"] = "and"; } this.router.navigate([location.pathname], {queryParams: this.parameters}); } }