openaire-library/landingPages/landing-utils/relatedTo.component.ts

90 lines
2.9 KiB
TypeScript

import {Component, Input} 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";
@Component({
selector: 'relatedTo',
template: `
<div class="uk-text-muted">Communities</div>
<div class="uk-margin-small-left" *ngFor="let item of contexts.slice(0, showNum); let i=index">
<span>
<a *ngIf="item['link']; else noLink" [href]="item['link']" target="_blank">{{item['labelContext']}}</a>
<ng-template #noLink>
<span>{{item['labelContext']}}</span>
</ng-template>
<span *ngIf="item['labelCategory']"><span
uk-icon="icon: arrow-right"></span>{{item['labelCategory']}}</span>
<span *ngIf="item['labelConcept']">: {{item['labelConcept']}}</span>
</span>
</div>
<div *ngIf="showNum > threshold" class="uk-text-right">
<a (click)="showNum = threshold; scroll()">
View less
</a>
</div>
<div *ngIf="showNum == threshold && contexts && contexts.length > threshold" class="uk-text-right">
<a (click)="showNum = contexts.length;">
View more
</a>
</div>
`
})
export class RelatedToComponent {
@Input() contexts: { "idContext": string, "labelContext": string, "labelCategory": string, "labelConcept": string, "link": string }[];
public threshold: number = 5;
public showNum: number = 5;
private subscriptions = [];
constructor(private communityService: CommunityService,
private userManagementService: UserManagementService) {
}
ngOnInit() {
if(properties.environment === "development") {
this.contexts.forEach( context => {
if(context.idContext) {
this.subscriptions.push(
this.userManagementService.getUserInfo().subscribe( user => {
//- handling subscribe errors?
this.subscriptions.push(
this.communityService.getCommunity(context.idContext).subscribe( community => {
if(community && !ConnectHelper.isPrivate(community,user) && (ConnectHelper.getCommunityFromDomain(properties.domain) != context.idContext)) {
// creating the link, based on the enviroment
let url = '';
if(properties.environment == "beta") {
url = 'https://beta.' + context.idContext + '.openaire.eu';
} else {
url = 'https://' + context.idContext + '.openaire.eu';
}
context.link = url;
}
})
);
})
);
}
});
}
}
ngOnDestroy() {
this.subscriptions.forEach(subscription => {
if(subscription instanceof Subscriber) {
subscription.unsubscribe();
}
});
}
public scroll() {
HelperFunctions.scroll();
}
}