import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; import {Subscriber} from 'rxjs'; import {properties} from 'src/environments/environment'; import {CommunityService} from '../../connect/community/community.service'; import {ConnectHelper} from '../../connect/connectHelper'; import {UserManagementService} from '../../services/user-management.service'; import {HelperFunctions} from "../../utils/HelperFunctions.class"; import {Context} from "../../utils/entities/resultLandingInfo"; import {OpenaireEntities} from "../../utils/properties/searchFields"; @Component({ selector: 'relatedTo, [relatedTo]', template: `
{{title}} {{title}} View less View more
{{community.labelContext}} {{community.labelContext}} {{community.labelCategory}} : {{community.labelConcept}}
` }) export class RelatedToComponent implements OnInit { @Input() contexts: Context[]; @Input() viewAll: boolean = false; @Output() viewAllClicked = new EventEmitter(); @Output() noCommunities = new EventEmitter(); public lessBtn: boolean = false; public threshold: number = 4; public communities: Context[] = []; public currentCommunity = ConnectHelper.getCommunityFromDomain(properties.domain); private subscriptions = []; public title: string = "Related to "+OpenaireEntities.COMMUNITIES; constructor(private communityService: CommunityService, private userManagementService: UserManagementService) { } ngOnInit() { this.contexts.sort(this.compare); let index = 0; this.contexts.forEach( context => { if(context.idContext) { this.subscriptions.push( this.userManagementService.getUserInfo().subscribe( user => { //- handling subscribe errors? this.subscriptions.push( this.communityService.getCommunityInfo(context.idContext).subscribe( community => { if(community && !ConnectHelper.isPrivate(community,user) && (this.currentCommunity != context.idContext)) { if(properties.environment == "beta") { context.link = 'https://beta.' + context.idContext + '.openaire.eu'; } else { context.link = 'https://' + context.idContext + '.openaire.eu'; } } for(let community of this.communities) { if(community.link == context.link) { index++; if(index == this.contexts.length) { this.communitiesInitialized(); } return; // skips so that we don't get duplicate gateways } } this.communities.push(context); index++; if(index == this.contexts.length) { this.communitiesInitialized(); } }, error => { index++; if(index == this.contexts.length) { this.communitiesInitialized(); } }) ); }) ); } }); } ngOnDestroy() { this.subscriptions.forEach(subscription => { if(subscription instanceof Subscriber) { subscription.unsubscribe(); } }); } public scroll() { HelperFunctions.scroll(); } public communitiesInitialized() { if(this.communities.length == 0) { this.noCommunities.emit(true); } this.communities.sort(this.compare); } public compare(a, b) { if (a.link && !b.link) { return -1; } else if(!a.link && b.link) { return 1; } else { if (a.labelContext < b.labelContext) { return -1; } else if (a.labelContext > b.labelContext) { return 1; } } return 0; } public viewAllClick() { if(this.communities.length <= this.threshold*2) { this.viewAll = true; this.lessBtn = true; } else { this.viewAll = true; this.viewAllClicked.emit('relatedTo'); } } public viewLessClick() { this.viewAll = false; this.viewAllClicked.emit(""); } }