All files / tools/ReFlex.TrackingServer/ClientApp/src/app/processing/interactions-visualization interactions-visualization.component.ts

95.55% Statements 43/45
86.66% Branches 26/30
100% Functions 12/12
95.23% Lines 40/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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 1592x                                           2x 2x 2x 2x                 16x         9x           9x   9x 9x 9x 9x 9x     9x             9x     28x         1x       8x         6x     1x 1x           9x       28x 5x                 23x                   3x   3x         3x   3x       3x 7x         7x         7x         5x   5x 1x     4x 1x     3x 3x            
import { CommonModule } from '@angular/common';
import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  ElementRef,
  Input,
  OnDestroy,
  OnInit,
  ViewChild,
  inject
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import {
  CompleteInteractionData,
  ElementPosition,
  ExtremumDescription,
  ExtremumType,
  FrameSizeDefinition,
  Interaction
} from '@reflex/shared-types';
import { Subscription } from 'rxjs';
import { LogService } from 'src/app/log/log.service';
import { CalibrationService } from 'src/shared/services/calibration.service';
import { markViewForCheck } from 'src/shared/util/mark-view-for-check';
import { HistoryVisualizationComponent } from '../history-visualization/history-visualization.component';
 
@Component({
  selector: 'app-interactions-visualization',
  templateUrl: './interactions-visualization.component.html',
  styleUrls: ['./interactions-visualization.component.scss'],
  changeDetection: ChangeDetectionStrategy.Eager,
  imports: [CommonModule, FormsModule, HistoryVisualizationComponent]
})
export class InteractionsVisualizationComponent implements OnInit, OnDestroy {
 
  @ViewChild('interactionVis')
  public container?: ElementRef<HTMLDivElement>;
 
  public interactions: CompleteInteractionData = {
    raw: [],
    absolute: [],
    normalized: []
  };
 
  public calibratedInteractions: Array<Interaction> = [];
 
  public fullScreen = false;
  public maxConfidence = 30;
  private readonly changeDetector = inject(ChangeDetectorRef);
  private readonly calibrationService = inject(CalibrationService);
  private readonly logService = inject(LogService);
 
  private frameSizeSubscription?: Subscription;
  private frameSize: FrameSizeDefinition = {
    top: 0,
    left: 0,
    width: 500,
    height: 400
  };
 
  private pEventId = 0;
 
  public get eventId(): number {
    return this.pEventId;
  }
 
  @Input()
  public set eventId(value: number) {
    this.pEventId = value;
  }
 
  public ngOnInit(): void {
    this.frameSizeSubscription = this.calibrationService
      .getFrameSize()
      .pipe(markViewForCheck(this.changeDetector))
      .subscribe(
        (result) => {
          this.frameSize = result;
        },
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
  }
 
  public ngOnDestroy(): void {
    this.frameSizeSubscription?.unsubscribe();
  }
 
  public getInteractionsViewStyle(): ElementPosition {
    if (this.fullScreen) {
      return {
        position: 'absolute',
        top: `${this.frameSize.top}px`,
        left: `${this.frameSize.left}px`,
        width: `${this.frameSize.width}px`,
        height: `${this.frameSize.height}px`
      };
    }
 
    return {
      position: 'relative',
      top: `0`,
      left: `0`,
      width: `100%`,
      height: `40vh`
    };
  }
 
  public updateCalibratedInteractions(result: CompleteInteractionData): void {
    this.interactions = result;
 
    this.calibratedInteractions = this.fullScreen
      ? result.absolute
      : result.normalized;
 
    // Also used imperatively by ProcessingComponent through ViewChild.
    this.changeDetector.markForCheck();
 
    Iif (this.container === undefined) {
      return;
    }
 
    this.calibratedInteractions.forEach((int) => {
      int.position.x = this.fullScreen
        ? int.position.x - this.frameSize.left
        : int.position.x
          * ((this.container?.nativeElement as HTMLElement | undefined)
            ?.clientWidth ?? 0);
      int.position.y = this.fullScreen
        ? int.position.y - this.frameSize.top
        : int.position.y
          * ((this.container?.nativeElement as HTMLElement | undefined)
            ?.clientHeight ?? 0);
      int.position.z = Math.abs(int.position.z) * 2;
    });
  }
 
  public getClass(extremum: ExtremumDescription): string {
    const extremumDef = extremum.type;
 
    if (extremumDef === ExtremumType.Undefined) {
      return 'interaction-undefined';
    }
 
    if (extremumDef === ExtremumType.Maximum) {
      return 'interaction-maximum';
    }
 
    Eif (extremumDef === ExtremumType.Minimum) {
      return 'interaction-minimum';
    }
 
    return 'interaction-invalid';
  }
}