openaire-library/searchPages/searchUtils/portal-search-result.compon...

117 lines
4.0 KiB
TypeScript
Raw Normal View History

import {Component, Input, OnChanges, OnInit, SimpleChanges, ViewChild} from '@angular/core';
import {ErrorCodes} from '../../utils/properties/errorCodes';
import {RouterHelper} from '../../utils/routerHelper.class';
import {EnvProperties} from '../../utils/properties/env-properties';
import {CommunityInfo, prodReadyCommunities} from "../../connect/community/communityInfo";
import {Router} from "@angular/router";
import {LocalStorageService} from "../../services/localStorage.service";
import {Stakeholder, StakeholderInfo, Visibility} from "../../monitor/entities/stakeholder";
import {Subscriber} from "rxjs";
@Component({
selector: 'portal-search-result',
templateUrl: 'portal-search-result.component.html'
})
export class PortalSearchResultComponent implements OnInit, OnChanges{
@Input() results: (CommunityInfo & StakeholderInfo)[];
@Input() status: number;
@Input() type: string;
@Input() showType = false;
@Input() showLoading: boolean = false;
@Input() custom_class: string = "search-results";
@Input() properties: EnvProperties;
@Input() maxCharacters: number = 150;
@ViewChild('AlertModal') modal;
/*visibilityIcon: Map<Visibility, string> = new Map<Visibility, string> ([
["PUBLIC", 'earth'],
["PRIVATE", 'lock'],
["RESTRICTED", 'group']
]);*/
public urlParam: string;
public errorCodes: ErrorCodes = new ErrorCodes();
public routerHelper: RouterHelper = new RouterHelper();
public errorMessage: string = "No results found";
public selected: CommunityInfo & Stakeholder;
public directLink: boolean = true;
sub;
constructor(private router: Router,
private localStorageService: LocalStorageService) {
}
ngOnDestroy() {
if(this.sub){
this.sub.unsubscribe();
}
}
ngOnInit() {
this.sub = this.localStorageService.get().subscribe(value => {
this.directLink = value;
});
}
ngOnChanges(changes: SimpleChanges) {
console.log(changes);
}
getProductionPrefix(id:string): string {
return (this.properties.environment == "production" && prodReadyCommunities.indexOf(id)!=-1) ? "" : "beta.";
}
isProduction(): boolean {
return this.properties.environment != "development";
}
public _formatDescription(description) {
return (((description).length > this.maxCharacters) ? (description.substring(0, (this.maxCharacters - ('...').length)) + "...") : description);
}
public confirmModalOpen(result: CommunityInfo & Stakeholder) {
this.selected = result;
this.modal.cancelButton = true;
this.modal.okButton = true;
if(this.type === 'stakeholder') {
this.modal.alertTitle = 'You are going to visit ' + result.name + ' Monitor Dashboard';
} else if (this.type === 'community') {
this.modal.alertTitle = 'You are going to visit ' + ((result.title) ? result.title : result.shortTitle) +' Gateway';
}
this.modal.alertMessage = false;
this.modal.okButtonLeft = false;
this.modal.okButtonText = 'Yes';
this.modal.cancelButtonText = 'No';
this.modal.choice = true;
this.modal.open();
}
public getCommunityPageUrl(communityInfo: CommunityInfo): string {
let url = '';
if (this.isProduction()) {
url = 'https://' + this.getProductionPrefix(communityInfo.communityId ) + communityInfo.communityId + '.openaire.eu';
} else {
url = this.router.createUrlTree(['/'], {
queryParams: {'communityId': communityInfo.communityId}
}).toString();
}
return url;
}
public getStakeholderPageUrl(stakeholder: Stakeholder) {
return this.properties.domain + this.properties.baseLink +"/dashboard/" + stakeholder.alias;
}
public goToPage(data: any) {
if (data.value == true) {
let url = '';
if (this.type === 'stakeholder') {
url = this.getStakeholderPageUrl(this.selected);
} else if (this.type === 'community') {
url = this.getCommunityPageUrl(this.selected);
}
this.localStorageService.setCommunityDirectLink(data.choice);
window.open(url, '_blank');
}
}
}