Skip to content

Commit

Permalink
added copy functionality foer mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
ck-c8y committed Oct 21, 2022
1 parent 27864fb commit 0635e6f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
67 changes: 44 additions & 23 deletions frontend/mqtt-mapping/src/mqtt-mapping/grid/mapping.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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],
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions frontend/mqtt-mapping/src/shared/configuration.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class MappingSubstitution {

export interface Mapping {
id: number;
ident: string;
subscriptionTopic: string;
templateTopic: string;
templateTopicSample: string;
Expand Down

0 comments on commit 0635e6f

Please sign in to comment.