Skip to content

Commit

Permalink
fix closing last winbox
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed May 24, 2022
1 parent 212068b commit d2e7f96
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: shinywb
Title: 'WinBox' Window for 'shiny' Applications
Version: 0.0.0.9130
Version: 0.0.0.9140
Authors@R: c(
person("Victor", "Perrier", , "[email protected]", role = c("aut", "cre")),
person("Fanny", "Meyer", role = "aut"),
Expand Down
2 changes: 1 addition & 1 deletion R/WinBox.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ html_dependency_winbox <- function(css_rules = "body{min-height:100vh}.winbox.mo
#' @param options List of options, see [wbOptions()].
#' @param controls List of controls, see [wbControls()].
#' @param id An unique identifier for the window, if a window with the same `id` is already open,
#' it will be closed before opening the new one
#' it will be closed before opening the new one. When closing windows, use `id = NULL` to close last one opened.
#' @param padding Padding for the window content.
#' @param auto_height Automatically set height of the window according to content.
#' Note that if content does not have a fix height it may not work properly.
Expand Down
21 changes: 19 additions & 2 deletions inst/examples/close.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@

library(shiny)
library(shinywb)

ui <- fluidPage(
tags$style("body {min-height: 100vh;}"),
html_dependency_winbox(),
actionButton(inputId = "show", label = "Show WinBox"),
actionButton(inputId = "close", label = "Close WinBox")
tags$p("With an ID:"),
actionButton(inputId = "show", label = "Show WinBox with ID"),
actionButton(inputId = "close", label = "Close WinBox with ID"),
tags$p("Without ID, close last one:"),
actionButton(inputId = "show_mult", label = "Show multiple WinBox"),
actionButton(inputId = "close_last", label = "Close last WinBox")
)

server <- function(input, output, session) {
Expand All @@ -24,6 +29,18 @@ server <- function(input, output, session) {

observeEvent(input$close, closeWinBox("mywinbox"))

observeEvent(input$show_mult, {
WinBox(
title = paste("WinBox window", input$show_mult),
ui = tags$div(
style = "padding: 10px;",
tags$h2("Hello from WinBox!"),
"Text content of winbox."
)
)
})
observeEvent(input$close_last, closeWinBox(NULL))

}

shinyApp(ui, server)
2 changes: 1 addition & 1 deletion inst/packer/WinBox.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion man/WinBox.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions srcjs/exts/WinBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ Shiny.addCustomMessageHandler("WinBox-close", msg => {
delete winboxes[msg.id];
}
} else {
if (winboxes.length > 0) {
var last = winboxes.at(-1);
winboxes[last].close();
delete winboxes[last];
var keys = Object.keys(winboxes);
var last = keys.length - 1;
if (last > -1) {
winboxes[keys[last]].close();
delete winboxes[keys[last]];
}
}
});
Expand Down

0 comments on commit d2e7f96

Please sign in to comment.