|
|
|
@ -16,7 +16,7 @@ |
|
|
|
|
|
|
|
import { ChangeDetectorRef, Component, Input, OnInit, output } from '@angular/core'; |
|
|
|
import { TbPopoverComponent } from '@shared/components/popover.component'; |
|
|
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
|
|
|
import { FormBuilder, FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms'; |
|
|
|
import { charsWithNumRegex, noLeadTrailSpacesRegex } from '@shared/models/regex.constants'; |
|
|
|
import { |
|
|
|
ArgumentEntityType, |
|
|
|
@ -52,11 +52,12 @@ export class CalculatedFieldArgumentPanelComponent implements OnInit { |
|
|
|
@Input() tenantId: string; |
|
|
|
@Input() entityName: string; |
|
|
|
@Input() calculatedFieldType: CalculatedFieldType; |
|
|
|
@Input() usedArgumentNames: string[]; |
|
|
|
|
|
|
|
argumentsDataApplied = output<{ value: CalculatedFieldArgumentValue, index: number }>(); |
|
|
|
|
|
|
|
argumentFormGroup = this.fb.group({ |
|
|
|
argumentName: ['', [Validators.required, Validators.pattern(charsWithNumRegex), Validators.maxLength(255)]], |
|
|
|
argumentName: ['', [Validators.required, this.uniqNameRequired(), Validators.pattern(charsWithNumRegex), Validators.maxLength(255)]], |
|
|
|
refEntityId: this.fb.group({ |
|
|
|
entityType: [ArgumentEntityType.Current], |
|
|
|
id: [''] |
|
|
|
@ -109,6 +110,12 @@ export class CalculatedFieldArgumentPanelComponent implements OnInit { |
|
|
|
return this.argumentFormGroup.get('refEntityKey') as FormGroup; |
|
|
|
} |
|
|
|
|
|
|
|
get enableAttributeScopeSelection(): boolean { |
|
|
|
return this.entityType === ArgumentEntityType.Device |
|
|
|
|| (this.entityType === ArgumentEntityType.Current |
|
|
|
&& (this.entityId.entityType === EntityType.DEVICE || this.entityId.entityType === EntityType.DEVICE_PROFILE)) |
|
|
|
} |
|
|
|
|
|
|
|
ngOnInit(): void { |
|
|
|
this.argumentFormGroup.patchValue(this.argument, {emitEvent: false}); |
|
|
|
this.currentEntityFilter = getCalculatedFieldCurrentEntityFilter(this.entityName, this.entityId); |
|
|
|
@ -188,9 +195,21 @@ export class CalculatedFieldArgumentPanelComponent implements OnInit { |
|
|
|
this.argumentFormGroup.get('refEntityId').get('id').setValue(''); |
|
|
|
this.argumentFormGroup.get('refEntityId') |
|
|
|
.get('id')[type === ArgumentEntityType.Tenant || type === ArgumentEntityType.Current ? 'disable' : 'enable'](); |
|
|
|
if (!this.enableAttributeScopeSelection) { |
|
|
|
this.refEntityKeyFormGroup.get('scope').setValue(AttributeScope.SERVER_SCOPE); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private uniqNameRequired(): ValidatorFn { |
|
|
|
return (control: FormControl) => { |
|
|
|
const newName = control.value.trim().toLowerCase(); |
|
|
|
const isDuplicate = this.usedArgumentNames?.some(name => name.toLowerCase() === newName); |
|
|
|
|
|
|
|
return isDuplicate ? { duplicateName: true } : null; |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
private observeEntityKeyChanges(): void { |
|
|
|
this.argumentFormGroup.get('refEntityKey').get('type').valueChanges |
|
|
|
.pipe(takeUntilDestroyed()) |
|
|
|
|