Skip to content

Commit

Permalink
Call widget.resize in more situations
Browse files Browse the repository at this point in the history
e.g. rstudio/leaflet wasn't working if you had a widget like this:

```{r}
library(leaflet)
leaflet() %>% addTiles()
```

in an rmarkdown ioslide on the 5th slide and the slide deck was
showing slide 1 when the browser loaded it.

I'll submit more repro cases in the pull request.
  • Loading branch information
jcheng5 committed May 12, 2015
1 parent cb29c82 commit b772404
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions inst/www/htmlwidgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@

if (binding.resize) {
var lastSize = {};
on(window, "resize", function(e) {
var resizeHandler = function(e) {
var size = {
w: sizeObj ? sizeObj.getWidth() : el.offsetWidth,
h: sizeObj ? sizeObj.getHeight() : el.offsetHeight
Expand All @@ -463,7 +463,35 @@
return;
lastSize = size;
binding.resize(el, size.w, size.h, initResult);
});
};

on(window, "resize", resizeHandler);

// This is needed for cases where we're running in a Shiny
// app, but the widget itself is not a Shiny output, but
// rather a simple static widget. One example of this is
// an rmarkdown document that has runtime:shiny and widget
// that isn't in a render function. Shiny only knows to
// call resize handlers for Shiny outputs, not for static
// widgets, so we do it ourselves.
if (window.jQuery) {
window.jQuery(document).on("shown", resizeHandler);
window.jQuery(document).on("hidden", resizeHandler);
}

// This is needed for the specific case of ioslides, which
// flips slides between display:none and display:block.
// Ideally we would not have to have ioslide-specific code
// here, but rather have ioslides raise a generic event,
// but the rmarkdown package just went to CRAN so the
// window to getting that fixed may be long.
if (window.addEventListener) {
// It's OK to limit this to window.addEventListener
// browsers because ioslides itself only supports
// such browsers.
on(document, "slideenter", resizeHandler);
on(document, "slideleave", resizeHandler);
}
}

var scriptData = document.querySelector("script[data-for='" + el.id + "'][type='application/json']");
Expand Down

0 comments on commit b772404

Please sign in to comment.