2020-11-13 16:08:17 +01:00
import { Component , ViewChild } from '@angular/core' ;
2019-09-30 14:51:38 +02:00
import { ActivatedRoute , Router } from '@angular/router' ;
import { Meta , Title } from '@angular/platform-browser' ;
import { EnvProperties } from '../openaireLibrary/utils/properties/env-properties' ;
import { PiwikService } from '../openaireLibrary/utils/piwik/piwik.service' ;
import { ErrorCodes } from '../openaireLibrary/utils/properties/errorCodes' ;
import { ErrorMessagesComponent } from '../openaireLibrary/utils/errorMessages.component' ;
import { HelperService } from "../openaireLibrary/utils/helper/helper.service" ;
import { SEOService } from "../openaireLibrary/sharedComponents/SEO/SEO.service" ;
2019-12-03 12:10:05 +01:00
import { RefineFieldResultsService } from "../openaireLibrary/services/refineFieldResults.service" ;
import { NumberUtils } from "../openaireLibrary/utils/number-utils.class" ;
import { SearchResearchResultsService } from "../openaireLibrary/services/searchResearchResults.service" ;
2020-06-03 23:55:32 +02:00
import { StakeholderService } from "../openaireLibrary/monitor/services/stakeholder.service" ;
2020-06-09 11:19:25 +02:00
import { LocalStorageService } from "../openaireLibrary/services/localStorage.service" ;
2022-02-17 16:31:58 +01:00
import { Stakeholder , StakeholderInfo } from "../openaireLibrary/monitor/entities/stakeholder" ;
import { User } from "../openaireLibrary/login/utils/helper.class" ;
2020-06-10 23:51:10 +02:00
import { UserManagementService } from "../openaireLibrary/services/user-management.service" ;
2020-11-13 16:08:17 +01:00
import { properties } from "../../environments/environment" ;
import { Subscriber } from "rxjs" ;
2019-09-30 14:51:38 +02:00
@Component ( {
selector : 'home' ,
templateUrl : 'home.component.html' ,
2022-02-16 11:03:03 +01:00
styleUrls : [ 'home.component.css' ]
2019-09-30 14:51:38 +02:00
} )
export class HomeComponent {
public pageTitle = "OpenAIRE | Monitor" ;
2022-02-16 11:03:03 +01:00
public description = "OpenAIRE - Monitor, A new era of monitoring research. Open data. Open methodologies. Work together with us to view, understand and visualize research statistics and indicators." ;
2022-02-17 16:31:58 +01:00
public stakeholders : StakeholderInfo [ ] = [ ] ;
2020-06-09 11:19:25 +02:00
public selected : Stakeholder = null ;
2019-09-30 14:51:38 +02:00
public pageContents = null ;
public divContents = null ;
2019-12-03 12:10:05 +01:00
// Message variables
2019-09-30 14:51:38 +02:00
public status : number ;
public loading : boolean = true ;
public errorCodes : ErrorCodes ;
2022-02-16 11:03:03 +01:00
public properties : EnvProperties = properties ;
2020-06-09 11:19:25 +02:00
public type : string = null ;
2022-02-16 11:03:03 +01:00
public directLink : boolean = true ;
2020-06-03 23:55:32 +02:00
public publicationsSize : any = null ;
public datasetsSize : any = null ;
2019-12-03 12:10:05 +01:00
public softwareSize : any = null ;
public otherSize : any = null ;
2020-06-03 23:55:32 +02:00
public fundersSize : any = null ;
2020-06-09 11:19:25 +02:00
@ViewChild ( 'AlertModal' ) modal ;
2022-02-16 11:03:03 +01:00
private errorMessages : ErrorMessagesComponent ;
private subscriptions = [ ] ;
2020-06-10 23:51:10 +02:00
private user : User ;
2020-06-03 23:55:32 +02:00
2019-09-30 14:51:38 +02:00
constructor (
private route : ActivatedRoute ,
private _router : Router ,
private _meta : Meta ,
private _title : Title ,
private _piwikService : PiwikService ,
2019-12-03 12:10:05 +01:00
private _stakeholderService : StakeholderService ,
2020-06-09 11:19:25 +02:00
private localStorageService : LocalStorageService ,
2020-06-10 23:51:10 +02:00
private userManagementService : UserManagementService ,
2019-09-30 14:51:38 +02:00
private helper : HelperService ,
2019-12-03 12:10:05 +01:00
private seoService : SEOService ,
2020-06-03 23:55:32 +02:00
private _refineFieldResultsService : RefineFieldResultsService , private _searchResearchResultsService : SearchResearchResultsService ) {
2022-02-16 11:03:03 +01:00
this . _meta . updateTag ( { content : this.description } , "name='description'" ) ;
this . _meta . updateTag ( { content : this.description } , "property='og:description'" ) ;
this . _meta . updateTag ( { content : this.pageTitle } , "property='og:title'" ) ;
this . _title . setTitle ( this . pageTitle ) ;
2019-09-30 14:51:38 +02:00
this . errorCodes = new ErrorCodes ( ) ;
this . errorMessages = new ErrorMessagesComponent ( ) ;
this . status = this . errorCodes . LOADING ;
}
2020-11-13 16:08:17 +01:00
2019-09-30 14:51:38 +02:00
public ngOnInit() {
2022-02-16 11:03:03 +01:00
let url = this . properties . domain + this . properties . baseLink + this . _router . url ;
2020-11-13 16:08:17 +01:00
this . seoService . createLinkForCanonicalURL ( url , false ) ;
this . _meta . updateTag ( { content : url } , "property='og:url'" ) ;
if ( this . properties . enablePiwikTrack && ( typeof document !== 'undefined' ) ) {
this . subscriptions . push ( this . _piwikService . trackView ( this . properties , "OpenAIRE Monitor" , this . properties . piwikSiteId ) . subscribe ( ) ) ;
}
this . getNumbers ( ) ;
this . subscriptions . push ( this . localStorageService . get ( ) . subscribe ( value = > {
this . directLink = value ;
} ) ) ;
this . subscriptions . push ( this . userManagementService . getUserInfo ( ) . subscribe ( user = > {
this . user = user ;
2022-02-17 16:31:58 +01:00
this . getStakeholders ( ) ;
2020-11-13 16:08:17 +01:00
} ) ) ;
2020-06-03 23:55:32 +02:00
}
2019-09-30 14:51:38 +02:00
private getPageContents() {
2020-11-13 16:08:17 +01:00
this . subscriptions . push ( this . helper . getPageHelpContents ( this . properties , 'monitor' , this . _router . url ) . subscribe ( contents = > {
2019-09-30 14:51:38 +02:00
this . pageContents = contents ;
2020-11-13 16:08:17 +01:00
} ) ) ;
2019-09-30 14:51:38 +02:00
}
2020-06-03 23:55:32 +02:00
2019-09-30 14:51:38 +02:00
private getDivContents() {
2020-11-13 16:08:17 +01:00
this . subscriptions . push ( this . helper . getDivHelpContents ( this . properties , 'monitor' , this . _router . url ) . subscribe ( contents = > {
2019-09-30 14:51:38 +02:00
this . divContents = contents ;
2020-11-13 16:08:17 +01:00
} ) ) ;
2019-09-30 14:51:38 +02:00
}
2020-06-03 23:55:32 +02:00
2020-06-09 11:19:25 +02:00
public get stakeholdersNumber ( ) : number {
if ( this . type === null ) {
return this . stakeholders . length ;
} else {
return this . stakeholders . filter ( stakeholder = > stakeholder . type === this . type ) . length ;
}
}
2020-06-03 23:55:32 +02:00
getNumbers() {
2020-11-13 16:08:17 +01:00
this . subscriptions . push ( this . _refineFieldResultsService . getRefineFieldsResultsByEntityName ( [ "funder" ] , "project" , this . properties ) . subscribe (
2019-12-03 12:10:05 +01:00
data = > {
2020-06-03 23:55:32 +02:00
if ( data [ 1 ] . length > 0 && data [ 1 ] [ 0 ] . filterId == "funder" && data [ 1 ] [ 0 ] . values ) {
2019-12-03 12:10:05 +01:00
this . fundersSize = NumberUtils . roundNumber ( data [ 1 ] [ 0 ] . values . length ) ;
}
} ,
err = > {
//console.log(err);
this . handleError ( "Error getting 'funder' field results of projects" , err ) ;
} ) ) ;
2020-11-13 16:08:17 +01:00
this . subscriptions . push ( this . _searchResearchResultsService . numOfSearchResults ( "publication" , "" , this . properties ) . subscribe (
2020-06-03 23:55:32 +02:00
data = > {
if ( data && data > 0 ) {
this . publicationsSize = NumberUtils . roundNumber ( data ) ;
2019-12-03 12:10:05 +01:00
}
2020-06-03 23:55:32 +02:00
} ,
err = > {
//console.log(err);
this . handleError ( "Error getting number of publications" , err ) ;
}
) ) ;
2020-11-13 16:08:17 +01:00
this . subscriptions . push ( this . _searchResearchResultsService . numOfSearchResults ( "dataset" , "" , this . properties ) . subscribe (
2020-06-03 23:55:32 +02:00
data = > {
if ( data && data > 0 ) {
this . datasetsSize = NumberUtils . roundNumber ( data ) ;
2019-12-03 12:10:05 +01:00
}
2020-06-03 23:55:32 +02:00
} ,
err = > {
//console.log(err);
this . handleError ( "Error getting number of research data" , err ) ;
}
) ) ;
2020-11-13 16:08:17 +01:00
this . subscriptions . push ( this . _searchResearchResultsService . numOfSearchResults ( "software" , "" , this . properties ) . subscribe (
2020-06-03 23:55:32 +02:00
data = > {
if ( data && data > 0 ) {
this . softwareSize = NumberUtils . roundNumber ( data ) ;
2019-12-03 12:10:05 +01:00
}
2020-06-03 23:55:32 +02:00
} ,
err = > {
this . handleError ( "Error getting number of software data" , err ) ;
}
) ) ;
2020-11-13 16:08:17 +01:00
this . subscriptions . push ( this . _searchResearchResultsService . numOfSearchResults ( "other" , "" , this . properties ) . subscribe (
2020-06-03 23:55:32 +02:00
data = > {
if ( data && data > 0 ) {
this . otherSize = NumberUtils . roundNumber ( data ) ;
2019-12-03 12:10:05 +01:00
}
2020-06-03 23:55:32 +02:00
} ,
err = > {
this . handleError ( "Error getting number of software data" , err ) ;
}
) ) ;
2019-12-03 12:10:05 +01:00
}
2020-06-03 23:55:32 +02:00
2020-06-10 23:51:10 +02:00
public getStakeholders() {
2019-12-03 12:10:05 +01:00
this . loading = true ;
this . status = this . errorCodes . LOADING ;
2020-11-13 16:08:17 +01:00
this . subscriptions . push ( this . _stakeholderService . getStakeholders ( this . properties . monitorServiceAPIURL ) . subscribe (
2020-06-03 23:55:32 +02:00
stakeholders = > {
if ( ! stakeholders || stakeholders . length == 0 ) {
this . status = this . errorCodes . NONE ;
} else {
2022-02-17 16:31:58 +01:00
this . stakeholders = StakeholderInfo . toStakeholderInfo ( stakeholders , this . user ) ;
this . sort ( this . stakeholders ) ;
this . stakeholders = this . publicStakeholders . concat ( this . privateStakeholders ) ;
2019-12-03 12:10:05 +01:00
}
this . loading = false ;
} ,
error = > {
2022-02-16 11:03:03 +01:00
this . status = this . handleError ( "Error getting stakeholders" , error ) ;
2019-12-03 12:10:05 +01:00
this . loading = false ;
}
2020-11-13 16:08:17 +01:00
) ) ;
2019-12-03 12:10:05 +01:00
}
2020-06-03 23:55:32 +02:00
2022-02-17 16:31:58 +01:00
private sort ( results : StakeholderInfo [ ] ) {
results . sort ( ( left , right ) : number = > {
if ( left . name > right . name ) {
return 1 ;
} else if ( left . name < right . name ) {
return - 1 ;
} else {
return 0 ;
}
} )
}
public slider ( stakeholders : StakeholderInfo [ ] , size : number = 6 ) : Stakeholder [ ] [ ] {
2022-02-17 13:44:23 +01:00
let slider : Stakeholder [ ] [ ] = [ ] ;
for ( let i = 0 ; i < ( stakeholders . length / size ) ; i ++ ) {
slider . push ( stakeholders . slice ( i * size , ( ( i + 1 ) * size ) ) ) ;
}
return slider ;
}
2022-02-17 16:31:58 +01:00
get publicStakeholders ( ) : StakeholderInfo [ ] {
return this . stakeholders . filter ( stakeholder = > stakeholder . visibility === "PUBLIC" ) ;
}
2022-02-17 13:44:23 +01:00
2022-02-17 16:31:58 +01:00
get privateStakeholders ( ) : StakeholderInfo [ ] {
return this . stakeholders . filter ( stakeholder = > stakeholder . visibility !== "PUBLIC" ) ;
}
get funders ( ) : StakeholderInfo [ ] {
2022-02-17 13:44:23 +01:00
if ( this . stakeholders ) {
return this . stakeholders . filter ( stakeholder = > stakeholder . type === "funder" ) ;
} else {
return [ ] ;
}
}
2022-02-17 16:31:58 +01:00
get researchInitiatives ( ) : StakeholderInfo [ ] {
2022-02-17 13:44:23 +01:00
if ( this . stakeholders ) {
return this . stakeholders . filter ( stakeholder = > stakeholder . type === "ri" ) ;
} else {
return [ ] ;
}
}
2022-02-17 16:31:58 +01:00
get organizations ( ) : StakeholderInfo [ ] {
2022-02-17 13:44:23 +01:00
if ( this . stakeholders ) {
return this . stakeholders . filter ( stakeholder = > stakeholder . type === "organization" ) ;
} else {
return [ ] ;
}
2022-02-16 11:03:03 +01:00
}
2020-11-13 16:08:17 +01:00
ngOnDestroy() {
this . subscriptions . forEach ( subscription = > {
if ( subscription instanceof Subscriber ) {
subscription . unsubscribe ( ) ;
}
} ) ;
2019-09-30 14:51:38 +02:00
}
2020-06-03 23:55:32 +02:00
2019-09-30 14:51:38 +02:00
private handleError ( message : string , error ) : number {
2022-02-16 11:03:03 +01:00
let code = "" ;
2019-09-30 14:51:38 +02:00
if ( ! error . status ) {
code = error . code ;
} else {
code = error . status ;
}
2020-10-26 14:11:13 +01:00
console . error ( "Home Component: " + message , error ) ;
2019-09-30 14:51:38 +02:00
return this . errorMessages . getErrorCode ( code ) ;
}
}