connect/src/app/community/community.component.ts

348 lines
15 KiB
TypeScript
Raw Normal View History

import {Component, Input, Output, ElementRef} from '@angular/core';
import {EventEmitter, ViewChild} 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 {Observable} from 'rxjs/Observable';
import "rxjs/add/observable/zip";
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
import {ErrorCodes} from '../openaireLibrary/utils/properties/errorCodes';
import {CommunityService} from "../openaireLibrary/connect/community/community.service";
import {ConfigurationService} from '../openaireLibrary/utils/configuration/configuration.service';
import {PiwikService} from '../openaireLibrary/utils/piwik/piwik.service';
import {SearchEntriesService} from '../searchEntries/searchEntries.service';
import {SearchZenodoCommunitiesService} from '../openaireLibrary/connect/zenodoCommunities/searchZenodoCommunities.service';
import {ZenodoCommunitiesService} from '../openaireLibrary/connect/zenodoCommunities/zenodo-communities.service';
import {Session} from '../openaireLibrary/login/utils/helper.class';
import {StatisticsForDashboardComponent} from '../statistics/statistics.component';
import {PiwikHelper} from '../utils/piwikHelper';
@Component({
selector: 'community',
templateUrl: 'community.component.html',
})
export class CommunityComponent {
public sub: any;
public piwiksub: any;
public subfunders: any;
public publicationTotal = null;
public researchDataTotal = null;
public softwareTotal = null;
public orpTotal = null;
public projectTotal = null;
public contentProviderTotal = null;
public organizationTotal = null;
params: any ={};
properties:EnvProperties;
public publicationResults = null;
public researchDataResults = null;
public softwareResults = null;
public orpResults = null;
@Input() communityId = null;
public community = null;
public communityInfo = null;
public showLoading: boolean = true;
masterZenodoCommunity = null;
zenodoCommunityIdS = [];
// TODO Unsubscribe
public subPublications;
public subResearchData;
public subSoftware;
public subOrps;
@ViewChild (StatisticsForDashboardComponent) statistics : StatisticsForDashboardComponent =null;
public activeTab = "publication";
constructor (
private element: ElementRef,
private route: ActivatedRoute,
private _router: Router,
private location: Location,
private _meta: Meta,
private _title: Title,
private _piwikService:PiwikService,
private config: ConfigurationService,
private _communityService:CommunityService,
private _searchEntriesService:SearchEntriesService,
private _searchZenodoCommunitiesService: SearchZenodoCommunitiesService,
private _ΖenodoCommunitieService: ZenodoCommunitiesService) {
var description = "OpenAIRE - Connect, Community Dashboard, research community";
var title = "OpenAIRE - Connect";
this._meta.updateTag({content:description},"name='description'");
this._meta.updateTag({content:description},"property='og:description'");
this._meta.updateTag({content:title},"property='og:title'");
this._title.setTitle(title);
}
public ngOnInit() {
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
var url = data.envSpecific.baseLink+this._router.url;
this._meta.updateTag({content:url},"property='og:url'");
if (this.communityId != null && this.communityId != '') {
this._communityService.getCommunity(this.properties, this.properties.communityAPI+this.communityId).subscribe (
community => {
this.community = community;
if(this.properties.environment == "development"){
this.params = {communityId: community.communityId};
}
this._meta.updateTag({content:community.description},"name='description'");
this._meta.updateTag({content:community.description},"property='og:description'");
this._meta.updateTag({content:community.title},"property='og:title'");
this._title.setTitle(community.title);
if(this.properties.enablePiwikTrack && (typeof document !== 'undefined')){
this.piwiksub = this._piwikService.trackView(this.properties, community.title,PiwikHelper.siteIDs[this.communityId]).subscribe();
}
if(this.community.zenodoCommunity){
this._ΖenodoCommunitieService.getZenodoCommunityById(this.properties, this.properties.zenodoCommunities+this.community.zenodoCommunity, null).subscribe(
result => {
this.masterZenodoCommunity = result;
},
error => {
// var emptyCommunity:ZenodoCommunityInfo = new ZenodoCommunityInfo();
// emptyCommunity.id = this.masterZenodoCommunityId;
// emptyCommunity.title = this.masterZenodoCommunityId;
// this.masterZenodoCommunity = emptyCommunity;
//console.log("Master Zenodo community'" + this.community.zenodoCommunity + "' couldn't be loaded");
this.handleError("Error getting Master Zenodo community with id: "+this.community.zenodoCommunityId, error);
}
);
}
this._searchZenodoCommunitiesService.searchZCommunities(this.properties,this.communityId).subscribe (
result => {
this.zenodoCommunityIdS = result;
},
error => {
//console.error("list of zenodo communities couldn't be loaded");
this.handleError("Error getting list of zenodo communities for community with openaire id: "+this.communityId, error);
} //this.handleError('System error retrieving community profile', error)
);
//console.log(community);
});
this._searchEntriesService.getTotal(this.properties.searchAPIURLLAst+'publications/count?format=json&fq=communityid='+this.communityId).subscribe(
publicationTotal => {
this.publicationTotal = publicationTotal;
this.showLoading = true;
this.searchPublications();
},
error => {
this.handleError("Error getting number of publications for community with id: "+this.communityId, error);
}
);
this._searchEntriesService.getTotal(this.properties.searchAPIURLLAst+'datasets/count?format=json&fq=communityid='+this.communityId).subscribe(
researchDataTotal => {
this.researchDataTotal = researchDataTotal;
},
error => {
this.handleError("Error getting number of research data for community with id: "+this.communityId, error);
}
);
this._searchEntriesService.getTotal(this.properties.searchAPIURLLAst+'software/count?format=json&fq=communityid='+this.communityId).subscribe(
softwareTotal => {
this.softwareTotal = softwareTotal;
},
error => {
this.handleError("Error getting number of software for community with id: "+this.communityId, error);
}
);
this._searchEntriesService.getTotal(this.properties.searchAPIURLLAst+'other/count?format=json&fq=communityid='+this.communityId).subscribe(
orpTotal => {
this.orpTotal = orpTotal;
},
error => {
this.handleError("Error getting number of other research products for community with id: "+this.communityId, error);
}
);
this._searchEntriesService.countTotal(this.properties.communityAPI+this.communityId+'/projects').subscribe(
projectTotal => {
this.projectTotal = projectTotal;
//console.log(projectTotal);
},
error => {
this.handleError("Error getting number of projects for community with id: "+this.communityId, error);
}
);
this._searchEntriesService.countTotal(this.properties.communityAPI+this.communityId+'/contentproviders').subscribe(
contentProviderTotal => {
this.contentProviderTotal = contentProviderTotal;
//console.log(contentProviderTotal);
},
error => {
this.handleError("Error getting number of content providers for community with id: "+this.communityId, error);
}
);
// this._searchEntriesService.countTotal(this.properties.communityAPI+this.communityId+'/organizations').subscribe(
// organizationTotal => {
// this.organizationTotal = organizationTotal;
// console.log(organizationTotal);
// });
/**/
this.config.getCommunityInformation(this.properties.adminToolsAPIURL, this.communityId).subscribe(
res => {
this.communityInfo = res;
},
error => {
//console.log(error)
this.handleError("Error getting community with id: "+this.communityId, error);
}
);
}
});
// if (typeof document !== 'undefined') {
// this.element.nativeElement.scrollIntoView();
// }
}
public searchPublications() {
this.activeTab = "publication";
if (this.publicationTotal > 0 && this.publicationResults == null) {
this._searchEntriesService.getResults(this.properties.searchAPIURLLAst+'publications?fq=communityid%20exact%20%22'+this.communityId+'%22&sortBy=resultdateofacceptance,descending&format=json&size=5').subscribe(
publicationResults => {
this.publicationResults = publicationResults;
this.showLoading = false;
//console.log(publicationResults);
},
error => {
this.handleError("Error getting publications for community with id: "+this.communityId, error);
}
);
}
}
public searchResearchData() {
this.activeTab = "dataset";
if (this.researchDataTotal > 0 && this.researchDataResults == null) {
this._searchEntriesService.getResults(this.properties.searchAPIURLLAst+'datasets?fq=communityid%20exact%20%22'+this.communityId+'%22&sortBy=resultdateofacceptance,descending&format=json&size=5').subscribe(
researchDataResults => {
this.researchDataResults = researchDataResults;
this.showLoading = false;
},
error => {
this.handleError("Error getting research data for community with id: "+this.communityId, error);
}
);
}
}
public searchSoftware() {
this.activeTab = "software";
if (this.softwareTotal > 0 && this.softwareResults == null) {
this._searchEntriesService.getResults(this.properties.searchAPIURLLAst+'software?fq=communityid%20exact%20%22'+this.communityId+'%22&sortBy=resultdateofacceptance,descending&format=json&size=5').subscribe(
softwareResults => {
this.softwareResults = softwareResults;
this.showLoading = false;
},
error => {
this.handleError("Error getting software for community with id: "+this.communityId, error);
}
);
}
}
public searchOrps() {
this.activeTab = "orp";
if (this.orpTotal > 0 && this.orpResults == null) {
this._searchEntriesService.getResults(this.properties.searchAPIURLLAst+'other?fq=communityid%20exact%20%22'+this.communityId+'%22&sortBy=resultdateofacceptance,descending&format=json&size=5').subscribe(
orpResults => {
this.orpResults = orpResults;
this.showLoading = false;
},
error => {
this.handleError("Error getting other research products for community with id: "+this.communityId, error);
}
);
}
}
public ngOnDestroy() {
if(this.piwiksub){
this.piwiksub.unsubscribe();
}
}
isEntityEnabled(entity: string) {
return this.communityInfo.entities.some(x => x['pid'] == entity && x['isEnabled']===true);
}
isRouteEnabled(route: string) {
return this.communityInfo.pages.some(x => x['route'] == route && x['isEnabled']===true);
}
showInvite(){
// console.log(Session.isPortalAdministrator() + Session.isCommunityCurator())
var email = Session.getUserEmail();
var index =-1;
if(email && this.community != null && this.community.managers != null){
index = this.community.managers.indexOf(email);
}
return Session.isPortalAdministrator() || Session.isCommunityCurator() || index != -1;
}
private handleError(message: string, error) {
console.error("Community (component): "+message, error);
}
getSearchPlaceHolder(){
var entities = [];
if(this.isEntityEnabled("publication")){
entities.push("publications");
}
if(this.isEntityEnabled("dataset")){
entities.push("research data");
}
if(this.isEntityEnabled("software")){
entities.push("software");
}
if(this.isEntityEnabled("orp")){
entities.push("other research products");
}
return "Search"+ ((entities.length > 0 )?(" for "+entities.join(', ')+"..."):"");
}
countOtherGraphs(){
var count = 0;
if(this.statistics && this.statistics.allowedEntities){
for(var entity of this.statistics.allowedEntities){
if(this.statistics.statisticsSum[entity].total>0 && this.statistics.allowedCharts[entity].length>0 && this.activeTab!=entity){
count+=this.statistics.allowedCharts[entity].length;
}
}
}
return count;
}
}