25 changed files with 395 additions and 69 deletions
@ -0,0 +1,33 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2020 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. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.query; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonIgnore; |
||||
|
import lombok.Data; |
||||
|
import lombok.Getter; |
||||
|
|
||||
|
@Data |
||||
|
public class DynamicValue<T> { |
||||
|
|
||||
|
@JsonIgnore |
||||
|
private T resolvedValue; |
||||
|
|
||||
|
@Getter |
||||
|
private final DynamicValueSourceType sourceType; |
||||
|
@Getter |
||||
|
private final String sourceAttribute; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2020 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. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.query; |
||||
|
|
||||
|
public enum DynamicValueSourceType { |
||||
|
CURRENT_TENANT, |
||||
|
CURRENT_CUSTOMER, |
||||
|
CURRENT_USER |
||||
|
} |
||||
@ -0,0 +1,63 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2020 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. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.query; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonCreator; |
||||
|
import com.fasterxml.jackson.annotation.JsonIgnore; |
||||
|
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
|
import lombok.Data; |
||||
|
import lombok.Getter; |
||||
|
|
||||
|
@Data |
||||
|
public class FilterPredicateValue<T> { |
||||
|
|
||||
|
@Getter |
||||
|
private final T defaultValue; |
||||
|
@Getter |
||||
|
private final DynamicValue<T> dynamicValue; |
||||
|
|
||||
|
public FilterPredicateValue(T defaultValue) { |
||||
|
this(defaultValue, null); |
||||
|
} |
||||
|
|
||||
|
@JsonCreator |
||||
|
public FilterPredicateValue(@JsonProperty("defaultValue") T defaultValue, |
||||
|
@JsonProperty("dynamicValue") DynamicValue<T> dynamicValue) { |
||||
|
this.defaultValue = defaultValue; |
||||
|
this.dynamicValue = dynamicValue; |
||||
|
} |
||||
|
|
||||
|
@JsonIgnore |
||||
|
public T getValue() { |
||||
|
if (this.dynamicValue != null && this.dynamicValue.getResolvedValue() != null) { |
||||
|
return this.dynamicValue.getResolvedValue(); |
||||
|
} else { |
||||
|
return defaultValue; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static FilterPredicateValue<Double> fromDouble(double value) { |
||||
|
return new FilterPredicateValue<>(value); |
||||
|
} |
||||
|
|
||||
|
public static FilterPredicateValue<String> fromString(String value) { |
||||
|
return new FilterPredicateValue<>(value); |
||||
|
} |
||||
|
|
||||
|
public static FilterPredicateValue<Boolean> fromBoolean(boolean value) { |
||||
|
return new FilterPredicateValue<>(value); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,48 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
Copyright © 2016-2020 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 fxFlex fxLayout="row" fxLayoutAlign="start center" fxLayoutGap="8px" [formGroup]="filterPredicateValueFormGroup"> |
||||
|
<div fxFlex fxLayout="column"> |
||||
|
<div fxFlex fxLayout="column" [ngSwitch]="valueType"> |
||||
|
<ng-template [ngSwitchCase]="valueTypeEnum.STRING"> |
||||
|
<mat-form-field floatLabel="always" hideRequiredMarker fxFlex class="mat-block"> |
||||
|
<mat-label></mat-label> |
||||
|
<input matInput formControlName="defaultValue" placeholder="{{'filter.value' | translate}}"> |
||||
|
</mat-form-field> |
||||
|
</ng-template> |
||||
|
<ng-template [ngSwitchCase]="valueTypeEnum.NUMERIC"> |
||||
|
<mat-form-field floatLabel="always" hideRequiredMarker fxFlex class="mat-block"> |
||||
|
<mat-label></mat-label> |
||||
|
<input required type="number" matInput formControlName="defaultValue" |
||||
|
placeholder="{{'filter.value' | translate}}"> |
||||
|
</mat-form-field> |
||||
|
</ng-template> |
||||
|
<ng-template [ngSwitchCase]="valueTypeEnum.DATE_TIME"> |
||||
|
<tb-datetime fxFlex formControlName="defaultValue" |
||||
|
dateText="filter.date" |
||||
|
timeText="filter.time" |
||||
|
required [showLabel]="false"></tb-datetime> |
||||
|
</ng-template> |
||||
|
<ng-template [ngSwitchCase]="valueTypeEnum.BOOLEAN"> |
||||
|
<mat-checkbox fxFlex formControlName="defaultValue"> |
||||
|
{{ (filterPredicateValueFormGroup.get('defaultValue').value ? 'value.true' : 'value.false') | translate }} |
||||
|
</mat-checkbox> |
||||
|
</ng-template> |
||||
|
</div> |
||||
|
<div class="tb-hint">Default value</div> |
||||
|
</div> |
||||
|
</div> |
||||
@ -0,0 +1,126 @@ |
|||||
|
///
|
||||
|
/// Copyright © 2016-2020 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 { Component, forwardRef, Input, OnInit } from '@angular/core'; |
||||
|
import { |
||||
|
ControlValueAccessor, |
||||
|
FormBuilder, |
||||
|
FormGroup, |
||||
|
NG_VALUE_ACCESSOR, |
||||
|
ValidatorFn, |
||||
|
Validators |
||||
|
} from '@angular/forms'; |
||||
|
import { EntityKeyValueType, FilterPredicateValue } from '@shared/models/query/query.models'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-filter-predicate-value', |
||||
|
templateUrl: './filter-predicate-value.component.html', |
||||
|
styleUrls: ['./filter-predicate.scss'], |
||||
|
providers: [ |
||||
|
{ |
||||
|
provide: NG_VALUE_ACCESSOR, |
||||
|
useExisting: forwardRef(() => FilterPredicateValueComponent), |
||||
|
multi: true |
||||
|
} |
||||
|
] |
||||
|
}) |
||||
|
export class FilterPredicateValueComponent implements ControlValueAccessor, OnInit { |
||||
|
|
||||
|
@Input() disabled: boolean; |
||||
|
|
||||
|
@Input() |
||||
|
valueType: EntityKeyValueType; |
||||
|
|
||||
|
valueTypeEnum = EntityKeyValueType; |
||||
|
|
||||
|
filterPredicateValueFormGroup: FormGroup; |
||||
|
|
||||
|
private propagateChange = null; |
||||
|
|
||||
|
constructor(private fb: FormBuilder) { |
||||
|
} |
||||
|
|
||||
|
ngOnInit(): void { |
||||
|
let defaultValue: string | number | boolean; |
||||
|
let defaultValueValidators: ValidatorFn[]; |
||||
|
switch (this.valueType) { |
||||
|
case EntityKeyValueType.STRING: |
||||
|
defaultValue = ''; |
||||
|
defaultValueValidators = []; |
||||
|
break; |
||||
|
case EntityKeyValueType.NUMERIC: |
||||
|
defaultValue = 0; |
||||
|
defaultValueValidators = [Validators.required]; |
||||
|
break; |
||||
|
case EntityKeyValueType.BOOLEAN: |
||||
|
defaultValue = false; |
||||
|
defaultValueValidators = []; |
||||
|
break; |
||||
|
case EntityKeyValueType.DATE_TIME: |
||||
|
defaultValue = Date.now(); |
||||
|
defaultValueValidators = [Validators.required]; |
||||
|
break; |
||||
|
} |
||||
|
this.filterPredicateValueFormGroup = this.fb.group({ |
||||
|
defaultValue: [defaultValue, defaultValueValidators], |
||||
|
dynamicValue: this.fb.group( |
||||
|
{ |
||||
|
sourceType: [null], |
||||
|
sourceAttribute: [null] |
||||
|
} |
||||
|
) |
||||
|
}); |
||||
|
this.filterPredicateValueFormGroup.valueChanges.subscribe(() => { |
||||
|
this.updateModel(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
registerOnChange(fn: any): void { |
||||
|
this.propagateChange = fn; |
||||
|
} |
||||
|
|
||||
|
registerOnTouched(fn: any): void { |
||||
|
} |
||||
|
|
||||
|
setDisabledState?(isDisabled: boolean): void { |
||||
|
this.disabled = isDisabled; |
||||
|
if (this.disabled) { |
||||
|
this.filterPredicateValueFormGroup.disable({emitEvent: false}); |
||||
|
} else { |
||||
|
this.filterPredicateValueFormGroup.enable({emitEvent: false}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
writeValue(predicateValue: FilterPredicateValue<string | number | boolean>): void { |
||||
|
this.filterPredicateValueFormGroup.get('defaultValue').patchValue(predicateValue.defaultValue, {emitEvent: false}); |
||||
|
this.filterPredicateValueFormGroup.get('dynamicValue').patchValue(predicateValue.dynamicValue ? |
||||
|
predicateValue.dynamicValue : { sourceType: null, sourceAttribute: null }, {emitEvent: false}); |
||||
|
} |
||||
|
|
||||
|
private updateModel() { |
||||
|
let predicateValue: FilterPredicateValue<string | number | boolean> = null; |
||||
|
if (this.filterPredicateValueFormGroup.valid) { |
||||
|
predicateValue = this.filterPredicateValueFormGroup.getRawValue(); |
||||
|
if (predicateValue.dynamicValue) { |
||||
|
if (!predicateValue.dynamicValue.sourceType || !predicateValue.dynamicValue.sourceAttribute) { |
||||
|
predicateValue.dynamicValue = null; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
this.propagateChange(predicateValue); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue