2019-12-23 14:36:44 +02:00
|
|
|
///
|
|
|
|
|
/// Copyright © 2016-2019 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 {
|
|
|
|
|
AfterViewInit,
|
|
|
|
|
Component, ElementRef,
|
|
|
|
|
EventEmitter, forwardRef,
|
|
|
|
|
Input,
|
|
|
|
|
OnChanges,
|
|
|
|
|
OnInit,
|
|
|
|
|
Output,
|
|
|
|
|
SimpleChanges,
|
|
|
|
|
ViewChild,
|
|
|
|
|
Compiler,
|
|
|
|
|
Injector, ComponentRef, OnDestroy
|
|
|
|
|
} from '@angular/core';
|
|
|
|
|
import { PageComponent } from '@shared/components/page.component';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR, NgForm, Validators } from '@angular/forms';
|
|
|
|
|
import { FcRuleNode, FcRuleEdge } from './rulechain-page.models';
|
|
|
|
|
import { RuleNodeType, LinkLabel, RuleNodeDefinition, RuleNodeConfiguration, IRuleNodeConfigurationComponent } from '@shared/models/rule-node.models';
|
|
|
|
|
import { EntityType } from '@shared/models/entity-type.models';
|
|
|
|
|
import { Observable, of, Subscription } from 'rxjs';
|
|
|
|
|
import { RuleChainService } from '@core/http/rule-chain.service';
|
|
|
|
|
import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
|
|
|
|
import { deepClone } from '@core/utils';
|
|
|
|
|
import { EntityAlias } from '@shared/models/alias.models';
|
|
|
|
|
import { TruncatePipe } from '@shared/pipe/truncate.pipe';
|
|
|
|
|
import { MatChipList, MatAutocomplete, MatChipInputEvent, MatAutocompleteSelectedEvent } from '@angular/material';
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
|
import { COMMA, ENTER, SEMICOLON } from '@angular/cdk/keycodes';
|
|
|
|
|
import { catchError, map, mergeMap, share } from 'rxjs/operators';
|
|
|
|
|
import { DynamicWidgetComponent } from '@home/components/widget/dynamic-widget.component';
|
|
|
|
|
import { SharedModule } from '@shared/shared.module';
|
|
|
|
|
import { WidgetComponentsModule } from '@home/components/widget/widget-components.module';
|
|
|
|
|
import { DynamicComponentFactoryService } from '@core/services/dynamic-component-factory.service';
|
|
|
|
|
import { ViewContainerRef } from '@angular/core';
|
|
|
|
|
import { JsonObjectEditComponent } from '@shared/components/json-object-edit.component';
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-rule-node-config',
|
|
|
|
|
templateUrl: './rule-node-config.component.html',
|
|
|
|
|
styleUrls: ['./rule-node-config.component.scss'],
|
|
|
|
|
providers: [{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => RuleNodeConfigComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
}]
|
|
|
|
|
})
|
|
|
|
|
export class RuleNodeConfigComponent implements ControlValueAccessor, OnInit, OnDestroy, AfterViewInit {
|
|
|
|
|
|
|
|
|
|
@ViewChild('definedConfigContent', {read: ViewContainerRef, static: true}) definedConfigContainer: ViewContainerRef;
|
|
|
|
|
|
|
|
|
|
@ViewChild('jsonObjectEditComponent', {static: false}) jsonObjectEditComponent: JsonObjectEditComponent;
|
|
|
|
|
|
|
|
|
|
private requiredValue: boolean;
|
|
|
|
|
get required(): boolean {
|
|
|
|
|
return this.requiredValue;
|
|
|
|
|
}
|
|
|
|
|
@Input()
|
|
|
|
|
set required(value: boolean) {
|
|
|
|
|
this.requiredValue = coerceBooleanProperty(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
ruleNodeId: string;
|
|
|
|
|
|
|
|
|
|
nodeDefinitionValue: RuleNodeDefinition;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
set nodeDefinition(nodeDefinition: RuleNodeDefinition) {
|
|
|
|
|
if (this.nodeDefinitionValue !== nodeDefinition) {
|
|
|
|
|
this.nodeDefinitionValue = nodeDefinition;
|
|
|
|
|
if (this.nodeDefinitionValue) {
|
|
|
|
|
this.validateDefinedDirective();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get nodeDefinition(): RuleNodeDefinition {
|
|
|
|
|
return this.nodeDefinitionValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
definedDirectiveError: string;
|
|
|
|
|
|
|
|
|
|
ruleNodeConfigFormGroup: FormGroup;
|
|
|
|
|
|
|
|
|
|
changeSubscription: Subscription;
|
|
|
|
|
|
|
|
|
|
private definedConfigComponentRef: ComponentRef<IRuleNodeConfigurationComponent>;
|
|
|
|
|
private definedConfigComponent: IRuleNodeConfigurationComponent;
|
|
|
|
|
|
|
|
|
|
private configuration: RuleNodeConfiguration;
|
|
|
|
|
|
|
|
|
|
private propagateChange = (v: any) => { };
|
|
|
|
|
|
|
|
|
|
constructor(private translate: TranslateService,
|
|
|
|
|
private ruleChainService: RuleChainService,
|
|
|
|
|
private fb: FormBuilder) {
|
|
|
|
|
this.ruleNodeConfigFormGroup = this.fb.group({
|
|
|
|
|
configuration: [null, Validators.required]
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
|
if (this.definedConfigComponentRef) {
|
|
|
|
|
this.definedConfigComponentRef.destroy();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngAfterViewInit(): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
|
|
|
|
if (this.disabled) {
|
|
|
|
|
this.ruleNodeConfigFormGroup.disable({emitEvent: false});
|
|
|
|
|
} else {
|
|
|
|
|
this.ruleNodeConfigFormGroup.enable({emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(value: RuleNodeConfiguration): void {
|
|
|
|
|
this.configuration = value;
|
|
|
|
|
if (this.changeSubscription) {
|
|
|
|
|
this.changeSubscription.unsubscribe();
|
|
|
|
|
this.changeSubscription = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.definedConfigComponent) {
|
|
|
|
|
this.definedConfigComponent.configuration = this.configuration;
|
|
|
|
|
this.changeSubscription = this.definedConfigComponent.configurationChanged.subscribe((configuration) => {
|
|
|
|
|
this.updateModel(configuration);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.ruleNodeConfigFormGroup.get('configuration').patchValue(value, {emitEvent: false});
|
|
|
|
|
this.changeSubscription = this.ruleNodeConfigFormGroup.get('configuration').valueChanges.subscribe(
|
|
|
|
|
(configuration: RuleNodeConfiguration) => {
|
|
|
|
|
this.updateModel(configuration);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useDefinedDirective(): boolean {
|
|
|
|
|
return this.nodeDefinition &&
|
|
|
|
|
(this.nodeDefinition.configDirective &&
|
|
|
|
|
this.nodeDefinition.configDirective.length) && !this.definedDirectiveError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private updateModel(configuration: RuleNodeConfiguration) {
|
|
|
|
|
if (this.definedConfigComponent || this.ruleNodeConfigFormGroup.valid) {
|
|
|
|
|
this.propagateChange(configuration);
|
|
|
|
|
} else {
|
|
|
|
|
this.propagateChange(this.required ? null : configuration);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private validateDefinedDirective() {
|
|
|
|
|
if (this.definedConfigComponentRef) {
|
|
|
|
|
this.definedConfigComponentRef.destroy();
|
|
|
|
|
this.definedConfigComponentRef = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.nodeDefinition.uiResourceLoadError && this.nodeDefinition.uiResourceLoadError.length) {
|
|
|
|
|
this.definedDirectiveError = this.nodeDefinition.uiResourceLoadError;
|
|
|
|
|
} else if (this.nodeDefinition.configDirective && this.nodeDefinition.configDirective.length) {
|
|
|
|
|
if (this.changeSubscription) {
|
|
|
|
|
this.changeSubscription.unsubscribe();
|
|
|
|
|
this.changeSubscription = null;
|
|
|
|
|
}
|
|
|
|
|
this.definedConfigContainer.clear();
|
|
|
|
|
const factory = this.ruleChainService.getRuleNodeConfigFactory(this.nodeDefinition.configDirective);
|
|
|
|
|
this.definedConfigComponentRef = this.definedConfigContainer.createComponent(factory);
|
|
|
|
|
this.definedConfigComponent = this.definedConfigComponentRef.instance;
|
|
|
|
|
this.definedConfigComponent.ruleNodeId = this.ruleNodeId;
|
|
|
|
|
this.definedConfigComponent.configuration = this.configuration;
|
|
|
|
|
this.changeSubscription = this.definedConfigComponent.configurationChanged.subscribe((configuration) => {
|
|
|
|
|
this.updateModel(configuration);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-24 13:54:00 +02:00
|
|
|
|
|
|
|
|
validate() {
|
|
|
|
|
if (this.useDefinedDirective()) {
|
|
|
|
|
this.definedConfigComponent.validate();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-23 14:36:44 +02:00
|
|
|
}
|