@@ -297,7 +297,7 @@ HTMLWidgets.widget({
297
297
298
298
if ( allSets . length > 0 ) {
299
299
300
- // On plotly event, update crosstalk variable selection value
300
+ // Set a crosstalk variable selection value, triggering an update
301
301
graphDiv . on ( x . highlight . on , function turnOn ( e ) {
302
302
if ( e ) {
303
303
var selectedKeys = pointsToKeys ( e . points ) ;
@@ -312,9 +312,12 @@ HTMLWidgets.widget({
312
312
}
313
313
} ) ;
314
314
315
- // On a plotly "clear" event, set crosstalk variable value to null
316
315
graphDiv . on ( x . highlight . off , function turnOff ( e ) {
316
+ // remove any visual clues
317
317
removeBrush ( el ) ;
318
+ // remove any selection history
319
+ crosstalk . var ( "plotlySelectionHistory" ) . set ( null ) ;
320
+ // trigger the actual removal of selection traces
318
321
for ( var i = 0 ; i < allSets . length ; i ++ ) {
319
322
crosstalk . group ( allSets [ i ] ) . var ( "selection" ) . set ( null , { sender : el } ) ;
320
323
}
@@ -364,29 +367,44 @@ HTMLWidgets.widget({
364
367
} ) ;
365
368
}
366
369
367
- // When crosstalk selection changes, update plotly style
368
370
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
+ }
375
387
}
376
388
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
+
377
397
// 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 ) ;
387
403
}
388
-
404
+ selectize . addItems ( e . value , true ) ;
405
+ selectize . close ( ) ;
389
406
}
407
+
390
408
} ) ;
391
409
392
410
grp . var ( "filter" ) . on ( "change" , function crosstalk_filter_change ( e ) {
0 commit comments