Skip to content

Commit 499bdaa

Browse files
committed
track selection history to avoid adding redundant traces; fixes #915
1 parent 3743306 commit 499bdaa

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

inst/htmlwidgets/plotly.js

+37-19
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ HTMLWidgets.widget({
297297

298298
if (allSets.length > 0) {
299299

300-
// On plotly event, update crosstalk variable selection value
300+
// Set a crosstalk variable selection value, triggering an update
301301
graphDiv.on(x.highlight.on, function turnOn(e) {
302302
if (e) {
303303
var selectedKeys = pointsToKeys(e.points);
@@ -312,9 +312,12 @@ HTMLWidgets.widget({
312312
}
313313
});
314314

315-
// On a plotly "clear" event, set crosstalk variable value to null
316315
graphDiv.on(x.highlight.off, function turnOff(e) {
316+
// remove any visual clues
317317
removeBrush(el);
318+
// remove any selection history
319+
crosstalk.var("plotlySelectionHistory").set(null);
320+
// trigger the actual removal of selection traces
318321
for (var i = 0; i < allSets.length; i++) {
319322
crosstalk.group(allSets[i]).var("selection").set(null, {sender: el});
320323
}
@@ -364,29 +367,44 @@ HTMLWidgets.widget({
364367
});
365368
}
366369

367-
// When crosstalk selection changes, update plotly style
368370
grp.var("selection").on("change", function crosstalk_sel_change(e) {
369-
if (e.sender !== el) {
370-
// If we're not the originator of this selection, and we have an
371-
// active selection outline box, we need to remove it. Otherwise
372-
// it could appear like there are two active brushes in one plot
373-
// group.
374-
removeBrush(el);
371+
372+
// array of "event objects" tracking the selection history
373+
var selectionHistory = crosstalk.var("plotlySelectionHistory").get() || [];
374+
375+
// do nothing if the event isn't "new"
376+
// TODO: is there a smarter way to check object equality?
377+
var event = {};
378+
event[set] = e.value;
379+
if (selectionHistory.length > 0) {
380+
var ev = JSON.stringify(event);
381+
for (var i = 0; i < selectionHistory.length; i++) {
382+
var sel = JSON.stringify(selectionHistory[i]);
383+
if (sel == ev) {
384+
return;
385+
}
386+
}
375387
}
376388

389+
// accumulate history for persistent selection
390+
if (!x.highlight.persistent) {
391+
selectionHistory = [event];
392+
} else {
393+
selectionHistory.push(event);
394+
}
395+
crosstalk.var("plotlySelectionHistory").set(selectionHistory);
396+
377397
// e.value is either null, or an array of newly selected values
378-
if (e.oldValue !== e.value) {
379-
traceManager.updateSelection(set, e.value);
380-
// https://github.com/selectize/selectize.js/blob/master/docs/api.md#methods_items
381-
if (x.selectize) {
382-
if (!x.highlight.persistent || e.value === null) {
383-
selectize.clear(true);
384-
}
385-
selectize.addItems(e.value, true);
386-
selectize.close();
398+
traceManager.updateSelection(set, e.value);
399+
// https://github.com/selectize/selectize.js/blob/master/docs/api.md#methods_items
400+
if (x.selectize) {
401+
if (!x.highlight.persistent || e.value === null) {
402+
selectize.clear(true);
387403
}
388-
404+
selectize.addItems(e.value, true);
405+
selectize.close();
389406
}
407+
390408
});
391409

392410
grp.var("filter").on("change", function crosstalk_filter_change(e) {

0 commit comments

Comments
 (0)