Skip to content

Commit 8f73bb2

Browse files
committed
Add support for resize(el, width, height) method on output bindings
1 parent 9e3a7f5 commit 8f73bb2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

inst/www/shared/shiny.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@
7272
throw "Blob doesn't support slice";
7373
}
7474

75+
// Given an element and a function(width, height), returns a function(). When
76+
// the output function is called, it calls the input function with the offset
77+
// width and height of the input element--but only if the size of the element
78+
// is non-zero and the size is different than the last time the output
79+
// function was called.
80+
//
81+
// Basically we are trying to filter out extraneous calls to func, so that
82+
// when the window size changes or whatever, we don't run resize logic for
83+
// elements that haven't actually changed size or aren't visible anyway.
84+
function makeResizeFilter(el, func) {
85+
var lastSize = {};
86+
return function() {
87+
var size = { w: el.offsetWidth, h: el.offsetHeight };
88+
if (size.w === 0 && size.h === 0)
89+
return;
90+
if (size.w === lastSize.w && size.h === lastSize.h)
91+
return;
92+
lastSize = size;
93+
func(size.w, size.h);
94+
}
95+
}
96+
7597
var _BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder ||
7698
window.MozBlobBuilder || window.MSBlobBuilder;
7799

@@ -2655,6 +2677,14 @@
26552677
var OutputBindingAdapter = function(el, binding) {
26562678
this.el = el;
26572679
this.binding = binding;
2680+
2681+
// If the binding actually has a resize method, override the prototype of
2682+
// onResize with a version that does a makeResizeFilter on the element.
2683+
if (binding.resize) {
2684+
this.onResize = makeResizeFilter(el, function(width, height) {
2685+
binding.resize(el, width, height);
2686+
});
2687+
}
26582688
};
26592689
(function() {
26602690
this.onValueChange = function(data) {
@@ -2666,6 +2696,9 @@
26662696
this.showProgress = function(show) {
26672697
this.binding.showProgress(this.el, show);
26682698
};
2699+
this.onResize = function() {
2700+
// Intentionally left blank; see constructor
2701+
};
26692702
}).call(OutputBindingAdapter.prototype);
26702703

26712704

@@ -2996,6 +3029,9 @@
29963029
inputs.setInput('.clientdata_output_' + this.id + '_height', this.offsetHeight);
29973030
}
29983031
});
3032+
$('.shiny-bound-output').each(function() {
3033+
$(this).data('shiny-output-binding').onResize();
3034+
});
29993035
}
30003036

30013037
// Return true if the object or one of its ancestors in the DOM tree has

0 commit comments

Comments
 (0)