Browse Source

refactoring

pull/11319/head
mpetrov 2 years ago
parent
commit
27bd1b2fa0
  1. 16
      ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-basic-config/modbus-basic-config.component.ts
  2. 17
      ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.ts

16
ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-basic-config/modbus-basic-config.component.ts

@ -22,7 +22,6 @@ import {
FormGroup, FormGroup,
NG_VALIDATORS, NG_VALIDATORS,
NG_VALUE_ACCESSOR, NG_VALUE_ACCESSOR,
UntypedFormControl,
ValidationErrors, ValidationErrors,
Validator, Validator,
} from '@angular/forms'; } from '@angular/forms';
@ -95,6 +94,7 @@ export class ModbusBasicConfigComponent implements ControlValueAccessor, Validat
.subscribe(enable => { .subscribe(enable => {
this.updateSlaveEnabling(enable); this.updateSlaveEnabling(enable);
this.basicFormGroup.get('slave').updateValueAndValidity({emitEvent: !!this.onChange}); this.basicFormGroup.get('slave').updateValueAndValidity({emitEvent: !!this.onChange});
this.basicFormGroup.get('master').updateValueAndValidity({emitEvent: !!this.onChange});
}); });
} }
@ -121,16 +121,10 @@ export class ModbusBasicConfigComponent implements ControlValueAccessor, Validat
this.enableSlaveControl.setValue(!!basicConfig.slave && !isEqual(basicConfig.slave, {})); this.enableSlaveControl.setValue(!!basicConfig.slave && !isEqual(basicConfig.slave, {}));
} }
validate(basicConfigControl: UntypedFormControl): ValidationErrors | null { validate(): ValidationErrors | null {
const masterHasSlaves = !!basicConfigControl.value.master?.slaves?.length; return this.basicFormGroup.valid ? null : {
const slaveEnabled = this.enableSlaveControl.value; basicFormGroup: {valid: false}
const slaveIsValid = this.basicFormGroup.valid; };
if ((slaveEnabled && slaveIsValid) || (masterHasSlaves && !slaveEnabled)) {
return null;
}
return { basicFormGroup: { valid: false } };
} }
private updateSlaveEnabling(isEnabled: boolean): void { private updateSlaveEnabling(isEnabled: boolean): void {

17
ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.ts

@ -34,8 +34,12 @@ 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,
@ -58,12 +62,17 @@ import { TbTableDatasource } from '@shared/components/table/table-datasource.abs
provide: NG_VALUE_ACCESSOR, provide: NG_VALUE_ACCESSOR,
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, AfterViewInit, OnInit, OnDestroy { export class ModbusMasterTableComponent implements ControlValueAccessor, Validator, AfterViewInit, OnInit, OnDestroy {
@ViewChild('searchInput') searchInputField: ElementRef; @ViewChild('searchInput') searchInputField: ElementRef;
@ -130,6 +139,12 @@ export class ModbusMasterTableComponent implements ControlValueAccessor, AfterVi
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();

Loading…
Cancel
Save