Skip to content

Commit

Permalink
Merge pull request #530 from Rdornier/duplicate-labels
Browse files Browse the repository at this point in the history
Duplicated labels removal
  • Loading branch information
will-moore authored Dec 4, 2023
2 parents e310ddb + 06d2d50 commit c537264
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/js/models/panel_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,23 @@
var oldLabs = this.get("labels");
// Need to clone the list of labels...
var labs = [];
var oldLabKeys = [];

// Keep old labels unchanged...
for (var i=0; i<oldLabs.length; i++) {
labs.push( $.extend(true, {}, oldLabs[i]) );
var lbl = oldLabs[i];
var lbl_key = this.get_label_key(lbl);
oldLabKeys.push(lbl_key);
labs.push( $.extend(true, {}, lbl));
}
// ... then add new labels ...
for (var j=0; j<labels.length; j++) {
labs.push( $.extend(true, {}, labels[j]) );
var lbl = labels[j];
var lbl_key = this.get_label_key(lbl);
if (!oldLabKeys.includes(lbl_key)) {
// otherwise leave un-edited
labs.push( $.extend(true, {}, lbl));
}
}
// ... so that we get the changed event triggering OK
this.save('labels', labs);
Expand Down Expand Up @@ -439,7 +450,7 @@
// labels_map is {labelKey: {size:s, text:t, position:p, color:c}} or {labelKey: false} to delete
// where labelKey specifies the label to edit. "l.text + '_' + l.size + '_' + l.color + '_' + l.position"
edit_labels: function(labels_map) {

var oldLabs = this.get('labels');
// Need to clone the list of labels...
var labs = [],
Expand All @@ -461,8 +472,16 @@
labs.push( lbl );
}
}

// Extract all the keys (even duplicates)
var keys = labs.map(lbl => this.get_label_key(lbl));

// get all unique labels based on filtering keys
//(i.e removing duplicate keys based on the index of the first occurrence of the value)
var filtered_lbls = labs.filter((lbl, index) => index == keys.indexOf(this.get_label_key(lbl)));

// ... so that we get the changed event triggering OK
this.save('labels', labs);
this.save('labels', filtered_lbls);
},

save_channel: function(cIndex, attr, value) {
Expand Down

0 comments on commit c537264

Please sign in to comment.