connect/src/app/communities/communities.component.ts

247 lines
9.1 KiB
TypeScript
Raw Normal View History

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';
import {Observable} from 'rxjs/Observable';
import "rxjs/add/observable/zip";
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';
import {StringUtils} from '../openaireLibrary/utils/string-utils.class';
import {ErrorCodes} from '../openaireLibrary/utils/properties/errorCodes';
import {ErrorMessagesComponent} from '../openaireLibrary/utils/errorMessages.component';
@Component({
selector: 'communities',
templateUrl: 'communities.component.html',
})
export class CommunitiesComponent {
public piwiksub: any;
public subfunders: any;
public pageTitle = "OpenAIRE"
public communitiesResults = null;
public communitiesToShow = null;
public researchCommunities = [];
public researchInitiatives = [];
public subscriberOfCommunities = [];
public managerOfCommunities = [];
// Message variables
public status: number;
public subscriberOfCommunitiesStatus: number;
public loading: boolean = true;
public errorCodes: ErrorCodes;
private errorMessages: ErrorMessagesComponent;
properties:EnvProperties;
public keyword:string="";
public type:string="all";
constructor (
private route: ActivatedRoute,
private _router: Router,
private location: Location,
private _meta: Meta,
private _title: Title,
private _piwikService:PiwikService,
private _communitiesService:CommunitiesService,
private _subscribeService: SubscribeService,
private config: ConfigurationService) {
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;
this.subscriberOfCommunitiesStatus = 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();
}
this.getCommunities();
});
}
public getCommunities() {
this.loading = true;
this.status = this.errorCodes.LOADING;
this.subscriberOfCommunitiesStatus = this.errorCodes.LOADING;
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);
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;
}else if(community['status'] == "manager"){
if(mail == null){ // no user
showCommunity = false;
}else if(Session.isCommunityCurator() || Session.isPortalAdministrator()){
isManager = true;
}else if(community.managers.indexOf(mail)!=-1){
isManager = true;
} else {
showCommunity = false;
}
}
if(showCommunity) {
if(community.type == "community"){
this.researchCommunities.push(community);
} else if(community.type == "ri") {
this.researchInitiatives.push(community);
}
if(isManager) {
this.managerOfCommunities.push(community);
}
}
this.status = this.errorCodes.DONE;
if(mail != null && showCommunity && !isManager){
this._subscribeService.isSubscribedToCommunity(community.communityId, mail, this.properties.adminToolsAPIURL).subscribe (
res => {
isSubscriber = res;
if(isSubscriber) {
this.subscriberOfCommunities.push(community);
}
this.subscriberOfCommunitiesStatus = this.errorCodes.DONE;
this.loading = false;
},
error => {
this.handleError("Error getting response if email: "+mail+" is subscribed to community with id: "+community.communityId, error, this.subscriberOfCommunitiesStatus);
this.loading = false;
});
} else {
this.subscriberOfCommunitiesStatus = this.errorCodes.DONE;
this.loading = false;
}
});
},
error => {
this.handleError("Error getting communities", error, status);
this.subscriberOfCommunitiesStatus = this.errorCodes.DONE;
this.loading = false;
}
);
}
public quote(param: string): string {
return StringUtils.quote(param);
}
public ngOnDestroy() {
if(this.piwiksub){
this.piwiksub.unsubscribe();
}
}
isProduction():boolean{
return this.properties.environment!="development";
}
getProductionPrefix():string{
return (this.properties.environment =="beta")?"beta.":""
}
showCommunity(community):boolean{
if(community['status'] == "hidden"){
return false;
}else if(community['status'] == "manager"){
var mail = Session.getUserEmail();
if(mail == null){ // no user
return false;
}else if(Session.isCommunityCurator() || Session.isPortalAdministrator()){
return true;
}else if(community.managers.indexOf(mail)!=-1){
return true;
}
return false;
}
return true;
}
existsIn(word, keyword):boolean{
if(word!= null && (keyword=="" || word.toLowerCase().indexOf(keyword.toLowerCase())!=-1)){
return true;
}
return false;
}
isType(communityType):boolean{
if(this.type == "all" || communityType == this.type){
return true;
}
return false;
}
searchChanged(){
this.communitiesToShow = [];
for(var i=0; i <this.communitiesResults.length; i++){
if((this.existsIn(this.communitiesResults[i].description, this.keyword) ||
this.existsIn(this.communitiesResults[i].title, this.keyword) ||
this.existsIn(this.communitiesResults[i].shortTitle, this.keyword))
&& this.isType(this.communitiesResults[i].type)){
this.communitiesToShow.push(this.communitiesResults[i]);
}
}
}
private handleError(message: string, error, status) {
var code = "";
if(!error.status) {
var error = error.json();
code = error.code;
} else {
code = error.status;
}
status = this.errorMessages.getErrorCode(code);
console.error("Communities (component): "+message, error);
}
}