Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicated labels removal #530

Merged
merged 8 commits into from
Dec 4, 2023
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 @@ -429,7 +440,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 @@ -451,8 +462,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