refactoring

This commit is contained in:
mpetrov 2024-08-01 11:37:58 +03:00
parent 27bd1b2fa0
commit eb39f36007
5 changed files with 38 additions and 60 deletions

View File

@ -23,16 +23,6 @@
<tb-modbus-master-table formControlName="master"></tb-modbus-master-table> <tb-modbus-master-table formControlName="master"></tb-modbus-master-table>
</mat-tab> </mat-tab>
<mat-tab label="{{ 'gateway.server-config' | translate }}"> <mat-tab label="{{ 'gateway.server-config' | translate }}">
<div class="tb-form-panel no-border no-padding padding-top">
<div class="tb-form-hint tb-primary-fill tb-flex center">{{ 'gateway.hints.modbus-server' | translate }}</div>
<div class="tb-form-row" fxLayoutAlign="space-between center">
<mat-slide-toggle class="mat-slide" [formControl]="enableSlaveControl">
<mat-label>
{{ 'gateway.enable' | translate }}
</mat-label>
</mat-slide-toggle>
</div>
</div>
<tb-modbus-slave-config formControlName="slave"></tb-modbus-slave-config> <tb-modbus-slave-config formControlName="slave"></tb-modbus-slave-config>
</mat-tab> </mat-tab>
</mat-tab-group> </mat-tab-group>

View File

@ -18,10 +18,10 @@ import { ChangeDetectionStrategy, Component, forwardRef, Input, OnDestroy, Templ
import { import {
ControlValueAccessor, ControlValueAccessor,
FormBuilder, FormBuilder,
FormControl,
FormGroup, FormGroup,
NG_VALIDATORS, NG_VALIDATORS,
NG_VALUE_ACCESSOR, NG_VALUE_ACCESSOR,
UntypedFormControl,
ValidationErrors, ValidationErrors,
Validator, Validator,
} from '@angular/forms'; } from '@angular/forms';
@ -68,7 +68,6 @@ export class ModbusBasicConfigComponent implements ControlValueAccessor, Validat
@Input() generalTabContent: TemplateRef<any>; @Input() generalTabContent: TemplateRef<any>;
basicFormGroup: FormGroup; basicFormGroup: FormGroup;
enableSlaveControl: FormControl<boolean>;
onChange: (value: ModbusBasicConfig) => void; onChange: (value: ModbusBasicConfig) => void;
onTouched: () => void; onTouched: () => void;
@ -80,7 +79,6 @@ export class ModbusBasicConfigComponent implements ControlValueAccessor, Validat
master: [], master: [],
slave: [], slave: [],
}); });
this.enableSlaveControl = new FormControl(false);
this.basicFormGroup.valueChanges this.basicFormGroup.valueChanges
.pipe(takeUntil(this.destroy$)) .pipe(takeUntil(this.destroy$))
@ -88,14 +86,6 @@ export class ModbusBasicConfigComponent implements ControlValueAccessor, Validat
this.onChange(value); this.onChange(value);
this.onTouched(); this.onTouched();
}); });
this.enableSlaveControl.valueChanges
.pipe(takeUntil(this.destroy$))
.subscribe(enable => {
this.updateSlaveEnabling(enable);
this.basicFormGroup.get('slave').updateValueAndValidity({emitEvent: !!this.onChange});
this.basicFormGroup.get('master').updateValueAndValidity({emitEvent: !!this.onChange});
});
} }
ngOnDestroy(): void { ngOnDestroy(): void {
@ -118,20 +108,16 @@ export class ModbusBasicConfigComponent implements ControlValueAccessor, Validat
}; };
this.basicFormGroup.setValue(editedBase, {emitEvent: false}); this.basicFormGroup.setValue(editedBase, {emitEvent: false});
this.enableSlaveControl.setValue(!!basicConfig.slave && !isEqual(basicConfig.slave, {}));
} }
validate(): ValidationErrors | null { validate(basicFormControl: UntypedFormControl): ValidationErrors | null {
return this.basicFormGroup.valid ? null : { const { master, slave } = basicFormControl.value;
const isEmpty = !master?.slaves?.length && (isEqual(slave, {}) || !slave);
if (!this.basicFormGroup.valid || isEmpty) {
return {
basicFormGroup: {valid: false} basicFormGroup: {valid: false}
}; };
} }
return null;
private updateSlaveEnabling(isEnabled: boolean): void {
if (isEnabled) {
this.basicFormGroup.get('slave').enable({emitEvent: false});
} else {
this.basicFormGroup.get('slave').disable({emitEvent: false});
}
} }
} }

