Browse Source

Fixed color range settings being corrupted when rangeList has no advancedMode (#16342)

* Fixed color range settings being corrupted when rangeList has no advancedMode

* Additional fixes
pull/16393/head
Maksym Tsymbarov 1 month ago
committed by GitHub
parent
commit
caee746fcd
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 6
      ui-ngx/src/app/modules/home/components/widget/config/basic/gauge/digital-simple-gauge-basic-config.component.ts
  2. 9
      ui-ngx/src/app/modules/home/components/widget/lib/settings/common/color-range-list.component.ts
  3. 12
      ui-ngx/src/app/modules/home/components/widget/lib/settings/common/color-settings.component.ts
  4. 2
      ui-ngx/src/app/modules/home/components/widget/lib/settings/common/count-widget-settings.component.ts
  5. 4
      ui-ngx/src/app/modules/home/components/widget/lib/settings/gauge/digital-gauge-widget-settings.component.ts
  6. 40
      ui-ngx/src/app/shared/models/widget-settings.models.ts

6
ui-ngx/src/app/modules/home/components/widget/config/basic/gauge/digital-simple-gauge-basic-config.component.ts

@ -27,7 +27,7 @@ import {
DigitalGaugeSettings,
DigitalGaugeType
} from '@home/components/widget/lib/digital-gauge.models';
import { ColorSettings, ColorType } from '@shared/models/widget-settings.models';
import { ColorSettings, ColorType, normalizeColorRangeSettings } from '@shared/models/widget-settings.models';
import { getSourceTbUnitSymbol } from '@shared/models/unit.models';
@Component({
@ -161,8 +161,8 @@ export class DigitalSimpleGaugeBasicConfigComponent extends BasicWidgetConfigCom
if (barColor.type === ColorType.range) {
this.widgetConfig.config.settings.useFixedLevelColor = true;
this.widgetConfig.config.settings.fixedLevelColors =
barColor.rangeList.advancedMode ? barColor.rangeList.rangeAdvanced : barColor.rangeList.range;
const rangeList = normalizeColorRangeSettings(barColor.rangeList);
this.widgetConfig.config.settings.fixedLevelColors = rangeList?.advancedMode ? rangeList.rangeAdvanced : rangeList?.range;
} else {
this.widgetConfig.config.settings.useFixedLevelColor = false;
}

9
ui-ngx/src/app/modules/home/components/widget/lib/settings/common/color-range-list.component.ts

@ -16,12 +16,13 @@ import {
AdvancedColorRange,
ColorRange,
ColorRangeSettings,
normalizeColorRangeSettings,
ValueSourceType
} from '@shared/models/widget-settings.models';
import { TbPopoverComponent } from '@shared/components/popover.component';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { deepClone, isDefinedAndNotNull, isUndefined } from '@core/utils';
import { isDefinedAndNotNull } from '@core/utils';
import { CdkDragDrop } from '@angular/cdk/drag-drop';
import { IAliasController } from '@core/api/widget-api.models';
import { coerceBoolean } from '@shared/decorators/coercion';
@ -121,11 +122,11 @@ export class ColorRangeListComponent implements OnInit, ControlValueAccessor, On
writeValue(value: any): void {
if (value) {
let rangeList: ColorRangeSettings = {};
if (this.simpleRange || (isUndefined(value?.advancedMode) && value?.length)) {
if (this.simpleRange) {
rangeList.advancedMode = false;
rangeList.range = value;
rangeList.range = normalizeColorRangeSettings(value).range;
} else {
rangeList = deepClone(value);
rangeList = normalizeColorRangeSettings(value);
}
this.colorRangeListFormGroup.get('advancedMode').patchValue(rangeList.advancedMode || false, {emitEvent: false});
if (isDefinedAndNotNull(rangeList?.range)) {

12
ui-ngx/src/app/modules/home/components/widget/lib/settings/common/color-settings.component.ts

@ -11,14 +11,14 @@ import {
ViewContainerRef
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { ColorRange, ColorSettings, ColorType, ComponentStyle } from '@shared/models/widget-settings.models';
import { ColorSettings, ColorType, ComponentStyle, normalizeColorRangeSettings } from '@shared/models/widget-settings.models';
import { MatButton } from '@angular/material/button';
import { TbPopoverService } from '@shared/components/popover.service';
import {
ColorSettingsPanelComponent
} from '@home/components/widget/lib/settings/common/color-settings-panel.component';
import { IAliasController } from '@core/api/widget-api.models';
import { deepClone, isDefinedAndNotNull } from '@core/utils';
import { isDefinedAndNotNull } from '@core/utils';
import { coerceBoolean } from '@shared/decorators/coercion';
import { DataKeysCallbacks } from '@home/components/widget/lib/settings/common/key/data-keys.component.models';
import { Datasource } from '@shared/models/widget.models';
@ -129,11 +129,9 @@ export class ColorSettingsComponent implements OnInit, ControlValueAccessor, OnD
writeValue(value: ColorSettings): void {
if (value) {
this.modelValue = value;
if (isDefinedAndNotNull(this.modelValue.rangeList) && !isDefinedAndNotNull(this.modelValue.rangeList?.advancedMode)) {
const range = deepClone(this.modelValue.rangeList) as ColorRange[];
this.modelValue.rangeList = deepClone({advancedMode: false, range});
}
this.modelValue = isDefinedAndNotNull(value.rangeList)
? {...value, rangeList: normalizeColorRangeSettings(value.rangeList)}
: value;
this.updateColorStyle();
}
}

2
ui-ngx/src/app/modules/home/components/widget/lib/settings/common/count-widget-settings.component.ts

@ -140,7 +140,7 @@ export class CountWidgetSettingsComponent extends PageComponent implements OnIni
}
private updateModel() {
const value: CountWidgetSettings = this.countWidgetConfigForm.value;
const value: CountWidgetSettings = this.countWidgetConfigForm.getRawValue();
this.propagateChange(value);
}

4
ui-ngx/src/app/modules/home/components/widget/lib/settings/gauge/digital-gauge-widget-settings.component.ts

@ -28,6 +28,7 @@ import {
ColorSettings,
ColorType,
constantColor,
normalizeColorRangeSettings,
simpleDateFormat,
ValueSourceConfig,
ValueSourceType
@ -219,7 +220,8 @@ export class DigitalGaugeWidgetSettingsComponent extends WidgetSettingsComponent
if (barColor.type === ColorType.range) {
settings.useFixedLevelColor = true;
settings.fixedLevelColors = barColor.rangeList.advancedMode ? barColor.rangeList.rangeAdvanced : barColor.rangeList.range;
const rangeList = normalizeColorRangeSettings(barColor.rangeList);
settings.fixedLevelColors = rangeList?.advancedMode ? rangeList.rangeAdvanced : rangeList?.range;
} else {
settings.useFixedLevelColor = false;
}

40
ui-ngx/src/app/shared/models/widget-settings.models.ts

@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
// SPDX-License-Identifier: Apache-2.0
import {
deepClone,
isDefinedAndNotNull,
isNotEmptyStr,
isNumber,
@ -147,6 +148,28 @@ export interface ColorRangeSettings extends AdvancedColorMode {
rangeAdvanced?: AdvancedColorRange[];
}
/**
* Converts any stored rangeList shape to ColorRangeSettings. Always returns a new deep copy (null/undefined pass through),
* so callers may mutate the result freely.
*/
export const normalizeColorRangeSettings = (rangeList: ColorRangeSettings | ColorRange[]): ColorRangeSettings => {
if (!isDefinedAndNotNull(rangeList)) {
return rangeList as ColorRangeSettings;
}
if (Array.isArray(rangeList)) {
return {advancedMode: false, range: deepClone(rangeList)};
}
const result: ColorRangeSettings = deepClone(rangeList);
result.advancedMode = result.advancedMode ?? false;
// Unwraps the legacy corrupted shape {range: {advancedMode, range: [...]}} (possibly nested several levels deep)
let range: any = result.range;
while (isDefinedAndNotNull(range) && !Array.isArray(range)) {
range = range.range;
}
result.range = range;
return result;
};
export interface ColorGradientSettings extends AdvancedColorMode {
gradient?: string[];
gradientAdvanced?: AdvancedGradient[];
@ -473,9 +496,9 @@ export abstract class AdvancedModeColorProcessor extends ColorProcessor {
class RangeColorProcessor extends AdvancedModeColorProcessor {
constructor(protected settings: ColorSettings,
protected ctx: WidgetContext) {
super(settings, ctx);
constructor(settings: ColorSettings,
ctx: WidgetContext) {
super({...settings, rangeList: normalizeColorRangeSettings(settings.rangeList)}, ctx);
}
update(value: any): void {
@ -519,15 +542,8 @@ class RangeColorProcessor extends AdvancedModeColorProcessor {
}
private computeFromRange(value: any): string {
let rangeList: any = this.settings.rangeList as Array<ColorRange>;
let advancedMode = false;
if (isDefinedAndNotNull(this.settings.rangeList?.advancedMode)) {
advancedMode = this.settings.rangeList.advancedMode;
rangeList = advancedMode ?
this.settings.rangeList.rangeAdvanced as Array<AdvancedColorRange> :
this.settings.rangeList.range as Array<ColorRange>;
}
const advancedMode = this.settings.rangeList?.advancedMode ?? false;
const rangeList: any = advancedMode ? this.settings.rangeList?.rangeAdvanced : this.settings.rangeList?.range;
if (rangeList?.length && isDefinedAndNotNull(value) && isNumeric(value)) {
const num = Number(value);

Loading…
Cancel
Save