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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | 127x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 7x 1x 8x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 16x 2x 2x 2x 2x 125x | import { Component, OnInit, inject } from '@angular/core';
import { FormsModule, ReactiveFormsModule, FormGroup, FormBuilder, FormControl } from '@angular/forms';
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
import { CustomView } from '../../models/customView';
import { MessageService } from 'primeng/api';
import { TwinpadApiService } from '../../services/twinpad-api.service';
@Component({
selector: 'app-view-builder',
imports: [FormsModule, ReactiveFormsModule, RouterLink],
templateUrl: './view-builder.component.html',
styleUrl: './view-builder.component.scss'
})
export class ViewBuilderComponent implements OnInit {
private fb = inject(FormBuilder);
private route = inject(ActivatedRoute);
private twinpadApiService = inject(TwinpadApiService);
private messageService = inject(MessageService);
private router = inject(Router);
isNameErrorVisible: boolean = false;
isConfErrorVisible: boolean = false;
isDataErrorVisible: boolean = false;
// userId: string;
viewId: string;
buildedView: CustomView = new CustomView();
customViewForm: FormGroup;
configurationText: string;
componentText: string;
jsonConfiguration: any = {}; // eslint-disable-line @typescript-eslint/no-explicit-any
jsonComponent: any = {}; // eslint-disable-line @typescript-eslint/no-explicit-any
error: string = '';
ngOnInit(): void {
this.viewId = this.route.snapshot.params['view_id'] || null;
this.customViewForm = this.fb.group({
customViewName: new FormControl(),
customViewConfiguration: new FormControl(),
componentExample: new FormControl()
});
this.jsonConfiguration = [];
this.configurationText = JSON.stringify(this.jsonConfiguration, null, 4);
if(this.viewId !== null){
this.twinpadApiService.getCustomViewById(this.viewId).subscribe({
next: value => {
Eif(value !== null) {
this.buildedView = value;
this.customViewForm.get('customViewName')?.setValue(value.name);
this.customViewForm.get('customViewConfiguration')?.setValue(JSON.stringify(value.configuration, null, 4));
}
else {
this.router.navigate(['/customViews']);
}
},
error: error => {
console.log(error);
}
});
}
}
newComponent(newValue: Event){
const componentSelected = (newValue.target as HTMLInputElement).value;
switch(componentSelected) {
case 'PidComponent': {
this.jsonComponent = {
"type": "PidComponent",
"location": "../pid/pid.component",
"data": {
"deviceId": ""
}
};
break;
}
case 'PetriComponent': {
this.jsonComponent = {
"type": "PetriComponent",
"location": "../petri/petri.component",
"data": {
"deviceId": ""
}
};
break;
}
case 'VideosComponent': {
this.jsonComponent = {
"type": "VideosComponent",
"location": "../videos/videos.component",
"data": {}
};
break;
}
case 'DeviceCardLogicComponent': {
this.jsonComponent = {
"type": "DeviceCardLogicComponent",
"location": "../device-card-logic/device-card-logic.component",
"data": {
"deviceId": ""
}
};
break;
}
case 'SignalsComponent': {
this.jsonComponent = {
"type": "SignalsComponent",
"location": "../signals/signals.component",
"data": {}
};
break;
}
case 'SignalCardComponent': {
this.jsonComponent = {
"type": "SignalCardComponent",
"location": "../signal-card/signal-card.component",
"data": {
"signalId": ""
}
};
break;
}
case 'SignalsGraphsComponent': {
this.jsonComponent = {
"type": "SignalsGraphsComponent",
"location": "../signals-graphs/signals-graphs.component",
"data": {}
};
break;
}
case 'CommandCenterComponent': {
this.jsonComponent = {
"type": "CommandCenterComponent",
"location": "../command-center/command-center.component",
"data": {}
};
break;
}
case 'EventsComponent': {
this.jsonComponent = {
"type": "EventsComponent",
"location": "../events/events.component",
"data": {}
};
break;
}
case 'CountdownComponent': {
this.jsonComponent = {
"type": "CountdownComponent",
"location": "../countdown/countdown.component",
"data": {
"compactMode": true,
"targetSignalId": "",
"targetTimestamp": null
}
};
break;
}
default: {
break;
}
}
this.componentText = JSON.stringify(this.jsonComponent, null, 4);
}
addToConfiguration(){
this.error = '';
const brackPos = this.configurationText.lastIndexOf("]");
let jsonConfigurationFile = this.configurationText.slice(0, brackPos);
try {
if(jsonConfigurationFile[jsonConfigurationFile.length - 2] === "}"){
jsonConfigurationFile += "," + this.componentText + "]";
}
else {
jsonConfigurationFile += this.componentText + "]";
}
this.configurationText = JSON.stringify(JSON.parse(jsonConfigurationFile), null, 4);
}
catch(error){
if (error instanceof SyntaxError) { // check if it's a SyntaxError
this.error = error.message; // store the error message in a string variable
}
else {
console.error('Unknown error occurred:', error);
}
}
}
saveCustomView(){
Eif(this.buildView()){
if(this.viewId !== null){
this.twinpadApiService.updateCustomView(this.buildedView).subscribe({
next: _ => {
this.messageService.add({severity: 'success', summary: 'Edit custom view', detail: "Custom view successfully saved" });
},
error: error => {
this.messageService.add({severity: 'error', summary: 'Edit custom view', detail: error.error });
}
});
}
else{
this.twinpadApiService.createCustomView(this.buildedView).subscribe({
next: value => {
const responseId = value.id;
this.messageService.add({severity: 'success', summary: 'Add custom view', detail: "Custom view successfully created" });
this.router.navigate([`/custom-views/${responseId}`]);
},
error: error => {
this.messageService.add({severity: 'error', summary: 'Add custom view', detail: error.error });
}
});
}
}
}
deleteCustomView(customViewId: string, customViewName: string){
Eif(confirm(`Are you sure to delete ${customViewName} ?`)){
this.twinpadApiService.deleteCustomView(customViewId).subscribe({
next: value => {
Eif(value){
this.messageService.add({severity: 'success', summary: 'Delete custom view', detail: "Custom view successfully deleted"});
this.router.navigate(['/custom-views']);
}
else{
this.messageService.add({severity: 'error', summary: 'Delete custom view', detail: "An error occurred while deleting this view"});
}
},
error: error => {
this.messageService.add({severity: 'error', summary: 'Delete custom view', detail: error.error});
}
});
}
}
buildView(): boolean{
try{
const viewName = this.customViewForm.get('customViewName')?.value;
const viewConfiguration = this.customViewForm.get('customViewConfiguration')?.value;
// Check View Name
Iif(viewName === null || viewName.length === 0){
this.isNameErrorVisible = true;
}
else{
this.isNameErrorVisible = false;
}
// Check View Configurator
const parsedConf: [] = JSON.parse(viewConfiguration);
Iif(parsedConf.length === 0){
this.isConfErrorVisible = true;
}
else{
this.isConfErrorVisible = false;
}
// Check data values
this.isDataErrorVisible = parsedConf.some(component =>
!component['data'] || // If "data" not exist
(component['data']['deviceId'] === "") // Si "deviceId" est une chaƮne vide
);
// If one check isn't correct return false
Iif(this.isNameErrorVisible || this.isConfErrorVisible || this.isDataErrorVisible){
return false;
}
// Create custom view object or retrieve it if exist
this.buildedView.name = viewName;
this.buildedView.configuration = parsedConf;
// if(this.buildedView.user_id === null || this.buildedView.user_id === undefined){
// this.buildedView.user_id = this.userId;
// }
return true;
}
catch(error){
return false;
}
}
}
|