From 7a19dee3eb4a441bf17dfd99e9978351bf5862fa Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Fri, 3 Jul 2020 12:15:11 +0300 Subject: [PATCH] UI: Dynamic filter value configuration --- .../data/query/FilterPredicateValue.java | 16 ++++++-- ...lex-filter-predicate-dialog.component.html | 2 +- .../filter-predicate-value.component.html | 38 ++++++++++++++++++- .../filter-predicate-value.component.ts | 26 +++++++++++-- .../filter/filters-edit-panel.component.html | 4 +- .../filter/key-filter-dialog.component.html | 2 +- .../filter/user-filter-dialog.component.ts | 11 ++++-- .../app/shared/models/query/query.models.ts | 1 + .../assets/locale/locale.constant-en_US.json | 8 +++- 9 files changed, 90 insertions(+), 18 deletions(-) diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/query/FilterPredicateValue.java b/common/data/src/main/java/org/thingsboard/server/common/data/query/FilterPredicateValue.java index b2611f7216..8897d35586 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/query/FilterPredicateValue.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/query/FilterPredicateValue.java @@ -27,25 +27,33 @@ public class FilterPredicateValue { @Getter private final T defaultValue; @Getter + private final T userValue; + @Getter private final DynamicValue dynamicValue; public FilterPredicateValue(T defaultValue) { - this(defaultValue, null); + this(defaultValue, null, null); } @JsonCreator public FilterPredicateValue(@JsonProperty("defaultValue") T defaultValue, + @JsonProperty("userValue") T userValue, @JsonProperty("dynamicValue") DynamicValue dynamicValue) { this.defaultValue = defaultValue; + this.userValue = userValue; this.dynamicValue = dynamicValue; } @JsonIgnore public T getValue() { - if (this.dynamicValue != null && this.dynamicValue.getResolvedValue() != null) { - return this.dynamicValue.getResolvedValue(); + if (this.userValue != null) { + return this.userValue; } else { - return defaultValue; + if (this.dynamicValue != null && this.dynamicValue.getResolvedValue() != null) { + return this.dynamicValue.getResolvedValue(); + } else { + return defaultValue; + } } } diff --git a/ui-ngx/src/app/modules/home/components/filter/complex-filter-predicate-dialog.component.html b/ui-ngx/src/app/modules/home/components/filter/complex-filter-predicate-dialog.component.html index 32fc06a2e1..fe0b217063 100644 --- a/ui-ngx/src/app/modules/home/components/filter/complex-filter-predicate-dialog.component.html +++ b/ui-ngx/src/app/modules/home/components/filter/complex-filter-predicate-dialog.component.html @@ -15,7 +15,7 @@ limitations under the License. --> -
+

filter.complex-filter

diff --git a/ui-ngx/src/app/modules/home/components/filter/filter-predicate-value.component.html b/ui-ngx/src/app/modules/home/components/filter/filter-predicate-value.component.html index c97ee2a100..e9e38c71f5 100644 --- a/ui-ngx/src/app/modules/home/components/filter/filter-predicate-value.component.html +++ b/ui-ngx/src/app/modules/home/components/filter/filter-predicate-value.component.html @@ -16,7 +16,7 @@ -->
-
+
@@ -43,6 +43,40 @@
-
Default value
+
filter.default-value
+
+
+
+ + + + + {{'filter.no-dynamic-value' | translate}} + + + {{dynamicValueSourceTypeTranslations.get(dynamicValueSourceTypeEnum[sourceType]) | translate}} + + + +
filter.dynamic-source-type
+
+
+ + + + +
filter.source-attribute
+
+
+
+
diff --git a/ui-ngx/src/app/modules/home/components/filter/filter-predicate-value.component.ts b/ui-ngx/src/app/modules/home/components/filter/filter-predicate-value.component.ts index 4997d270db..548137976e 100644 --- a/ui-ngx/src/app/modules/home/components/filter/filter-predicate-value.component.ts +++ b/ui-ngx/src/app/modules/home/components/filter/filter-predicate-value.component.ts @@ -23,7 +23,12 @@ import { ValidatorFn, Validators } from '@angular/forms'; -import { EntityKeyValueType, FilterPredicateValue } from '@shared/models/query/query.models'; +import { + DynamicValueSourceType, + dynamicValueSourceTypeTranslationMap, + EntityKeyValueType, + FilterPredicateValue +} from '@shared/models/query/query.models'; @Component({ selector: 'tb-filter-predicate-value', @@ -46,8 +51,14 @@ export class FilterPredicateValueComponent implements ControlValueAccessor, OnIn valueTypeEnum = EntityKeyValueType; + dynamicValueSourceTypes = Object.keys(DynamicValueSourceType); + dynamicValueSourceTypeEnum = DynamicValueSourceType; + dynamicValueSourceTypeTranslations = dynamicValueSourceTypeTranslationMap; + filterPredicateValueFormGroup: FormGroup; + dynamicMode = false; + private propagateChange = null; constructor(private fb: FormBuilder) { @@ -83,6 +94,13 @@ export class FilterPredicateValueComponent implements ControlValueAccessor, OnIn } ) }); + 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(); }); @@ -106,8 +124,10 @@ export class FilterPredicateValueComponent implements ControlValueAccessor, OnIn writeValue(predicateValue: FilterPredicateValue): 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}); + 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() { diff --git a/ui-ngx/src/app/modules/home/components/filter/filters-edit-panel.component.html b/ui-ngx/src/app/modules/home/components/filter/filters-edit-panel.component.html index a1d1822a6b..3de6b75bc4 100644 --- a/ui-ngx/src/app/modules/home/components/filter/filters-edit-panel.component.html +++ b/ui-ngx/src/app/modules/home/components/filter/filters-edit-panel.component.html @@ -16,7 +16,7 @@ -->
-
+
{{filter.value.filter}}
- +
diff --git a/ui-ngx/src/app/modules/home/components/filter/key-filter-dialog.component.html b/ui-ngx/src/app/modules/home/components/filter/key-filter-dialog.component.html index 4a555eff2f..c69c74c45f 100644 --- a/ui-ngx/src/app/modules/home/components/filter/key-filter-dialog.component.html +++ b/ui-ngx/src/app/modules/home/components/filter/key-filter-dialog.component.html @@ -15,7 +15,7 @@ limitations under the License. --> - +

{{(data.isAdd ? 'filter.add-key-filter' : 'filter.edit-key-filter') | translate}}

diff --git a/ui-ngx/src/app/modules/home/components/filter/user-filter-dialog.component.ts b/ui-ngx/src/app/modules/home/components/filter/user-filter-dialog.component.ts index 58634d7424..d743058de5 100644 --- a/ui-ngx/src/app/modules/home/components/filter/user-filter-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/filter/user-filter-dialog.component.ts @@ -33,10 +33,11 @@ import { DialogComponent } from '@app/shared/components/dialog.component'; import { TranslateService } from '@ngx-translate/core'; import { EntityKeyValueType, - Filter, + Filter, FilterPredicateValue, filterToUserFilterInfoList, UserFilterInputInfo } from '@shared/models/query/query.models'; +import { isDefinedAndNotNull } from '@core/utils'; export interface UserFilterDialogData { filter: Filter; @@ -81,15 +82,17 @@ export class UserFilterDialogComponent extends DialogComponent = (userInput.info.keyFilterPredicate as any).value; + const value = isDefinedAndNotNull(predicateValue.userValue) ? predicateValue.userValue : predicateValue.defaultValue; const userInputControl = this.fb.group({ label: [userInput.label], valueType: [userInput.valueType], - value: [(userInput.info.keyFilterPredicate as any).value.defaultValue, + value: [value, userInput.valueType === EntityKeyValueType.NUMERIC || userInput.valueType === EntityKeyValueType.DATE_TIME ? [Validators.required] : []] }); - userInputControl.get('value').valueChanges.subscribe(value => { - (userInput.info.keyFilterPredicate as any).value.defaultValue = value; + userInputControl.get('value').valueChanges.subscribe(userValue => { + (userInput.info.keyFilterPredicate as any).value.userValue = userValue; }); return userInputControl; } diff --git a/ui-ngx/src/app/shared/models/query/query.models.ts b/ui-ngx/src/app/shared/models/query/query.models.ts index d9bd0ca15f..f45c79967e 100644 --- a/ui-ngx/src/app/shared/models/query/query.models.ts +++ b/ui-ngx/src/app/shared/models/query/query.models.ts @@ -255,6 +255,7 @@ export interface DynamicValue { export interface FilterPredicateValue { defaultValue: T; + userValue?: T; dynamicValue?: DynamicValue; } diff --git a/ui-ngx/src/assets/locale/locale.constant-en_US.json b/ui-ngx/src/assets/locale/locale.constant-en_US.json index 0a97e56e96..3a145894ed 100644 --- a/ui-ngx/src/assets/locale/locale.constant-en_US.json +++ b/ui-ngx/src/assets/locale/locale.constant-en_US.json @@ -1230,7 +1230,13 @@ "time": "Time", "current-tenant": "Current tenant", "current-customer": "Current customer", - "current-user": "Current user" + "current-user": "Current user", + "default-value": "Default value", + "dynamic-source-type": "Dynamic source type", + "no-dynamic-value": "No dynamic value", + "source-attribute": "Source attribute", + "switch-to-dynamic-value": "Switch to dynamic value", + "switch-to-default-value": "Switch to default value" }, "fullscreen": { "expand": "Expand to fullscreen",