import { Component, OnInit, Input, OnDestroy } from '@angular/core'; import { Exercise, IExerciseType } from 'app/shared/model/exercise.model'; import { Person } from 'app/shared/model/person.model'; import { Subscription } from 'rxjs'; import { PluginActionInfo } from 'app/shared/model/search/search-result-dto.model'; import { PluginService } from 'app/shared/service/plugin-service'; import { ShoppingBasketInfo, ShoppingBasketRedirectInfoDTO } from 'app/shared/model/basket/shopping-basket-info.model'; import { AccountService } from 'app/core/auth/account.service'; import { Account } from 'app/core/user/account.model'; import { SearchService } from 'app/search/service/search-service.ts'; import { HttpResponse } from '@angular/common/http'; import { JhiAlertService } from 'ng-jhipster'; @Component({ selector: 'jhi-exercise-details', templateUrl: './exercise-details.component.html', styleUrls: ['./exercise-details.component.scss'], }) export class ExerciseDetailsComponent implements OnInit, OnDestroy { @Input() exercise: Exercise | undefined; account: Account | null = null; authSubscription?: Subscription; constructor( private accountService: AccountService, protected pluginService: PluginService, private searchService: SearchService, private jhiAlertService: JhiAlertService ) {} ngOnInit(): void { this.authSubscription = this.accountService.getAuthenticationState().subscribe(account => (this.account = account)); } public get exerciseType(): typeof IExerciseType { return IExerciseType; } public isAuthenticated(): boolean { return this.accountService.isAuthenticated(); } public getPersonName(person: Person): string { return person.name; } public getPersonDetails(person: Person): string { return person.name + ', ' + person.affiliation; } public getPersonDetailsWithEmail(person: Person): string { return "<a class='text-dark' href='mailto:" + person.email + "'>" + person.name + ', ' + person.affiliation + '</a>'; } public arrayToString(array: string[]): string { let result = ''; let i = 1; array.forEach(element => { if (array.length > 1 && array.length !== i) { result += element + ', '; } else { result += element; } if (i % 5 === 0) { result += '<br>'; } i++; }); return result; } public startAction(action: PluginActionInfo, exercise: Exercise): void { const basketInfo: ShoppingBasketInfo = { plugin: action.plugin, action: action.action, itemInfos: [exercise.originalResult], }; this.pluginService.getRedirectLink(basketInfo).subscribe( (redirectInfo: ShoppingBasketRedirectInfoDTO) => { // alert('redirecting to ' + redirectInfo.redirectURL); // location.href = redirectInfo.redirectURL; window.open(redirectInfo.redirectURL, action.action); }, () => alert('Search failed') ); } public download(): void { this.exportExercise(Number(this.exercise!.originalResult.project.project_id)); } exportExercise(projectId: number) { return this.searchService.exportExercise(projectId).subscribe( (response: HttpResponse<Blob>) => { this.jhiAlertService.success('artemisApp.programmingExercise.export.successMessage'); if (response.body) { const zipFile = new Blob([response.body], { type: 'application/zip' }); const url = window.URL.createObjectURL(zipFile); const link = document.createElement('a'); link.setAttribute('href', url); link.setAttribute('download', response.headers.get('filename')!); document.body.appendChild(link); // Required for FF link.click(); window.URL.revokeObjectURL(url); } }, () => alert('Unable to export exercise. Please log in to export.') ); } public getViews(): number { return 5; } openLink(link: string): void { window.open(link); } ngOnDestroy(): void { if (this.authSubscription) { this.authSubscription.unsubscribe(); } } /** * correct missing image urls */ correctImageURL(event: Event): void { if (event.srcElement) { event.srcElement['src'] = '/content/img/gitLab.png'; } } }