This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
markDownViewer.component.ts 2.72 KiB
Newer Older
Michael Breu's avatar
Michael Breu committed
import { Component, OnInit, Input } from '@angular/core';
import { Exercise, IExerciseType } from 'app/shared/model/exercise.model';
import { Person } from 'app/shared/model/person.model';
import { SearchService } from 'app/search/service/search-service.ts';
import { HttpResponse } from '@angular/common/http';
import { JhiAlertService } from 'ng-jhipster';

@Component({
  selector: 'jhi-markdown-viewer',
  templateUrl: './markDownViewer.component.html',
  styleUrls: ['./markdownViewer.component.scss'],
})
export class MarkDownViewerComponent implements OnInit {
   @Input()  exercise: Exercise | undefined;

  constructor(
    private searchService: SearchService,
    private jhiAlertService: JhiAlertService
  ) {}

  ngOnInit(): void {
  }

  public get exerciseType(): typeof IExerciseType {
    return IExerciseType;
  }

  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 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);
  }

  /**
   * correct missing image urls
   */
  correctImageURL(event: Event): void {
    if (event.srcElement) {
      event.srcElement['src'] = '/content/img/gitLab.png';
    }
  }
}