All files / src/app/components/theme-builder theme-builder.component.ts

91.89% Statements 68/74
71.42% Branches 10/14
81.25% Functions 13/16
91.78% Lines 67/73

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                                158x 48x 48x 48x   48x   48x 48x 48x       48x   48x 48x     48x   48x                   48x   48x           55x 50x   5x 5x 5x 5x 5x 5x 5x 5x         53372x 53372x       2x       2x   2x 2x 2x 2x 2x 2x 2x   2x   2x 2x 2x 2x 2x                 1x 1x 1x 1x 1x 1x 1x   1x   1x 1x 1x                 1x   1x 1x 1x 1x                         50x 50x 50x 50x 50x 50x 50x       1x 110x    
import { Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, inject } from '@angular/core';
import { FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import { SelectModule } from 'primeng/select';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { ToggleSwitchModule } from 'primeng/toggleswitch';
import { ColorPickerModule } from 'primeng/colorpicker';
import { MessageModule } from 'primeng/message';
import { GraphTheme, LineStyle } from '../../models/colorSetting';
import { MessageService } from 'primeng/api';
 
@Component({
  selector: 'app-theme-builder',
  imports: [SelectModule, FormsModule, ReactiveFormsModule, SelectModule, ToggleSwitchModule, ColorPickerModule, MessageModule],
  templateUrl: './theme-builder.component.html',
  styleUrl: './theme-builder.component.scss'
})
export class ThemeBuilderComponent implements OnInit, OnChanges {
  private formBuilder = inject(FormBuilder);
  private twinpadApiService = inject(TwinpadApiService);
  private messageService = inject(MessageService);
 
  @Input() editMode: boolean = false;
  @Input() inputGraphTheme: GraphTheme;
  @Output() editModeChange = new EventEmitter<boolean>();
  @Output() cancelEditEvent = new EventEmitter();
  @Output() themesChangedEvent = new EventEmitter();
  graphThemeForm: FormGroup;
  signalIds: string[];
  lineStyles: string[];
  formSubmitted: boolean = false;
 
  private readonly defaultValueColor = "#0000ff";
  private readonly defaultForcedValueColor = "#ff0000";
 
  ngOnInit() {
    this.lineStyles = Object.values(LineStyle);
 
    this.graphThemeForm = this.formBuilder.group({
      themeName: ["", [Validators.required, Validators.minLength(4)]],
      targetSignalId: ["", [Validators.required]],
      valueColor: [this.defaultValueColor],
      valueLineStyle: ["", [Validators.required]],
      forcedValueColor: [this.defaultForcedValueColor],
      forcedValueLineStyle: ["", [Validators.required]],
      isShared: [false]
    });
 
    this.twinpadApiService.getSignalsIds().subscribe({
      next: signalIds => {
        this.signalIds = signalIds;
      }
    });
  }
 
  ngOnChanges(_: SimpleChanges): void {
    if (!this.editMode) {
      this.resetTheme();
    }
    else Eif (this.inputGraphTheme !== undefined) {
      this.graphThemeForm.get("themeName")?.setValue(this.inputGraphTheme.name);
      this.graphThemeForm.get("targetSignalId")?.setValue(this.inputGraphTheme.signal_id);
      this.graphThemeForm.get("valueColor")?.setValue(this.inputGraphTheme.value_color);
      this.graphThemeForm.get("valueLineStyle")?.setValue(this.inputGraphTheme.value_line_style);
      this.graphThemeForm.get("forcedValueColor")?.setValue(this.inputGraphTheme.forced_value_color);
      this.graphThemeForm.get("forcedValueLineStyle")?.setValue(this.inputGraphTheme.forced_value_line_style);
      this.graphThemeForm.get("isShared")?.setValue(!this.inputGraphTheme.private);
    }
  }
 
  isInvalid(controlName: string): boolean {
    const control = this.graphThemeForm.get(controlName);
    return (control?.invalid && (control.touched || this.formSubmitted)) ?? true;
  }
 
  createTheme() {
    Iif (this.graphThemeForm.invalid) {
      this.messageService.add({ severity: 'error', summary: 'Nice try', detail: "https://www.youtube.com/watch?v=dQw4w9WgXcQ" });
      return;
    }
    const newTheme = new GraphTheme();
 
    newTheme.name = this.graphThemeForm.get("themeName")?.value;
    newTheme.signal_id = this.graphThemeForm.get("targetSignalId")?.value;
    newTheme.value_color = this.graphThemeForm.get("valueColor")?.value;
    newTheme.value_line_style = this.graphThemeForm.get("valueLineStyle")?.value;
    newTheme.forced_value_color = this.graphThemeForm.get("forcedValueColor")?.value;
    newTheme.forced_value_line_style = this.graphThemeForm.get("forcedValueLineStyle")?.value;
    newTheme.private = this.graphThemeForm.get("isShared")?.value === false;
 
    this.twinpadApiService.createGraphTheme(newTheme).subscribe({
      next: createdTheme => {
        this.themesChangedEvent.emit();
        this.editMode = true;
        this.editModeChange.emit(this.editMode);
        this.inputGraphTheme = createdTheme;
        this.messageService.add({ severity: 'success', summary: 'Theme creation successful', detail: `Theme ${newTheme.name} has been created` });
      },
      error: err => {
        this.messageService.add({ severity: 'error', summary: 'Theme creation failed', detail: err.error.detail });
      }
    });
  }
 
  editTheme() {
    this.inputGraphTheme.name = this.graphThemeForm.get("themeName")?.value;
    this.inputGraphTheme.signal_id = this.graphThemeForm.get("targetSignalId")?.value;
    this.inputGraphTheme.value_color = this.graphThemeForm.get("valueColor")?.value;
    this.inputGraphTheme.value_line_style = this.graphThemeForm.get("valueLineStyle")?.value;
    this.inputGraphTheme.forced_value_color = this.graphThemeForm.get("forcedValueColor")?.value;
    this.inputGraphTheme.forced_value_line_style = this.graphThemeForm.get("forcedValueLineStyle")?.value;
    this.inputGraphTheme.private = this.graphThemeForm.get("isShared")?.value === false;
 
    this.twinpadApiService.updateGraphTheme(this.inputGraphTheme.id, this.inputGraphTheme).subscribe({
      next: editedTheme => {
        this.themesChangedEvent.emit();
        this.inputGraphTheme = editedTheme;
        this.messageService.add({ severity: 'success', summary: 'Theme modification successful', detail: `Theme ${this.inputGraphTheme.name} has been modified` });
      },
      error: err => {
        this.messageService.add({ severity: 'error', summary: 'Theme modification failed', detail: err.error.detail });
      }
    });
  }
 
  deleteTheme() {
    this.twinpadApiService.deleteGraphTheme(this.inputGraphTheme.id).subscribe({
      next: isDeleted => {
        Eif (isDeleted) {
          this.themesChangedEvent.emit();
          this.cancelEditEvent.emit();
          this.messageService.add({ severity: 'success', summary: 'Theme deletion successful', detail: `Theme ${this.inputGraphTheme.name} has been deleted` });
        }
        else {
          this.messageService.add({ severity: 'error', summary: 'Theme deletion failed', detail: "Check if this theme hasn't already been deleted" });
        }
      },
      error: err => {
        this.messageService.add({ severity: 'error', summary: 'Theme deletion failed', detail: err.error.detail });
      }
    });
  }
 
  resetTheme() {
    this.graphThemeForm?.get("themeName")?.setValue("");
    this.graphThemeForm?.get("targetSignalId")?.setValue("");
    this.graphThemeForm?.get("valueColor")?.setValue(this.defaultValueColor);
    this.graphThemeForm?.get("valueLineStyle")?.setValue("");
    this.graphThemeForm?.get("forcedValueColor")?.setValue(this.defaultForcedValueColor);
    this.graphThemeForm?.get("forcedValueLineStyle")?.setValue("");
    this.graphThemeForm?.get("isShared")?.setValue(false);
  }
 
  cancelEdit() {
    this.cancelEditEvent.emit();
  }
}