All files / src/app/components/custom-view custom-view.component.ts

88.33% Statements 53/60
69.56% Branches 16/23
87.5% Functions 7/8
87.71% Lines 50/57

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                      112x 1x 1x 1x   1x           1x 1x 1x   1x 1x   1x 1x 1x 1x                         1x 1x 1x   1x 8x 8x 8x 8x 4x   8x 1x 1x 1x   8x       8x   8x   1x 1x     1x 1x     1x 1x     1x 1x     1x 1x     1x 1x     1x 1x             1x 1x                   8x 8x 8x 6x        
import { Component, OnInit, ViewChild, ViewContainerRef, inject } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { CustomView } from '../../models/customView';
import { TwinpadApiService } from '../../services/twinpad-api.service';
 
@Component({
  selector: 'app-custom-view',
  imports: [],
  templateUrl: './custom-view.component.html',
  styleUrl: './custom-view.component.scss'
})
export class CustomViewComponent implements OnInit {
  private route = inject(ActivatedRoute);
  private twinpadApiService = inject(TwinpadApiService);
  private router = inject(Router);
 
  @ViewChild('dynamicContainer', { read: ViewContainerRef }) container: ViewContainerRef;
  customViewId: string;
  customView: CustomView;
 
 
  ngOnInit(): void {
    this.customViewId = this.route.snapshot.params['view_id'] || null;
    Eif (this.customViewId !== null){
      this.twinpadApiService.getCustomViewById(this.customViewId).subscribe({
        next: value => {
          Eif(value !== null){
            this.customView = value;
            // When reloading custom view, clear extra parameters to avoid duplicating input signal ids
            const extraParamsIndex = this.router.url.indexOf("?");
            const url = this.router.url.substring(0, extraParamsIndex < 0 ? undefined : extraParamsIndex);
            this.router.navigate([url]);
            this.loadComponents(this.customView);
          }
          else {
            this.router.navigate(['/custom-views']);
          }
        },
        error: error => {
          console.log(error);
        }
      });
    }
  }
 
  async loadComponents(customView: CustomView) {
    this.container.clear();
    let numberSignalGraphs = 0;
 
    for(const component in customView.configuration){
      const componentName = customView.configuration[component]['type'];
      const componentData = customView.configuration[component]['data'];
      const newData: {[key: string]: any} = {}; // eslint-disable-line  @typescript-eslint/no-explicit-any
      for (const [key, value] of Object.entries(componentData)){
        newData[key] = value;
      }
      if (componentName === "SignalsGraphsComponent") {
        numberSignalGraphs++;
        newData["graphId"] = `_${numberSignalGraphs}`;
        newData["displayCommandCenter"] = false;
      }
      this.setContainer(componentName, newData);
    }
  }
 
  async setContainer(componentName: string, componentData: {[key: string]: any}){ // eslint-disable-line  @typescript-eslint/no-explicit-any
    let module: any; // eslint-disable-line  @typescript-eslint/no-explicit-any
    switch(componentName) {
      case 'PidComponent': {
        module = await import('../pid/pid.component');
        break;
      }
      case 'PetriComponent': {
        module = await import('../petri/petri.component');
        break;
      }
      case 'VideosComponent': {
        module = await import('../videos/videos.component');
        break;
      }
      case 'DeviceCardLogicComponent': {
        module = await import('../device-card-logic/device-card-logic.component');
        break;
      }
      case 'SignalsComponent': {
        module = await import('../signals/signals.component');
        break;
      }
      case 'SignalCardComponent': {
        module = await import('../signal-card/signal-card.component');
        break;
      }
      case 'SignalsGraphsComponent': {
        module = await import('../signals-graphs/signals-graphs.component');
        break;
      }
      case 'CommandCenterComponent': {
        module = await import('../command-center/command-center.component');
        break;
      }
      case 'EventsComponent': {
        module = await import('../events/events.component');
        break;
      }
      case 'CountdownComponent': {
        module = await import('../countdown/countdown.component');
        break;
      }
      default: {
        break;
     }
    }
    const dynamicComponent = module[componentName];
    const component = this.container.createComponent(dynamicComponent);
    for (const [key, value] of Object.entries(componentData)){
      component.setInput(key, value);
    }
  }
}