r introjs bsModal

时间:2018-06-23 02:06:24

标签: javascript r shiny bootstrap-modal

我正在尝试使用intro.js的r包装器突出显示bsModal的元素,但是无法使其正常工作。我也尝试过包括自定义js脚本,但是我的js很糟糕。

我还设置了多个不同的测试,希望它可以粘在某些东西上,但是似乎intro.js找不到模式的div或其中的任何元素。我正在使用 rintrojs

下面是一些让人们使用javascript工作的示例:

但是我个人不太了解Javascript,无法自己集成定制解决方案。我已经尝试过:(

以下是我主持的与此问题相关的示例的链接: https://arun-sharma.shinyapps.io/introjs/

有人知道我如何使以下虚拟示例工作?

library(rintrojs)
library(shiny)
library(shinydashboard)


intro_df <- data.frame(element = c('#plot_box', '#bttn2', '#box', '#modal'),
                       intro = c('test plot_box', 'test bttn2', 'test box', 'test modal'))

ui <- shinyUI(fluidPage(
    introjsUI(), 
    mainPanel(
        bsModal('modal', '', '', uiOutput('plot_box'), size = 'large'),
        actionButton("bttn", "Start intro")
    )))
server <- shinyServer(function(input, output, session) {

    output$plot <- renderPlot({
        plot(rnorm(50))
    })

    output$plot_box <- renderUI({
        box(id = 'box',
            actionButton('bttn2', 'dummy'),
            plotOutput('plot'), width = '100%'
        )
    })

    observeEvent(input$bttn,{

        toggleModal(session, 'modal', toggle = 'toggle')
        introjs(session, options = list(steps = intro_df))
    })

})
shinyApp(ui = ui, server = server)

2 个答案:

答案 0 :(得分:1)

最终,我认为此请求可以在rintrojs库中提供一些有用的功能。无论如何,您的问题有两个:

    在HTML中的模式可用之前,
  1. introjs不应该触发。最简单的方法是使用模式中的按钮来触发教程。如果您希望它是自动的,则需要一些JavaScript,这些JavaScript会等到Modal准备就绪后再触发。
  2. introjs要使背景变灰并突出显示本教程中的当前项目。这意味着它需要与模态子级“交织”。因为模态是它自己的堆栈上下文,所以需要从模态内部触发introjs来查看模态子代。如果您想查看 entire 模态,那么从父级激发introjs就足够了。该功能似乎尚未包含在rintrojs软件包中,但是在JavaScript库中。

为了实现#1,我添加了一个JavaScript函数,以在模式加载时触发introjs(在HTML元素加载发生可配置的延迟之后)。这需要shinyjs软件包。注意introJs(modal_id),这可以确保教程在模式中触发。在纯JavaScript中,它将为introJs('#modal')

 run_introjs_on_modal_up <- function(
      modal_id
      , input_data
      , wait
    ) {
      runjs(
        paste0(
          "$('"
          , modal_id
          , "').on('shown.bs.modal', function(e) {
          setTimeout(function(){
            introJs('", modal_id, "').addSteps("
          , jsonlite::toJSON(input_data, auto_unbox=TRUE)
          , ").start()
          }, ", wait, ")
          })"
        )
      )
    }

我还添加了一个简单的帮助程序,用于在离开模态时关闭introjs教程。

introjs_exit <- function(){
  runjs("introJs().exit()")
}

还需要一行CSS来修正模式背景,以免过度渴望并接管DOM:

.modal-backdrop { z-index: -10;}

还有一个具有多个模态的(大/非最小)工作示例。

library(rintrojs)
library(shiny)
library(shinydashboard)
library(shinyBS)
library(shinyjs)

intro_df <- data.frame(element = c('#plot_box', '#bttn2', '#box', '#modal'),
                       intro = c('test plot_box', 'test bttn2', 'test box', 'test modal'))

intro_df2 <- data.frame(element = c('#plot_box2'),
                       intro = c('test plot_box'))

    run_introjs_on_modal_up <- function(
      modal_id
      , input_data
      , wait
    ) {
      runjs(
        paste0(
          "$('"
          , modal_id
          , "').on('shown.bs.modal', function(e) {
          setTimeout(function(){
            introJs('", modal_id, "').addSteps("
          , jsonlite::toJSON(input_data, auto_unbox=TRUE)
          , ").start()
          }, ", wait, ")
          })"
        )
      )
    }

    introjs_exit <- function(){
      runjs("introJs().exit()")
    }

ui <- shinyUI(fluidPage(
  useShinyjs(),
  tags$head(tags$style(".modal-backdrop { z-index: -10;}")),
  introjsUI(), 
  mainPanel(
    bsModal('modal', '', '', uiOutput('plot_box'), size = 'large'),
    bsModal('modalblah', '', '', uiOutput('plot_box2'), size = 'large'),
    actionButton("bttn", "Start intro")
  )))
server <- shinyServer(function(input, output, session) {
  output$plot <- renderPlot({
    plot(rnorm(50))
  })
  output$plot2 <- renderPlot({
    plot(rnorm(50))
  })

  output$plot_box <- renderUI({
    box(id = 'box',
        actionButton('bttn2', 'dummy'),
        plotOutput('plot'), width = '100%'
    )
  })
  output$plot_box2 <- renderUI({
    box(id = 'box2',
        plotOutput('plot2'), width = '100%'
    )
  })

    run_introjs_on_modal_up("#modal",intro_df, 1000)
    run_introjs_on_modal_up("#modalblah",intro_df2, 1000)

  observeEvent(input$bttn,{

    toggleModal(session, 'modal', toggle = 'toggle')
  })

  observeEvent(input$bttn2, {
    toggleModal(session, 'modal', toggle = 'toggle')
    introjs_exit()

    toggleModal(session, 'modalblah', toggle = 'toggle')
  })

})
shinyApp(ui = ui, server = server)

答案 1 :(得分:0)

我能够通过添加来解决问题

.introjs-fixParent.modal {
  z-index:1050 !important;  
}

到我的CSS。

工作示例:

library(rintrojs)
library(shiny)
library(shinydashboard)
library(shinyBS)
library(shinyjs)

intro_df <- data.frame(element = c('#plot_box', '#bttn2_intro', '#box', '#plot', '#shiny-modal'),
                       intro = c('test plot_box', 'test bttn2', 'test box', 'plot', 'test modal'))

ui <- shinyUI(fluidPage(
  dashboardPage(dashboardHeader(title = "Test"),
                dashboardSidebar(sidebarMenu(menuItem("item1", tabName = "item1"))),
                dashboardBody(tabItems(tabItem("item1", actionButton("bttn", "start intro"))))),
  useShinyjs(),
  tags$head(tags$style(".introjs-fixParent.modal {
                          z-index:1050 !important;  
                        }")),
  introjsUI()
  ))

server <- shinyServer(function(input, output, session) {
  output$plot <- renderPlot({
    plot(rnorm(50))
  })

  output$plot_box <- renderUI({
    box(id = 'box',
        div(id = "bttn2_intro", actionButton('bttn2', 'dummy')),
        plotOutput('plot'), width = '100%'
    )
  })

  observeEvent(input$bttn,{
    showModal(modalDialog(uiOutput('plot_box')))
  })

  observeEvent(input$bttn2, {
    introjs(session, options = list(steps = intro_df))
  })

})
shinyApp(ui = ui, server = server)
相关问题