Skip to content

Commit a40e7a9

Browse files
committed
new input events to get at persistent click/hover data, addresses #1401
1 parent e266a3c commit a40e7a9

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

inst/htmlwidgets/plotly.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,29 +277,71 @@ HTMLWidgets.widget({
277277
);
278278
});
279279
graphDiv.on('plotly_hover', function(d) {
280+
280281
Shiny.onInputChange(
281282
".clientValue-plotly_hover-" + x.source,
282283
JSON.stringify(eventDataWithKey(d))
283284
);
285+
286+
// When persistent selection is enabled (either through
287+
// shift or highlight()), remember click data
288+
if (x.highlight.persistentShift) {
289+
var dShift = graphDiv._shiny_plotly_hover || {points: []};
290+
var pts = [].concat(dShift.points, d.points);
291+
var d = {points: pts, event: d.event};
292+
graphDiv._shiny_plotly_hover = d;
293+
} else {
294+
graphDiv._shiny_plotly_hover = undefined;
295+
}
296+
297+
Shiny.onInputChange(
298+
".clientValue-plotly_hover_persist_on_shift-" + x.source,
299+
JSON.stringify(eventDataWithKey(d))
300+
);
301+
284302
});
285303
graphDiv.on('plotly_click', function(d) {
304+
286305
Shiny.onInputChange(
287-
".clientValue-plotly_click-" + x.source,
306+
".clientValue-plotly_click-" + x.source,
288307
JSON.stringify(eventDataWithKey(d))
289308
);
309+
310+
// When persistent selection is enabled (either through
311+
// shift or highlight()), remember click data
312+
if (x.highlight.persistentShift) {
313+
var dShift = graphDiv._shiny_plotly_click || {points: []};
314+
var pts = [].concat(dShift.points, d.points);
315+
var d = {points: pts, event: d.event};
316+
graphDiv._shiny_plotly_click = d;
317+
} else {
318+
graphDiv._shiny_plotly_click = undefined;
319+
}
320+
321+
Shiny.onInputChange(
322+
".clientValue-plotly_click_persist_on_shift-" + x.source,
323+
JSON.stringify(eventDataWithKey(d))
324+
);
325+
290326
});
327+
291328
graphDiv.on('plotly_selected', function(d) {
292329
Shiny.onInputChange(
293330
".clientValue-plotly_selected-" + x.source,
294331
JSON.stringify(eventDataWithKey(d))
295332
);
296333
});
334+
297335
graphDiv.on('plotly_unhover', function(eventData) {
298-
Shiny.onInputChange(".clientValue-plotly_hover-" + x.source, null);
336+
if (!x.highlight.persistentShift) {
337+
Shiny.onInputChange(".clientValue-plotly_hover-" + x.source, null);
338+
}
299339
});
340+
300341
graphDiv.on('plotly_doubleclick', function(eventData) {
301342
Shiny.onInputChange(".clientValue-plotly_click-" + x.source, null);
302343
});
344+
303345
// 'plotly_deselect' is code for doubleclick when in select mode
304346
graphDiv.on('plotly_deselect', function(eventData) {
305347
Shiny.onInputChange(".clientValue-plotly_selected-" + x.source, null);

0 commit comments

Comments
 (0)