2019-04-18 14:50:14 +02:00
import { Component } from '@angular/core' ;
2019-06-05 15:39:02 +02:00
import { ActivatedRoute , Router } from '@angular/router' ;
import { Meta , Title } from '@angular/platform-browser' ;
import { EnvProperties } from '../openaireLibrary/utils/properties/env-properties' ;
import { CommunitiesService } from '../openaireLibrary/connect/communities/communities.service' ;
import { SubscribeService } from '../openaireLibrary/utils/subscribe/subscribe.service' ;
import { CommunityInfo } from '../openaireLibrary/connect/community/communityInfo' ;
2018-03-14 14:28:45 +01:00
2019-06-05 15:39:02 +02:00
import { PiwikService } from '../openaireLibrary/utils/piwik/piwik.service' ;
import { Session } from '../openaireLibrary/login/utils/helper.class' ;
import { StringUtils } from '../openaireLibrary/utils/string-utils.class' ;
2019-04-08 14:17:39 +02:00
2019-06-05 15:39:02 +02:00
import { ErrorCodes } from '../openaireLibrary/utils/properties/errorCodes' ;
import { ErrorMessagesComponent } from '../openaireLibrary/utils/errorMessages.component' ;
2019-07-18 13:36:05 +02:00
import { HelperService } from "../openaireLibrary/utils/helper/helper.service" ;
2019-08-02 16:56:03 +02:00
import { SEOService } from "../openaireLibrary/sharedComponents/SEO/SEO.service" ;
2018-03-14 14:28:45 +01:00
2018-01-26 16:00:07 +01:00
@Component ( {
2019-07-18 13:36:05 +02:00
selector : 'communities' ,
templateUrl : 'communities.component.html' ,
2018-01-26 16:00:07 +01:00
} )
2018-03-14 14:28:45 +01:00
2018-01-26 16:00:07 +01:00
export class CommunitiesComponent {
public piwiksub : any ;
public pageTitle = "OpenAIRE"
2019-06-24 16:11:27 +02:00
// TODO remove initiatives
2019-04-08 14:17:39 +02:00
public researchCommunities = [ ] ;
public researchInitiatives = [ ] ;
public subscriberOfCommunities = [ ] ;
public managerOfCommunities = [ ] ;
2019-07-18 13:36:05 +02:00
public gifs : { "gif" : string , "header" : string , "text" } [ ] = [ ] ;
public pageContents = null ;
public divContents = null ;
2019-04-08 14:17:39 +02:00
// 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 ;
2019-07-18 13:36:05 +02:00
properties : EnvProperties ;
public keyword : string = "" ;
public type : string = "all" ;
constructor (
private route : ActivatedRoute ,
private _router : Router ,
private _meta : Meta ,
private _title : Title ,
private _piwikService : PiwikService ,
private _communitiesService : CommunitiesService ,
private _subscribeService : SubscribeService ,
2019-08-02 16:56:03 +02:00
private helper : HelperService ,
private seoService : SEOService ) {
2019-07-18 13:36:05 +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 ;
2019-04-08 14:17:39 +02:00
}
public ngOnInit() {
this . route . data
2019-07-18 13:36:05 +02:00
. subscribe ( ( data : { envSpecific : EnvProperties } ) = > {
this . properties = data . envSpecific ;
2019-08-02 16:56:03 +02:00
var url = data . envSpecific . baseLink + this . _router . url ;
this . seoService . createLinkForCanonicalURL ( url , false ) ;
2019-07-18 13:36:05 +02:00
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 ( ) ;
this . createGifs ( ) ;
//this.getDivContents();
this . getPageContents ( ) ;
} ) ;
}
2018-01-26 16:00:07 +01:00
2019-07-18 18:03:37 +02:00
private getPageContents() {
2019-07-22 11:19:34 +02:00
this . helper . getPageHelpContents ( this . _router . url , this . properties , 'connect' ) . subscribe ( contents = > {
2019-07-18 13:36:05 +02:00
this . pageContents = contents ;
} )
}
2019-07-18 18:03:37 +02:00
private getDivContents() {
2019-07-22 11:19:34 +02:00
this . helper . getDivHelpContents ( this . _router . url , this . properties , 'connect' ) . subscribe ( contents = > {
2019-07-18 13:36:05 +02:00
this . divContents = contents ;
} )
}
public getCommunities() {
this . loading = true ;
this . status = this . errorCodes . LOADING ;
this . subscriberErrorMessage = "" ;
2019-04-08 14:17:39 +02:00
2019-07-18 13:36:05 +02:00
this . researchCommunities = [ ] ;
this . researchInitiatives = [ ] ;
this . subscriberOfCommunities = [ ] ;
this . managerOfCommunities = [ ] ;
this . _communitiesService . getCommunitiesState ( ) . subscribe (
communitiesResults = > {
2019-07-30 10:59:40 +02:00
if ( ! communitiesResults ) {
return ;
}
2019-07-30 17:25:30 +02:00
if ( communitiesResults . length == 0 ) {
this . status = this . errorCodes . DONE ;
return ;
}
2019-07-18 13:36:05 +02:00
//this.communitiesResults = communitiesResults;
//this.communitiesToShow = this.communitiesResults.slice();
//console.log(communitiesResults);
this . sort ( communitiesResults ) ;
var subscribedLoading = communitiesResults . length ;
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 ( mail == null && community [ 'status' ] == "manager" ) { // no user
showCommunity = false ;
} else if ( Session . isCommunityCurator ( ) || Session . isPortalAdministrator ( ) ) {
isManager = true ;
} else if ( community . managers . indexOf ( mail ) != - 1 ) {
isManager = true ;
} else if ( community [ 'status' ] == "manager" ) {
2019-04-08 14:17:39 +02:00
showCommunity = false ;
}
2019-07-18 13:36:05 +02:00
}
2018-02-12 12:41:40 +01:00
2019-07-18 13:36:05 +02:00
if ( showCommunity ) {
// TODO remove
/ * i f ( c o m m u n i t y . t y p e = = " c o m m u n i t y " ) {
2019-06-24 16:11:27 +02:00
this . researchCommunities . push ( community ) ;
2019-07-18 13:36:05 +02:00
} else if ( community . type == "ri" ) {
this . researchInitiatives . push ( community ) ;
} * /
this . researchCommunities . push ( community ) ;
if ( isManager ) {
community . isManager = true ;
this . managerOfCommunities . push ( community ) ;
2019-04-08 14:17:39 +02:00
}
2019-07-18 13:36:05 +02:00
}
2019-04-08 14:17:39 +02:00
2019-07-18 13:36:05 +02:00
this . status = this . errorCodes . DONE ;
2019-04-08 14:17:39 +02:00
2019-07-18 13:36:05 +02:00
if ( mail != null && showCommunity ) {
this . _subscribeService . isSubscribedToCommunity ( this . properties , community . communityId , mail ) . subscribe (
res = > {
isSubscriber = res ;
if ( isSubscriber ) {
community . isSubscribed = true ;
if ( isManager ) {
this . subscriberOfCommunities . push ( community ) ;
} else {
this . subscriberOfCommunities . unshift ( community ) ;
2019-04-16 12:31:30 +02:00
}
2019-07-18 13:36:05 +02:00
}
subscribedLoading -- ;
if ( subscribedLoading == 0 ) {
this . loading = false ;
}
} ,
error = > {
// if(this.properties.environment == "development") {
// this.subscriberErrorMessage = "Error fetching communities you are subscribed to";
// }
this . handleError ( "Error getting response if email: " + mail + " is subscribed to community with id: " + community . communityId , error ) ;
subscribedLoading -- ;
if ( subscribedLoading == 0 ) {
this . loading = false ;
}
} ) ;
} else {
subscribedLoading -- ;
if ( subscribedLoading == 0 ) {
this . loading = false ;
2019-04-08 14:17:39 +02:00
}
2019-07-18 13:36:05 +02:00
}
} ) ;
} ,
error = > {
this . status = this . handleError ( "Error getting communities" , error ) ;
this . loading = false ;
}
) ;
}
2019-06-24 21:22:27 +02:00
2019-07-18 13:36:05 +02:00
private createGifs() {
this . gifs . push ( {
2019-07-26 12:59:38 +02:00
gif : "assets/connect-assets/home/gifs/deposit.gif" ,
2019-07-18 13:36:05 +02:00
header : "Find a repository to deposit your research outcome" ,
text : "This is OpenAIRE’ s key service for research communities, both established and emerging ones. Our service helps you reach out and engage all your researchers to practice open science out-of-the-box."
} ) ;
this . gifs . push ( {
2019-07-26 12:59:38 +02:00
gif : "assets/connect-assets/home/gifs/link.gif" ,
2019-07-18 13:36:05 +02:00
header : "Link your research output with your community, funding, and other research products" ,
text : "This is OpenAIRE’ s key service for research communities, both established and emerging ones. Our service helps you reach out and engage all your researchers to practice open science out-of-the-box."
} ) ;
this . gifs . push ( {
2019-07-26 12:59:38 +02:00
gif : "assets/connect-assets/home/gifs/overview.gif" ,
2019-07-18 13:36:05 +02:00
header : "View community's overview at a glance" ,
text : "This is OpenAIRE’ s key service for research communities, both established and emerging ones. Our service helps you reach out and engage all your researchers to practice open science out-of-the-box."
} ) ;
this . gifs . push ( {
2019-07-26 12:59:38 +02:00
gif : "assets/connect-assets/home/gifs/results.gif" ,
2019-07-18 13:36:05 +02:00
header : "Search & browse your community's research products. " ,
text : "This is OpenAIRE’ s key service for research communities, both established and emerging ones. Our service helps you reach out and engage all your researchers to practice open science out-of-the-box."
} ) ;
this . gifs . push ( {
2019-07-26 12:59:38 +02:00
gif : "assets/connect-assets/home/gifs/graph-analysis.gif" ,
2019-07-18 13:36:05 +02:00
header : "View statistics for your community's research products." ,
text : "This is OpenAIRE’ s key service for research communities, both established and emerging ones. Our service helps you reach out and engage all your researchers to practice open science out-of-the-box."
} ) ;
}
2019-04-08 14:17:39 +02:00
2019-07-18 13:36:05 +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 ) {
2019-04-15 12:06:58 +02:00
return 1 ;
2019-07-18 13:36:05 +02:00
} else if ( left . title < right . title ) {
return - 1 ;
2019-04-15 12:06:58 +02:00
} else {
2019-07-18 13:36:05 +02:00
return 0 ;
2019-04-15 12:06:58 +02:00
}
2019-07-18 13:36:05 +02:00
}
} )
}
2019-04-15 12:06:58 +02:00
2019-07-18 13:36:05 +02:00
public quote ( param : string ) : string {
return StringUtils . quote ( param ) ;
}
2019-04-08 14:17:39 +02:00
2019-07-18 13:36:05 +02:00
public ngOnDestroy() {
if ( this . piwiksub ) {
this . piwiksub . unsubscribe ( ) ;
2019-04-08 14:17:39 +02:00
}
2019-07-18 13:36:05 +02:00
}
2019-04-08 14:17:39 +02:00
2019-07-18 13:36:05 +02:00
private handleError ( message : string , error ) : number {
var code = "" ;
if ( ! error . status ) {
var error = error . json ( ) ;
code = error . code ;
} else {
code = error . status ;
}
2019-04-08 14:17:39 +02:00
2019-07-18 13:36:05 +02:00
console . error ( "Communities (component): " + message , error ) ;
2019-04-09 20:48:03 +02:00
2019-07-18 13:36:05 +02:00
return this . errorMessages . getErrorCode ( code ) ;
}
2018-01-26 16:00:07 +01:00
}