View File

@ -34,12 +34,8 @@ import {
ControlValueAccessor, ControlValueAccessor,
FormArray, FormArray,
FormBuilder, FormBuilder,
NG_VALIDATORS,
NG_VALUE_ACCESSOR, NG_VALUE_ACCESSOR,
UntypedFormControl,
UntypedFormGroup, UntypedFormGroup,
ValidationErrors,
Validator,
} from '@angular/forms'; } from '@angular/forms';
import { import {
ModbusMasterConfig, ModbusMasterConfig,
@ -63,16 +59,11 @@ import { TbTableDatasource } from '@shared/components/table/table-datasource.abs
useExisting: forwardRef(() => ModbusMasterTableComponent), useExisting: forwardRef(() => ModbusMasterTableComponent),
multi: true multi: true
}, },
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => ModbusMasterTableComponent),
multi: true
}
], ],
standalone: true, standalone: true,
imports: [CommonModule, SharedModule] imports: [CommonModule, SharedModule]
}) })
export class ModbusMasterTableComponent implements ControlValueAccessor, Validator, AfterViewInit, OnInit, OnDestroy { export class ModbusMasterTableComponent implements ControlValueAccessor, AfterViewInit, OnInit, OnDestroy {
@ViewChild('searchInput') searchInputField: ElementRef; @ViewChild('searchInput') searchInputField: ElementRef;
@ -139,12 +130,6 @@ export class ModbusMasterTableComponent implements ControlValueAccessor, Validat
this.pushDataAsFormArrays(master.slaves); this.pushDataAsFormArrays(master.slaves);
} }
validate(masterControl: UntypedFormControl): ValidationErrors | null {
return masterControl.parent.get('slave').enabled || this.slaves.controls.length ? null : {
slavesFormGroup: {valid: false}
};
}
enterFilterMode(): void { enterFilterMode(): void {
this.textSearchMode = true; this.textSearchMode = true;
this.cdr.detectChanges(); this.cdr.detectChanges();

View File

@ -16,6 +16,16 @@
--> -->
<div [formGroup]="slaveConfigFormGroup" class="slave-container"> <div [formGroup]="slaveConfigFormGroup" class="slave-container">
<div class="tb-form-panel no-border no-padding padding-top">
<div class="tb-form-hint tb-primary-fill tb-flex center">{{ 'gateway.hints.modbus-server' | translate }}</div>
<div class="tb-form-row" fxLayoutAlign="space-between center">
<mat-slide-toggle class="mat-slide" [formControl]="enableSlaveControl">
<mat-label>
{{ 'gateway.enable' | translate }}
</mat-label>
</mat-slide-toggle>
</div>
</div>
<div class="slave-content tb-form-panel no-border no-padding padding-top" > <div class="slave-content tb-form-panel no-border no-padding padding-top" >
<div class="tb-flex row space-between align-center no-gap fill-width"> <div class="tb-flex row space-between align-center no-gap fill-width">
<div class="fixed-title-width" translate>gateway.server-slave-config</div> <div class="fixed-title-width" translate>gateway.server-slave-config</div>

View File

@ -77,6 +77,7 @@ import { isEqual } from '@core/utils';
export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validator, OnDestroy { export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validator, OnDestroy {
slaveConfigFormGroup: UntypedFormGroup; slaveConfigFormGroup: UntypedFormGroup;
enableSlaveControl: FormControl<boolean>;
showSecurityControl: FormControl<boolean>; showSecurityControl: FormControl<boolean>;
ModbusProtocolLabelsMap = ModbusProtocolLabelsMap; ModbusProtocolLabelsMap = ModbusProtocolLabelsMap;
ModbusMethodLabelsMap = ModbusMethodLabelsMap; ModbusMethodLabelsMap = ModbusMethodLabelsMap;
@ -89,7 +90,6 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat
readonly ModbusProtocolType = ModbusProtocolType; readonly ModbusProtocolType = ModbusProtocolType;
readonly modbusBaudrates = ModbusBaudrates; readonly modbusBaudrates = ModbusBaudrates;
private isSlaveEnabled = false;
private readonly serialSpecificControlKeys = ['serialPort', 'baudrate']; private readonly serialSpecificControlKeys = ['serialPort', 'baudrate'];
private readonly tcpUdpSpecificControlKeys = ['port', 'security', 'host']; private readonly tcpUdpSpecificControlKeys = ['port', 'security', 'host'];
@ -100,6 +100,7 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat
constructor(private fb: FormBuilder) { constructor(private fb: FormBuilder) {
this.showSecurityControl = this.fb.control(false); this.showSecurityControl = this.fb.control(false);
this.enableSlaveControl = this.fb.control(false);
this.slaveConfigFormGroup = this.fb.group({ this.slaveConfigFormGroup = this.fb.group({
type: [ModbusProtocolType.TCP], type: [ModbusProtocolType.TCP],
host: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], host: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]],
@ -127,6 +128,7 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat
this.observeValueChanges(); this.observeValueChanges();
this.observeTypeChange(); this.observeTypeChange();
this.observeShowSecurity(); this.observeShowSecurity();
this.observeFormEnable();
} }
get protocolType(): ModbusProtocolType { get protocolType(): ModbusProtocolType {
@ -153,15 +155,11 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat
} }
writeValue(slaveConfig: ModbusSlave): void { writeValue(slaveConfig: ModbusSlave): void {
this.enableSlaveControl.patchValue(!!slaveConfig && !isEqual(slaveConfig, {}));
this.showSecurityControl.patchValue(!!slaveConfig.security && !isEqual(slaveConfig.security, {})); this.showSecurityControl.patchValue(!!slaveConfig.security && !isEqual(slaveConfig.security, {}));
this.updateSlaveConfig(slaveConfig); this.updateSlaveConfig(slaveConfig);
} }
setDisabledState(isDisabled: boolean): void {
this.isSlaveEnabled = !isDisabled;
this.updateFormEnableState();
}
private observeValueChanges(): void { private observeValueChanges(): void {
this.slaveConfigFormGroup.valueChanges.pipe( this.slaveConfigFormGroup.valueChanges.pipe(
takeUntil(this.destroy$) takeUntil(this.destroy$)
@ -175,11 +173,20 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat
}); });
} }
private observeFormEnable(): void {
this.enableSlaveControl.valueChanges
.pipe(takeUntil(this.destroy$))
.subscribe(value => {
this.updateFormEnableState(value);
this.slaveConfigFormGroup.updateValueAndValidity({emitEvent: !!this.onChange});
});
}
private observeTypeChange(): void { private observeTypeChange(): void {
this.slaveConfigFormGroup.get('type').valueChanges this.slaveConfigFormGroup.get('type').valueChanges
.pipe(takeUntil(this.destroy$)) .pipe(takeUntil(this.destroy$))
.subscribe(type => { .subscribe(type => {
this.updateFormEnableState(); this.updateFormEnableState(this.enableSlaveControl.value);
this.updateMethodType(type); this.updateMethodType(type);
}); });
} }
@ -195,8 +202,8 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat
} }
} }
private updateFormEnableState(): void { private updateFormEnableState(enabled: boolean): void {
if (this.isSlaveEnabled) { if (enabled) {
this.slaveConfigFormGroup.enable({emitEvent: false}); this.slaveConfigFormGroup.enable({emitEvent: false});
this.showSecurityControl.enable({emitEvent: false}); this.showSecurityControl.enable({emitEvent: false});
} else { } else {
@ -214,7 +221,7 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat
} }
private updateSecurityEnable(securityEnabled: boolean): void { private updateSecurityEnable(securityEnabled: boolean): void {
if (securityEnabled && this.isSlaveEnabled && this.protocolType !== ModbusProtocolType.Serial) { if (securityEnabled && this.enableSlaveControl.value && this.protocolType !== ModbusProtocolType.Serial) {
this.slaveConfigFormGroup.get('security').enable({emitEvent: false}); this.slaveConfigFormGroup.get('security').enable({emitEvent: false});
} else { } else {
this.slaveConfigFormGroup.get('security').disable({emitEvent: false}); this.slaveConfigFormGroup.get('security').disable({emitEvent: false});
@ -226,7 +233,7 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat
const enableKeys = isSerial ? this.serialSpecificControlKeys : this.tcpUdpSpecificControlKeys; const enableKeys = isSerial ? this.serialSpecificControlKeys : this.tcpUdpSpecificControlKeys;
const disableKeys = isSerial ? this.tcpUdpSpecificControlKeys : this.serialSpecificControlKeys; const disableKeys = isSerial ? this.tcpUdpSpecificControlKeys : this.serialSpecificControlKeys;
if (this.isSlaveEnabled) { if (this.enableSlaveControl.value) {
enableKeys.forEach(key => this.slaveConfigFormGroup.get(key)?.enable({ emitEvent: false })); enableKeys.forEach(key => this.slaveConfigFormGroup.get(key)?.enable({ emitEvent: false }));
} }