UI: Refactoring code

This commit is contained in:
Vladyslav_Prykhodko 2023-01-30 16:35:19 +02:00
parent 0fe0a84d32
commit 00e9d2ce75
3 changed files with 44 additions and 65 deletions

View File

@ -729,3 +729,37 @@ function prepareMessageFromData(data): string {
return data;
}
}
export function genNextLabel(name: string, datasources: Datasource[]): string {
let label = name;
let i = 1;
let matches = false;
if (datasources) {
do {
matches = false;
datasources.forEach((datasource) => {
if (datasource) {
if (datasource.dataKeys) {
datasource.dataKeys.forEach((dataKey) => {
if (dataKey.label === label) {
i++;
label = name + ' ' + i;
matches = true;
}
});
}
if (datasource.latestDataKeys) {
datasource.latestDataKeys.forEach((dataKey) => {
if (dataKey.label === label) {
i++;
label = name + ' ' + i;
matches = true;
}
});
}
}
});
} while (matches);
}
return label;
}

View File

@ -19,7 +19,8 @@ import { PageComponent } from '@shared/components/page.component';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import {
ComparisonResultType, comparisonResultTypeTranslationMap,
ComparisonResultType,
comparisonResultTypeTranslationMap,
DataKey,
dataKeyAggregationTypeHintTranslationMap,
Widget,
@ -50,6 +51,7 @@ import { WidgetService } from '@core/http/widget.service';
import { Dashboard } from '@shared/models/dashboard.models';
import { IAliasController } from '@core/api/widget-api.models';
import { aggregationTranslations, AggregationType, ComparisonDuration } from '@shared/models/time/time.models';
import { genNextLabel } from '@core/utils';
@Component({
selector: 'tb-data-key-config',
@ -201,7 +203,7 @@ export class DataKeyConfigComponent extends PageComponent implements OnInit, Con
let newLabel = this.dataKeyFormGroup.get('name').value;
if (aggType !== AggregationType.NONE) {
const prefix = this.translate.instant(aggregationTranslations.get(aggType));
newLabel = this.genNextLabelWithAggregation(prefix + ' ' + newLabel);
newLabel = genNextLabel(prefix + ' ' + newLabel, this.widget.config.datasources);
}
this.dataKeyFormGroup.get('label').patchValue(newLabel);
}
@ -241,30 +243,6 @@ export class DataKeyConfigComponent extends PageComponent implements OnInit, Con
);
}
private genNextLabelWithAggregation(name: string): string {
let label = name;
let i = 1;
let matches = false;
const datasources = this.widget.config.datasources;
if (datasources) {
do {
matches = false;
datasources.forEach((datasource) => {
if (datasource && datasource?.dataKeys) {
datasource.dataKeys.forEach((dataKey) => {
if (dataKey.label === label) {
i++;
label = name + ' ' + i;
matches = true;
}
});
}
});
} while (matches);
}
return label;
}
registerOnChange(fn: any): void {
this.propagateChange = fn;
}

View File

@ -20,7 +20,9 @@ import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import {
DataKey,
Datasource, datasourcesHasAggregation, datasourcesHasOnlyComparisonAggregation,
Datasource,
datasourcesHasAggregation,
datasourcesHasOnlyComparisonAggregation,
DatasourceType,
datasourceTypeTranslationMap,
defaultLegendConfig,
@ -42,7 +44,7 @@ import {
Validators
} from '@angular/forms';
import { WidgetConfigComponentData } from '@home/models/widget-component.models';
import { deepClone, isDefined, isObject } from '@app/core/utils';
import { deepClone, genNextLabel, isDefined, isObject } from '@app/core/utils';
import {
alarmFields,
AlarmSearchStatus,
@ -74,7 +76,6 @@ import { FilterDialogComponent, FilterDialogData } from '@home/components/filter
import { COMMA, ENTER, SEMICOLON } from '@angular/cdk/keycodes';
import { MatChipInputEvent } from '@angular/material/chips';
import { CdkDragDrop } from '@angular/cdk/drag-drop';
import { AggregationType } from '@shared/models/time/time.models';
const emptySettingsSchema: JsonSchema = {
type: 'object',
@ -794,7 +795,8 @@ export class WidgetConfigComponent extends PageComponent implements OnInit, Cont
label = this.translate.instant(keyField.name);
}
}
label = this.genNextLabel(label);
const datasources = this.widgetType === widgetType.alarm ? [this.modelValue.config.alarmSource] : this.modelValue.config.datasources;
label = genNextLabel(label, datasources);
const result: DataKey = {
name: chip,
type,
@ -819,41 +821,6 @@ export class WidgetConfigComponent extends PageComponent implements OnInit, Cont
}
}
private genNextLabel(name: string): string {
let label = name;
let i = 1;
let matches = false;
const datasources = this.widgetType === widgetType.alarm ? [this.modelValue.config.alarmSource] : this.modelValue.config.datasources;
if (datasources) {
do {
matches = false;
datasources.forEach((datasource) => {
if (datasource) {
if (datasource.dataKeys) {
datasource.dataKeys.forEach((dataKey) => {
if (dataKey.label === label) {
i++;
label = name + ' ' + i;
matches = true;
}
});
}
if (datasource.latestDataKeys) {
datasource.latestDataKeys.forEach((dataKey) => {
if (dataKey.label === label) {
i++;
label = name + ' ' + i;
matches = true;
}
});
}
}
});
} while (matches);
}
return label;
}
private genNextColor(): string {
let i = 0;
const datasources = this.widgetType === widgetType.alarm ? [this.modelValue.config.alarmSource] : this.modelValue.config.datasources;