From 0635e6f6c0f18b343175416a232b634e1b86657b Mon Sep 17 00:00:00 2001 From: sagIoTPower Date: Fri, 21 Oct 2022 21:24:11 +0200 Subject: [PATCH] added copy functionality foer mappings --- .../mqtt-mapping/grid/mapping.component.ts | 67 ++++++++++++------- .../src/shared/configuration.model.ts | 1 + 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/frontend/mqtt-mapping/src/mqtt-mapping/grid/mapping.component.ts b/frontend/mqtt-mapping/src/mqtt-mapping/grid/mapping.component.ts index e6740f8a..556046e0 100644 --- a/frontend/mqtt-mapping/src/mqtt-mapping/grid/mapping.component.ts +++ b/frontend/mqtt-mapping/src/mqtt-mapping/grid/mapping.component.ts @@ -125,10 +125,17 @@ export class MappingComponent implements OnInit { ngOnInit() { this.loadMappings(); - this.actionControls.push({ - type: BuiltInActionType.Edit, - callback: this.editMapping.bind(this) - }, + this.actionControls.push( + { + type: BuiltInActionType.Edit, + callback: this.editMapping.bind(this) + }, + { + text: 'Copy', + type: 'COPY', + icon: 'copy', + callback: this.copyMapping.bind(this) + }, { type: BuiltInActionType.Delete, callback: this.deleteMapping.bind(this) @@ -137,14 +144,14 @@ export class MappingComponent implements OnInit { async addMapping() { this.editMode = false; - let l = (this.mappings.length == 0 ? 0 : Math.max(...this.mappings.map(item => item.id))) + 1; + let l = this.newId(); let mapping = { id: l, ident: uuidv4(), subscriptionTopic: '', templateTopic: '', - templateTopicSample:'', + templateTopicSample: '', targetAPI: API.MEASUREMENT, source: '{}', target: SAMPLE_TEMPLATES[API.MEASUREMENT], @@ -166,6 +173,10 @@ export class MappingComponent implements OnInit { this.showConfigMapping = true; } + private newId() { + return (this.mappings.length == 0 ? 0 : Math.max(...this.mappings.map(item => item.id))) + 1; + } + editMapping(mapping: Mapping) { this.editMode = true; // create deep copy of existing mapping, in case user cancels changes @@ -174,6 +185,16 @@ export class MappingComponent implements OnInit { this.showConfigMapping = true; } + copyMapping(mapping: Mapping) { + this.editMode = true; + // create deep copy of existing mapping, in case user cancels changes + this.mappingToUpdate = JSON.parse(JSON.stringify(mapping)) as Mapping; + this.mappingToUpdate.ident = uuidv4(); + this.mappingToUpdate.id = this.newId(); + console.log("Copying mapping", this.mappingToUpdate) + this.showConfigMapping = true; + } + deleteMapping(mapping: Mapping) { console.log("Deleting mapping:", mapping) let i = this.mappings.map(item => item.id).findIndex(m => m == mapping.id) // find index of your object @@ -194,25 +215,25 @@ export class MappingComponent implements OnInit { async onCommit(mapping: Mapping) { // test if new/updated mapping was commited or if cancel // if (mapping) { - mapping.lastUpdate = Date.now(); - let i = this.mappings.map(item => item.id).findIndex(m => m == mapping.id) - console.log("Changed mapping:", mapping, i); - - if (isTemplateTopicUnique(mapping, this.mappings)) { - if (i == -1) { - // new mapping - console.log("Push new mapping:", mapping, i); - this.mappings.push(mapping) - } else { - console.log("Update existing mapping:", this.mappings[i], mapping, i); - this.mappings[i] = mapping; - } - this.mappingGridComponent.reload(); - this.saveMappings(); - this.activateMappings(); + mapping.lastUpdate = Date.now(); + let i = this.mappings.map(item => item.id).findIndex(m => m == mapping.id) + console.log("Changed mapping:", mapping, i); + + if (isTemplateTopicUnique(mapping, this.mappings)) { + if (i == -1) { + // new mapping + console.log("Push new mapping:", mapping, i); + this.mappings.push(mapping) } else { - this.alertService.danger(gettext('Topic is already used: ' + mapping.subscriptionTopic + ". Please use a different topic.")); + console.log("Update existing mapping:", this.mappings[i], mapping, i); + this.mappings[i] = mapping; } + this.mappingGridComponent.reload(); + this.saveMappings(); + this.activateMappings(); + } else { + this.alertService.danger(gettext('Topic is already used: ' + mapping.subscriptionTopic + ". Please use a different topic.")); + } //} this.showConfigMapping = false; } diff --git a/frontend/mqtt-mapping/src/shared/configuration.model.ts b/frontend/mqtt-mapping/src/shared/configuration.model.ts index dad6d675..a6a506dd 100644 --- a/frontend/mqtt-mapping/src/shared/configuration.model.ts +++ b/frontend/mqtt-mapping/src/shared/configuration.model.ts @@ -39,6 +39,7 @@ export class MappingSubstitution { export interface Mapping { id: number; + ident: string; subscriptionTopic: string; templateTopic: string; templateTopicSample: string;