Skip to content

Commit

Permalink
Functional shiny binding via a MutationObserver. This feels somewhat …
Browse files Browse the repository at this point in the history
…hacky so I'm not sure this is the final thing yet.
  • Loading branch information
jonthegeek committed Oct 18, 2024
1 parent e1bb502 commit 79d435b
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions inst/shiny/gtShiny.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ $.extend(gtShinyBinding, {
* @returns {jQuery} The jQuery object containing the gtShiny element.
*/
find: function(scope) {
console.log('find() called');
var found = $(scope).find('.gt_shiny');
if (found.length) {
console.log('find() found', found.length, 'elements');
}
return found;
return $(scope).find('.gt_shiny');
},
/**
* Gets the reactable element within the given gtShiny element.
Expand All @@ -26,7 +21,6 @@ $.extend(gtShinyBinding, {
* @returns {HTMLElement|null} The reactable element, or null if not found.
*/
getReactable: function(el) {
console.log('getReactable() called');
return el.querySelector('.reactable');
},
/**
Expand All @@ -36,11 +30,7 @@ $.extend(gtShinyBinding, {
* @returns {Array|null} The selected row IDs, or null if no rows are selected.
*/
getValue: function(el) {
console.log('getValue() called');
var rctbl = this.getReactable(el);
if (rctbl) {
console.log('Selected rows:', Reactable.getState(rctbl.id).selected);
}
return rctbl ? Reactable.getState(rctbl.id).selected : null;
},
/**
Expand All @@ -51,12 +41,10 @@ $.extend(gtShinyBinding, {
* element changes.
*/
subscribe: function(el, callback) {
console.log('subscribe() called');
var rctbl = this.getReactable(el);
if (rctbl) {
console.log('Subscribing to changes for:', rctbl.id);
rctbl.__reactableStateChangeListener = function() {
console.log('State change detected');
callback();
};
Reactable.onStateChange(rctbl.id, rctbl.__reactableStateChangeListener);
Expand All @@ -71,8 +59,6 @@ $.extend(gtShinyBinding, {
console.log('unsubscribe() called');
var rctbl = this.getReactable(el);
if (rctbl && rctbl.__reactableStateChangeListener) {
console.log('Unsubscribing from changes for:', rctbl.id);
// Using a custom way to track and remove listeners
var listenerFn = rctbl.__reactableStateChangeListener;
Reactable.onStateChange(rctbl.id, listenerFn, { remove: true });
delete rctbl.__reactableStateChangeListener;
Expand All @@ -82,3 +68,22 @@ $.extend(gtShinyBinding, {

// Register the input binding with Shiny
Shiny.inputBindings.register(gtShinyBinding, 'gt.gtShinyBinding');


// Mutation Observer to detect when gtShiny tables are fully loaded
(function() {
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var gtShinyObjs = document.querySelectorAll('.gt_shiny');
gtShinyObjs.forEach(function(obj) {
var rows = obj.querySelectorAll('.rt-tbody .rt-tr');
if (rows.length > 0) {
Shiny.bindAll();
observer.disconnect(); // Stop observing once the element is found and loaded
}
});
});
});

observer.observe(document.body, { childList: true, subtree: true });
})();

0 comments on commit 79d435b

Please sign in to comment.