2019-04-08 14:17:39 +02:00
|
|
|
import {Component, Input, Output, EventEmitter} from '@angular/core';
|
|
|
|
import {ViewChild, 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';
|
2018-03-14 14:28:45 +01:00
|
|
|
|
2019-04-08 14:17:39 +02:00
|
|
|
import {Observable} from 'rxjs/Observable';
|
2018-04-11 13:24:57 +02:00
|
|
|
|
2018-01-26 16:00:07 +01:00
|
|
|
import "rxjs/add/observable/zip";
|
2018-03-06 14:54:17 +01:00
|
|
|
|
2019-04-08 14:17:39 +02:00
|
|
|
import {ConnectHelper} from '../openaireLibrary/connect/connectHelper';
|
|
|
|
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
|
|
|
|
|
|
|
|
import {ConfigurationService} from '../openaireLibrary/utils/configuration/configuration.service';
|
|
|
|
import {CommunitiesService} from '../openaireLibrary/connect/communities/communities.service';
|
|
|
|
import {SubscribeService} from '../openaireLibrary/utils/subscribe/subscribe.service';
|
|
|
|
import {CommunityInfo} from '../openaireLibrary/connect/community/communityInfo';
|
|
|
|
|
|
|
|
import {PiwikService} from '../openaireLibrary/utils/piwik/piwik.service';
|
|
|
|
import {Session} from '../openaireLibrary/login/utils/helper.class';
|
2019-04-09 20:48:03 +02:00
|
|
|
import {StringUtils} from '../openaireLibrary/utils/string-utils.class';
|
2019-04-08 14:17:39 +02:00
|
|
|
|
|
|
|
import {ErrorCodes} from '../openaireLibrary/utils/properties/errorCodes';
|
|
|
|
import {ErrorMessagesComponent} from '../openaireLibrary/utils/errorMessages.component';
|
2018-03-14 14:28:45 +01:00
|
|
|
|
2018-01-26 16:00:07 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'communities',
|
|
|
|
templateUrl: 'communities.component.html',
|
|
|
|
})
|
2018-03-14 14:28:45 +01:00
|
|
|
|
2018-01-26 16:00:07 +01:00
|
|
|
export class CommunitiesComponent {
|
|
|
|
public piwiksub: any;
|
|
|
|
public subfunders: any;
|
|
|
|
|
|
|
|
public pageTitle = "OpenAIRE"
|
|
|
|
|
2018-02-27 11:25:34 +01:00
|
|
|
public communitiesResults = null;
|
2018-10-05 01:10:13 +02:00
|
|
|
public communitiesToShow = null;
|
2018-02-27 11:25:34 +01:00
|
|
|
|
2019-04-08 14:17:39 +02:00
|
|
|
public researchCommunities = [];
|
|
|
|
public researchInitiatives = [];
|
|
|
|
public subscriberOfCommunities = [];
|
|
|
|
public managerOfCommunities = [];
|
|
|
|
|
|
|
|
// Message variables
|
|
|
|
public status: number;
|
|
|
|
public loading: boolean = true;
|
2019-04-09 20:48:03 +02:00
|
|
|
public subscriberErrorMessage: string = "";
|
2019-04-08 14:17:39 +02:00
|
|
|
public errorCodes: ErrorCodes;
|
|
|
|
private errorMessages: ErrorMessagesComponent;
|
|
|
|
|
2018-04-11 13:24:57 +02:00
|
|
|
properties:EnvProperties;
|
2018-10-05 01:10:13 +02:00
|
|
|
public keyword:string="";
|
|
|
|
public type:string="all";
|
2018-01-26 16:00:07 +01:00
|
|
|
constructor (
|
2018-03-14 14:28:45 +01:00
|
|
|
private route: ActivatedRoute,
|
2018-01-26 16:00:07 +01:00
|
|
|
private _router: Router,
|
2018-04-11 13:24:57 +02:00
|
|
|
private location: Location,
|
|
|
|
private _meta: Meta,
|
|
|
|
private _title: Title,
|
2018-03-06 14:54:17 +01:00
|
|
|
private _piwikService:PiwikService,
|
|
|
|
private _communitiesService:CommunitiesService,
|
2019-04-08 14:17:39 +02:00
|
|
|
private _subscribeService: SubscribeService,
|
2018-04-11 13:24:57 +02:00
|
|
|
private config: ConfigurationService) {
|
|
|
|
|
2019-04-08 14:17:39 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
this.errorCodes = new ErrorCodes();
|
|
|
|
this.errorMessages = new ErrorMessagesComponent();
|
|
|
|
this.status = this.errorCodes.LOADING;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.properties.enablePiwikTrack && (typeof document !== 'undefined')){
|
|
|
|
this.piwiksub = this._piwikService.trackView(this.properties, "OpenAIRE Connect", this.properties.piwikSiteId).subscribe();
|
|
|
|
}
|
2018-01-26 16:00:07 +01:00
|
|
|
|
2019-04-08 14:17:39 +02:00
|
|
|
this.getCommunities();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public getCommunities() {
|
|
|
|
this.loading = true;
|
|
|
|
this.status = this.errorCodes.LOADING;
|
2019-04-09 20:48:03 +02:00
|
|
|
this.subscriberErrorMessage = "";
|
2019-04-08 14:17:39 +02:00
|
|
|
|
|
|
|
this.researchCommunities = [];
|
|
|
|
this.researchInitiatives = [];
|
|
|
|
this.subscriberOfCommunities = [];
|
|
|
|
this.managerOfCommunities = [];
|
|
|
|
|
|
|
|
this._communitiesService.getCommunities(this.properties, this.properties.communitiesAPI).subscribe(
|
|
|
|
communitiesResults => {
|
|
|
|
//this.communitiesResults = communitiesResults;
|
|
|
|
//this.communitiesToShow = this.communitiesResults.slice();
|
|
|
|
//console.log(communitiesResults);
|
2019-04-15 12:06:58 +02:00
|
|
|
this.sort(communitiesResults);
|
|
|
|
|
2019-04-16 12:31:30 +02:00
|
|
|
var subscribedLoading = communitiesResults.length;
|
2019-04-08 14:17:39 +02:00
|
|
|
var mail = Session.getUserEmail();
|
|
|
|
communitiesResults.forEach((community, index) => {
|
|
|
|
let showCommunity: boolean = true;
|
|
|
|
let isManager: boolean = false;
|
|
|
|
let isSubscriber: boolean = false;
|
|
|
|
|
|
|
|
if(community['status'] == "hidden"){
|
|
|
|
showCommunity = false;
|
2019-04-15 12:06:58 +02:00
|
|
|
}else {
|
|
|
|
if(mail == null && community['status'] == "manager"){ // no user
|
2019-04-08 14:17:39 +02:00
|
|
|
showCommunity = false;
|
|
|
|
}else if(Session.isCommunityCurator() || Session.isPortalAdministrator()){
|
|
|
|
isManager = true;
|
|
|
|
}else if(community.managers.indexOf(mail)!=-1){
|
|
|
|
isManager = true;
|
2019-04-15 12:06:58 +02:00
|
|
|
} else if(community['status'] == "manager") {
|
2019-04-08 14:17:39 +02:00
|
|
|
showCommunity = false;
|
|
|
|
}
|
|
|
|
}
|
2018-02-12 12:41:40 +01:00
|
|
|
|
2019-04-08 14:17:39 +02:00
|
|
|
if(showCommunity) {
|
|
|
|
if(community.type == "community"){
|
|
|
|
this.researchCommunities.push(community);
|
|
|
|
} else if(community.type == "ri") {
|
|
|
|
this.researchInitiatives.push(community);
|
|
|
|
}
|
2018-02-27 11:25:34 +01:00
|
|
|
|
2019-04-08 14:17:39 +02:00
|
|
|
if(isManager) {
|
2019-04-15 12:06:58 +02:00
|
|
|
community.isManager = true;
|
2019-04-08 14:17:39 +02:00
|
|
|
this.managerOfCommunities.push(community);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.status = this.errorCodes.DONE;
|
|
|
|
|
2019-04-15 12:06:58 +02:00
|
|
|
if(mail != null && showCommunity) {
|
2019-04-08 14:17:39 +02:00
|
|
|
this._subscribeService.isSubscribedToCommunity(community.communityId, mail, this.properties.adminToolsAPIURL).subscribe (
|
|
|
|
res => {
|
|
|
|
isSubscriber = res;
|
|
|
|
if(isSubscriber) {
|
2019-04-15 12:06:58 +02:00
|
|
|
community.isSubscribed = true;
|
2019-04-16 14:31:25 +02:00
|
|
|
if(isManager) {
|
2019-04-15 12:06:58 +02:00
|
|
|
this.subscriberOfCommunities.push(community);
|
2019-04-16 14:31:25 +02:00
|
|
|
} else {
|
|
|
|
this.subscriberOfCommunities.unshift(community);
|
2019-04-15 12:06:58 +02:00
|
|
|
}
|
2019-04-08 14:17:39 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 12:31:30 +02:00
|
|
|
subscribedLoading--;
|
|
|
|
if(subscribedLoading == 0) {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
2019-04-08 14:17:39 +02:00
|
|
|
},
|
|
|
|
error => {
|
2019-04-16 12:31:30 +02:00
|
|
|
// if(this.properties.environment == "development") {
|
|
|
|
// this.subscriberErrorMessage = "Error fetching communities you are subscribed to";
|
|
|
|
// }
|
2019-04-09 20:48:03 +02:00
|
|
|
this.handleError("Error getting response if email: "+mail+" is subscribed to community with id: "+community.communityId, error);
|
2019-04-16 12:31:30 +02:00
|
|
|
subscribedLoading--;
|
|
|
|
if(subscribedLoading == 0) {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
2019-04-08 14:17:39 +02:00
|
|
|
});
|
|
|
|
} else {
|
2019-04-16 12:31:30 +02:00
|
|
|
subscribedLoading--;
|
|
|
|
if(subscribedLoading == 0) {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
2019-04-08 14:17:39 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
error => {
|
2019-04-09 20:48:03 +02:00
|
|
|
this.status = this.handleError("Error getting communities", error);
|
2019-04-08 14:17:39 +02:00
|
|
|
this.loading = false;
|
2018-02-27 11:25:34 +01:00
|
|
|
}
|
2019-04-08 14:17:39 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-15 12:06:58 +02:00
|
|
|
private sort(results: CommunityInfo[]) {
|
|
|
|
results.sort((left, right): number => {
|
|
|
|
if (!right.date || left.date > right.date) {
|
|
|
|
return -1;
|
|
|
|
} else if (!left.date || left.date < right.date) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
if (left.title > right.title) {
|
|
|
|
return 1;
|
|
|
|
} else if (left.title < right.title) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-08 14:17:39 +02:00
|
|
|
public quote(param: string): string {
|
|
|
|
return StringUtils.quote(param);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnDestroy() {
|
|
|
|
if(this.piwiksub){
|
|
|
|
this.piwiksub.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 20:48:03 +02:00
|
|
|
private handleError(message: string, error): number {
|
2019-04-08 14:17:39 +02:00
|
|
|
var code = "";
|
|
|
|
if(!error.status) {
|
|
|
|
var error = error.json();
|
|
|
|
code = error.code;
|
|
|
|
} else {
|
|
|
|
code = error.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.error("Communities (component): "+message, error);
|
2019-04-09 20:48:03 +02:00
|
|
|
|
|
|
|
return this.errorMessages.getErrorCode(code);
|
2019-04-08 14:17:39 +02:00
|
|
|
}
|
2018-01-26 16:00:07 +01:00
|
|
|
}
|