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

108 lines
3.7 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} from "../../monitor/entities/stakeholder";
@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;
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;
constructor(private router: Router,
private localStorageService: LocalStorageService) {
}
ngOnInit() {
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.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');
}
}
}