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

Skip to content
Snippets Groups Projects
exercise-card.component.ts 2.31 KiB
Newer Older
Michael Breu's avatar
Michael Breu committed
import { Component, Input, Output, EventEmitter, AfterViewChecked } from '@angular/core';
import { Exercise } from 'app/shared/model/exercise.model';
import { SearchService } from 'app/search/service/search-service';
Michael Breu's avatar
Michael Breu committed
import { WatchlistManager } from 'app/shared/watchlist/watchlist-manager';
import { ExerciseService } from '../service/exercise.service';
import { AccountService } from 'app/core/auth/account.service';
  selector: 'jhi-exercise-card',
  templateUrl: './exercise-card.component.html',
  styleUrls: ['./exercise-card.component.scss'],
Michael Breu's avatar
Michael Breu committed
export class ExerciseCardComponent implements AfterViewChecked {
  @Input() exercise: Exercise | undefined;
Michael Breu's avatar
Michael Breu committed
  @Input() autoOpenDetails = false; // if true opens details dialog automatically
  @Output() exerciseSelectionEvent = new EventEmitter<Exercise>();
  bookmarked = false;
  constructor(
    protected searchService: SearchService,
    private watchlistManager: WatchlistManager,
    private exerciseService: ExerciseService,
    private accountService: AccountService
Michael Breu's avatar
Michael Breu committed
  ngAfterViewChecked(): void {
    if (this.autoOpenDetails) {
      const button = document.querySelector('.exerciseDetailButton');
      if (button instanceof HTMLElement) {
        button.click();
        this.autoOpenDetails = false;
      }
    }
Michael Breu's avatar
Michael Breu committed
  }

  isLoggedIn(): boolean {
    return this.accountService.isAuthenticated();
  }

    this.bookmarked = this.watchlistManager.isExerciseOnCurrentWatchlist(this.exercise!);
    this.exercise = this.exerciseService.populateExerciseWithData(this.exercise!);
    this.exerciseSelectionEvent.emit(this.exercise);
  }
  isOnCurrentWatchlist(e: Exercise): boolean {
    return this.watchlistManager.isExerciseOnCurrentWatchlist(e);
  }

  handleForCurrentWatchlist(): void {
    if (this.watchlistManager.isExerciseOnCurrentWatchlist(this.exercise!)) {
      this.bookmarked = false;
      this.watchlistManager.handleCheckForCurrentWatchlist(this.exercise!, false);
    } else {
      this.bookmarked = true;
      this.watchlistManager.handleCheckForCurrentWatchlist(this.exercise!, true);
    }
  /**
   * correct missing image urls
   */
  correctImageURL(event: Event): void {
    const img = event.srcElement as HTMLImageElement;
    if (img) {
      img.src = '/content/images/Logo_codeAbility_4c_300dpi_RGB3.gif';