@@ -4,6 +4,9 @@ spyCTableBinding.find = function(scope) {
44 return $ ( scope ) . find ( ".spyctable" ) ;
55}
66
7+ var globalSpyCTableIndex = new Map ( ) ;
8+ var spyCTableSelectionBuffer = new Array ( ) ;
9+
710// Its its true then it's dragging
811var is_dragging = false ;
912
@@ -16,18 +19,23 @@ function enable_dragging() {
1619}
1720
1821function selected_deselected ( el ) {
19- const selection = el . selection ;
22+ // This is a pointer to the selection array
23+ const selection = globalSpyCTableIndex . get ( el . tableId ) ;
2024 let is_selected = el . classList . contains ( 'selected' ) ;
2125 if ( is_selected ) {
22- selection . delete ( el . coords ) ;
26+ selection . delete ( el ) ;
2327 el . classList . remove ( "bg-primary" ) ;
2428 el . classList . remove ( "selected" ) ;
2529 } else {
26- selection . add ( el . coords ) ;
30+ selection . add ( el ) ;
2731 el . classList . add ( "bg-primary" ) ;
2832 el . classList . add ( "selected" ) ;
2933 }
30- Shiny . setInputValue ( el . inputId , Array . from ( selection ) )
34+ spyCTableSelectionBuffer . length = 0 ;
35+ for ( const element of selection ) {
36+ spyCTableSelectionBuffer . push ( element . coords ) ;
37+ }
38+ Shiny . setInputValue ( el . inputId , spyCTableSelectionBuffer ) ;
3139}
3240
3341function mouse_over_event ( ) {
@@ -40,13 +48,27 @@ function mouse_down_event() {
4048 selected_deselected ( this )
4149}
4250
51+ // This function is to deselect everything in the table
52+ function spyctable_deselect_all ( tableId ) {
53+ const selection = globalSpyCTableIndex . get ( tableId ) ;
54+ if ( selection !== undefined ) {
55+ for ( const element of selection ) {
56+ element . classList . remove ( "bg-primary" ) ;
57+ element . classList . remove ( "selected" ) ;
58+ }
59+ selection . clear ( ) ;
60+ spyCTableSelectionBuffer . length = 0 ;
61+ Shiny . setInputValue ( el . inputId , spyCTableSelectionBuffer ) ;
62+ }
63+ }
64+
4365// If anywhere on the page the mouseup event is found
4466// then we disable dragging
4567addEventListener ( "mouseup" , ( _event ) => {
4668 disable_dragging ( ) ;
4769} ) ;
4870
49- function build_tbody ( selection , inputId , len_x , len_y , data , keys ) {
71+ function build_tbody ( tableId , inputId , len_x , len_y , data , keys ) {
5072 var tbody = document . createElement ( "tbody" ) ;
5173
5274 // If the user clicks then we enable dragging
@@ -68,7 +90,8 @@ function build_tbody(selection, inputId, len_x, len_y, data, keys) {
6890 current_cel . coords = [ c , i ] ;
6991 current_cel . innerText = data [ keys [ c ] ] [ i ] ;
7092 current_cel . classList . add ( "user-select-none" ) ;
71- current_cel . selection = selection ;
93+ //We passed the pointer to every single cell
94+ current_cel . tableId = tableId ;
7295 current_cel . onmouseover = mouse_over_event ;
7396 current_cel . onmousedown = mouse_down_event ;
7497 current_cel . inputId = inputId ;
@@ -109,17 +132,21 @@ spyCTableBinding.renderValue = function(el, msg) {
109132 Shiny . setInputValue ( inputId , new Array ( ) )
110133 }
111134
135+ var selection = globalSpyCTableIndex . get ( id ) ;
136+ if ( selection === undefined ) {
137+ selection = new Set ( ) ;
138+ globalSpyCTableIndex . set ( id , selection ) ;
139+ }
112140 let data = msg . data ;
113141 let thead_content = msg . thead ;
114- el . selection = new Set ( ) ;
115142 let keys = Object . keys ( data ) ;
116143 let len_x = keys . length ;
117144 let len_y = data [ keys [ 0 ] ] . length ;
118145 var table = document . createElement ( "table" ) ;
119146 table . classList . add ( "table" ) ;
120147 table . id = id + '_inner_table' ;
121148 table . appendChild ( fromHTML ( thead_content ) ) ;
122- table . appendChild ( build_tbody ( el . selection , inputId , len_x , len_y , data , keys ) ) ;
149+ table . appendChild ( build_tbody ( id , inputId , len_x , len_y , data , keys ) ) ;
123150 el . appendChild ( table ) ;
124151
125152 let scroll_y = el . getAttribute ( "scroll-y" ) ;
0 commit comments