All files / src/app/group-monitor/test-session test-session.component.ts

40.9% Statements 18/44
23.52% Branches 8/34
35.71% Functions 5/14
38.09% Lines 16/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126                                                  1x 4x 4x 4x 4x 4x   4x 4x 4x   4x   4x   4x     30x     15x                           16x                     16x                                                                                                            
import {
  Component, EventEmitter, Input, Output
} from '@angular/core';
import { MatCheckboxChange } from '@angular/material/checkbox';
import {
  Testlet, Unit, TestViewDisplayOptions,
  isUnit, Selected, TestSession, TestSessionSuperState
} from '../group-monitor.interfaces';
import { TestSessionUtil } from './test-session.util';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { superStates } from './super-states';
 
interface IconData {
  icon: string,
  tooltip: string,
  class?: string,
  description?: string
}
 
@Component({
  selector: 'tc-test-session',
  templateUrl: './test-session.component.html',
  styleUrls: ['./test-session.component.css']
})
export class TestSessionComponent {
  @Input() testSession: TestSession = {} as TestSession;
  @Input() displayOptions: TestViewDisplayOptions = {} as TestViewDisplayOptions;
  @Input() marked: Selected | null = null;
  @Input() selected: Selected | null = null;
  @Input() checked: boolean = false;
 
  @Output() markedElement$ = new EventEmitter<Selected>();
  @Output() selectedElement$ = new EventEmitter<Selected>();
  @Output() checked$ = new EventEmitter<boolean>();
 
  superStateIcons: { [key in TestSessionSuperState]: IconData } = superStates;
 
  stateString = TestSessionUtil.stateString;
 
  hasState = TestSessionUtil.hasState;
 
  // eslint-disable-next-line class-methods-use-this
  getTestletType = (testletOrUnit: Unit | Testlet): 'testlet' | 'unit' => (isUnit(testletOrUnit) ? 'unit' : 'testlet');
 
  // eslint-disable-next-line class-methods-use-this
  trackUnits = (index: number, testlet: Testlet | Unit): string => testlet.id || index.toString();
 
  mark(testletOrNull: Testlet | null = null): void {
    Iif ((testletOrNull != null) && !testletOrNull.blockId) {
      return;
    }
    Iif (['pending', 'locked'].includes(this.testSession.state)) {
      return;
    }
    this.marked = this.asSelectionObject(testletOrNull);
    this.markedElement$.emit(this.marked);
  }
 
  isSelected(testletOrNull: Testlet | null = null): boolean {
    return !!testletOrNull &&
      (this.selected?.element?.blockId === testletOrNull?.blockId) &&
      (this.selected?.originSession.booklet.species === this.testSession.booklet.species);
  }
 
  isSelectedHere(testletOrNull: Testlet | null = null): boolean {
    return this.isSelected(testletOrNull) &&
      (this.selected?.originSession.data.testId === this.testSession.data.testId);
  }
 
  isMarked(testletOrNull: Testlet | null = null): boolean {
    return !!testletOrNull &&
      (!['pending', 'locked'].includes(this.testSession.state)) &&
      (this.marked?.element?.blockId === testletOrNull.blockId) &&
      (this.marked?.originSession.booklet.species === this.testSession.booklet.species);
  }
 
  select($event: Event, testletOrNull: Testlet | null): void {
    Iif ((testletOrNull != null) && !testletOrNull.blockId) {
      return;
    }
    $event.stopPropagation();
    this.applySelection(testletOrNull);
  }
 
  deselect($event: MouseEvent | null): void {
    Iif ($event && ($event.currentTarget === $event.target)) {
      this.applySelection();
    }
  }
 
  deselectForce($event: Event): boolean {
    this.applySelection();
    $event.stopImmediatePropagation();
    $event.stopPropagation();
    $event.preventDefault();
    return false;
  }
 
  invertSelection(): boolean {
    this.applySelection(this.selected?.element, true);
    return false;
  }
 
  check($event: MatCheckboxChange): void {
    this.checked$.emit($event.checked);
  }
 
  private applySelection(testletOrNull: Testlet | null = null, inversion = false): void {
    Iif (['pending', 'locked'].includes(this.testSession.state)) {
      return;
    }
    this.selected = this.asSelectionObject(testletOrNull, inversion);
    this.selectedElement$.emit(this.selected);
  }
 
  private asSelectionObject(testletOrNull: Testlet | null = null, inversion = false): Selected {
    return {
      element: testletOrNull,
      originSession: this.testSession,
      spreading: this.isSelectedHere(testletOrNull) ? !(this.selected?.spreading) : !testletOrNull,
      inversion
    };
  }
}