10 changed files with 510 additions and 25 deletions
@ -0,0 +1,192 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
Copyright © 2016-2025 The Thingsboard Authors |
||||
|
|
||||
|
Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
you may not use this file except in compliance with the License. |
||||
|
You may obtain a copy of the License at |
||||
|
|
||||
|
http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
|
||||
|
Unless required by applicable law or agreed to in writing, software |
||||
|
distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
See the License for the specific language governing permissions and |
||||
|
limitations under the License. |
||||
|
|
||||
|
--> |
||||
|
<div class="tb-details-buttons xs:flex xs:flex-col" *ngIf="!standalone"> |
||||
|
<button mat-raised-button color="primary" |
||||
|
[disabled]="(isLoading$ | async)" |
||||
|
(click)="onEntityAction($event, 'open')" |
||||
|
[class.!hidden]="isEdit || isDetailsPage"> |
||||
|
{{'common.open-details-page' | translate }} |
||||
|
</button> |
||||
|
<button mat-raised-button color="primary" |
||||
|
[disabled]="(isLoading$ | async)" |
||||
|
(click)="onEntityAction($event, 'export')" |
||||
|
[class.!hidden]="isEdit"> |
||||
|
{{'action.export' | translate }} |
||||
|
</button> |
||||
|
<button mat-raised-button color="primary" |
||||
|
[disabled]="(isLoading$ | async)" |
||||
|
(click)="onEntityAction($event, 'delete')" |
||||
|
[class.!hidden]="hideDelete() || isEdit"> |
||||
|
{{'action.delete' | translate }} |
||||
|
</button> |
||||
|
<!-- <div class="flex flex-row xs:flex-col">--> |
||||
|
<!-- <button mat-raised-button--> |
||||
|
<!-- ngxClipboard--> |
||||
|
<!-- (cbOnSuccess)="onAssetProfileIdCopied($event)"--> |
||||
|
<!-- [cbContent]="entity?.id?.id"--> |
||||
|
<!-- [class.!hidden]="isEdit">--> |
||||
|
<!-- <mat-icon svgIcon="mdi:clipboard-arrow-left"></mat-icon>--> |
||||
|
<!-- <span translate>asset-profile.copyId</span>--> |
||||
|
<!-- </button>--> |
||||
|
<!-- </div>--> |
||||
|
</div> |
||||
|
<div class="mat-padding flex flex-col"> |
||||
|
<!-- <mat-form-field class="mat-block"--> |
||||
|
<!-- [class.!hidden]="isEdit || !isAssignedToCustomer(entity)--> |
||||
|
<!-- || entity?.customerIsPublic || assetScope !== 'tenant'">--> |
||||
|
<!-- <mat-label translate>asset.assignedToCustomer</mat-label>--> |
||||
|
<!-- <input matInput disabled [ngModel]="entity?.customerTitle">--> |
||||
|
<!-- </mat-form-field>--> |
||||
|
<!-- <div class="tb-small" style="padding-bottom: 10px; padding-left: 2px;"--> |
||||
|
<!-- [class.!hidden]="isEdit || !entity?.customerIsPublic || (assetScope !== 'customer' && assetScope !== 'tenant')">--> |
||||
|
<!-- {{ 'asset.asset-public' | translate }}--> |
||||
|
<!-- </div>--> |
||||
|
<!-- <form [formGroup]="entityForm">--> |
||||
|
<!-- <fieldset [disabled]="(isLoading$ | async) || !isEdit">--> |
||||
|
<!-- <mat-form-field class="mat-block">--> |
||||
|
<!-- <mat-label translate>asset.name</mat-label>--> |
||||
|
<!-- <input matInput formControlName="name" required>--> |
||||
|
<!-- <mat-error *ngIf="entityForm.get('name').hasError('required')">--> |
||||
|
<!-- {{ 'asset.name-required' | translate }}--> |
||||
|
<!-- </mat-error>--> |
||||
|
<!-- <mat-error *ngIf="entityForm.get('name').hasError('maxlength')">--> |
||||
|
<!-- {{ 'asset.name-max-length' | translate }}--> |
||||
|
<!-- </mat-error>--> |
||||
|
<!-- </mat-form-field>--> |
||||
|
<!-- <mat-form-field class="mat-block">--> |
||||
|
<!-- <mat-label translate>asset.label</mat-label>--> |
||||
|
<!-- <input matInput formControlName="label">--> |
||||
|
<!-- <mat-error *ngIf="entityForm.get('label').hasError('maxlength')">--> |
||||
|
<!-- {{ 'asset.label-max-length' | translate }}--> |
||||
|
<!-- </mat-error>--> |
||||
|
<!-- </mat-form-field>--> |
||||
|
<!-- <tb-asset-profile-autocomplete--> |
||||
|
<!-- [selectDefaultProfile]="isAdd"--> |
||||
|
<!-- required--> |
||||
|
<!-- formControlName="assetProfileId"--> |
||||
|
<!-- [showDetailsPageLink]="true"--> |
||||
|
<!-- (assetProfileUpdated)="onAssetProfileUpdated()">--> |
||||
|
<!-- </tb-asset-profile-autocomplete>--> |
||||
|
<!-- <tb-entity-autocomplete--> |
||||
|
<!-- *ngIf="isAdd"--> |
||||
|
<!-- useFullEntityId--> |
||||
|
<!-- formControlName="customerId"--> |
||||
|
<!-- labelText="asset.assign-to-customer"--> |
||||
|
<!-- [entityType]="entityType.CUSTOMER">--> |
||||
|
<!-- </tb-entity-autocomplete>--> |
||||
|
<!-- <div formGroupName="additionalInfo">--> |
||||
|
<!-- <mat-form-field class="mat-block">--> |
||||
|
<!-- <mat-label translate>asset.description</mat-label>--> |
||||
|
<!-- <textarea matInput formControlName="description" rows="2"></textarea>--> |
||||
|
<!-- </mat-form-field>--> |
||||
|
<!-- </div>--> |
||||
|
<!-- </fieldset>--> |
||||
|
<!-- </form>--> |
||||
|
<div mat-dialog-content class="flex-1" [formGroup]="entityForm"> |
||||
|
<div class="tb-form-panel no-border no-padding"> |
||||
|
<div class="tb-form-panel no-gap"> |
||||
|
<div class="tb-form-panel-title mb-4">{{ 'common.general' | translate }}</div> |
||||
|
<div class="flex items-center gap-2"> |
||||
|
<mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic"> |
||||
|
<mat-label>{{ 'entity-field.title' | translate }}</mat-label> |
||||
|
<input matInput maxlength="255" formControlName="name" required> |
||||
|
@if (entityForm.get('name').errors && entityForm.get('name').touched) { |
||||
|
<mat-error> |
||||
|
@if (entityForm.get('name').hasError('required')) { |
||||
|
{{ 'common.hint.title-required' | translate }} |
||||
|
} @else if (entityForm.get('name').hasError('pattern')) { |
||||
|
{{ 'common.hint.title-pattern' | translate }} |
||||
|
} @else if (entityForm.get('name').hasError('maxlength')) { |
||||
|
{{ 'common.hint.title-max-length' | translate }} |
||||
|
} |
||||
|
</mat-error> |
||||
|
} |
||||
|
<mat-hint></mat-hint> |
||||
|
</mat-form-field> |
||||
|
<tb-entity-debug-settings-button |
||||
|
formControlName="debugSettings" |
||||
|
style="margin-bottom: 22px" |
||||
|
[entityType]="EntityType.CALCULATED_FIELD" |
||||
|
[additionalActionConfig]="entitiesTableConfig.additionalDebugActionConfig" |
||||
|
/> |
||||
|
</div> |
||||
|
<tb-entity-select formControlName="entityId" appearance="outline" [allowedEntityTypes]="calculatedFieldsEntityTypeList"></tb-entity-select> |
||||
|
<mat-form-field appearance="outline" subscriptSizing="dynamic"> |
||||
|
<mat-label>{{ 'common.type' | translate }}</mat-label> |
||||
|
<mat-select formControlName="type"> |
||||
|
@for (type of fieldTypes; track type) { |
||||
|
<mat-option [value]="type">{{ CalculatedFieldTypeTranslations.get(type).name | translate}}</mat-option> |
||||
|
} |
||||
|
</mat-select> |
||||
|
@if (CalculatedFieldTypeTranslations.get(entityForm.get('type').value).hint) { |
||||
|
<mat-hint>{{ CalculatedFieldTypeTranslations.get(entityForm.get('type').value).hint | translate }}</mat-hint> |
||||
|
} |
||||
|
</mat-form-field> |
||||
|
</div> |
||||
|
@switch (entityForm.get('type').value) { |
||||
|
@case (CalculatedFieldType.GEOFENCING) { |
||||
|
<tb-geofencing-configuration formControlName="configuration" |
||||
|
[class.tb-form-panel-disabled]="disabledConfiguration" |
||||
|
[entityId]="entityId" |
||||
|
[entityName]="entityName" |
||||
|
[ownerId]="ownerId" |
||||
|
[tenantId]="tenantId" |
||||
|
></tb-geofencing-configuration> |
||||
|
} |
||||
|
@case (CalculatedFieldType.PROPAGATION) { |
||||
|
<tb-propagation-configuration formControlName="configuration" |
||||
|
[class.tb-form-panel-disabled]="disabledConfiguration" |
||||
|
[entityId]="entityId" |
||||
|
[entityName]="entityName" |
||||
|
[tenantId]="tenantId" |
||||
|
[ownerId]="ownerId" |
||||
|
[testScript]="onTestScript.bind(this)" |
||||
|
></tb-propagation-configuration> |
||||
|
} |
||||
|
@case (CalculatedFieldType.RELATED_ENTITIES_AGGREGATION) { |
||||
|
<tb-related-entities-aggregation-component formControlName="configuration" |
||||
|
[class.tb-form-panel-disabled]="disabledConfiguration" |
||||
|
[entityId]="entityId" |
||||
|
[entityName]="entityName" |
||||
|
[tenantId]="tenantId" |
||||
|
[testScript]="onTestScript.bind(this)" |
||||
|
></tb-related-entities-aggregation-component> |
||||
|
} |
||||
|
@case (CalculatedFieldType.ENTITY_AGGREGATION) { |
||||
|
<tb-entity-aggregation-component formControlName="configuration" |
||||
|
[class.tb-form-panel-disabled]="disabledConfiguration" |
||||
|
[entityId]="entityId" |
||||
|
[entityName]="entityName" |
||||
|
[tenantId]="tenantId" |
||||
|
></tb-entity-aggregation-component> |
||||
|
} |
||||
|
@default { |
||||
|
<tb-simple-configuration formControlName="configuration" |
||||
|
[class.tb-form-panel-disabled]="disabledConfiguration" |
||||
|
[entityId]="entityId" |
||||
|
[entityName]="entityName" |
||||
|
[ownerId]="ownerId" |
||||
|
[tenantId]="tenantId" |
||||
|
[isScript]="entityForm.get('type').value === CalculatedFieldType.SCRIPT" |
||||
|
[testScript]="onTestScript.bind(this)" |
||||
|
></tb-simple-configuration> |
||||
|
} |
||||
|
} |
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
||||
@ -0,0 +1,18 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2025 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
:host { |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,166 @@ |
|||||
|
///
|
||||
|
/// Copyright © 2016-2025 The Thingsboard Authors
|
||||
|
///
|
||||
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
/// you may not use this file except in compliance with the License.
|
||||
|
/// You may obtain a copy of the License at
|
||||
|
///
|
||||
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
///
|
||||
|
/// Unless required by applicable law or agreed to in writing, software
|
||||
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
/// See the License for the specific language governing permissions and
|
||||
|
/// limitations under the License.
|
||||
|
///
|
||||
|
|
||||
|
import { ChangeDetectorRef, Component, DestroyRef, Inject, Input } from '@angular/core'; |
||||
|
import { Store } from '@ngrx/store'; |
||||
|
import { AppState } from '@core/core.state'; |
||||
|
import { EntityComponent } from '../../components/entity/entity.component'; |
||||
|
import { FormBuilder, FormGroup, UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; |
||||
|
import { AliasEntityType, EntityType } from '@shared/models/entity-type.models'; |
||||
|
import { NULL_UUID } from '@shared/models/id/has-uuid'; |
||||
|
import { ActionNotificationShow } from '@core/notification/notification.actions'; |
||||
|
import { TranslateService } from '@ngx-translate/core'; |
||||
|
import { AssetInfo } from '@app/shared/models/asset.models'; |
||||
|
import { EntityTableConfig } from '@home/models/entity/entities-table-config.models'; |
||||
|
import { |
||||
|
CalculatedFieldConfiguration, |
||||
|
CalculatedFieldInfo, calculatedFieldsEntityTypeList, |
||||
|
CalculatedFieldType, calculatedFieldTypes, CalculatedFieldTypeTranslations, OutputStrategyType |
||||
|
} from '@shared/models/calculated-field.models'; |
||||
|
import { oneSpaceInsideRegex } from '@shared/models/regex.constants'; |
||||
|
import { isDefined } from '@core/utils'; |
||||
|
import { EntityId } from '@shared/models/id/entity-id'; |
||||
|
import { pairwise, switchMap } from 'rxjs/operators'; |
||||
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; |
||||
|
import { BaseData } from '@shared/models/base-data'; |
||||
|
import { Observable, of } from 'rxjs'; |
||||
|
import { CalculatedFieldsService } from '@core/http/calculated-fields.service'; |
||||
|
import { getCurrentAuthUser } from '@core/auth/auth.selectors'; |
||||
|
import { |
||||
|
CalculatedFieldsTableConfig, |
||||
|
CalculatedFieldsTableEntity |
||||
|
} from '@home/components/calculated-fields/calculated-fields-table-config'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-calculated-field', |
||||
|
templateUrl: './calculated-field.component.html', |
||||
|
styleUrls: ['./calculated-field.component.scss'] |
||||
|
}) |
||||
|
export class CalculatedFieldComponent extends EntityComponent<CalculatedFieldsTableEntity> { |
||||
|
|
||||
|
@Input() |
||||
|
standalone = false; |
||||
|
|
||||
|
@Input() |
||||
|
entityName: string; |
||||
|
|
||||
|
disabledConfiguration = false; |
||||
|
|
||||
|
readonly tenantId = getCurrentAuthUser(this.store).tenantId; |
||||
|
readonly ownerId = getCurrentAuthUser(this.store).tenantId; |
||||
|
readonly EntityType = EntityType; |
||||
|
readonly calculatedFieldsEntityTypeList = calculatedFieldsEntityTypeList; |
||||
|
readonly CalculatedFieldType = CalculatedFieldType; |
||||
|
readonly fieldTypes = calculatedFieldTypes; |
||||
|
readonly CalculatedFieldTypeTranslations = CalculatedFieldTypeTranslations; |
||||
|
|
||||
|
constructor(protected store: Store<AppState>, |
||||
|
protected translate: TranslateService, |
||||
|
@Inject('entity') protected entityValue: CalculatedFieldInfo, |
||||
|
@Inject('entitiesTableConfig') protected entitiesTableConfigValue: CalculatedFieldsTableConfig, |
||||
|
protected fb: FormBuilder, |
||||
|
protected cd: ChangeDetectorRef, |
||||
|
private destroyRef: DestroyRef, |
||||
|
private calculatedFieldsService: CalculatedFieldsService) { |
||||
|
super(store, fb, entityValue, entitiesTableConfigValue, cd); |
||||
|
} |
||||
|
|
||||
|
hideDelete() { |
||||
|
if (this.entitiesTableConfig) { |
||||
|
return !this.entitiesTableConfig.deleteEnabled(this.entity); |
||||
|
} else { |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
isAssignedToCustomer(entity: AssetInfo): boolean { |
||||
|
return entity && entity.customerId && entity.customerId.id !== NULL_UUID; |
||||
|
} |
||||
|
|
||||
|
get entityId(): EntityId { |
||||
|
return this.entityForm.get('entityId').value; |
||||
|
} |
||||
|
|
||||
|
get entitiesTableConfig(): CalculatedFieldsTableConfig { |
||||
|
return this.entitiesTableConfigValue; |
||||
|
} |
||||
|
|
||||
|
changeEntity(entity: BaseData<EntityId>): void { |
||||
|
this.entityName = entity?.name; |
||||
|
} |
||||
|
|
||||
|
buildForm(entity?: CalculatedFieldInfo): FormGroup { |
||||
|
const form = this.fb.group({ |
||||
|
name: ['', [Validators.required, Validators.pattern(oneSpaceInsideRegex), Validators.maxLength(255)]], |
||||
|
entityId: [null], |
||||
|
type: [CalculatedFieldType.SIMPLE], |
||||
|
debugSettings: [], |
||||
|
configuration: this.fb.control<CalculatedFieldConfiguration>({} as CalculatedFieldConfiguration), |
||||
|
}); |
||||
|
form.get('type').valueChanges.pipe( |
||||
|
pairwise(), |
||||
|
takeUntilDestroyed(this.destroyRef) |
||||
|
).subscribe(([prevType, nextType]) => { |
||||
|
if (![CalculatedFieldType.SIMPLE, CalculatedFieldType.SCRIPT].includes(prevType) || |
||||
|
![CalculatedFieldType.SIMPLE, CalculatedFieldType.SCRIPT].includes(nextType)) { |
||||
|
form.get('configuration').setValue(({} as CalculatedFieldConfiguration), {emitEvent: false}); |
||||
|
} |
||||
|
}); |
||||
|
return form; |
||||
|
} |
||||
|
|
||||
|
updateForm(entity: CalculatedFieldInfo) { |
||||
|
const { configuration = {} as CalculatedFieldConfiguration, type = CalculatedFieldType.SIMPLE, debugSettings = { failuresEnabled: true, allEnabled: true }, entityId = this.entityId, ...value } = entity ?? {}; |
||||
|
if (configuration.type !== CalculatedFieldType.ALARM) { |
||||
|
if (isDefined(configuration?.output) && !configuration?.output?.strategy) { |
||||
|
configuration.output.strategy = {type: OutputStrategyType.RULE_CHAIN}; |
||||
|
} |
||||
|
} |
||||
|
this.entityForm.patchValue({ configuration, type, debugSettings, entityId, ...value }, {emitEvent: false}); |
||||
|
setTimeout(() => this.entityForm.get('type').updateValueAndValidity({onlySelf: true})); |
||||
|
if (!entityId) { |
||||
|
this.entityForm.get('configuration').disable({emitEvent: false}); |
||||
|
this.disabledConfiguration = true; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
onTestScript(expression?: string): Observable<string> { |
||||
|
const calculatedFieldId = this.entityId?.id; |
||||
|
if (calculatedFieldId) { |
||||
|
return this.calculatedFieldsService.getLatestCalculatedFieldDebugEvent(calculatedFieldId, {ignoreLoading: true}) |
||||
|
.pipe( |
||||
|
switchMap(event => { |
||||
|
const args = event?.arguments ? JSON.parse(event.arguments) : null; |
||||
|
return this.entitiesTableConfig.getTestScriptDialog(this.entityFormValue(), args, false, expression); |
||||
|
}), |
||||
|
takeUntilDestroyed(this.destroyRef) |
||||
|
) |
||||
|
} |
||||
|
|
||||
|
return this.entitiesTableConfig.getTestScriptDialog(this.entityFormValue(), null, false, expression); |
||||
|
} |
||||
|
|
||||
|
updateFormState() { |
||||
|
if (this.entityForm) { |
||||
|
if (this.isEditValue) { |
||||
|
this.entityForm.enable({emitEvent: false}); |
||||
|
this.entityForm.get('entityId').disable({emitEvent: false}); |
||||
|
} else { |
||||
|
this.entityForm.disable({emitEvent: false}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue