29 changed files with 476 additions and 78 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,71 @@ |
|||
/** |
|||
* 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 T userValue; |
|||
@Getter |
|||
private final DynamicValue<T> dynamicValue; |
|||
|
|||
public FilterPredicateValue(T defaultValue) { |
|||
this(defaultValue, null, null); |
|||
} |
|||
|
|||
@JsonCreator |
|||
public FilterPredicateValue(@JsonProperty("defaultValue") T defaultValue, |
|||
@JsonProperty("userValue") T userValue, |
|||
@JsonProperty("dynamicValue") DynamicValue<T> dynamicValue) { |
|||
this.defaultValue = defaultValue; |
|||
this.userValue = userValue; |
|||
this.dynamicValue = dynamicValue; |
|||
} |
|||
|
|||
@JsonIgnore |
|||
public T getValue() { |
|||
if (this.userValue != null) { |
|||
return this.userValue; |
|||
} else { |
|||
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,82 @@ |
|||
<!-- |
|||
|
|||
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" [fxShow]="!dynamicMode"> |
|||
<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" translate>filter.default-value</div> |
|||
</div> |
|||
<div fxFlex fxLayout="column" [fxShow]="dynamicMode"> |
|||
<div fxFlex formGroupName="dynamicValue" fxLayout="row" fxLayoutAlign="start center" fxLayoutGap="8px"> |
|||
<div fxFlex fxLayout="column"> |
|||
<mat-form-field floatLabel="always" hideRequiredMarker fxFlex class="mat-block"> |
|||
<mat-label></mat-label> |
|||
<mat-select formControlName="sourceType" placeholder="{{'filter.dynamic-source-type' | translate}}"> |
|||
<mat-option [value]="null"> |
|||
{{'filter.no-dynamic-value' | translate}} |
|||
</mat-option> |
|||
<mat-option *ngFor="let sourceType of dynamicValueSourceTypes" [value]="sourceType"> |
|||
{{dynamicValueSourceTypeTranslations.get(dynamicValueSourceTypeEnum[sourceType]) | translate}} |
|||
</mat-option> |
|||
</mat-select> |
|||
</mat-form-field> |
|||
<div class="tb-hint" translate>filter.dynamic-source-type</div> |
|||
</div> |
|||
<div fxFlex fxLayout="column"> |
|||
<mat-form-field floatLabel="always" hideRequiredMarker fxFlex class="mat-block"> |
|||
<mat-label></mat-label> |
|||
<input matInput formControlName="sourceAttribute" placeholder="{{'filter.source-attribute' | translate}}"> |
|||
</mat-form-field> |
|||
<div class="tb-hint" translate>filter.source-attribute</div> |
|||
</div> |
|||
</div> |
|||
</div> |
|||
<button mat-icon-button |
|||
class="mat-elevation-z1 tb-mat-32" |
|||
color="primary" |
|||
type="button" |
|||
matTooltip="{{ (dynamicMode ? 'filter.switch-to-default-value' : 'filter.switch-to-dynamic-value') | translate }}" |
|||
matTooltipPosition="above" |
|||
(click)="dynamicMode = !dynamicMode"> |
|||
<mat-icon class="tb-mat-20" [svgIcon]="dynamicMode ? 'mdi:numeric' : 'mdi:variable'"></mat-icon> |
|||
</button> |
|||
</div> |
|||
@ -0,0 +1,146 @@ |
|||
///
|
|||
/// 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 { |
|||
DynamicValueSourceType, |
|||
dynamicValueSourceTypeTranslationMap, |
|||
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; |
|||
|
|||
dynamicValueSourceTypes = Object.keys(DynamicValueSourceType); |
|||
dynamicValueSourceTypeEnum = DynamicValueSourceType; |
|||
dynamicValueSourceTypeTranslations = dynamicValueSourceTypeTranslationMap; |
|||
|
|||
filterPredicateValueFormGroup: FormGroup; |
|||
|
|||
dynamicMode = false; |
|||
|
|||
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.get('dynamicValue').get('sourceType').valueChanges.subscribe( |
|||
(sourceType) => { |
|||
if (!sourceType) { |
|||
this.filterPredicateValueFormGroup.get('dynamicValue').get('sourceAttribute').patchValue(null, {emitEvent: false}); |
|||
} |
|||
} |
|||
); |
|||
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').get('sourceType').patchValue(predicateValue.dynamicValue ? |
|||
predicateValue.dynamicValue.sourceType : null, {emitEvent: false}); |
|||
this.filterPredicateValueFormGroup.get('dynamicValue').get('sourceAttribute').patchValue(predicateValue.dynamicValue ? |
|||
predicateValue.dynamicValue.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