From de0a196d7a4b9c435698c812c217b81670282f6a Mon Sep 17 00:00:00 2001 From: Kalutka Zhenya Date: Wed, 2 Nov 2022 11:58:33 +0200 Subject: [PATCH] Refactoring --- ui-ngx/src/app/core/utils.ts | 17 +++++++---------- .../widget/lib/markdown-widget.component.ts | 4 ++-- .../widget/lib/qrcode-widget.component.ts | 4 ++-- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/ui-ngx/src/app/core/utils.ts b/ui-ngx/src/app/core/utils.ts index 8b288cd5a1..ab64f0c165 100644 --- a/ui-ngx/src/app/core/utils.ts +++ b/ui-ngx/src/app/core/utils.ts @@ -22,10 +22,8 @@ import { EntityId } from '@shared/models/id/entity-id'; import { NULL_UUID } from '@shared/models/id/has-uuid'; import { EntityType, baseDetailsPageByEntityType } from '@shared/models/entity-type.models'; import { HttpErrorResponse } from '@angular/common/http'; -import { letterSpacing } from 'html2canvas/dist/types/css/property-descriptors/letter-spacing'; import { TranslateService } from '@ngx-translate/core'; import { serverErrorCodesTranslations } from '@shared/models/constants'; -import { isNotNullOrUndefined } from 'codelyzer/util/isNotNullOrUndefined'; const varsRegex = /\${([^}]*)}/g; @@ -480,17 +478,16 @@ export function flatFormattedData(input: FormattedData[]): FormattedData { return result; } -export function filteringData(data: FormattedData[]): FormattedData { - const processingKeyValue = {}; - for (const deviceData of data) { - Object.keys(deviceData).forEach((valueKey) => { - if (!isNotNullOrUndefined(processingKeyValue[valueKey]) && isNotNullOrUndefined(deviceData[valueKey]) && - deviceData[valueKey] !== '') { - processingKeyValue[valueKey] = deviceData[valueKey]; +export function flatDataWithoutOverride(data: FormattedData[]): FormattedData { + const processingKeyValue = data[0]; + for (let i = 1; i < data.length; i++) { + Object.keys(data[i]).forEach((key) => { + if (!isDefinedAndNotNull(processingKeyValue[key]) || isEmptyStr(processingKeyValue[key])) { + processingKeyValue[key] = data[i][key]; } }); } - return processingKeyValue as FormattedData; + return processingKeyValue; } export function mergeFormattedData(first: FormattedData[], second: FormattedData[]): FormattedData[] { diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/markdown-widget.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/markdown-widget.component.ts index f37d5eb17d..d76e43d765 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/markdown-widget.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/markdown-widget.component.ts @@ -23,7 +23,7 @@ import { DatasourceData, FormattedData } from '@shared/models/widget.models'; import { DataKeyType } from '@shared/models/telemetry/telemetry.models'; import { createLabelFromPattern, - filteringData, + flatDataWithoutOverride, formattedDataFormDatasourceData, hashCode, isDefinedAndNotNull, isNotEmptyStr, @@ -118,7 +118,7 @@ export class MarkdownWidgetComponent extends PageComponent implements OnInit { const data = formattedDataFormDatasourceData(initialData); let markdownText = this.settings.useMarkdownTextFunction ? safeExecute(this.markdownTextFunction, [data]) : this.settings.markdownTextPattern; - const allData: FormattedData = filteringData(data); + const allData: FormattedData = flatDataWithoutOverride(data); markdownText = createLabelFromPattern(markdownText, allData); if (this.markdownText !== markdownText) { this.markdownText = this.utils.customTranslation(markdownText, markdownText); diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/qrcode-widget.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/qrcode-widget.component.ts index 88207caeb2..94f7d10471 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/qrcode-widget.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/qrcode-widget.component.ts @@ -23,7 +23,7 @@ import { DatasourceData, FormattedData } from '@shared/models/widget.models'; import { DataKeyType } from '@shared/models/telemetry/telemetry.models'; import { createLabelFromPattern, - filteringData, + flatDataWithoutOverride, formattedDataFormDatasourceData, isNumber, isObject, @@ -101,7 +101,7 @@ export class QrCodeWidgetComponent extends PageComponent implements OnInit, Afte const data = formattedDataFormDatasourceData(initialData); const pattern = this.settings.useQrCodeTextFunction ? safeExecute(this.qrCodeTextFunction, [data]) : this.settings.qrCodeTextPattern; - const allData: FormattedData = filteringData(data); + const allData: FormattedData = flatDataWithoutOverride(data); qrCodeText = createLabelFromPattern(pattern, allData); this.updateQrCodeText(qrCodeText); }